-
Notifications
You must be signed in to change notification settings - Fork 2
/
bin.rs
3051 lines (2833 loc) · 116 KB
/
bin.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// src/bin/bin.rs
//
// ‥ … ≤ ≥ ≠ ≟
//! Driver program _s4_.
//!
//! Processes user-passed command-line arguments.
//! Then processes paths passed; directories are enumerated for parseable files,
//! archive files (`.tar`) are enumerated for file entries, other
//! paths tested for suitability (readable? is it a file? etc.).
//!
//! For each parseable file found, a file processing thread is created.
//! Each file processing thread advances through the stages of processing
//! using a [`SyslogProcessor`] instance.
//!
//! During the main processing stage, [`Stage3StreamSyslines`], each thread
//! sends the last processed [`Sysline`] to the main processing thread.
//! The main processing thread compares the last [`DateTimeL`] received
//! from all processing threads.
//! The `Sysline` with the earliest `DateTimeL` is printed.
//! That file processing thread then processes another `Sysline`.
//! This continues until each file processing thread sends a message to the
//! main processing thread that is has completed processing,
//! or in case of errors, abruptly closes it's [sending channel].
//!
//! Then, if passed CLI option `--summary`, the main processing thread
//! prints a [`Summary`] about each file processed, and one [`SummaryPrinted`].
//!
//! [`Stage3StreamSyslines`]: s4lib::readers::syslogprocessor::ProcessingStage#variant.Stage3StreamSyslines
//! [`DateTimeL`]: s4lib::data::datetime::DateTimeL
//! [`Sysline`]: s4lib::data::sysline::Sysline
//! [sending channel]: self::ChanSendDatum
//! [`SyslogProcessor`]: s4lib::readers::syslogprocessor::SyslogProcessor
//! [`Summary`]: s4lib::readers::summary::Summary
//! [`SummaryPrinted`]: self::SummaryPrinted
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::process::ExitCode;
use std::str;
use std::thread;
extern crate chrono;
use chrono::{DateTime, Duration, FixedOffset, Local, TimeZone, Datelike, Timelike};
extern crate clap;
use clap::{ValueEnum, Parser};
extern crate const_format;
use const_format::concatcp;
extern crate crossbeam_channel;
extern crate lazy_static;
use lazy_static::lazy_static;
extern crate mime_guess;
use mime_guess::MimeGuess;
extern crate regex;
use regex::Regex;
extern crate si_trace_print;
use si_trace_print::{dpfn, dpfo, dpfx, dpfñ, dpn, dpo, stack::stack_offset_set};
extern crate unicode_width;
// `s4lib` is the local compiled `[lib]` of super_speedy_syslog_searcher
extern crate s4lib;
use s4lib::common::{Count, FPath, FPaths, FileOffset, FileType, NLu8a};
use s4lib::data::datetime::{
datetime_parse_from_str, datetime_parse_from_str_w_tz,
DateTimeLOpt, DateTimeParseInstr, DateTimePattern_str,
DATETIME_PARSE_DATAS, MAP_TZZ_TO_TZz, Utc,
};
#[allow(unused_imports)]
use s4lib::debug::printers::{dp_err, dp_wrn, p_err, p_wrn};
use s4lib::printer::printers::{
color_rand,
print_colored_stderr,
write_stdout,
// termcolor imports
Color,
ColorChoice,
PrinterSysline,
//
COLOR_DEFAULT,
COLOR_ERROR,
};
use s4lib::data::sysline::{SyslineP, SyslineP_Opt};
use s4lib::readers::blockreader::{BlockSz, BLOCKSZ_DEF, BLOCKSZ_MAX, BLOCKSZ_MIN};
use s4lib::readers::filepreprocessor::{process_path, ProcessPathResult, ProcessPathResults};
use s4lib::readers::helpers::basename;
use s4lib::readers::summary::{Summary, SummaryOpt};
use s4lib::readers::syslinereader::ResultS3SyslineFind;
use s4lib::readers::syslogprocessor::{FileProcessingResultBlockZero, SyslogProcessor};
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// command-line parsing
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
lazy_static! {
/// for user-passed strings of a duration that will be offset from the
/// current datetime.
static ref UTC_NOW: DateTime<Utc> = Utc::now();
static ref LOCAL_NOW: DateTime<Local> = DateTime::from(UTC_NOW.clone());
}
/// CLI enum that maps to [`termcolor::ColorChoice`].
///
/// [`termcolor::ColorChoice`]: https://docs.rs/termcolor/1.1.3/termcolor/enum.ColorChoice.html
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
ValueEnum, // from `clap`
)]
enum CLI_Color_Choice {
always,
auto,
never,
}
/// Subset of [`DateTimeParseInstr`] for calls to
/// function [`datetime_parse_from_str`].
///
/// (DateTimePattern_str, has year, has timezone, has time)
///
/// [`DateTimeParseInstr`]: s4lib::data::datetime::DateTimeParseInstr
/// [`datetime_parse_from_str`]: s4lib::data::datetime#fn.datetime_parse_from_str
type CLI_DT_Filter_Pattern<'b> = (&'b DateTimePattern_str, bool, bool, bool);
// TODO: reject ambiguous timezone names.
// best way to do this is to modify `DTPD!` defined in `datetime.rs` to
// have a flag, "is it acceptable for CLI?". Then gather those at
// run-time (or build-time), and iterate through them.
// This allows re-using the facilities built in datetime.rs, and not having
// divergent methods for transforming datetime string to `DateTimeL`.
const CLI_DT_FILTER_PATTERN1: CLI_DT_Filter_Pattern = ("%Y%m%dT%H%M%S", true, false, true);
const CLI_DT_FILTER_PATTERN2: CLI_DT_Filter_Pattern = ("%Y%m%dT%H%M%S%z", true, true, true);
const CLI_DT_FILTER_PATTERN3: CLI_DT_Filter_Pattern = ("%Y%m%dT%H%M%S%:z", true, true, true);
const CLI_DT_FILTER_PATTERN4: CLI_DT_Filter_Pattern = ("%Y%m%dT%H%M%S%#z", true, true, true);
const CLI_DT_FILTER_PATTERN5: CLI_DT_Filter_Pattern = ("%Y%m%dT%H%M%S%Z", true, true, true);
const CLI_DT_FILTER_PATTERN6: CLI_DT_Filter_Pattern = ("%Y-%m-%d %H:%M:%S", true, false, true);
const CLI_DT_FILTER_PATTERN7: CLI_DT_Filter_Pattern = ("%Y-%m-%d %H:%M:%S %z", true, true, true);
const CLI_DT_FILTER_PATTERN8: CLI_DT_Filter_Pattern = ("%Y-%m-%d %H:%M:%S %:z", true, true, true);
const CLI_DT_FILTER_PATTERN9: CLI_DT_Filter_Pattern = ("%Y-%m-%d %H:%M:%S %#z", true, true, true);
const CLI_DT_FILTER_PATTERN10: CLI_DT_Filter_Pattern = ("%Y-%m-%d %H:%M:%S %Z", true, true, true);
const CLI_DT_FILTER_PATTERN11: CLI_DT_Filter_Pattern = ("%Y-%m-%dT%H:%M:%S", true, false, true);
const CLI_DT_FILTER_PATTERN12: CLI_DT_Filter_Pattern = ("%Y-%m-%dT%H:%M:%S %z", true, true, true);
const CLI_DT_FILTER_PATTERN13: CLI_DT_Filter_Pattern = ("%Y-%m-%dT%H:%M:%S %:z", true, true, true);
const CLI_DT_FILTER_PATTERN14: CLI_DT_Filter_Pattern = ("%Y-%m-%dT%H:%M:%S %#z", true, true, true);
const CLI_DT_FILTER_PATTERN15: CLI_DT_Filter_Pattern = ("%Y-%m-%dT%H:%M:%S %Z", true, true, true);
const CLI_DT_FILTER_PATTERN16: CLI_DT_Filter_Pattern = ("%Y/%m/%d %H:%M:%S", true, false, true);
const CLI_DT_FILTER_PATTERN17: CLI_DT_Filter_Pattern = ("%Y/%m/%d %H:%M:%S %z", true, true, true);
const CLI_DT_FILTER_PATTERN18: CLI_DT_Filter_Pattern = ("%Y/%m/%d %H:%M:%S %:z", true, true, true);
const CLI_DT_FILTER_PATTERN19: CLI_DT_Filter_Pattern = ("%Y/%m/%d %H:%M:%S %#z", true, true, true);
const CLI_DT_FILTER_PATTERN20: CLI_DT_Filter_Pattern = ("%Y/%m/%d %H:%M:%S %Z", true, true, true);
const CLI_DT_FILTER_PATTERN21: CLI_DT_Filter_Pattern = ("%Y%m%d", true, false, false);
const CLI_DT_FILTER_PATTERN22: CLI_DT_Filter_Pattern = ("%Y-%m-%d", true, false, false);
const CLI_DT_FILTER_PATTERN23: CLI_DT_Filter_Pattern = ("%Y/%m/%d", true, false, false);
const CLI_DT_FILTER_PATTERN24: CLI_DT_Filter_Pattern = ("%Y%m%d %z", true, true, false);
const CLI_DT_FILTER_PATTERN25: CLI_DT_Filter_Pattern = ("%Y%m%d %:z", true, true, false);
const CLI_DT_FILTER_PATTERN26: CLI_DT_Filter_Pattern = ("%Y%m%d %#z", true, true, false);
const CLI_DT_FILTER_PATTERN27: CLI_DT_Filter_Pattern = ("%Y%m%d %Z", true, true, false);
const CLI_DT_FILTER_PATTERN28: CLI_DT_Filter_Pattern = ("+%s", false, false, true);
const CLI_FILTER_PATTERNS_COUNT: usize = 28;
/// CLI acceptable datetime filter patterns for the user-passed `-a` or `-b`
const CLI_FILTER_PATTERNS: [&CLI_DT_Filter_Pattern; CLI_FILTER_PATTERNS_COUNT] = [
&CLI_DT_FILTER_PATTERN1,
&CLI_DT_FILTER_PATTERN2,
&CLI_DT_FILTER_PATTERN3,
&CLI_DT_FILTER_PATTERN4,
&CLI_DT_FILTER_PATTERN5,
&CLI_DT_FILTER_PATTERN6,
&CLI_DT_FILTER_PATTERN7,
&CLI_DT_FILTER_PATTERN8,
&CLI_DT_FILTER_PATTERN9,
&CLI_DT_FILTER_PATTERN10,
&CLI_DT_FILTER_PATTERN11,
&CLI_DT_FILTER_PATTERN12,
&CLI_DT_FILTER_PATTERN13,
&CLI_DT_FILTER_PATTERN14,
&CLI_DT_FILTER_PATTERN15,
&CLI_DT_FILTER_PATTERN16,
&CLI_DT_FILTER_PATTERN17,
&CLI_DT_FILTER_PATTERN18,
&CLI_DT_FILTER_PATTERN19,
&CLI_DT_FILTER_PATTERN20,
&CLI_DT_FILTER_PATTERN21,
&CLI_DT_FILTER_PATTERN22,
&CLI_DT_FILTER_PATTERN23,
&CLI_DT_FILTER_PATTERN24,
&CLI_DT_FILTER_PATTERN25,
&CLI_DT_FILTER_PATTERN26,
&CLI_DT_FILTER_PATTERN27,
&CLI_DT_FILTER_PATTERN28,
];
const CGN_DUR_OFFSET_TYPE: &str = "offset_type";
const CGN_DUR_OFFSET_ADDSUB: &str = "offset_addsub";
const CGN_DUR_OFFSET_SECONDS: &str = "seconds";
const CGN_DUR_OFFSET_MINUTES: &str = "minutes";
const CGN_DUR_OFFSET_HOURS: &str = "hours";
const CGN_DUR_OFFSET_DAYS: &str = "days";
const CGN_DUR_OFFSET_WEEKS: &str = "weeks";
const CGP_DUR_OFFSET_TYPE: &str = concatcp!("(?P<", CGN_DUR_OFFSET_TYPE, r">[@]?)");
const CGP_DUR_OFFSET_ADDSUB: &str = concatcp!("(?P<", CGN_DUR_OFFSET_ADDSUB, r">[+\-])");
const CGP_DUR_OFFSET_SECONDS: &str = concatcp!("(?P<", CGN_DUR_OFFSET_SECONDS, r">[\d]+s)");
const CGP_DUR_OFFSET_MINUTES: &str = concatcp!("(?P<", CGN_DUR_OFFSET_MINUTES, r">[\d]+m)");
const CGP_DUR_OFFSET_HOURS: &str = concatcp!("(?P<", CGN_DUR_OFFSET_HOURS, r">[\d]+h)");
const CGP_DUR_OFFSET_DAYS: &str = concatcp!("(?P<", CGN_DUR_OFFSET_DAYS, r">[\d]+d)");
const CGP_DUR_OFFSET_WEEKS: &str = concatcp!("(?P<", CGN_DUR_OFFSET_WEEKS, r">[\d]+w)");
lazy_static! {
/// user-passed strings of a duration that is a relative offset.
static ref REGEX_DUR_OFFSET: Regex = {
Regex::new(
concatcp!(
CGP_DUR_OFFSET_TYPE,
CGP_DUR_OFFSET_ADDSUB, "(",
CGP_DUR_OFFSET_SECONDS, "|",
CGP_DUR_OFFSET_MINUTES, "|",
CGP_DUR_OFFSET_HOURS, "|",
CGP_DUR_OFFSET_DAYS, "|",
CGP_DUR_OFFSET_WEEKS,
")+"
)
).unwrap()
};
}
/// Duration offset type; for CLI options `-a` and `-b` relative offset value.
/// Either relative offset from now (program run-time) or relative offset
/// from the other CLI option.
#[derive(Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
enum DUR_OFFSET_TYPE {
Now,
Other,
}
/// Duration offset is added or subtracted from a `DateTime`?
#[derive(Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
enum DUR_OFFSET_ADDSUB {
Add = 1,
Sub = -1
}
/// CLI time to append in `fn process_dt` when `has_time` is `false`.
const CLI_DT_FILTER_APPEND_TIME_VALUE: &str = " T000000";
/// CLI strftime format pattern to append in function `process_dt`
/// when `has_time` is `false`.
const CLI_DT_FILTER_APPEND_TIME_PATTERN: &str = " T%H%M%S";
/// default separator for prepended strings
const CLI_PREPEND_SEP: &str = ":";
/// default CLI datetime format printed for CLI options `-u` or `-l`.
const CLI_OPT_PREPEND_FMT: &str = "%Y%m%dT%H%M%S%.3f%z";
/// `--help` _afterword_ message.
const CLI_HELP_AFTER: &str = concatcp!(
"\
DateTime Filters may be strftime specifier patterns:
\"",
CLI_DT_FILTER_PATTERN1.0,
"\"
\"",
CLI_DT_FILTER_PATTERN2.0,
"\"
\"",
CLI_DT_FILTER_PATTERN3.0,
"\"
\"",
CLI_DT_FILTER_PATTERN4.0,
"\"
\"",
CLI_DT_FILTER_PATTERN5.0,
"\"
\"",
CLI_DT_FILTER_PATTERN6.0,
"\"
\"",
CLI_DT_FILTER_PATTERN7.0,
"\"
\"",
CLI_DT_FILTER_PATTERN8.0,
"\"
\"",
CLI_DT_FILTER_PATTERN9.0,
"\"
\"",
CLI_DT_FILTER_PATTERN10.0,
"\"
\"",
CLI_DT_FILTER_PATTERN11.0,
"\"
\"",
CLI_DT_FILTER_PATTERN12.0,
"\"
\"",
CLI_DT_FILTER_PATTERN13.0,
"\"
\"",
CLI_DT_FILTER_PATTERN14.0,
"\"
\"",
CLI_DT_FILTER_PATTERN15.0,
"\"
\"",
CLI_DT_FILTER_PATTERN16.0,
"\"
\"",
CLI_DT_FILTER_PATTERN17.0,
"\"
\"",
CLI_DT_FILTER_PATTERN18.0,
"\"
\"",
CLI_DT_FILTER_PATTERN19.0,
"\"
\"",
CLI_DT_FILTER_PATTERN20.0,
"\"
\"",
CLI_DT_FILTER_PATTERN21.0,
"\"
\"",
CLI_DT_FILTER_PATTERN22.0,
"\"
\"",
CLI_DT_FILTER_PATTERN23.0,
"\"
\"",
CLI_DT_FILTER_PATTERN24.0,
"\"
\"",
CLI_DT_FILTER_PATTERN25.0,
"\"
\"",
CLI_DT_FILTER_PATTERN26.0,
"\"
\"",
CLI_DT_FILTER_PATTERN27.0,
"\"
\"",
CLI_DT_FILTER_PATTERN28.0,
"\"
Or, DateTime Filter may be custom relative offset patterns:
\"+DwDdDhDmDs\" or \"-DwDdDhDmDs\"
\"@+DwDdDhDmDs\" or \"@-DwDdDhDmDs\"
Pattern \"+%s\" is Unix epoch timestamp in seconds with a preceding \"+\".
For example, value \"+946684800\" is be January 1, 2000 at 00:00, GMT.
Custom relative offset pattern \"+DwDdDhDmDs\" and \"-DwDdDhDmDs\" is the offset
from now (program start time) where \"D\" is a decimal number.
Each lowercase identifier is an offset duration:
\"w\" is weeks, \"d\" is days, \"h\" is hours, \"m\" is minutes, \"s\" is seconds.
For example, value \"-1w22h\" is one week and twenty-two hours in the past.
Value \"+30s\" is thirty seconds in the future.
Custom relative offset pattern \"@+DwDdDhDmDs\" and \"@-DwDdDhDmDs\" is relative
offset from the other datetime.
Arguments \"-a 20220102 -b @+1d\" are equivalent to \"-a 20220102 -b 20220103\".
Arguments \"-a @-6h -b 20220101T120000\" are equivalent to
\"-a 20220101T060000 -b 20220101T120000\".
Without a timezone offset (strftime specifier \"%z\" or \"%Z\"),
the Datetime Filter is presumed to be the local system timezone.
Ambiguous named timezones will be rejected, e.g. \"SST\".
Resolved values of \"--dt-after\" and \"--dt-before\" can be reviewed in
the \"--summary\" output.
DateTime strftime specifiers are described at https://docs.rs/chrono/latest/chrono/format/strftime/
DateTimes supported are only of the Gregorian calendar.
DateTimes supported language is English."
);
/// clap command-line arguments build-time definitions.
//
// Useful clap references:
// * inference types <https://github.com/clap-rs/clap/blob/v3.1.6/examples/derive_ref/README.md#arg-types>
// * other `clap::App` options <https://docs.rs/clap/latest/clap/struct.App.html>
//
// Note:
// * the `about` is taken from `Cargo.toml:[package]:description`.
#[derive(Parser, Debug)]
#[clap(
version,
about,
after_help = CLI_HELP_AFTER,
)]
struct CLI_Args {
/// Path(s) of syslog files or directories.
/// Directories will be recursed, remaining on the same filesystem.
/// Symlinks will be followed.
#[clap(required = true)]
paths: Vec<String>,
/// DateTime Filter after.
#[clap(
short = 'a',
long,
help = "DateTime Filter After: print syslog lines with a datetime that is at or after this datetime. For example, \"20200102T120000\" or \"-5d\""
)]
dt_after: Option<String>,
/// DateTime Filter before.
#[clap(
short = 'b',
long,
help = "DateTime Filter Before: print syslog lines with a datetime that is at or before this datetime. For example, \"20200103T230000\" or \"@+1d+11h\""
)]
dt_before: Option<String>,
/// Default timezone offset for datetimes without a timezone.
#[clap(
short = 't',
long,
help = "DateTime Timezone Offset for syslines with a datetime that does not include a timezone, this will be used. For example, \"-0800\", \"+02:00\", or \"EDT\". Ambiguous named timezones parsed from logs will use this value, e.g. timezone \"IST\". (to pass a value with leading \"-\", use \", e.g. \"-t=-0800\"). Default is local system timezone offset.",
value_parser = cli_process_tz_offset,
default_value_t=*Local.timestamp(0, 0).offset(),
)]
tz_offset: FixedOffset,
/// Prepend DateTime in the UTC Timezone for every line.
#[clap(
short = 'u',
long = "prepend-utc",
groups = &[
"group_prepend_dt",
],
)]
prepend_utc: bool,
/// Prepend DateTime in the Local Timezone for every line.
#[clap(
short = 'l',
long = "prepend-local",
groups = &[
"group_prepend_dt",
]
)]
prepend_local: bool,
/// Prepend DateTime using strftime format string.
#[clap(
short = 'd',
long = "prepend-dt-format",
groups = &[
"group_prepend_dt_format",
],
requires = "group_prepend_dt",
value_parser = cli_parser_prepend_dt_format,
default_value_t = String::from(CLI_OPT_PREPEND_FMT),
)]
prepend_dt_format: String,
/// Prepend file basename to every line.
#[clap(
short = 'n',
long = "prepend-filename",
groups = &[
"group_prepend_file",
]
)]
prepend_filename: bool,
/// Prepend file full path to every line.
#[clap(
short = 'p',
long = "prepend-filepath",
groups = &[
"group_prepend_file",
]
)]
prepend_filepath: bool,
/// Align column widths of prepended data.
#[clap(
short = 'w',
long = "prepend-file-align",
requires = "group_prepend_file",
)]
prepend_file_align: bool,
/// Separator string for prepended data.
#[clap(
long = "prepend-separator",
// TODO: how to require `any("prepend_file", "prepend_dt")`
default_value_t = String::from(CLI_PREPEND_SEP)
)]
prepend_separator: String,
/// Choose to print to terminal using colors.
#[clap(
required = false,
short = 'c',
long = "color",
value_enum,
default_value_t=CLI_Color_Choice::auto,
)]
color_choice: CLI_Color_Choice,
/// Read blocks of this size in bytes.
/// May pass value as any radix (hexadecimal, decimal, octal, binary).
/// Using the default value is recommended.
/// Most useful for developers.
#[clap(
required = false,
short = 'z',
long,
default_value_t = BLOCKSZ_DEF.to_string(),
value_parser = cli_parse_blocksz,
)]
blocksz: String,
/// Print a summary of files processed to stderr.
/// Most useful for developers.
#[clap(short, long)]
summary: bool,
}
/// `clap` argument processor for `--blocksz`.
/// This implementation, as opposed to clap built-in number parsing, allows more
/// flexibility for how the user may pass a number
/// e.g. "0xF00", or "0b10100", etc.
fn cli_process_blocksz(blockszs: &String) -> std::result::Result<u64, String> {
// TODO: there must be a more concise way to parse numbers with radix formatting
let blocksz_: BlockSz;
let errs = format!("Unable to parse a number for --blocksz {:?}", blockszs);
if blockszs.starts_with("0x") {
blocksz_ = match BlockSz::from_str_radix(blockszs.trim_start_matches("0x"), 16) {
Ok(val) => val,
Err(err) => return Err(format!("{} {}", errs, err)),
};
} else if blockszs.starts_with("0o") {
blocksz_ = match BlockSz::from_str_radix(blockszs.trim_start_matches("0o"), 8) {
Ok(val) => val,
Err(err) => return Err(format!("{} {}", errs, err)),
};
} else if blockszs.starts_with("0b") {
blocksz_ = match BlockSz::from_str_radix(blockszs.trim_start_matches("0b"), 2) {
Ok(val) => val,
Err(err) => return Err(format!("{} {}", errs, err)),
};
} else {
blocksz_ = match blockszs.parse::<BlockSz>() {
Ok(val) => val,
Err(err) => return Err(format!("{} {}", errs, err)),
};
}
let max_min = std::cmp::max(BLOCKSZ_MIN, SyslogProcessor::BLOCKSZ_MIN);
if !(max_min <= blocksz_ && blocksz_ <= BLOCKSZ_MAX) {
return Err(format!(
"--blocksz must be {} ≤ BLOCKSZ ≤ {}, it was {:?}",
max_min, BLOCKSZ_MAX, blockszs
));
}
Ok(blocksz_)
}
/// `clap` argument parser for `--blocksz`.
fn cli_parse_blocksz(blockszs: &str) -> Result<String, String> {
match cli_process_blocksz(&String::from(blockszs)) {
Ok(val) => {
Ok(val.to_string())
}
Err(err) => {
Err(err)
}
}
}
/// CLI argument processing
fn cli_process_tz_offset(tzo: &str) -> std::result::Result<FixedOffset, String> {
let tzo_ = match MAP_TZZ_TO_TZz.get(tzo) {
Some(tz_offset) => {
match tz_offset.is_empty() {
// an empty value signifies an ambiguous named timezone
true => {
return Err(
format!("Given ambiguous timezone {:?} (this timezone abbreviation refers to several timezone offsets)", tzo)
);
}
// unambiguous named timezone passed
false => tz_offset,
}
},
// no entry found, `tzo` is probably a numeric timezone offset,
// e.g. `+01:00`
None => tzo,
};
// transform the timezone string to a `FixedOffset` instance
// using a dummy `DateTimeL`
let mut data: String = String::from("2000-01-02 03:04:05 ");
data.push_str(tzo_);
for pattern in [
"%Y-%m-%d %H:%M:%S %:z",
"%Y-%m-%d %H:%M:%S %z",
"%Y-%m-%d %H:%M:%S %#z",
] {
let dt = datetime_parse_from_str_w_tz(
data.as_str(), pattern,
);
dpfo!("datetime_parse_from_str_w_tz({:?}, {:?}) returned {:?}", data, pattern, dt);
match dt {
Some(dt_) => {
dpfx!("return {:?}", dt_.offset());
return Ok(*dt_.offset());
}
None => {}
}
};
Err(
format!("Unable to parse a timezone offset for --tz-offset {:?}", tzo)
)
}
/// `clap` argument validator for `--prepend-dt-format`.
fn cli_parser_prepend_dt_format(prepend_dt_format: &str) -> std::result::Result<String, String> {
let dt = Utc.ymd(2000, 1, 1).and_hms(0, 0, 0);
dt.format(prepend_dt_format);
Ok(String::from(prepend_dt_format))
}
// maps named capture group matches of `CGP_DUR_OFFSET_TYPE` to
// `DUR_OFFSET_TYPE`
// helper to `string_wdhms_to_duration`
fn offset_match_to_offset_duration_type(offset_str: &str) -> DUR_OFFSET_TYPE {
match offset_str.chars().next() {
Some('@') => {
DUR_OFFSET_TYPE::Other
}
_ => {
DUR_OFFSET_TYPE::Now
}
}
}
// maps named capture group matches of `CGP_DUR_OFFSET_ADDSUB` to
// `DUR_OFFSET_ADDSUB`
// helper to `string_wdhms_to_duration`
fn offset_match_to_offset_addsub(offset_str: &str) -> DUR_OFFSET_ADDSUB {
match offset_str.chars().next() {
Some('+') => {
DUR_OFFSET_ADDSUB::Add
}
Some('-') => {
DUR_OFFSET_ADDSUB::Sub
}
_ => {
panic!("Bad match offset_str {:?}, cannot determine DUR_OFFSET_ADDSUB", offset_str);
}
}
}
// regular expression processing of a user-passed duration string like `"-4m2s"`
// becomes duration of 4 minutes + 2 seconds
// helper function to `process_dt`
fn string_wdhms_to_duration(val: &String) -> Option<(Duration, DUR_OFFSET_TYPE)> {
dpfn!("({:?})", val);
let mut duration_offset_type: DUR_OFFSET_TYPE = DUR_OFFSET_TYPE::Now;
let mut duration_addsub: DUR_OFFSET_ADDSUB = DUR_OFFSET_ADDSUB::Add;
let mut seconds: i64 = 0;
let mut minutes: i64 = 0;
let mut hours: i64 = 0;
let mut days: i64 = 0;
let mut weeks: i64 = 0;
let captures = match REGEX_DUR_OFFSET.captures(val.as_str()) {
Some(caps) => caps,
None => {
dpfx!("REGEX_DUR_OFFSET.captures(…) None");
return None;
}
};
match captures.name(CGN_DUR_OFFSET_TYPE) {
Some(match_) => {
dpfo!("matched named group {:?}, match {:?}", CGN_DUR_OFFSET_TYPE, match_.as_str());
duration_offset_type = offset_match_to_offset_duration_type(match_.as_str());
}
None => {}
}
match captures.name(CGN_DUR_OFFSET_ADDSUB) {
Some(match_) => {
dpfo!("matched named group {:?}, match {:?}", CGN_DUR_OFFSET_ADDSUB, match_.as_str());
duration_addsub = offset_match_to_offset_addsub(match_.as_str());
}
None => {}
}
let addsub: i64 = duration_addsub as i64;
match captures.name(CGN_DUR_OFFSET_SECONDS) {
Some(match_) => {
dpfo!("matched named group {:?}, match {:?}", CGN_DUR_OFFSET_SECONDS, match_.as_str());
let s_count = match_.as_str().replace("s", "");
match i64::from_str_radix(s_count.as_str(), 10) {
Ok(val) => {
seconds = val * addsub;
}
Err(err) => {
eprintln!("ERROR: Unable to parse seconds from {:?} {}", match_.as_str(), err);
std::process::exit(1);
}
}
}
None => {}
}
match captures.name(CGN_DUR_OFFSET_MINUTES) {
Some(match_) => {
dpfo!("matched named group {:?}, match {:?}", CGN_DUR_OFFSET_MINUTES, match_.as_str());
let s_count = match_.as_str().replace("m", "");
match i64::from_str_radix(s_count.as_str(), 10) {
Ok(val) => {
minutes = val * addsub;
}
Err(err) => {
eprintln!("ERROR: Unable to parse minutes from {:?} {}", match_.as_str(), err);
std::process::exit(1);
}
}
}
None => {}
}
match captures.name(CGN_DUR_OFFSET_HOURS) {
Some(match_) => {
dpfo!("matched named group {:?}, match {:?}", CGN_DUR_OFFSET_HOURS, match_.as_str());
let s_count = match_.as_str().replace("h", "");
match i64::from_str_radix(s_count.as_str(), 10) {
Ok(val) => {
hours = val * addsub;
}
Err(err) => {
eprintln!("ERROR: Unable to parse hours from {:?} {}", match_.as_str(), err);
std::process::exit(1);
}
}
}
None => {}
}
match captures.name(CGN_DUR_OFFSET_DAYS) {
Some(match_) => {
dpfo!("matched named group {:?}, match {:?}", CGN_DUR_OFFSET_DAYS, match_.as_str());
let s_count = match_.as_str().replace("d", "");
match i64::from_str_radix(s_count.as_str(), 10) {
Ok(val) => {
days = val * addsub;
}
Err(err) => {
eprintln!("ERROR: Unable to parse days from {:?} {}", match_.as_str(), err);
std::process::exit(1);
}
}
}
None => {}
}
match captures.name(CGN_DUR_OFFSET_WEEKS) {
Some(match_) => {
dpfo!("matched named group {:?}, match {:?}", CGN_DUR_OFFSET_WEEKS, match_.as_str());
let s_count = match_.as_str().replace("w", "");
match i64::from_str_radix(s_count.as_str(), 10) {
Ok(val) => {
weeks = val * addsub;
}
Err(err) => {
eprintln!("ERROR: Unable to parse weeks from {:?} {}", match_.as_str(), err);
std::process::exit(1);
}
}
}
None => {}
}
let duration = Duration::seconds(seconds)
+ Duration::minutes(minutes)
+ Duration::hours(hours)
+ Duration::days(days)
+ Duration::weeks(weeks);
dpfx!("return {:?}, {:?}", duration, duration_offset_type);
Some((duration, duration_offset_type))
}
// Process duration string like `"-4m2s"` as relative offset of now,
// or relative offset of other user-passed datetime argument (`dt_other`).
// `val="-1d"` is one day ago.
// `val="+1m"` is one day added to the `dt_other`.
// helper function to function `process_dt`.
fn string_to_rel_offset_datetime(val: &String, tz_offset: &FixedOffset, dt_other_opt: &DateTimeLOpt, now_utc: &DateTime<Utc>) -> DateTimeLOpt {
let (duration, duration_offset_type) = match string_wdhms_to_duration(val) {
Some((dur, dur_type)) => (dur, dur_type),
None => {
return None;
}
};
match duration_offset_type {
DUR_OFFSET_TYPE::Now => {
// drop fractional seconds
let now_utc_ = Utc
.ymd(now_utc.year(), now_utc.month(), now_utc.day())
.and_hms(now_utc.hour(), now_utc.minute(), now_utc.second());
// convert `Utc` to `DateTimeL`
let now =
tz_offset.from_utc_datetime(&now_utc_.naive_utc());
dpfo!("now {:?}", now);
let now_off = now.checked_add_signed(duration);
dpfo!("now_sub {:?}", now_off.unwrap());
now_off
}
DUR_OFFSET_TYPE::Other => {
match dt_other_opt {
Some(dt_other) => {
dpfo!("other {:?}", dt_other);
let other_off = dt_other.checked_add_signed(duration);
dpfo!("other_off {:?}", other_off.unwrap());
other_off
}
None => {
eprintln!("ERROR: passed relative offset to other datetime {:?}, but other datetime was not set", val);
std::process::exit(1);
}
}
}
}
}
/// Transform a user-passed datetime `String` into a [`DateTimeL`].
///
/// Helper function to function `cli_process_args`.
///
/// [`DateTimeL`]: s4lib::data::datetime::DateTimeL
fn process_dt(
dts_opt: Option<String>,
tz_offset: &FixedOffset,
dt_other: &DateTimeLOpt,
now_utc: &DateTime<Utc>,
) -> DateTimeLOpt {
dpfn!("({:?}, {:?}, {:?}, {:?})", dts_opt, tz_offset, dt_other, now_utc);
// parse datetime filters
let dts = match dts_opt {
Some(dts) => dts,
None => {
return None;
}
};
let dto: DateTimeLOpt;
// try to match user-passed string to chrono strftime format strings
for (pattern_, _has_year, has_tz, has_time) in CLI_FILTER_PATTERNS.iter() {
let mut pattern: String = String::from(*pattern_);
let mut dts_: String = dts.clone();
// if !has_time then modify the value and pattern
// e.g. `"20220101"` becomes `"20220101 T000000"`
// `"%Y%d%m"` becomes `"%Y%d%m T%H%M%S"`
if !has_time {
dts_.push_str(CLI_DT_FILTER_APPEND_TIME_VALUE);
pattern.push_str(CLI_DT_FILTER_APPEND_TIME_PATTERN);
dpfo!(
"appended {:?}, {:?}",
CLI_DT_FILTER_APPEND_TIME_VALUE,
CLI_DT_FILTER_APPEND_TIME_PATTERN
);
}
dpfo!("datetime_parse_from_str({:?}, {:?}, {:?}, {:?})", dts_, pattern, has_tz, tz_offset);
if let Some(val) =
datetime_parse_from_str(dts_.as_str(), pattern.as_str(), *has_tz, tz_offset)
{
dto = Some(val);
dpfx!("return {:?}", dto);
return dto;
};
} // end for … in CLI_FILTER_PATTERNS
// could not match specific datetime pattern
// try relative offset pattern matching, e.g. `"-30m5s"`, `"+2d"`
dto = match string_to_rel_offset_datetime(&dts, tz_offset, dt_other, now_utc) {
Some(dto) => {
Some(dto)
}
None => None
};
// user-passed string was not parseable
if dto.is_none() {
eprintln!("ERROR: Unable to parse a datetime from {:?}", dts);
std::process::exit(1);
}
dpfx!("return {:?}", dto);
dto
}
/// Process user-passed CLI argument strings into expected types.
///
/// This function will [`std::process::exit`] if there is an [`Err`].
fn cli_process_args(
) -> (FPaths, BlockSz, DateTimeLOpt, DateTimeLOpt, FixedOffset, ColorChoice, bool, bool, String, bool, bool, bool, String, bool)
{
let args = CLI_Args::parse();
dpfo!("args {:?}", args);
//
// process string arguments into specific types
//
let blockszs: String = args.blocksz;
let blocksz: BlockSz = match cli_process_blocksz(&blockszs) {
Ok(val) => val,
Err(err) => {
eprintln!("ERROR: {}", err);
std::process::exit(1);
}
};
dpfo!("blocksz {:?}", blocksz);
let mut fpaths: Vec<FPath> = Vec::<FPath>::new();
for path in args.paths.iter() {
fpaths.push(path.clone());
}
let tz_offset: FixedOffset = args.tz_offset;
dpfo!("tz_offset {:?}", tz_offset);
let filter_dt_after: DateTimeLOpt;
let filter_dt_before: DateTimeLOpt;
let empty_str: String = String::from("");
let args_dt_after_s: &String = args.dt_after.as_ref().unwrap_or(&empty_str);
let args_dt_before_s: &String = args.dt_before.as_ref().unwrap_or(&empty_str);
// peek at `-a` and `-b` values:
// if both are relative to the other then print error message and exit
// if `-a` is relative to `-b` then process `-b` first
// else process `-a` then `-b`
match (string_wdhms_to_duration(args_dt_after_s), string_wdhms_to_duration(args_dt_before_s)) {
(Some((_, DUR_OFFSET_TYPE::Other)), Some((_, DUR_OFFSET_TYPE::Other))) => {
eprintln!("ERROR: cannot pass both --dt-after and --dt-before as relative to the other");
std::process::exit(1);
}
(Some((_, DUR_OFFSET_TYPE::Other)), _) => {
// special-case: process `-b` value then process `-a` value
// e.g. `-a "@+1d" -b "20010203"`
filter_dt_before = process_dt(args.dt_before, &tz_offset, &None, &UTC_NOW);
dpfo!("filter_dt_before {:?}", filter_dt_before);
filter_dt_after = process_dt(args.dt_after, &tz_offset, &filter_dt_before, &UTC_NOW);
dpfo!("filter_dt_after {:?}", filter_dt_after);
}
_ => {
// normal case: process `-a` value then process `-b` value
filter_dt_after = process_dt(args.dt_after, &tz_offset, &None, &UTC_NOW);
dpfo!("filter_dt_after {:?}", filter_dt_after);
filter_dt_before = process_dt(args.dt_before, &tz_offset, &filter_dt_after, &UTC_NOW);
dpfo!("filter_dt_before {:?}", filter_dt_before);
}
}
#[allow(clippy::single_match)]
match (filter_dt_after, filter_dt_before) {
(Some(dta), Some(dtb)) => {
if dta > dtb {
eprintln!("ERROR: Datetime --dt-after ({}) is after Datetime --dt-before ({})", dta, dtb);
std::process::exit(1);