-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
log_format_impls.cc
2100 lines (1806 loc) · 71.1 KB
/
log_format_impls.cc
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
/**
* Copyright (c) 2007-2017, Timothy Stack
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @file log_format_impls.cc
*/
#include <algorithm>
#include <utility>
#include "log_format.hh"
#include <stdio.h>
#include "base/injector.bind.hh"
#include "base/opt_util.hh"
#include "config.h"
#include "formats/logfmt/logfmt.parser.hh"
#include "log_vtab_impl.hh"
#include "ptimec.hh"
#include "scn/scn.h"
#include "sql_util.hh"
#include "yajlpp/yajlpp.hh"
class piper_log_format : public log_format {
public:
const intern_string_t get_name() const override
{
static const intern_string_t RETVAL
= intern_string::lookup("lnav_piper_log");
return RETVAL;
}
scan_result_t scan(logfile& lf,
std::vector<logline>& dst,
const line_info& li,
shared_buffer_ref& sbr,
scan_batch_context& sbc) override
{
if (lf.has_line_metadata()
&& lf.get_text_format() == text_format_t::TF_LOG)
{
dst.emplace_back(
li.li_file_range.fr_offset, li.li_timestamp, li.li_level);
return scan_match{1};
}
return scan_no_match{""};
}
void annotate(logfile* lf,
uint64_t line_number,
string_attrs_t& sa,
logline_value_vector& values,
bool annotate_module) const override
{
auto lr = line_range{0, 0};
sa.emplace_back(lr, logline::L_TIMESTAMP.value());
log_format::annotate(lf, line_number, sa, values, annotate_module);
}
void get_subline(const logline& ll,
shared_buffer_ref& sbr,
bool full_message) override
{
this->plf_cached_line.resize(32);
auto tlen = sql_strftime(this->plf_cached_line.data(),
this->plf_cached_line.size(),
ll.get_timeval(),
'T');
this->plf_cached_line.resize(tlen);
{
char zone_str[16];
exttm tmptm;
tmptm.et_flags |= ETF_ZONE_SET;
tmptm.et_gmtoff
= lnav::local_time_to_info(
date::local_seconds{std::chrono::seconds{ll.get_time()}})
.first.offset.count();
off_t zone_len = 0;
ftime_z(zone_str, zone_len, sizeof(zone_str), tmptm);
for (off_t lpc = 0; lpc < zone_len; lpc++) {
this->plf_cached_line.push_back(zone_str[lpc]);
}
}
this->plf_cached_line.push_back(' ');
const auto prefix_len = this->plf_cached_line.size();
this->plf_cached_line.resize(this->plf_cached_line.size()
+ sbr.length());
memcpy(
&this->plf_cached_line[prefix_len], sbr.get_data(), sbr.length());
sbr.share(this->plf_share_manager,
this->plf_cached_line.data(),
this->plf_cached_line.size());
}
std::shared_ptr<log_format> specialized(int fmt_lock) override
{
auto retval = std::make_shared<piper_log_format>(*this);
retval->lf_specialized = true;
retval->lf_timestamp_flags |= ETF_ZONE_SET;
return retval;
}
private:
shared_buffer plf_share_manager;
std::vector<char> plf_cached_line;
};
class generic_log_format : public log_format {
public:
static const pcre_format* get_pcre_log_formats()
{
static const pcre_format log_fmt[] = {
pcre_format(
"^(?:\\*\\*\\*\\s+)?(?<timestamp>@[0-9a-zA-Z]{16,24})(.*)"),
pcre_format(
R"(^(?:\*\*\*\s+)?(?<timestamp>(?:\s|\d{4}[\-\/]\d{2}[\-\/]\d{2}|T|\d{1,2}:\d{2}(?::\d{2}(?:[\.,]\d{1,6})?)?|Z|[+\-]\d{2}:?\d{2}|(?!DBG|ERR|INFO|WARN|NONE)[A-Z]{3,4})+)(?:\s+|[:|])([^:]+))"),
pcre_format(
"^(?:\\*\\*\\*\\s+)?(?<timestamp>[\\w:+/\\.-]+) \\[\\w (.*)"),
pcre_format("^(?:\\*\\*\\*\\s+)?(?<timestamp>[\\w:,/\\.-]+) (.*)"),
pcre_format(
"^(?:\\*\\*\\*\\s+)?(?<timestamp>[\\w:,/\\.-]+) - (.*)"),
pcre_format(
"^(?:\\*\\*\\*\\s+)?(?<timestamp>[\\w: \\.,/-]+) - (.*)"),
pcre_format("^(?:\\*\\*\\*\\s+)?(?<timestamp>[\\w: "
"\\.,/-]+)\\[[^\\]]+\\](.*)"),
pcre_format("^(?:\\*\\*\\*\\s+)?(?<timestamp>[\\w: \\.,/-]+) (.*)"),
pcre_format(
R"(^(?:\*\*\*\s+)?\[(?<timestamp>[\w: \.,+/-]+)\]\s*(\w+):?)"),
pcre_format(
"^(?:\\*\\*\\*\\s+)?\\[(?<timestamp>[\\w: \\.,+/-]+)\\] (.*)"),
pcre_format("^(?:\\*\\*\\*\\s+)?\\[(?<timestamp>[\\w: "
"\\.,+/-]+)\\] \\[(\\w+)\\]"),
pcre_format("^(?:\\*\\*\\*\\s+)?\\[(?<timestamp>[\\w: "
"\\.,+/-]+)\\] \\w+ (.*)"),
pcre_format("^(?:\\*\\*\\*\\s+)?\\[(?<timestamp>[\\w: ,+/-]+)\\] "
"\\(\\d+\\) (.*)"),
pcre_format(),
};
return log_fmt;
}
std::string get_pattern_regex(uint64_t line_number) const override
{
int pat_index = this->pattern_index_for_line(line_number);
return get_pcre_log_formats()[pat_index].name;
}
const intern_string_t get_name() const override
{
static const intern_string_t RETVAL
= intern_string::lookup("generic_log");
return RETVAL;
}
scan_result_t scan(logfile& lf,
std::vector<logline>& dst,
const line_info& li,
shared_buffer_ref& sbr,
scan_batch_context& sbc) override
{
struct exttm log_time;
struct timeval log_tv;
string_fragment ts;
std::optional<string_fragment> level;
const char* last_pos;
if (dst.empty()) {
auto file_options = lf.get_file_options();
if (file_options) {
this->lf_date_time.dts_default_zone
= file_options->second.fo_default_zone.pp_value;
} else {
this->lf_date_time.dts_default_zone = nullptr;
}
}
if ((last_pos = this->log_scanf(dst.size(),
sbr.to_string_fragment(),
get_pcre_log_formats(),
nullptr,
&log_time,
&log_tv,
&ts,
&level))
!= nullptr)
{
log_level_t level_val = log_level_t::LEVEL_UNKNOWN;
if (level) {
level_val = string2level(level->data(), level->length());
}
if (!((log_time.et_flags & ETF_DAY_SET)
&& (log_time.et_flags & ETF_MONTH_SET)
&& (log_time.et_flags & ETF_YEAR_SET)))
{
this->check_for_new_year(dst, log_time, log_tv);
}
if (!(this->lf_timestamp_flags
& (ETF_MILLIS_SET | ETF_MICROS_SET | ETF_NANOS_SET))
&& !dst.empty() && dst.back().get_time() == log_tv.tv_sec
&& dst.back().get_millis() != 0)
{
auto log_ms
= std::chrono::milliseconds(dst.back().get_millis());
log_time.et_nsec
= std::chrono::duration_cast<std::chrono::nanoseconds>(
log_ms)
.count();
log_tv.tv_usec
= std::chrono::duration_cast<std::chrono::microseconds>(
log_ms)
.count();
}
dst.emplace_back(li.li_file_range.fr_offset, log_tv, level_val);
return scan_match{0};
}
return scan_no_match{"no patterns matched"};
}
void annotate(logfile* lf,
uint64_t line_number,
string_attrs_t& sa,
logline_value_vector& values,
bool annotate_module) const override
{
auto& line = values.lvv_sbr;
int pat_index = this->pattern_index_for_line(line_number);
const auto& fmt = get_pcre_log_formats()[pat_index];
int prefix_len = 0;
auto md = fmt.pcre->create_match_data();
auto match_res = fmt.pcre->capture_from(line.to_string_fragment())
.into(md)
.matches(PCRE2_NO_UTF_CHECK)
.ignore_error();
if (!match_res) {
return;
}
auto ts_cap = md[fmt.pf_timestamp_index].value();
auto lr = to_line_range(ts_cap.trim());
sa.emplace_back(lr, logline::L_TIMESTAMP.value());
values.lvv_values.emplace_back(TS_META, line, lr);
values.lvv_values.back().lv_meta.lvm_format = (log_format*) this;
prefix_len = ts_cap.sf_end;
auto level_cap = md[2];
if (level_cap) {
if (string2level(level_cap->data(), level_cap->length(), true)
!= LEVEL_UNKNOWN)
{
prefix_len = level_cap->sf_end;
values.lvv_values.emplace_back(
LEVEL_META, line, to_line_range(level_cap->trim()));
values.lvv_values.back().lv_meta.lvm_format
= (log_format*) this;
}
}
lr.lr_start = 0;
lr.lr_end = prefix_len;
sa.emplace_back(lr, logline::L_PREFIX.value());
lr.lr_start = prefix_len;
lr.lr_end = line.length();
sa.emplace_back(lr, SA_BODY.value());
log_format::annotate(lf, line_number, sa, values, annotate_module);
}
std::shared_ptr<log_format> specialized(int fmt_lock) override
{
auto retval = std::make_shared<generic_log_format>(*this);
retval->lf_specialized = true;
return retval;
}
bool hide_field(const intern_string_t field_name, bool val) override
{
if (field_name == TS_META.lvm_name) {
TS_META.lvm_user_hidden = val;
return true;
} else if (field_name == LEVEL_META.lvm_name) {
LEVEL_META.lvm_user_hidden = val;
return true;
}
return false;
}
std::map<intern_string_t, logline_value_meta> get_field_states() override
{
return {
{TS_META.lvm_name, TS_META},
{LEVEL_META.lvm_name, LEVEL_META},
};
}
private:
static logline_value_meta TS_META;
static logline_value_meta LEVEL_META;
};
logline_value_meta generic_log_format::TS_META{
intern_string::lookup("log_time"),
value_kind_t::VALUE_TEXT,
logline_value_meta::table_column{2},
};
logline_value_meta generic_log_format::LEVEL_META{
intern_string::lookup("log_level"),
value_kind_t::VALUE_TEXT,
logline_value_meta::table_column{4},
};
std::string
from_escaped_string(const char* str, size_t len)
{
std::string retval;
for (size_t lpc = 0; lpc < len; lpc++) {
switch (str[lpc]) {
case '\\':
if ((lpc + 3) < len && str[lpc + 1] == 'x') {
int ch;
if (sscanf(&str[lpc + 2], "%2x", &ch) == 1) {
retval.append(1, (char) ch & 0xff);
lpc += 3;
}
}
break;
default:
retval.append(1, str[lpc]);
break;
}
}
return retval;
}
std::optional<const char*>
lnav_strnstr(const char* s, const char* find, size_t slen)
{
char c, sc;
size_t len;
if ((c = *find++) != '\0') {
len = strlen(find);
do {
do {
if (slen < 1 || (sc = *s) == '\0') {
return std::nullopt;
}
--slen;
++s;
} while (sc != c);
if (len > slen) {
return std::nullopt;
}
} while (strncmp(s, find, len) != 0);
s--;
}
return s;
}
struct separated_string {
const char* ss_str;
size_t ss_len;
const char* ss_separator;
size_t ss_separator_len;
separated_string(const char* str, size_t len)
: ss_str(str), ss_len(len), ss_separator(","),
ss_separator_len(strlen(this->ss_separator))
{
}
separated_string& with_separator(const char* sep)
{
this->ss_separator = sep;
this->ss_separator_len = strlen(sep);
return *this;
}
struct iterator {
const separated_string& i_parent;
const char* i_pos;
const char* i_next_pos;
size_t i_index;
iterator(const separated_string& ss, const char* pos)
: i_parent(ss), i_pos(pos), i_next_pos(pos), i_index(0)
{
this->update();
}
void update()
{
const separated_string& ss = this->i_parent;
auto next_field
= lnav_strnstr(this->i_pos,
ss.ss_separator,
ss.ss_len - (this->i_pos - ss.ss_str));
if (next_field) {
this->i_next_pos = next_field.value() + ss.ss_separator_len;
} else {
this->i_next_pos = ss.ss_str + ss.ss_len;
}
}
iterator& operator++()
{
this->i_pos = this->i_next_pos;
this->update();
this->i_index += 1;
return *this;
}
string_fragment operator*()
{
const auto& ss = this->i_parent;
int end;
if (this->i_next_pos < (ss.ss_str + ss.ss_len)) {
end = this->i_next_pos - ss.ss_str - ss.ss_separator_len;
} else {
end = this->i_next_pos - ss.ss_str;
}
return string_fragment::from_byte_range(
ss.ss_str, this->i_pos - ss.ss_str, end);
}
bool operator==(const iterator& other) const
{
return (&this->i_parent == &other.i_parent)
&& (this->i_pos == other.i_pos);
}
bool operator!=(const iterator& other) const
{
return !(*this == other);
}
size_t index() const { return this->i_index; }
};
iterator begin() { return {*this, this->ss_str}; }
iterator end() { return {*this, this->ss_str + this->ss_len}; }
};
class bro_log_format : public log_format {
public:
struct field_def {
logline_value_meta fd_meta;
logline_value_meta* fd_root_meta;
std::string fd_collator;
std::optional<size_t> fd_numeric_index;
explicit field_def(const intern_string_t name,
size_t col,
log_format* format)
: fd_meta(name,
value_kind_t::VALUE_TEXT,
logline_value_meta::table_column{col},
format),
fd_root_meta(&FIELD_META.find(name)->second)
{
}
field_def& with_kind(value_kind_t kind,
bool identifier = false,
bool foreign_key = false,
const std::string& collator = "")
{
this->fd_meta.lvm_kind = kind;
this->fd_meta.lvm_identifier = identifier;
this->fd_meta.lvm_foreign_key = foreign_key;
this->fd_collator = collator;
return *this;
}
field_def& with_numeric_index(size_t index)
{
this->fd_numeric_index = index;
return *this;
}
};
static std::unordered_map<const intern_string_t, logline_value_meta>
FIELD_META;
static const intern_string_t get_opid_desc()
{
static const intern_string_t RETVAL = intern_string::lookup("std");
return RETVAL;
}
bro_log_format()
{
this->lf_structured = true;
this->lf_is_self_describing = true;
this->lf_time_ordered = false;
auto desc_v = std::make_shared<std::vector<opid_descriptor>>();
desc_v->emplace({});
this->lf_opid_description_def->emplace(get_opid_desc(),
opid_descriptors{desc_v});
}
const intern_string_t get_name() const override
{
static const intern_string_t name(intern_string::lookup("bro"));
return this->blf_format_name.empty() ? name : this->blf_format_name;
}
void clear() override
{
this->log_format::clear();
this->blf_format_name.clear();
this->blf_field_defs.clear();
}
scan_result_t scan_int(std::vector<logline>& dst,
const line_info& li,
shared_buffer_ref& sbr,
scan_batch_context& sbc)
{
static const intern_string_t STATUS_CODE
= intern_string::lookup("bro_status_code");
static const intern_string_t TS = intern_string::lookup("bro_ts");
static const intern_string_t UID = intern_string::lookup("bro_uid");
static const intern_string_t ID_ORIG_H
= intern_string::lookup("bro_id_orig_h");
separated_string ss(sbr.get_data(), sbr.length());
struct timeval tv;
struct exttm tm;
bool found_ts = false;
log_level_t level = LEVEL_INFO;
uint8_t opid = 0;
auto opid_cap = string_fragment::invalid();
auto host_cap = string_fragment::invalid();
ss.with_separator(this->blf_separator.get());
for (auto iter = ss.begin(); iter != ss.end(); ++iter) {
if (iter.index() == 0 && *iter == "#close") {
return scan_match{2000};
}
if (iter.index() >= this->blf_field_defs.size()) {
break;
}
const auto& fd = this->blf_field_defs[iter.index()];
if (TS == fd.fd_meta.lvm_name) {
string_fragment sf = *iter;
if (this->lf_date_time.scan(
sf.data(), sf.length(), nullptr, &tm, tv))
{
this->lf_timestamp_flags = tm.et_flags;
found_ts = true;
}
} else if (STATUS_CODE == fd.fd_meta.lvm_name) {
const auto sf = *iter;
if (!sf.empty() && sf[0] >= '4') {
level = LEVEL_ERROR;
}
} else if (UID == fd.fd_meta.lvm_name) {
opid_cap = *iter;
opid = hash_str(opid_cap.data(), opid_cap.length());
} else if (ID_ORIG_H == fd.fd_meta.lvm_name) {
host_cap = *iter;
}
if (fd.fd_numeric_index) {
switch (fd.fd_meta.lvm_kind) {
case value_kind_t::VALUE_INTEGER:
case value_kind_t::VALUE_FLOAT: {
const auto sv = (*iter).to_string_view();
auto scan_float_res = scn::scan_value<double>(sv);
if (scan_float_res) {
this->lf_value_stats[fd.fd_numeric_index.value()]
.add_value(scan_float_res.value());
}
break;
}
default:
break;
}
}
}
if (found_ts) {
if (!this->lf_specialized) {
for (auto& ll : dst) {
ll.set_ignore(true);
}
}
if (opid_cap.is_valid()) {
auto opid_iter
= sbc.sbc_opids.insert_op(sbc.sbc_allocator, opid_cap, tv);
opid_iter->second.otr_level_stats.update_msg_count(level);
auto& otr = opid_iter->second;
if (!otr.otr_description.lod_id && host_cap.is_valid()
&& otr.otr_description.lod_elements.empty())
{
otr.otr_description.lod_id = get_opid_desc();
otr.otr_description.lod_elements.emplace_back(
0, host_cap.to_string());
}
}
dst.emplace_back(li.li_file_range.fr_offset, tv, level, 0, opid);
return scan_match{2000};
}
return scan_no_match{};
}
scan_result_t scan(logfile& lf,
std::vector<logline>& dst,
const line_info& li,
shared_buffer_ref& sbr,
scan_batch_context& sbc) override
{
static const auto SEP_RE
= lnav::pcre2pp::code::from_const(R"(^#separator\s+(.+))");
if (dst.empty()) {
auto file_options = lf.get_file_options();
if (file_options) {
this->lf_date_time.dts_default_zone
= file_options->second.fo_default_zone.pp_value;
} else {
this->lf_date_time.dts_default_zone = nullptr;
}
}
if (!this->blf_format_name.empty()) {
return this->scan_int(dst, li, sbr, sbc);
}
if (dst.empty() || dst.size() > 20 || sbr.empty()
|| sbr.get_data()[0] == '#')
{
return scan_no_match{};
}
auto line_iter = dst.begin();
auto read_result = lf.read_line(line_iter);
if (read_result.isErr()) {
return scan_no_match{"unable to read first line"};
}
auto line = read_result.unwrap();
auto md = SEP_RE.create_match_data();
auto match_res = SEP_RE.capture_from(line.to_string_fragment())
.into(md)
.matches(PCRE2_NO_UTF_CHECK)
.ignore_error();
if (!match_res) {
return scan_no_match{"cannot read separator header"};
}
this->clear();
auto sep = from_escaped_string(md[1]->data(), md[1]->length());
this->blf_separator = intern_string::lookup(sep);
for (++line_iter; line_iter != dst.end(); ++line_iter) {
auto next_read_result = lf.read_line(line_iter);
if (next_read_result.isErr()) {
return scan_no_match{"unable to read header line"};
}
line = next_read_result.unwrap();
separated_string ss(line.get_data(), line.length());
ss.with_separator(this->blf_separator.get());
auto iter = ss.begin();
string_fragment directive = *iter;
if (directive.empty() || directive[0] != '#') {
continue;
}
++iter;
if (iter == ss.end()) {
continue;
}
if (directive == "#set_separator") {
this->blf_set_separator = intern_string::lookup(*iter);
} else if (directive == "#empty_field") {
this->blf_empty_field = intern_string::lookup(*iter);
} else if (directive == "#unset_field") {
this->blf_unset_field = intern_string::lookup(*iter);
} else if (directive == "#path") {
auto full_name = fmt::format(FMT_STRING("bro_{}_log"), *iter);
this->blf_format_name = intern_string::lookup(full_name);
} else if (directive == "#fields" && this->blf_field_defs.empty()) {
do {
auto field_name
= intern_string::lookup("bro_" + sql_safe_ident(*iter));
auto common_iter = FIELD_META.find(field_name);
if (common_iter == FIELD_META.end()) {
FIELD_META.emplace(field_name,
logline_value_meta{
field_name,
value_kind_t::VALUE_TEXT,
});
}
this->blf_field_defs.emplace_back(
field_name, this->blf_field_defs.size(), this);
++iter;
} while (iter != ss.end());
} else if (directive == "#types") {
static const char* KNOWN_IDS[] = {
"bro_conn_uids",
"bro_fuid",
"bro_host",
"bro_info_code",
"bro_method",
"bro_mime_type",
"bro_orig_fuids",
"bro_parent_fuid",
"bro_proto",
"bro_referrer",
"bro_resp_fuids",
"bro_service",
"bro_uid",
"bro_uri",
"bro_user_agent",
"bro_username",
};
static const char* KNOWN_FOREIGN[] = {
"bro_status_code",
};
int numeric_count = 0;
do {
string_fragment field_type = *iter;
auto& fd = this->blf_field_defs[iter.index() - 1];
if (field_type == "time") {
fd.with_kind(value_kind_t::VALUE_TIMESTAMP);
} else if (field_type == "string") {
bool ident = std::binary_search(std::begin(KNOWN_IDS),
std::end(KNOWN_IDS),
fd.fd_meta.lvm_name);
fd.with_kind(value_kind_t::VALUE_TEXT, ident);
} else if (field_type == "count") {
bool ident = std::binary_search(std::begin(KNOWN_IDS),
std::end(KNOWN_IDS),
fd.fd_meta.lvm_name);
bool foreign
= std::binary_search(std::begin(KNOWN_FOREIGN),
std::end(KNOWN_FOREIGN),
fd.fd_meta.lvm_name);
fd.with_kind(
value_kind_t::VALUE_INTEGER, ident, foreign)
.with_numeric_index(numeric_count);
numeric_count += 1;
} else if (field_type == "bool") {
fd.with_kind(value_kind_t::VALUE_BOOLEAN);
} else if (field_type == "addr") {
fd.with_kind(
value_kind_t::VALUE_TEXT, true, false, "ipaddress");
} else if (field_type == "port") {
fd.with_kind(value_kind_t::VALUE_INTEGER, true);
} else if (field_type == "interval") {
fd.with_kind(value_kind_t::VALUE_FLOAT)
.with_numeric_index(numeric_count);
numeric_count += 1;
}
++iter;
} while (iter != ss.end());
this->lf_value_stats.resize(numeric_count);
}
}
if (!this->blf_format_name.empty() && !this->blf_separator.empty()
&& !this->blf_field_defs.empty())
{
return this->scan_int(dst, li, sbr, sbc);
}
this->blf_format_name.clear();
this->lf_value_stats.clear();
return scan_no_match{};
}
void annotate(logfile* lf,
uint64_t line_number,
string_attrs_t& sa,
logline_value_vector& values,
bool annotate_module) const override
{
static const intern_string_t TS = intern_string::lookup("bro_ts");
static const intern_string_t UID = intern_string::lookup("bro_uid");
auto& sbr = values.lvv_sbr;
separated_string ss(sbr.get_data(), sbr.length());
ss.with_separator(this->blf_separator.get());
for (auto iter = ss.begin(); iter != ss.end(); ++iter) {
if (iter.index() >= this->blf_field_defs.size()) {
return;
}
const field_def& fd = this->blf_field_defs[iter.index()];
string_fragment sf = *iter;
if (sf == this->blf_empty_field) {
sf.clear();
} else if (sf == this->blf_unset_field) {
sf.invalidate();
}
auto lr = line_range(sf.sf_begin, sf.sf_end);
if (fd.fd_meta.lvm_name == TS) {
sa.emplace_back(lr, logline::L_TIMESTAMP.value());
} else if (fd.fd_meta.lvm_name == UID) {
sa.emplace_back(lr, logline::L_OPID.value());
}
if (lr.is_valid()) {
values.lvv_values.emplace_back(fd.fd_meta, sbr, lr);
} else {
values.lvv_values.emplace_back(fd.fd_meta);
}
values.lvv_values.back().lv_meta.lvm_user_hidden
= fd.fd_root_meta->lvm_user_hidden;
}
log_format::annotate(lf, line_number, sa, values, annotate_module);
}
const logline_value_stats* stats_for_value(
const intern_string_t& name) const override
{
const logline_value_stats* retval = nullptr;
for (const auto& blf_field_def : this->blf_field_defs) {
if (blf_field_def.fd_meta.lvm_name == name) {
if (!blf_field_def.fd_numeric_index) {
break;
}
retval = &this->lf_value_stats[blf_field_def.fd_numeric_index
.value()];
break;
}
}
return retval;
}
bool hide_field(const intern_string_t field_name, bool val) override
{
auto fd_iter = FIELD_META.find(field_name);
if (fd_iter == FIELD_META.end()) {
return false;
}
fd_iter->second.lvm_user_hidden = val;
return true;
}
std::map<intern_string_t, logline_value_meta> get_field_states() override
{
std::map<intern_string_t, logline_value_meta> retval;
for (const auto& fd : FIELD_META) {
retval.emplace(fd.first, fd.second);
}
return retval;
}
std::shared_ptr<log_format> specialized(int fmt_lock = -1) override
{
auto retval = std::make_shared<bro_log_format>(*this);
retval->lf_specialized = true;
return retval;
}
class bro_log_table : public log_format_vtab_impl {
public:
explicit bro_log_table(const bro_log_format& format)
: log_format_vtab_impl(format), blt_format(format)
{
}
void get_columns(std::vector<vtab_column>& cols) const override
{
for (const auto& fd : this->blt_format.blf_field_defs) {
auto type_pair = log_vtab_impl::logline_value_to_sqlite_type(
fd.fd_meta.lvm_kind);
cols.emplace_back(fd.fd_meta.lvm_name.to_string(),
type_pair.first,
fd.fd_collator,
false,
"",
type_pair.second);
}
}
void get_foreign_keys(
std::vector<std::string>& keys_inout) const override
{
this->log_vtab_impl::get_foreign_keys(keys_inout);
for (const auto& fd : this->blt_format.blf_field_defs) {
if (fd.fd_meta.lvm_identifier || fd.fd_meta.lvm_foreign_key) {
keys_inout.push_back(fd.fd_meta.lvm_name.to_string());
}
}
}
const bro_log_format& blt_format;
};
static std::map<intern_string_t, std::shared_ptr<bro_log_table>>&
get_tables()
{
static std::map<intern_string_t, std::shared_ptr<bro_log_table>> retval;
return retval;
}
std::shared_ptr<log_vtab_impl> get_vtab_impl() const override
{
if (this->blf_format_name.empty()) {
return nullptr;
}