• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 19243988760-2657

10 Nov 2025 07:37PM UTC coverage: 68.747% (-0.3%) from 69.055%
19243988760-2657

push

github

tstack
[logfile] lay groundwork for bounding log file times

Related to #1188

308 of 655 new or added lines in 35 files covered. (47.02%)

30 existing lines in 7 files now uncovered.

50645 of 73669 relevant lines covered (68.75%)

430651.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.6
/src/log_format.cc
1
/**
2
 * Copyright (c) 2007-2015, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29

30
#include <algorithm>
31
#include <chrono>
32
#include <memory>
33
#include <vector>
34

35
#include <fnmatch.h>
36
#include <stdio.h>
37
#include <string.h>
38

39
#include "base/fs_util.hh"
40
#include "base/humanize.hh"
41
#include "base/intern_string.hh"
42
#include "base/is_utf8.hh"
43
#include "base/itertools.enumerate.hh"
44
#include "base/itertools.hh"
45
#include "base/map_util.hh"
46
#include "base/opt_util.hh"
47
#include "base/snippet_highlighters.hh"
48
#include "base/string_attr_type.hh"
49
#include "base/string_util.hh"
50
#include "bookmarks.hh"
51
#include "command_executor.hh"
52
#include "config.h"
53
#include "fmt/format.h"
54
#include "lnav_util.hh"
55
#include "log_format_ext.hh"
56
#include "log_search_table.hh"
57
#include "log_vtab_impl.hh"
58
#include "ptimec.hh"
59
#include "readline_highlighters.hh"
60
#include "scn/scan.h"
61
#include "sql_util.hh"
62
#include "sqlite-extension-func.hh"
63
#include "sqlitepp.hh"
64
#include "yajlpp/yajlpp.hh"
65
#include "yajlpp/yajlpp_def.hh"
66

67
using namespace lnav::roles::literals;
68
using namespace std::chrono_literals;
69

70
static auto intern_lifetime = intern_string::get_table_lifetime();
71

72
constexpr string_attr_type<void> L_PREFIX("prefix");
73
constexpr string_attr_type<void> L_TIMESTAMP("timestamp");
74
constexpr string_attr_type<void> L_LEVEL("level");
75
constexpr string_attr_type<std::shared_ptr<logfile>> L_FILE("file");
76
constexpr string_attr_type<bookmark_metadata*> L_PARTITION("partition");
77
constexpr string_attr_type<void> L_OPID("opid");
78
constexpr string_attr_type<bookmark_metadata*> L_META("meta");
79

80
std::vector<std::shared_ptr<external_log_format>>
81
    external_log_format::GRAPH_ORDERED_FORMATS;
82

83
const intern_string_t log_format::LOG_TIME_STR
84
    = intern_string::lookup("log_time");
85
const intern_string_t log_format::LOG_LEVEL_STR
86
    = intern_string::lookup("log_level");
87
const intern_string_t log_format::LOG_OPID_STR
88
    = intern_string::lookup("log_opid");
89
const intern_string_t log_format::LOG_THREAD_ID_STR
90
    = intern_string::lookup("log_thread_id");
91

92
static constexpr uint32_t DATE_TIME_SET_FLAGS = ETF_YEAR_SET | ETF_MONTH_SET
93
    | ETF_DAY_SET | ETF_HOUR_SET | ETF_MINUTE_SET | ETF_SECOND_SET;
94

95
log_level_stats&
96
log_level_stats::operator|=(const log_level_stats& rhs)
854✔
97
{
98
    this->lls_error_count += rhs.lls_error_count;
854✔
99
    this->lls_warning_count += rhs.lls_warning_count;
854✔
100
    this->lls_total_count += rhs.lls_total_count;
854✔
101

102
    return *this;
854✔
103
}
104

105
log_op_description&
106
log_op_description::operator|=(const log_op_description& rhs)
383✔
107
{
108
    if (!this->lod_index && rhs.lod_index) {
383✔
109
        this->lod_index = rhs.lod_index;
×
110
    }
111
    if (this->lod_elements.size() < rhs.lod_elements.size()) {
383✔
112
        this->lod_elements = rhs.lod_elements;
×
113
    }
114

115
    return *this;
383✔
116
}
117

118
void
119
opid_time_range::clear()
×
120
{
121
    this->otr_range.invalidate();
×
122
    this->otr_sub_ops.clear();
×
123
    this->otr_level_stats = {};
×
124
}
125

126
opid_time_range&
127
opid_time_range::operator|=(const opid_time_range& rhs)
383✔
128
{
129
    this->otr_range |= rhs.otr_range;
383✔
130
    this->otr_description |= rhs.otr_description;
383✔
131
    this->otr_level_stats |= rhs.otr_level_stats;
383✔
132
    for (const auto& rhs_sub : rhs.otr_sub_ops) {
388✔
133
        bool found = false;
5✔
134

135
        for (auto& sub : this->otr_sub_ops) {
10✔
136
            if (sub.ostr_subid == rhs_sub.ostr_subid) {
5✔
137
                sub.ostr_range |= rhs_sub.ostr_range;
5✔
138
                found = true;
5✔
139
            }
140
        }
141
        if (!found) {
5✔
142
            this->otr_sub_ops.emplace_back(rhs_sub);
×
143
        }
144
    }
145
    std::stable_sort(this->otr_sub_ops.begin(), this->otr_sub_ops.end());
383✔
146

147
    return *this;
383✔
148
}
149

150
void
151
thread_id_time_range::clear()
×
152
{
153
    this->titr_range.invalidate();
×
154
    this->titr_level_stats = {};
×
155
}
156

157
thread_id_time_range&
158
thread_id_time_range::operator|=(const thread_id_time_range& rhs)
471✔
159
{
160
    this->titr_range |= rhs.titr_range;
471✔
161
    this->titr_level_stats |= rhs.titr_level_stats;
471✔
162

163
    return *this;
471✔
164
}
165

166
void
167
log_level_stats::update_msg_count(log_level_t lvl, int32_t amount)
23,342✔
168
{
169
    switch (lvl) {
23,342✔
170
        case LEVEL_FATAL:
1,229✔
171
        case LEVEL_CRITICAL:
172
        case LEVEL_ERROR:
173
            this->lls_error_count += amount;
1,229✔
174
            break;
1,229✔
175
        case LEVEL_WARNING:
111✔
176
            this->lls_warning_count += amount;
111✔
177
            break;
111✔
178
        default:
22,002✔
179
            break;
22,002✔
180
    }
181
    this->lls_total_count += amount;
23,342✔
182
}
23,342✔
183

184
void
185
opid_time_range::close_sub_ops(const string_fragment& subid)
×
186
{
187
    for (auto& other_sub : this->otr_sub_ops) {
×
188
        if (other_sub.ostr_subid == subid) {
×
189
            other_sub.ostr_open = false;
×
190
        }
191
    }
192
}
193

194
log_thread_id_map::iterator
195
log_thread_id_state::insert_tid(ArenaAlloc::Alloc<char>& alloc,
11,313✔
196
                                const string_fragment& tid,
197
                                const std::chrono::microseconds& us)
198
{
199
    auto retval = this->ltis_tid_ranges.find(tid);
11,313✔
200
    if (retval == this->ltis_tid_ranges.end()) {
11,313✔
201
        auto tid_copy = tid.to_owned(alloc);
6,178✔
202
        auto titr = thread_id_time_range{time_range{us, us}};
6,178✔
203
        auto emplace_res = this->ltis_tid_ranges.emplace(tid_copy, titr);
6,178✔
204
        retval = emplace_res.first;
6,178✔
205
    } else {
206
        retval->second.titr_range.extend_to(us);
5,135✔
207
    }
208

209
    return retval;
11,313✔
210
}
211

212
log_opid_map::iterator
213
log_opid_state::insert_op(ArenaAlloc::Alloc<char>& alloc,
11,965✔
214
                          const string_fragment& opid,
215
                          const std::chrono::microseconds& us,
216
                          timestamp_point_of_reference_t poref,
217
                          std::chrono::microseconds duration)
218
{
219
    auto retval = this->los_opid_ranges.find(opid);
11,965✔
220
    if (retval == this->los_opid_ranges.end()) {
11,965✔
221
        auto opid_copy = opid.to_owned(alloc);
6,082✔
222
        auto otr = opid_time_range{time_range{us, us}};
6,082✔
223
        auto emplace_res = this->los_opid_ranges.emplace(opid_copy, otr);
6,082✔
224
        retval = emplace_res.first;
6,082✔
225
    } else {
6,082✔
226
        retval->second.otr_range.extend_to(us);
5,883✔
227
    }
228
    if (duration > 0us) {
11,965✔
229
        auto other_us = us;
104✔
230
        switch (poref) {
104✔
231
            case timestamp_point_of_reference_t::send:
18✔
232
                other_us -= duration;
18✔
233
                break;
18✔
234
            case timestamp_point_of_reference_t::start:
86✔
235
                other_us += duration;
86✔
236
                break;
86✔
237
        }
238
        retval->second.otr_range.extend_to(other_us);
104✔
239
    }
240

241
    return retval;
11,965✔
242
}
243

244
opid_sub_time_range*
245
log_opid_state::sub_op_in_use(ArenaAlloc::Alloc<char>& alloc,
64✔
246
                              log_opid_map::iterator& op_iter,
247
                              const string_fragment& subid,
248
                              const std::chrono::microseconds& us,
249
                              log_level_t level)
250
{
251
    const auto& opid = op_iter->first;
64✔
252
    auto sub_iter = this->los_sub_in_use.find(subid);
64✔
253
    if (sub_iter == this->los_sub_in_use.end()) {
64✔
254
        auto emp_res
255
            = this->los_sub_in_use.emplace(subid.to_owned(alloc), opid);
43✔
256

257
        sub_iter = emp_res.first;
43✔
258
    }
259

260
    auto retval = sub_iter->first;
64✔
261
    if (sub_iter->second != opid) {
64✔
262
        auto other_otr
263
            = lnav::map::find(this->los_opid_ranges, sub_iter->second);
×
264
        if (other_otr) {
×
265
            other_otr->get().close_sub_ops(retval);
×
266
        }
267
    }
268
    sub_iter->second = opid;
64✔
269

270
    auto& otr = op_iter->second;
64✔
271
    auto sub_op_iter = otr.otr_sub_ops.rbegin();
64✔
272
    for (; sub_op_iter != otr.otr_sub_ops.rend(); ++sub_op_iter) {
64✔
273
        if (sub_op_iter->ostr_open && sub_op_iter->ostr_subid == retval) {
21✔
274
            break;
21✔
275
        }
276
    }
277
    if (sub_op_iter == otr.otr_sub_ops.rend()) {
64✔
278
        otr.otr_sub_ops.emplace_back(opid_sub_time_range{
43✔
279
            retval,
280
            time_range{us, us},
281
        });
282
        otr.otr_sub_ops.back().ostr_level_stats.update_msg_count(level);
43✔
283

284
        return &otr.otr_sub_ops.back();
43✔
285
    } else {
286
        sub_op_iter->ostr_range.extend_to(us);
21✔
287
        sub_op_iter->ostr_level_stats.update_msg_count(level);
21✔
288
        return &(*sub_op_iter);
21✔
289
    }
290
}
291

292
std::optional<std::string>
293
log_format::opid_descriptor::matches(const string_fragment& sf) const
2,618✔
294
{
295
    if (this->od_extractor.pp_value) {
2,618✔
296
        thread_local auto desc_md = lnav::pcre2pp::match_data::unitialized();
837✔
297

298
        auto desc_match_res = this->od_extractor.pp_value->capture_from(sf)
837✔
299
                                  .into(desc_md)
837✔
300
                                  .matches(PCRE2_NO_UTF_CHECK | PCRE2_ANCHORED)
1,674✔
301
                                  .ignore_error();
837✔
302
        if (desc_match_res) {
837✔
303
            return desc_md.to_string();
66✔
304
        }
305

306
        return std::nullopt;
771✔
307
    }
308
    return sf.to_string();
1,781✔
309
}
310

311
std::string
312
log_format::opid_descriptors::to_string(
1,023✔
313
    const lnav::map::small<size_t, std::string>& lod) const
314
{
315
    std::string retval;
1,023✔
316

317
    for (size_t lpc = 0; lpc < this->od_descriptors->size(); lpc++) {
2,057✔
318
        retval.append(this->od_descriptors->at(lpc).od_prefix);
1,034✔
319
        auto val = lod.value_for(lpc);
1,034✔
320
        if (val) {
1,034✔
321
            retval.append(*val.value());
1,033✔
322
        }
323
        retval.append(this->od_descriptors->at(lpc).od_suffix);
1,034✔
324
    }
325

326
    return retval;
1,023✔
327
}
×
328

329
chart_type_t
330
logline_value_meta::to_chart_type() const
38✔
331
{
332
    auto retval = chart_type_t::hist;
38✔
333
    switch (this->lvm_kind) {
38✔
334
        case value_kind_t::VALUE_NULL:
9✔
335
            retval = chart_type_t::none;
9✔
336
            break;
9✔
337
        case value_kind_t::VALUE_INTEGER:
10✔
338
            if (!this->lvm_identifier && !this->lvm_foreign_key) {
10✔
339
                retval = chart_type_t::spectro;
5✔
340
            }
341
            break;
10✔
342
        case value_kind_t::VALUE_FLOAT:
×
343
            retval = chart_type_t::spectro;
×
344
            break;
×
345
        case value_kind_t::VALUE_XML:
1✔
346
        case value_kind_t::VALUE_JSON:
347
        case value_kind_t::VALUE_BOOLEAN:
348
        case value_kind_t::VALUE_TIMESTAMP:
349
            retval = chart_type_t::none;
1✔
350
            break;
1✔
351
        default:
18✔
352
            break;
18✔
353
    }
354

355
    return retval;
38✔
356
}
357

358
struct line_range
359
logline_value::origin_in_full_msg(const char* msg, ssize_t len) const
×
360
{
361
    if (this->lv_sub_offset == 0) {
×
362
        return this->lv_origin;
×
363
    }
364

365
    if (len == -1) {
×
366
        len = strlen(msg);
×
367
    }
368

369
    struct line_range retval = this->lv_origin;
×
370
    const char *last = msg, *msg_end = msg + len;
×
371

372
    for (int lpc = 0; lpc < this->lv_sub_offset; lpc++) {
×
373
        const auto* next = (const char*) memchr(last, '\n', msg_end - last);
×
374
        require(next != nullptr);
×
375

376
        next += 1;
×
377
        int amount = (next - last);
×
378

379
        retval.lr_start += amount;
×
380
        if (retval.lr_end != -1) {
×
381
            retval.lr_end += amount;
×
382
        }
383

384
        last = next + 1;
×
385
    }
386

387
    if (retval.lr_end == -1) {
×
388
        const auto* eol = (const char*) memchr(last, '\n', msg_end - last);
×
389

390
        if (eol == nullptr) {
×
391
            retval.lr_end = len;
×
392
        } else {
393
            retval.lr_end = eol - msg;
×
394
        }
395
    }
396

397
    return retval;
×
398
}
399

400
logline_value::logline_value(logline_value_meta lvm,
629,963✔
401
                             shared_buffer_ref& sbr,
402
                             struct line_range origin)
629,963✔
403
    : lv_meta(std::move(lvm)), lv_origin(origin)
629,963✔
404
{
405
    if (sbr.get_data() == nullptr) {
629,963✔
406
        this->lv_meta.lvm_kind = value_kind_t::VALUE_NULL;
×
407
    }
408

409
    switch (this->lv_meta.lvm_kind) {
629,963✔
410
        case value_kind_t::VALUE_JSON:
319,478✔
411
        case value_kind_t::VALUE_XML:
412
        case value_kind_t::VALUE_STRUCT:
413
        case value_kind_t::VALUE_TEXT:
414
        case value_kind_t::VALUE_QUOTED:
415
        case value_kind_t::VALUE_W3C_QUOTED:
416
        case value_kind_t::VALUE_TIMESTAMP:
417
            require(origin.lr_end != -1);
319,478✔
418
            this->lv_frag = string_fragment::from_byte_range(
319,478✔
419
                sbr.get_data(), origin.lr_start, origin.lr_end);
319,478✔
420
            break;
319,478✔
421

422
        case value_kind_t::VALUE_NULL:
×
423
            break;
×
424

425
        case value_kind_t::VALUE_INTEGER: {
280,188✔
426
            auto scan_res
427
                = scn::scan_value<int64_t>(sbr.to_string_view(origin));
280,188✔
428
            if (scan_res) {
280,188✔
429
                this->lv_value.i = scan_res->value();
280,186✔
430
            } else {
431
                this->lv_value.i = 0;
2✔
432
            }
433
            break;
280,188✔
434
        }
435

436
        case value_kind_t::VALUE_FLOAT: {
30,297✔
437
            auto scan_res = scn::scan_value<double>(sbr.to_string_view(origin));
30,297✔
438
            if (scan_res) {
30,297✔
439
                this->lv_value.d = scan_res->value();
30,297✔
440
            } else {
441
                this->lv_value.d = 0;
×
442
            }
443
            break;
30,297✔
444
        }
445

446
        case value_kind_t::VALUE_BOOLEAN:
×
447
            if (strncmp(
×
448
                    sbr.get_data_at(origin.lr_start), "true", origin.length())
×
449
                    == 0
450
                || strncmp(
×
451
                       sbr.get_data_at(origin.lr_start), "yes", origin.length())
×
452
                    == 0)
453
            {
454
                this->lv_value.i = 1;
×
455
            } else {
456
                this->lv_value.i = 0;
×
457
            }
458
            break;
×
459

460
        case value_kind_t::VALUE_UNKNOWN:
×
461
        case value_kind_t::VALUE__MAX:
462
            ensure(0);
×
463
            break;
464
    }
465
}
629,963✔
466

467
std::string
468
logline_value::to_string() const
9,880✔
469
{
470
    char buffer[128];
471

472
    switch (this->lv_meta.lvm_kind) {
9,880✔
473
        case value_kind_t::VALUE_NULL:
15✔
474
            return "null";
30✔
475

476
        case value_kind_t::VALUE_JSON:
9,313✔
477
        case value_kind_t::VALUE_XML:
478
        case value_kind_t::VALUE_STRUCT:
479
        case value_kind_t::VALUE_TEXT:
480
        case value_kind_t::VALUE_TIMESTAMP:
481
            if (this->lv_str) {
9,313✔
482
                return this->lv_str.value();
1,418✔
483
            }
484
            if (this->lv_frag.empty()) {
7,895✔
485
                return this->lv_intern_string.to_string();
55✔
486
            }
487
            return this->lv_frag.to_string();
7,840✔
488

489
        case value_kind_t::VALUE_QUOTED:
7✔
490
        case value_kind_t::VALUE_W3C_QUOTED:
491
            if (this->lv_frag.empty()) {
7✔
492
                return "";
×
493
            } else {
494
                switch (this->lv_frag.data()[0]) {
7✔
495
                    case '\'':
7✔
496
                    case '"': {
497
                        auto unquote_func = this->lv_meta.lvm_kind
14✔
498
                                == value_kind_t::VALUE_W3C_QUOTED
499
                            ? unquote_w3c
7✔
500
                            : unquote;
501
                        stack_buf allocator;
7✔
502
                        auto* unquoted_str
503
                            = allocator.allocate(this->lv_frag.length());
7✔
504
                        size_t unquoted_len;
505

506
                        unquoted_len = unquote_func(unquoted_str,
7✔
507
                                                    this->lv_frag.data(),
508
                                                    this->lv_frag.length());
7✔
509
                        return {unquoted_str, unquoted_len};
14✔
510
                    }
7✔
511
                    default:
×
512
                        return this->lv_frag.to_string();
×
513
                }
514
            }
515
            break;
516

517
        case value_kind_t::VALUE_INTEGER:
507✔
518
            snprintf(buffer, sizeof(buffer), "%" PRId64, this->lv_value.i);
507✔
519
            break;
507✔
520

521
        case value_kind_t::VALUE_FLOAT:
32✔
522
            snprintf(buffer, sizeof(buffer), "%lf", this->lv_value.d);
32✔
523
            break;
32✔
524

525
        case value_kind_t::VALUE_BOOLEAN:
6✔
526
            if (this->lv_value.i) {
6✔
527
                return "true";
×
528
            } else {
529
                return "false";
12✔
530
            }
531
            break;
532
        case value_kind_t::VALUE_UNKNOWN:
×
533
        case value_kind_t::VALUE__MAX:
534
            ensure(0);
×
535
            break;
536
    }
537

538
    return {buffer};
1,078✔
539
}
540

541
string_fragment
542
logline_value::to_string_fragment(ArenaAlloc::Alloc<char>& alloc) const
1,095✔
543
{
544
    char buffer[128];
545

546
    switch (this->lv_meta.lvm_kind) {
1,095✔
547
        case value_kind_t::VALUE_NULL:
×
548
            return "null"_frag;
×
549

550
        case value_kind_t::VALUE_JSON:
1,095✔
551
        case value_kind_t::VALUE_XML:
552
        case value_kind_t::VALUE_STRUCT:
553
        case value_kind_t::VALUE_TEXT:
554
        case value_kind_t::VALUE_TIMESTAMP:
555
            if (this->lv_str) {
1,095✔
556
                return string_fragment::from_str(this->lv_str.value())
6✔
557
                    .to_owned(alloc);
6✔
558
            }
559
            if (this->lv_frag.empty()) {
1,089✔
560
                return this->lv_intern_string.to_string_fragment().to_owned(
×
561
                    alloc);
×
562
            }
563
            return this->lv_frag.to_owned(alloc);
1,089✔
564

565
        case value_kind_t::VALUE_QUOTED:
×
566
        case value_kind_t::VALUE_W3C_QUOTED:
567
            if (this->lv_frag.empty()) {
×
568
                return string_fragment{};
×
569
            } else {
570
                switch (this->lv_frag.data()[0]) {
×
571
                    case '\'':
×
572
                    case '"': {
573
                        auto unquote_func = this->lv_meta.lvm_kind
×
574
                                == value_kind_t::VALUE_W3C_QUOTED
575
                            ? unquote_w3c
×
576
                            : unquote;
577
                        stack_buf allocator;
×
578
                        auto* unquoted_str
579
                            = allocator.allocate(this->lv_frag.length());
×
580
                        size_t unquoted_len;
581

582
                        unquoted_len = unquote_func(unquoted_str,
×
583
                                                    this->lv_frag.data(),
584
                                                    this->lv_frag.length());
×
585
                        return string_fragment::from_bytes(unquoted_str,
×
586
                                                           unquoted_len)
587
                            .to_owned(alloc);
×
588
                    }
589
                    default:
×
590
                        return this->lv_frag.to_owned(alloc);
×
591
                }
592
            }
593
            break;
594

595
        case value_kind_t::VALUE_INTEGER:
×
596
            snprintf(buffer, sizeof(buffer), "%" PRId64, this->lv_value.i);
×
597
            break;
×
598

599
        case value_kind_t::VALUE_FLOAT:
×
600
            snprintf(buffer, sizeof(buffer), "%lf", this->lv_value.d);
×
601
            break;
×
602

603
        case value_kind_t::VALUE_BOOLEAN:
×
604
            if (this->lv_value.i) {
×
605
                return "true"_frag;
×
606
            }
607
            return "false"_frag;
×
608
            break;
609
        case value_kind_t::VALUE_UNKNOWN:
×
610
        case value_kind_t::VALUE__MAX:
611
            ensure(0);
×
612
            break;
613
    }
614

615
    return string_fragment::from_c_str(buffer).to_owned(alloc);
×
616
}
617

618
std::vector<std::shared_ptr<log_format>> log_format::lf_root_formats;
619

620
std::vector<std::shared_ptr<log_format>>&
621
log_format::get_root_formats()
16,701✔
622
{
623
    return lf_root_formats;
16,701✔
624
}
625

626
void
627
external_log_format::update_op_description(
5,428✔
628
    const std::vector<opid_descriptors*>& desc_defs_vec,
629
    log_op_description& lod,
630
    const pattern* fpat,
631
    const lnav::pcre2pp::match_data& md)
632
{
633
    std::optional<std::string> desc_elem_str;
5,428✔
634
    if (!lod.lod_index) {
5,428✔
635
        for (const auto& desc_defs : desc_defs_vec) {
3,971✔
636
            if (lod.lod_index) {
1,741✔
637
                break;
26✔
638
            }
639
            for (const auto& desc_def : *desc_defs->od_descriptors) {
2,526✔
640
                auto desc_field_index_iter = fpat->p_value_name_to_index.find(
1,813✔
641
                    desc_def.od_field.pp_value);
1,813✔
642

643
                if (desc_field_index_iter == fpat->p_value_name_to_index.end())
1,813✔
644
                {
645
                    continue;
43✔
646
                }
647

648
                auto desc_cap_opt = md[desc_field_index_iter->second];
1,809✔
649
                if (!desc_cap_opt) {
1,809✔
650
                    continue;
39✔
651
                }
652

653
                desc_elem_str = desc_def.matches(desc_cap_opt.value());
1,770✔
654
                if (desc_elem_str) {
1,770✔
655
                    lod.lod_index = desc_defs->od_index;
1,002✔
656
                    break;
1,002✔
657
                }
658
            }
659
        }
660
    }
661
    if (lod.lod_index) {
5,428✔
662
        const auto& desc_def_v
663
            = *desc_defs_vec[lod.lod_index.value()]->od_descriptors;
4,174✔
664
        auto& desc_v = lod.lod_elements;
4,174✔
665

666
        if (desc_def_v.size() == desc_v.size()
4,174✔
667
            || (this->elf_opid_field.empty() && !desc_v.empty()))
4,174✔
668
        {
669
            return;
3,172✔
670
        }
671
        for (size_t desc_def_index = 0; desc_def_index < desc_def_v.size();
2,857✔
672
             desc_def_index++)
673
        {
674
            const auto& desc_def = desc_def_v[desc_def_index];
1,855✔
675
            auto found_desc = desc_v.value_for(desc_def_index);
1,855✔
676
            auto desc_field_index_iter
677
                = fpat->p_value_name_to_index.find(desc_def.od_field.pp_value);
1,855✔
678

679
            if (desc_field_index_iter == fpat->p_value_name_to_index.end()) {
1,855✔
680
                continue;
35✔
681
            }
682
            auto desc_cap_opt = md[desc_field_index_iter->second];
1,855✔
683
            if (!desc_cap_opt) {
1,855✔
684
                continue;
35✔
685
            }
686

687
            if (!desc_elem_str) {
1,820✔
688
                desc_elem_str = desc_def.matches(desc_cap_opt.value());
818✔
689
            }
690
            if (desc_elem_str) {
1,820✔
691
                if (!found_desc) {
1,817✔
692
                    desc_v.insert(desc_def_index, desc_elem_str.value());
1,817✔
693
                } else if (!desc_elem_str->empty()) {
×
694
                    found_desc.value()->append(desc_def.od_joiner);
×
695
                    found_desc.value()->append(desc_elem_str.value());
×
696
                }
697
            }
698
            desc_elem_str = std::nullopt;
1,820✔
699
        }
700
    }
701
}
5,428✔
702

703
void
704
external_log_format::update_op_description(
2,417✔
705
    const std::vector<opid_descriptors*>& desc_defs_vec,
706
    log_op_description& lod)
707
{
708
    std::optional<std::string> desc_elem_str;
2,417✔
709
    if (!lod.lod_index) {
2,417✔
710
        for (const auto& desc_defs : desc_defs_vec) {
2,426✔
711
            if (lod.lod_index) {
15✔
712
                break;
×
713
            }
714
            for (const auto& desc_def : *desc_defs->od_descriptors) {
15✔
715
                auto desc_cap_iter
716
                    = this->lf_desc_captures.find(desc_def.od_field.pp_value);
15✔
717

718
                if (desc_cap_iter == this->lf_desc_captures.end()) {
15✔
719
                    continue;
×
720
                }
721
                desc_elem_str = desc_def.matches(desc_cap_iter->second);
15✔
722
                if (desc_elem_str) {
15✔
723
                    lod.lod_index = desc_defs->od_index;
15✔
724
                    break;
15✔
725
                }
726
            }
727
        }
728
    }
729
    if (lod.lod_index) {
2,417✔
730
        const auto& desc_def_v
731
            = *desc_defs_vec[lod.lod_index.value()]->od_descriptors;
21✔
732
        auto& desc_v = lod.lod_elements;
21✔
733

734
        if (desc_def_v.size() == desc_v.size()
21✔
735
            || (this->elf_opid_field.empty() && !desc_v.empty()))
21✔
736
        {
737
            return;
6✔
738
        }
739
        for (size_t desc_def_index = 0; desc_def_index < desc_def_v.size();
45✔
740
             desc_def_index++)
741
        {
742
            const auto& desc_def = desc_def_v[desc_def_index];
30✔
743
            auto found_desc = desc_v.value_for(desc_def_index);
30✔
744
            auto desc_cap_iter
745
                = this->lf_desc_captures.find(desc_def.od_field.pp_value);
30✔
746
            if (desc_cap_iter == this->lf_desc_captures.end()) {
30✔
747
                continue;
×
748
            }
749

750
            if (!desc_elem_str) {
30✔
751
                desc_elem_str = desc_def.matches(desc_cap_iter->second);
15✔
752
            }
753
            if (desc_elem_str) {
30✔
754
                if (!found_desc) {
30✔
755
                    desc_v.insert(desc_def_index, desc_elem_str.value());
30✔
756
                } else if (!desc_elem_str->empty()) {
×
757
                    found_desc.value()->append(desc_def.od_joiner);
×
758
                    found_desc.value()->append(desc_elem_str.value());
×
759
                }
760
            }
761
            desc_elem_str = std::nullopt;
30✔
762
        }
763
    }
764
}
2,417✔
765

766
static bool
767
next_format(
2,152,735✔
768
    const std::vector<std::shared_ptr<external_log_format::pattern>>& patterns,
769
    int& index,
770
    int& locked_index)
771
{
772
    bool retval = true;
2,152,735✔
773

774
    if (locked_index == -1) {
2,152,735✔
775
        index += 1;
2,145,509✔
776
        if (index >= (int) patterns.size()) {
2,145,509✔
777
            retval = false;
645,466✔
778
        }
779
    } else if (index == locked_index) {
7,226✔
780
        retval = false;
1✔
781
    } else {
782
        index = locked_index;
7,225✔
783
    }
784

785
    return retval;
2,152,735✔
786
}
787

788
bool
789
log_format::next_format(const pcre_format* fmt, int& index, int& locked_index)
168,970✔
790
{
791
    bool retval = true;
168,970✔
792

793
    if (locked_index == -1) {
168,970✔
794
        index += 1;
168,799✔
795
        if (fmt[index].name == nullptr) {
168,799✔
796
            retval = false;
9,452✔
797
        }
798
    } else if (index == locked_index) {
171✔
799
        retval = false;
31✔
800
    } else {
801
        index = locked_index;
140✔
802
    }
803

804
    return retval;
168,970✔
805
}
806

807
const char*
808
log_format::log_scanf(scan_batch_context& sbc,
11,846✔
809
                      uint32_t line_number,
810
                      string_fragment line,
811
                      const pcre_format* fmt,
812
                      const char* time_fmt[],
813
                      exttm* tm_out,
814
                      timeval* tv_out,
815

816
                      string_fragment* ts_out,
817
                      std::optional<string_fragment>* level_out)
818
{
819
    int curr_fmt = -1;
11,846✔
820
    const char* retval = nullptr;
11,846✔
821
    bool done = false;
11,846✔
822
    int pat_index = sbc.sbc_pattern_locks.last_pattern_index();
11,846✔
823

824
    while (!done && next_format(fmt, curr_fmt, pat_index)) {
171,333✔
825
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
159,487✔
826

827
        auto match_res = fmt[curr_fmt]
159,487✔
828
                             .pcre->capture_from(line)
159,487✔
829
                             .into(md)
159,487✔
830
                             .matches(PCRE2_NO_UTF_CHECK)
318,974✔
831
                             .ignore_error();
159,487✔
832
        if (!match_res) {
159,487✔
833
            retval = nullptr;
137,011✔
834
        } else {
835
            auto ts = md[fmt[curr_fmt].pf_timestamp_index];
22,476✔
836

837
            retval = this->lf_date_time.scan(
22,476✔
838
                ts->data(), ts->length(), nullptr, tm_out, *tv_out);
22,476✔
839

840
            if (retval == nullptr) {
22,476✔
841
                auto ls = this->lf_date_time.unlock();
20,113✔
842
                retval = this->lf_date_time.scan(
20,113✔
843
                    ts->data(), ts->length(), nullptr, tm_out, *tv_out);
20,113✔
844
                if (retval != nullptr) {
20,113✔
845
                    auto old_flags
×
846
                        = this->lf_timestamp_flags & DATE_TIME_SET_FLAGS;
×
847
                    auto new_flags = tm_out->et_flags & DATE_TIME_SET_FLAGS;
×
848

849
                    // It is unlikely a valid timestamp would lose much
850
                    // precision.
851
                    if (new_flags != old_flags) {
×
852
                        retval = nullptr;
×
853
                    }
854
                }
855
                if (retval == nullptr) {
20,113✔
856
                    this->lf_date_time.relock(ls);
20,113✔
857
                } else {
858
                    log_debug(
×
859
                        "%d: changed time format to '%s' due to %.*s",
860
                        line_number,
861
                        PTIMEC_FORMAT_STR[this->lf_date_time.dts_fmt_lock],
862
                        ts->length(),
863
                        ts->data());
864
                }
865
            }
866

867
            if (retval) {
22,476✔
868
                *ts_out = ts.value();
2,363✔
869
                if (md[2]) {
2,363✔
870
                    *level_out = md[2];
246✔
871
                } else {
872
                    *level_out = line.substr(md[0]->sf_end);
2,117✔
873
                }
874
                if (curr_fmt != pat_index) {
2,363✔
875
                    uint32_t lock_line;
876

877
                    if (sbc.sbc_pattern_locks.empty()) {
2,254✔
878
                        lock_line = 0;
2,254✔
879
                    } else {
880
                        lock_line = line_number;
×
881
                    }
882

883
                    sbc.sbc_pattern_locks.pl_lines.emplace_back(lock_line,
2,254✔
884
                                                                curr_fmt);
885
                }
886
                this->lf_timestamp_flags = tm_out->et_flags;
2,363✔
887
                done = true;
2,363✔
888
            }
889
        }
890
    }
891

892
    return retval;
11,846✔
893
}
894

895
void
896
log_format::annotate(logfile* lf,
36,931✔
897
                     uint64_t line_number,
898
                     string_attrs_t& sa,
899
                     logline_value_vector& values) const
900
{
901
    if (lf != nullptr && !values.lvv_opid_value) {
36,931✔
902
        const auto& bm = lf->get_bookmark_metadata();
2,416✔
903
        auto bm_iter = bm.find(line_number);
2,416✔
904
        if (bm_iter != bm.end() && !bm_iter->second.bm_opid.empty()) {
2,416✔
905
            values.lvv_opid_value = bm_iter->second.bm_opid;
18✔
906
            values.lvv_opid_provenance
907
                = logline_value_vector::opid_provenance::user;
18✔
908
        }
909
    }
910
}
36,931✔
911

912
void
913
log_format::check_for_new_year(std::vector<logline>& dst,
1,478✔
914
                               exttm etm,
915
                               timeval log_tv) const
916
{
917
    if (dst.empty()) {
1,478✔
918
        return;
217✔
919
    }
920

921
    time_t diff
922
        = dst.back().get_time<std::chrono::seconds>().count() - log_tv.tv_sec;
1,261✔
923
    int off_year = 0, off_month = 0, off_day = 0, off_hour = 0;
1,261✔
924
    bool do_change = true;
1,261✔
925

926
    if (diff <= 0) {
1,261✔
927
        return;
1,202✔
928
    }
929
    if ((etm.et_flags & ETF_MONTH_SET) && diff >= (24 * 60 * 60)) {
59✔
930
        off_year = 1;
10✔
931
    } else if (diff >= (24 * 60 * 60)) {
49✔
932
        off_month = 1;
2✔
933
    } else if (!(etm.et_flags & ETF_DAY_SET) && (diff >= (60 * 60))) {
47✔
934
        off_day = 1;
1✔
935
    } else if (!(etm.et_flags & ETF_HOUR_SET) && (diff >= 60)) {
46✔
936
        off_hour = 1;
4✔
937
    } else {
938
        do_change = false;
42✔
939
    }
940

941
    if (!do_change) {
59✔
942
        return;
42✔
943
    }
944
    log_debug("%zu:detected time rollover; offsets=%d %d %d %d",
17✔
945
              dst.size(),
946
              off_year,
947
              off_month,
948
              off_day,
949
              off_hour);
950
    for (auto& ll : dst) {
66✔
951
        time_t ot = ll.get_time<std::chrono::seconds>().count();
49✔
952
        struct tm otm;
953

954
        gmtime_r(&ot, &otm);
49✔
955
        otm.tm_yday = -1;
49✔
956
        if (otm.tm_year < off_year) {
49✔
957
            otm.tm_year = 0;
×
958
        } else {
959
            otm.tm_year -= off_year;
49✔
960
        }
961
        otm.tm_mon -= off_month;
49✔
962
        if (otm.tm_mon < 0) {
49✔
963
            otm.tm_mon += 12;
2✔
964
        }
965
        auto new_time = tm2sec(&otm);
49✔
966
        if (new_time == -1) {
49✔
967
            continue;
×
968
        }
969
        new_time -= (off_day * 24 * 60 * 60) + (off_hour * 60 * 60);
49✔
970
        auto old_sub = ll.get_subsecond_time<std::chrono::microseconds>();
49✔
971
        ll.set_time(std::chrono::seconds{new_time});
49✔
972
        ll.set_subsecond_time(old_sub);
49✔
973
    }
974
}
975

976
/*
977
 * XXX This needs some cleanup.
978
 */
979
struct json_log_userdata {
980
    json_log_userdata(shared_buffer_ref& sbr, scan_batch_context* sbc)
11,323✔
981
        : jlu_shared_buffer(sbr), jlu_batch_context(sbc)
22,646✔
982
    {
983
    }
11,323✔
984

985
    const external_log_format::value_def* get_field_def(
296,205✔
986
        yajlpp_parse_context* ypc)
987
    {
988
        const auto field_frag = ypc->get_path_as_string_fragment();
296,205✔
989
        auto* format = this->jlu_format;
296,205✔
990

991
        if (this->jlu_read_order_index < format->elf_value_def_read_order.size()
296,205✔
992
            && format->elf_value_def_read_order[this->jlu_read_order_index]
397,885✔
993
                    .first
994
                == field_frag)
101,680✔
995
        {
996
            return format
997
                ->elf_value_def_read_order[this->jlu_read_order_index++]
97,303✔
998
                .second;
97,303✔
999
        }
1000

1001
        format->elf_value_def_read_order.resize(this->jlu_read_order_index);
198,902✔
1002
        auto vd_iter = format->elf_value_def_frag_map.find(field_frag);
198,902✔
1003
        if (vd_iter != format->elf_value_def_frag_map.end()) {
198,902✔
1004
            format->elf_value_def_read_order.emplace_back(vd_iter->first,
172,778✔
1005
                                                          vd_iter->second);
172,778✔
1006
            this->jlu_read_order_index += 1;
172,778✔
1007
            return vd_iter->second;
172,778✔
1008
        }
1009

1010
        auto owned_frag = field_frag.to_owned(format->elf_allocator);
26,124✔
1011
        format->elf_value_def_frag_map[owned_frag] = nullptr;
26,124✔
1012
        format->elf_value_def_read_order.emplace_back(owned_frag, nullptr);
26,124✔
1013
        this->jlu_read_order_index += 1;
26,124✔
1014
        return nullptr;
26,124✔
1015
    }
1016

1017
    void add_sub_lines_for(const external_log_format::value_def* vd,
205,179✔
1018
                           bool top_level,
1019
                           std::optional<double> val,
1020
                           const unsigned char* str,
1021
                           ssize_t len,
1022
                           yajl_string_props_t* props)
1023
    {
1024
        auto res = this->jlu_format->value_line_count(
205,179✔
1025
            *this->jlu_batch_context, vd, top_level, val, str, len, props);
205,179✔
1026
        this->jlu_has_ansi |= res.vlcr_has_ansi;
205,179✔
1027
        if (!res.vlcr_valid_utf) {
205,179✔
1028
            this->jlu_valid_utf = false;
×
1029
        }
1030
        this->jlu_sub_line_count += res.vlcr_count;
205,179✔
1031
        this->jlu_quality += res.vlcr_line_format_count;
205,179✔
1032
        if (res.vlcr_line_format_index) {
205,179✔
1033
            this->jlu_format_hits[res.vlcr_line_format_index.value()] = true;
3,058✔
1034
        }
1035
    }
205,179✔
1036

1037
    external_log_format* jlu_format{nullptr};
1038
    const logline* jlu_line{nullptr};
1039
    logline* jlu_base_line{nullptr};
1040
    int jlu_sub_line_count{1};
1041
    bool jlu_has_ansi{false};
1042
    bool jlu_valid_utf{true};
1043
    yajl_handle jlu_handle{nullptr};
1044
    const char* jlu_line_value{nullptr};
1045
    size_t jlu_line_size{0};
1046
    size_t jlu_sub_start{0};
1047
    uint32_t jlu_quality{0};
1048
    uint32_t jlu_strikes{0};
1049
    std::vector<bool> jlu_format_hits;
1050
    shared_buffer_ref& jlu_shared_buffer;
1051
    scan_batch_context* jlu_batch_context;
1052
    std::optional<string_fragment> jlu_opid_frag;
1053
    std::optional<string_fragment> jlu_opid_desc_frag;
1054
    std::optional<string_fragment> jlu_tid_frag;
1055
    std::optional<int64_t> jlu_tid_number;
1056
    std::optional<std::string> jlu_subid;
1057
    hasher jlu_opid_hasher;
1058
    std::chrono::microseconds jlu_duration{0};
11,323✔
1059
    exttm jlu_exttm;
1060
    size_t jlu_read_order_index{0};
1061
    subline_options jlu_subline_opts;
1062
};
1063

1064
static int read_json_field(yajlpp_parse_context* ypc,
1065
                           const unsigned char* str,
1066
                           size_t len,
1067
                           yajl_string_props_t*);
1068

1069
static int
1070
read_json_null(yajlpp_parse_context* ypc)
2,992✔
1071
{
1072
    json_log_userdata* jlu = (json_log_userdata*) ypc->ypc_userdata;
2,992✔
1073
    const auto* vd = jlu->get_field_def(ypc);
2,992✔
1074

1075
    jlu->add_sub_lines_for(
5,984✔
1076
        vd, ypc->is_level(1), std::nullopt, nullptr, -1, nullptr);
2,992✔
1077

1078
    return 1;
2,992✔
1079
}
1080

1081
static int
1082
read_json_bool(yajlpp_parse_context* ypc, int val)
14,244✔
1083
{
1084
    json_log_userdata* jlu = (json_log_userdata*) ypc->ypc_userdata;
14,244✔
1085
    const auto* vd = jlu->get_field_def(ypc);
14,244✔
1086

1087
    jlu->add_sub_lines_for(
28,488✔
1088
        vd, ypc->is_level(1), std::nullopt, nullptr, -1, nullptr);
14,244✔
1089

1090
    return 1;
14,244✔
1091
}
1092

1093
static int
1094
read_json_number(yajlpp_parse_context* ypc,
22,297✔
1095
                 const char* numberVal,
1096
                 size_t numberLen)
1097
{
1098
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
22,297✔
1099
    auto number_frag = string_fragment::from_bytes(numberVal, numberLen);
22,297✔
1100
    std::optional<double> val;
22,297✔
1101

1102
    intern_string_t field_name;
22,297✔
1103
    const auto* vd = jlu->get_field_def(ypc);
22,297✔
1104
    if (vd != nullptr) {
22,297✔
1105
        field_name = vd->vd_meta.lvm_name;
691✔
1106
    }
1107

1108
    if (field_name.empty()) {
22,297✔
1109
    } else if (jlu->jlu_format->lf_timestamp_field == field_name) {
691✔
1110
        long long divisor = jlu->jlu_format->elf_timestamp_divisor;
113✔
1111
        auto scan_res = scn::scan_value<double>(number_frag.to_string_view());
113✔
1112
        if (!scan_res) {
113✔
1113
            log_error("invalid number %.*s", (int) numberLen, numberVal);
×
1114
            return 0;
×
1115
        }
1116
        auto ts_val = scan_res.value().value();
113✔
1117
        timeval tv;
1118
        tv.tv_sec = ts_val / divisor;
113✔
1119
        tv.tv_usec = fmod(ts_val, divisor) * (1000000.0 / divisor);
113✔
1120
        jlu->jlu_format->lf_date_time.to_localtime(tv.tv_sec, jlu->jlu_exttm);
113✔
1121
        tv.tv_sec = tm2sec(&jlu->jlu_exttm.et_tm);
113✔
1122
        jlu->jlu_exttm.et_gmtoff
1123
            = jlu->jlu_format->lf_date_time.dts_local_offset_cache;
113✔
1124
        jlu->jlu_exttm.et_flags
113✔
1125
            |= ETF_MACHINE_ORIENTED | ETF_SUB_NOT_IN_FORMAT | ETF_ZONE_SET;
113✔
1126
        if (divisor == 1000) {
113✔
1127
            jlu->jlu_exttm.et_flags |= ETF_MILLIS_SET;
17✔
1128
        } else {
1129
            jlu->jlu_exttm.et_flags |= ETF_MICROS_SET;
96✔
1130
        }
1131
        jlu->jlu_exttm.et_nsec = tv.tv_usec * 1000;
113✔
1132
        jlu->jlu_base_line->set_time(tv);
113✔
1133
    } else if (jlu->jlu_format->lf_subsecond_field == field_name) {
578✔
1134
        auto scan_res = scn::scan_value<double>(number_frag.to_string_view());
3✔
1135
        if (!scan_res) {
3✔
1136
            log_error("invalid number %.*s", (int) numberLen, numberVal);
×
1137
            return 0;
×
1138
        }
1139
        auto ts_val = scan_res.value().value();
3✔
1140

1141
        uint64_t millis = 0;
3✔
1142
        jlu->jlu_exttm.et_flags &= ~(ETF_MICROS_SET | ETF_MILLIS_SET);
3✔
1143
        switch (jlu->jlu_format->lf_subsecond_unit.value()) {
3✔
1144
            case log_format::subsecond_unit::milli:
×
1145
                millis = ts_val;
×
1146
                jlu->jlu_exttm.et_nsec = ts_val * 1000000;
×
1147
                jlu->jlu_exttm.et_flags |= ETF_MILLIS_SET;
×
1148
                break;
×
1149
            case log_format::subsecond_unit::micro:
×
1150
                millis = std::chrono::duration_cast<std::chrono::milliseconds>(
×
1151
                             std::chrono::microseconds((int64_t) ts_val))
×
1152
                             .count();
×
1153
                jlu->jlu_exttm.et_nsec = ts_val * 1000;
×
1154
                jlu->jlu_exttm.et_flags |= ETF_MICROS_SET;
×
1155
                break;
×
1156
            case log_format::subsecond_unit::nano:
3✔
1157
                millis = std::chrono::duration_cast<std::chrono::milliseconds>(
3✔
1158
                             std::chrono::nanoseconds((int64_t) ts_val))
3✔
1159
                             .count();
3✔
1160
                jlu->jlu_exttm.et_nsec = ts_val;
3✔
1161
                jlu->jlu_exttm.et_flags |= ETF_NANOS_SET;
3✔
1162
                break;
3✔
1163
        }
1164
        jlu->jlu_exttm.et_flags |= ETF_SUB_NOT_IN_FORMAT;
3✔
1165
        jlu->jlu_base_line->set_subsecond_time(
3✔
1166
            std::chrono::milliseconds(millis));
1167
    } else if (jlu->jlu_format->elf_level_field == field_name) {
575✔
1168
        if (jlu->jlu_format->elf_level_pairs.empty()) {
300✔
1169
            jlu->jlu_base_line->set_level(jlu->jlu_format->convert_level(
186✔
1170
                number_frag, jlu->jlu_batch_context));
1171
        } else {
1172
            auto scan_res
1173
                = scn::scan_int<int64_t>(number_frag.to_string_view());
114✔
1174
            if (!scan_res) {
114✔
1175
                log_error("invalid number %.*s", (int) numberLen, numberVal);
×
1176
                return 0;
×
1177
            }
1178
            auto level_int = scan_res.value().value();
114✔
1179

1180
            for (const auto& pair : jlu->jlu_format->elf_level_pairs) {
431✔
1181
                if (pair.first == level_int) {
385✔
1182
                    jlu->jlu_base_line->set_level(pair.second);
68✔
1183
                    break;
68✔
1184
                }
1185
            }
1186
        }
1187
    } else if (vd != nullptr) {
275✔
1188
        if (jlu->jlu_format->elf_thread_id_field == field_name) {
275✔
1189
            auto& sbc = *jlu->jlu_batch_context;
70✔
1190
            auto tid_iter = sbc.sbc_tids.ltis_tid_ranges.find(number_frag);
70✔
1191
            if (tid_iter == sbc.sbc_tids.ltis_tid_ranges.end()) {
70✔
1192
                jlu->jlu_tid_frag = number_frag.to_owned(sbc.sbc_allocator);
39✔
1193
            } else {
1194
                jlu->jlu_tid_frag = tid_iter->first;
31✔
1195
            }
1196
        }
1197
        if ((vd->vd_meta.lvm_kind == value_kind_t::VALUE_INTEGER
275✔
1198
             || vd->vd_meta.lvm_kind == value_kind_t::VALUE_FLOAT)
94✔
1199
            && !vd->vd_meta.lvm_foreign_key && !vd->vd_meta.lvm_identifier)
199✔
1200
        {
1201
            auto scan_res
1202
                = scn::scan_value<double>(number_frag.to_string_view());
151✔
1203
            if (!scan_res) {
151✔
1204
                log_error("invalid number %.*s", (int) numberLen, numberVal);
×
1205
                return 0;
×
1206
            }
1207
            val = scan_res.value().value();
151✔
1208
            if (jlu->jlu_format->elf_duration_field == field_name) {
151✔
1209
                jlu->jlu_duration = std::chrono::microseconds(
×
1210
                    static_cast<int64_t>(val.value() * 1000000));
18✔
1211
            }
1212
        }
1213
    }
1214

1215
    jlu->add_sub_lines_for(vd,
22,297✔
1216
                           ypc->is_level(1),
22,297✔
1217
                           val,
1218
                           (const unsigned char*) numberVal,
1219
                           numberLen,
1220
                           nullptr);
1221

1222
    return 1;
22,297✔
1223
}
1224

1225
static int
1226
json_array_start(void* ctx)
46,381✔
1227
{
1228
    auto* ypc = (yajlpp_parse_context*) ctx;
46,381✔
1229
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
46,381✔
1230

1231
    if (ypc->ypc_path_index_stack.size() == 2) {
46,381✔
1232
        const auto* vd = jlu->get_field_def(ypc);
14,901✔
1233

1234
        jlu->add_sub_lines_for(vd, true, std::nullopt, nullptr, -1, nullptr);
14,901✔
1235
        jlu->jlu_sub_start = yajl_get_bytes_consumed(jlu->jlu_handle) - 1;
14,901✔
1236
    }
1237

1238
    return 1;
46,381✔
1239
}
1240

1241
static int
1242
json_array_start_const(void* ctx)
13,480✔
1243
{
1244
    auto* ypc = (yajlpp_parse_context*) ctx;
13,480✔
1245
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
13,480✔
1246

1247
    if (ypc->ypc_path_index_stack.size() == 2) {
13,480✔
1248
        jlu->jlu_sub_start = yajl_get_bytes_consumed(jlu->jlu_handle) - 1;
3,635✔
1249
    }
1250

1251
    return 1;
13,480✔
1252
}
1253

1254
static int
1255
json_array_end(void* ctx)
13,460✔
1256
{
1257
    auto* ypc = (yajlpp_parse_context*) ctx;
13,460✔
1258
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
13,460✔
1259

1260
    if (ypc->ypc_path_index_stack.size() == 1) {
13,460✔
1261
        const intern_string_t field_name = ypc->get_path_fragment_i(0);
3,629✔
1262
        size_t sub_end = yajl_get_bytes_consumed(jlu->jlu_handle);
3,629✔
1263
        auto json_frag = string_fragment::from_byte_range(
3,629✔
1264
            jlu->jlu_shared_buffer.get_data(), jlu->jlu_sub_start, sub_end);
3,629✔
1265
        jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
3,629✔
1266
            jlu->jlu_format->get_value_meta(field_name,
7,258✔
1267
                                            value_kind_t::VALUE_JSON),
1268
            json_frag);
1269
        if (field_name == jlu->jlu_format->elf_opid_field) {
3,629✔
1270
            jlu->jlu_opid_desc_frag = json_frag;
28✔
1271
        }
1272
    }
1273

1274
    return 1;
13,460✔
1275
}
1276

1277
static int
1278
read_array_end(void* ctx)
46,188✔
1279
{
1280
    auto* ypc = (yajlpp_parse_context*) ctx;
46,188✔
1281
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
46,188✔
1282

1283
    if (ypc->ypc_path_index_stack.size() == 1) {
46,188✔
1284
        const intern_string_t field_name = ypc->get_path_fragment_i(0);
14,876✔
1285
        size_t sub_end = yajl_get_bytes_consumed(jlu->jlu_handle);
14,876✔
1286
        auto json_frag = string_fragment::from_byte_range(
14,876✔
1287
            jlu->jlu_shared_buffer.get_data(), jlu->jlu_sub_start, sub_end);
14,876✔
1288
        if (field_name == jlu->jlu_format->elf_opid_field) {
14,876✔
1289
            jlu->jlu_opid_desc_frag = json_frag;
2,232✔
1290
        }
1291
    }
1292

1293
    return 1;
46,188✔
1294
}
1295

1296
static const json_path_container json_log_handlers = {
1297
    yajlpp::pattern_property_handler("\\w+")
1298
        .add_cb(read_json_null)
1299
        .add_cb(read_json_bool)
1300
        .add_cb(read_json_number)
1301
        .add_cb(read_json_field),
1302
};
1303

1304
static int rewrite_json_field(yajlpp_parse_context* ypc,
1305
                              const unsigned char* str,
1306
                              size_t len,
1307
                              yajl_string_props_t*);
1308

1309
static int
1310
rewrite_json_null(yajlpp_parse_context* ypc)
1,957✔
1311
{
1312
    auto jlu = (json_log_userdata*) ypc->ypc_userdata;
1,957✔
1313
    const auto* vd = jlu->get_field_def(ypc);
1,957✔
1314

1315
    if (!ypc->is_level(1) && vd == nullptr) {
1,957✔
1316
        return 1;
1,957✔
1317
    }
1318
    jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
×
1319
        jlu->jlu_format->get_value_meta(ypc, vd, value_kind_t::VALUE_NULL));
×
1320

1321
    return 1;
×
1322
}
1323

1324
static int
1325
rewrite_json_bool(yajlpp_parse_context* ypc, int val)
8,809✔
1326
{
1327
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
8,809✔
1328
    const auto* vd = jlu->get_field_def(ypc);
8,809✔
1329

1330
    if (!ypc->is_level(1) && vd == nullptr) {
8,809✔
1331
        return 1;
7,861✔
1332
    }
1333
    jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
948✔
1334
        jlu->jlu_format->get_value_meta(ypc, vd, value_kind_t::VALUE_BOOLEAN),
948✔
1335
        (bool) val);
948✔
1336
    return 1;
948✔
1337
}
1338

1339
static int
1340
rewrite_json_int(yajlpp_parse_context* ypc, long long val)
10,755✔
1341
{
1342
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
10,755✔
1343
    const auto* vd = jlu->get_field_def(ypc);
10,755✔
1344

1345
    if (vd != nullptr) {
10,755✔
1346
        const intern_string_t field_name = vd->vd_meta.lvm_name;
748✔
1347
        if (jlu->jlu_format->lf_timestamp_field == field_name) {
748✔
1348
            long long divisor = jlu->jlu_format->elf_timestamp_divisor;
25✔
1349
            timeval tv;
1350

1351
            tv.tv_sec = val / divisor;
25✔
1352
            tv.tv_usec = fmod(val, divisor) * (1000000.0 / divisor);
25✔
1353
            jlu->jlu_format->lf_date_time.to_localtime(tv.tv_sec,
25✔
1354
                                                       jlu->jlu_exttm);
25✔
1355
            jlu->jlu_exttm.et_gmtoff
1356
                = jlu->jlu_format->lf_date_time.dts_local_offset_cache;
25✔
1357
            jlu->jlu_exttm.et_flags |= ETF_MACHINE_ORIENTED
25✔
1358
                | ETF_SUB_NOT_IN_FORMAT | ETF_ZONE_SET | ETF_Z_FOR_UTC;
1359
            if (divisor == 1) {
25✔
1360
                jlu->jlu_exttm.et_flags |= ETF_MICROS_SET;
4✔
1361
            } else {
1362
                jlu->jlu_exttm.et_flags |= ETF_MILLIS_SET;
21✔
1363
            }
1364
            jlu->jlu_exttm.et_nsec = tv.tv_usec * 1000;
25✔
1365
        } else if (jlu->jlu_format->lf_subsecond_field == field_name) {
723✔
1366
            jlu->jlu_exttm.et_flags &= ~(ETF_MICROS_SET | ETF_MILLIS_SET);
4✔
1367
            switch (jlu->jlu_format->lf_subsecond_unit.value()) {
4✔
1368
                case log_format::subsecond_unit::milli:
×
1369
                    jlu->jlu_exttm.et_nsec = val * 1000000;
×
1370
                    jlu->jlu_exttm.et_flags |= ETF_MILLIS_SET;
×
1371
                    break;
×
1372
                case log_format::subsecond_unit::micro:
×
1373
                    jlu->jlu_exttm.et_nsec = val * 1000;
×
1374
                    jlu->jlu_exttm.et_flags |= ETF_MICROS_SET;
×
1375
                    break;
×
1376
                case log_format::subsecond_unit::nano:
4✔
1377
                    jlu->jlu_exttm.et_nsec = val;
4✔
1378
                    jlu->jlu_exttm.et_flags |= ETF_NANOS_SET;
4✔
1379
                    break;
4✔
1380
            }
1381
            jlu->jlu_exttm.et_flags |= ETF_SUB_NOT_IN_FORMAT;
4✔
1382
        }
1383
    }
1384

1385
    if (!ypc->is_level(1) && vd == nullptr) {
10,755✔
1386
        return 1;
9,960✔
1387
    }
1388
    if (vd != nullptr
795✔
1389
        && vd->vd_meta.lvm_name == jlu->jlu_format->elf_thread_id_field)
795✔
1390
    {
1391
        jlu->jlu_tid_number = val;
114✔
1392
    }
1393
    jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
795✔
1394
        jlu->jlu_format->get_value_meta(ypc, vd, value_kind_t::VALUE_INTEGER),
795✔
1395
        (int64_t) val);
795✔
1396
    return 1;
795✔
1397
}
1398

1399
static int
1400
rewrite_json_double(yajlpp_parse_context* ypc, double val)
162✔
1401
{
1402
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
162✔
1403
    const auto* vd = jlu->get_field_def(ypc);
162✔
1404

1405
    if (vd != nullptr) {
162✔
1406
        const intern_string_t field_name = vd->vd_meta.lvm_name;
158✔
1407
        if (jlu->jlu_format->lf_timestamp_field == field_name) {
158✔
1408
            long long divisor = jlu->jlu_format->elf_timestamp_divisor;
130✔
1409
            timeval tv;
1410

1411
            tv.tv_sec = val / divisor;
130✔
1412
            tv.tv_usec = fmod(val, divisor) * (1000000.0 / divisor);
130✔
1413
            jlu->jlu_format->lf_date_time.to_localtime(tv.tv_sec,
130✔
1414
                                                       jlu->jlu_exttm);
130✔
1415
            jlu->jlu_exttm.et_gmtoff
1416
                = jlu->jlu_format->lf_date_time.dts_local_offset_cache;
130✔
1417
            jlu->jlu_exttm.et_flags |= ETF_MACHINE_ORIENTED
130✔
1418
                | ETF_SUB_NOT_IN_FORMAT | ETF_ZONE_SET | ETF_Z_FOR_UTC;
1419
            if (divisor == 1) {
130✔
1420
                jlu->jlu_exttm.et_flags |= ETF_MICROS_SET;
118✔
1421
            } else {
1422
                jlu->jlu_exttm.et_flags |= ETF_MILLIS_SET;
12✔
1423
            }
1424
            jlu->jlu_exttm.et_nsec = tv.tv_usec * 1000;
130✔
1425
        } else if (jlu->jlu_format->lf_subsecond_field == field_name) {
28✔
1426
            jlu->jlu_exttm.et_flags &= ~(ETF_MICROS_SET | ETF_MILLIS_SET);
×
1427
            switch (jlu->jlu_format->lf_subsecond_unit.value()) {
×
1428
                case log_format::subsecond_unit::milli:
×
1429
                    jlu->jlu_exttm.et_nsec = val * 1000000;
×
1430
                    jlu->jlu_exttm.et_flags |= ETF_MILLIS_SET;
×
1431
                    break;
×
1432
                case log_format::subsecond_unit::micro:
×
1433
                    jlu->jlu_exttm.et_nsec = val * 1000;
×
1434
                    jlu->jlu_exttm.et_flags |= ETF_MICROS_SET;
×
1435
                    break;
×
1436
                case log_format::subsecond_unit::nano:
×
1437
                    jlu->jlu_exttm.et_nsec = val;
×
1438
                    jlu->jlu_exttm.et_flags |= ETF_NANOS_SET;
×
1439
                    break;
×
1440
            }
1441
            jlu->jlu_exttm.et_flags |= ETF_SUB_NOT_IN_FORMAT;
×
1442
        } else if (jlu->jlu_format->elf_duration_field == field_name) {
28✔
1443
            jlu->jlu_duration = std::chrono::microseconds(
56✔
1444
                static_cast<int64_t>(val * 1000000.0));
28✔
1445
        }
1446
    }
1447

1448
    if (!ypc->is_level(1) && vd == nullptr) {
162✔
1449
        return 1;
×
1450
    }
1451
    jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
162✔
1452
        jlu->jlu_format->get_value_meta(ypc, vd, value_kind_t::VALUE_FLOAT),
324✔
1453
        val);
1454

1455
    return 1;
162✔
1456
}
1457

1458
static const json_path_container json_log_rewrite_handlers = {
1459
    yajlpp::pattern_property_handler("\\w+")
1460
        .add_cb(rewrite_json_null)
1461
        .add_cb(rewrite_json_bool)
1462
        .add_cb(rewrite_json_int)
1463
        .add_cb(rewrite_json_double)
1464
        .add_cb(rewrite_json_field),
1465
};
1466

1467
bool
1468
external_log_format::scan_for_partial(const log_format_file_state& lffs,
1✔
1469
                                      shared_buffer_ref& sbr,
1470
                                      size_t& len_out) const
1471
{
1472
    if (this->elf_type != elf_type_t::ELF_TYPE_TEXT) {
1✔
1473
        return false;
×
1474
    }
1475

1476
    const auto& pat
1477
        = this->elf_pattern_order[lffs.lffs_pattern_locks.last_pattern_index()];
1✔
1478
    if (!this->lf_multiline) {
1✔
1479
        len_out = pat->p_pcre.pp_value->match_partial(sbr.to_string_fragment());
1✔
1480
        return true;
1✔
1481
    }
1482

1483
    if (pat->p_timestamp_end == -1 || pat->p_timestamp_end > (int) sbr.length())
×
1484
    {
1485
        len_out = 0;
×
1486
        return false;
×
1487
    }
1488

1489
    len_out = pat->p_pcre.pp_value->match_partial(sbr.to_string_fragment());
×
1490
    return (int) len_out > pat->p_timestamp_end;
×
1491
}
1492

1493
std::vector<lnav::console::snippet>
1494
external_log_format::get_snippets() const
23✔
1495
{
1496
    std::vector<lnav::console::snippet> retval;
23✔
1497

1498
    for (const auto& src_pair : this->elf_format_sources) {
46✔
1499
        retval.emplace_back(lnav::console::snippet::from(src_pair.first, "")
46✔
1500
                                .with_line(src_pair.second));
23✔
1501
    }
1502

1503
    return retval;
23✔
1504
}
×
1505

1506
log_format::scan_result_t
1507
external_log_format::scan_json(std::vector<logline>& dst,
133,172✔
1508
                               const line_info& li,
1509
                               shared_buffer_ref& sbr,
1510
                               scan_batch_context& sbc)
1511
{
1512
    logline ll(
1513
        li.li_file_range.fr_offset, std::chrono::microseconds{0}, LEVEL_INFO);
133,172✔
1514
    auto line_frag = sbr.to_string_fragment();
133,172✔
1515

1516
    if (!line_frag.startswith("{")) {
133,172✔
1517
        if (!this->lf_specialized) {
124,272✔
1518
            return scan_no_match{"line is not a JSON object"};
124,267✔
1519
        }
1520

1521
        ll.set_time(dst.back().get_time<std::chrono::microseconds>());
5✔
1522
        ll.set_level(LEVEL_INVALID);
5✔
1523
        dst.emplace_back(ll);
5✔
1524
        return scan_match{0};
5✔
1525
    }
1526

1527
    auto& ypc = *(this->jlf_parse_context);
8,900✔
1528
    yajl_handle handle = this->jlf_yajl_handle.get();
8,900✔
1529
    json_log_userdata jlu(sbr, &sbc);
8,900✔
1530

1531
    if (li.li_partial) {
8,900✔
1532
        log_debug("skipping partial line at offset %lld",
×
1533
                  li.li_file_range.fr_offset);
1534
        if (this->lf_specialized) {
×
1535
            if (!dst.empty()) {
×
1536
                ll.set_time(dst.back().get_time<std::chrono::microseconds>());
×
1537
            }
1538
            ll.set_level(LEVEL_INVALID);
×
1539
            dst.emplace_back(ll);
×
1540
        }
1541
        return scan_incomplete{};
×
1542
    }
1543

1544
    const auto* line_data = (const unsigned char*) sbr.get_data();
8,900✔
1545

1546
    this->lf_desc_captures.clear();
8,900✔
1547
    this->lf_desc_allocator.reset();
8,900✔
1548

1549
    yajl_reset(handle);
8,900✔
1550
    ypc.set_static_handler(json_log_handlers.jpc_children[0]);
8,900✔
1551
    ypc.ypc_userdata = &jlu;
8,900✔
1552
    ypc.ypc_ignore_unused = true;
8,900✔
1553
    ypc.ypc_alt_callbacks.yajl_start_array = json_array_start;
8,900✔
1554
    ypc.ypc_alt_callbacks.yajl_start_map = json_array_start;
8,900✔
1555
    ypc.ypc_alt_callbacks.yajl_end_array = read_array_end;
8,900✔
1556
    ypc.ypc_alt_callbacks.yajl_end_map = read_array_end;
8,900✔
1557
    jlu.jlu_format = this;
8,900✔
1558
    jlu.jlu_base_line = &ll;
8,900✔
1559
    jlu.jlu_line_value = sbr.get_data();
8,900✔
1560
    jlu.jlu_line_size = sbr.length();
8,900✔
1561
    jlu.jlu_handle = handle;
8,900✔
1562
    jlu.jlu_format_hits.resize(this->jlf_line_format.size());
8,900✔
1563
    if (yajl_parse(handle, line_data, sbr.length()) == yajl_status_ok
8,900✔
1564
        && yajl_complete_parse(handle) == yajl_status_ok)
8,900✔
1565
    {
1566
        if (ll.get_time<std::chrono::microseconds>().count() == 0) {
8,732✔
1567
            if (this->lf_specialized) {
5,613✔
1568
                if (!dst.empty()) {
×
1569
                    ll.set_time(
×
1570
                        dst.back().get_time<std::chrono::microseconds>());
×
1571
                }
1572
                ll.set_ignore(true);
×
1573
                dst.emplace_back(ll);
×
1574
                return scan_match{0};
×
1575
            }
1576

1577
            return scan_no_match{
5,613✔
1578
                "JSON message does not have expected timestamp property"};
5,613✔
1579
        }
1580

1581
        if (jlu.jlu_tid_frag) {
3,119✔
1582
            this->jlf_line_values.lvv_thread_id_value
1583
                = jlu.jlu_tid_frag->to_string();
70✔
1584
            auto tid_iter = sbc.sbc_tids.insert_tid(
140✔
1585
                sbc.sbc_allocator,
1586
                jlu.jlu_tid_frag.value(),
70✔
1587
                ll.get_time<std::chrono::microseconds>());
70✔
1588
            tid_iter->second.titr_level_stats.update_msg_count(
70✔
1589
                ll.get_msg_level());
1590
        } else {
1591
            auto tid_iter = sbc.sbc_tids.insert_tid(
3,049✔
1592
                sbc.sbc_allocator,
1593
                string_fragment{},
×
1594
                ll.get_time<std::chrono::microseconds>());
3,049✔
1595
            tid_iter->second.titr_level_stats.update_msg_count(
3,049✔
1596
                ll.get_msg_level());
1597
        }
1598

1599
        auto found_opid_desc = false;
3,119✔
1600
        if (this->elf_opid_field.empty()
3,119✔
1601
            && this->lf_opid_source.value_or(opid_source_t::from_description)
694✔
1602
                == opid_source_t::from_description
1603
            && this->lf_opid_description_def->size() == 1)
3,813✔
1604
        {
1605
            const auto& od = this->lf_opid_description_def->begin()->second;
322✔
1606
            for (const auto& desc : *od.od_descriptors) {
966✔
1607
                auto desc_iter
1608
                    = this->lf_desc_captures.find(desc.od_field.pp_value);
644✔
1609
                if (desc_iter == this->lf_desc_captures.end()) {
644✔
1610
                    continue;
602✔
1611
                }
1612
                jlu.jlu_opid_hasher.update(desc_iter->second);
42✔
1613
                found_opid_desc = true;
42✔
1614
            }
1615

1616
        } else if (!jlu.jlu_opid_desc_frag && !jlu.jlu_opid_frag
3,362✔
1617
                   && jlu.jlu_duration > 0us)
3,362✔
1618
        {
1619
            jlu.jlu_opid_hasher.update(sbr.to_string_fragment());
×
1620
        }
1621

1622
        if (jlu.jlu_opid_desc_frag || jlu.jlu_duration > 0us
4,006✔
1623
            || (found_opid_desc && this->lf_opid_description_def->size() == 1))
4,006✔
1624
        {
1625
            char buf[hasher::STRING_SIZE];
1626
            jlu.jlu_opid_hasher.to_string(buf);
2,253✔
1627
            auto opid_frag = string_fragment::from_bytes(buf, sizeof(buf) - 1);
2,253✔
1628
            jlu.jlu_base_line->set_opid(opid_frag.hash());
2,253✔
1629
            auto opid_iter = sbc.sbc_opids.los_opid_ranges.find(opid_frag);
2,253✔
1630
            if (opid_iter == sbc.sbc_opids.los_opid_ranges.end()) {
2,253✔
1631
                jlu.jlu_opid_frag = opid_frag.to_owned(sbc.sbc_allocator);
2,241✔
1632
            } else {
1633
                jlu.jlu_opid_frag = opid_iter->first;
12✔
1634
            }
1635
        }
1636

1637
        if (jlu.jlu_opid_frag) {
3,119✔
1638
            this->jlf_line_values.lvv_opid_value
1639
                = jlu.jlu_opid_frag->to_string();
2,417✔
1640
            this->jlf_line_values.lvv_opid_provenance
1641
                = logline_value_vector::opid_provenance::file;
2,417✔
1642
            auto opid_iter = sbc.sbc_opids.insert_op(
4,834✔
1643
                sbc.sbc_allocator,
1644
                jlu.jlu_opid_frag.value(),
2,417✔
1645
                ll.get_time<std::chrono::microseconds>(),
2,417✔
1646
                this->lf_timestamp_point_of_reference,
1647
                jlu.jlu_duration);
1648
            opid_iter->second.otr_level_stats.update_msg_count(
2,417✔
1649
                ll.get_msg_level());
1650
            auto& elems = opid_iter->second.otr_description.lod_elements;
2,417✔
1651
            if (jlu.jlu_opid_desc_frag && elems.empty()) {
2,417✔
1652
                elems.insert(0,
×
1653
                             fmt::format(FMT_STRING(" {}"),
8,904✔
1654
                                         jlu.jlu_opid_desc_frag.value()));
1655
            }
1656

1657
            if (jlu.jlu_subid) {
2,417✔
1658
                auto subid_frag
1659
                    = string_fragment::from_str(jlu.jlu_subid.value());
×
1660

1661
                auto* ostr = sbc.sbc_opids.sub_op_in_use(
×
1662
                    sbc.sbc_allocator,
1663
                    opid_iter,
1664
                    subid_frag,
1665
                    ll.get_time<std::chrono::microseconds>(),
×
1666
                    ll.get_msg_level());
1667
                if (ostr != nullptr && ostr->ostr_description.empty()) {
×
1668
                    log_op_description sub_desc;
×
1669
                    this->update_op_description(
×
1670
                        *this->lf_subid_description_def_vec, sub_desc);
×
1671
                    if (!sub_desc.lod_elements.empty()) {
×
1672
                        auto& sub_desc_def
1673
                            = this->lf_subid_description_def_vec->at(
×
1674
                                sub_desc.lod_index.value());
×
1675
                        ostr->ostr_description
1676
                            = sub_desc_def->to_string(sub_desc.lod_elements);
×
1677
                    }
1678
                }
1679
            }
1680

1681
            auto& otr = opid_iter->second;
2,417✔
1682
            this->update_op_description(*this->lf_opid_description_def_vec,
2,417✔
1683
                                        otr.otr_description);
2,417✔
1684
        } else {
1685
            this->jlf_line_values.lvv_opid_value = std::nullopt;
702✔
1686
        }
1687

1688
        jlu.jlu_sub_line_count += this->jlf_line_format_init_count;
3,119✔
1689
        for (int lpc = 0; lpc < jlu.jlu_sub_line_count; lpc++) {
16,518✔
1690
            ll.set_sub_offset(lpc);
13,399✔
1691
            if (lpc > 0) {
13,399✔
1692
                ll.set_level(
10,280✔
1693
                    (log_level_t) (ll.get_level_and_flags() | LEVEL_CONTINUED));
10,280✔
1694
            }
1695
            ll.set_has_ansi(jlu.jlu_has_ansi);
13,399✔
1696
            ll.set_valid_utf(jlu.jlu_valid_utf);
13,399✔
1697
            dst.emplace_back(ll);
13,399✔
1698
        }
1699

1700
        if (!this->lf_specialized) {
3,119✔
1701
            static const intern_string_t ts_field
1702
                = intern_string::lookup("__timestamp__", -1);
2,601✔
1703
            static const intern_string_t level_field
1704
                = intern_string::lookup("__level__");
4,081✔
1705
            for (const auto& [index, jfe] :
24,354✔
1706
                 lnav::itertools::enumerate(this->jlf_line_format))
26,955✔
1707
            {
1708
                if (jfe.jfe_type != json_log_field::VARIABLE
57,643✔
1709
                    || jfe.jfe_value.pp_value == ts_field
14,289✔
1710
                    || jfe.jfe_value.pp_value == level_field
11,742✔
1711
                    || jfe.jfe_default_value != "-")
36,042✔
1712
                {
1713
                    continue;
14,137✔
1714
                }
1715
                if (!jlu.jlu_format_hits[index]) {
7,616✔
1716
                    jlu.jlu_strikes += 1;
7,200✔
1717
                }
1718
            }
1719
        }
1720
    } else {
1721
        unsigned char* msg;
1722
        int line_count = 2;
168✔
1723

1724
        msg = yajl_get_error(
336✔
1725
            handle, 1, (const unsigned char*) sbr.get_data(), sbr.length());
168✔
1726
        if (msg != nullptr) {
168✔
1727
            auto msg_frag = string_fragment::from_c_str(msg);
168✔
1728
            log_debug("Unable to parse line at offset %lld: %.*s",
168✔
1729
                      li.li_file_range.fr_offset,
1730
                      msg_frag.length(),
1731
                      msg_frag.data());
1732
            line_count = msg_frag.count('\n') + 1;
168✔
1733
            yajl_free_error(handle, msg);
168✔
1734
        }
1735
        if (!this->lf_specialized) {
168✔
1736
            return scan_no_match{"JSON parsing failed"};
165✔
1737
        }
1738
        for (int lpc = 0; lpc < line_count; lpc++) {
15✔
1739
            log_level_t level = LEVEL_INVALID;
12✔
1740

1741
            ll.set_time(dst.back().get_timeval());
12✔
1742
            if (lpc > 0) {
12✔
1743
                level = (log_level_t) (level | LEVEL_CONTINUED);
9✔
1744
            }
1745
            ll.set_level(level);
12✔
1746
            ll.set_sub_offset(lpc);
12✔
1747
            dst.emplace_back(ll);
12✔
1748
        }
1749
    }
1750

1751
    if (jlu.jlu_quality > 0) {
3,122✔
1752
        jlu.jlu_quality += 3000;
786✔
1753
    }
1754
    return scan_match{jlu.jlu_quality, jlu.jlu_strikes};
3,122✔
1755
}
8,900✔
1756

1757
log_format::scan_result_t
1758
external_log_format::scan(logfile& lf,
782,248✔
1759
                          std::vector<logline>& dst,
1760
                          const line_info& li,
1761
                          shared_buffer_ref& sbr,
1762
                          scan_batch_context& sbc)
1763
{
1764
    if (dst.empty()) {
782,248✔
1765
        auto file_options = lf.get_file_options();
32,022✔
1766

1767
        if (file_options) {
32,022✔
1768
            this->lf_date_time.dts_default_zone
1769
                = file_options->second.fo_default_zone.pp_value;
2,220✔
1770
        } else {
1771
            this->lf_date_time.dts_default_zone = nullptr;
29,802✔
1772
        }
1773
    }
32,022✔
1774

1775
    sbc.sbc_value_stats.resize(this->elf_value_defs.size());
782,248✔
1776
    if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
782,248✔
1777
        return this->scan_json(dst, li, sbr, sbc);
130,950✔
1778
    }
1779

1780
    int curr_fmt = -1, orig_lock = sbc.sbc_pattern_locks.last_pattern_index();
651,298✔
1781
    int pat_index = orig_lock;
651,298✔
1782
    auto line_sf = sbr.to_string_fragment();
651,298✔
1783
    thread_local auto md = lnav::pcre2pp::match_data::unitialized();
651,298✔
1784
    char tmp_opid_buf[hasher::STRING_SIZE];
1785

1786
    while (::next_format(this->elf_pattern_order, curr_fmt, pat_index)) {
2,152,735✔
1787
        auto* fpat = this->elf_pattern_order[curr_fmt].get();
1,507,268✔
1788
        auto* pat = fpat->p_pcre.pp_value.get();
1,507,268✔
1789

1790
        auto found_match
1791
            = pat->capture_from(line_sf).into(md).found_p(PCRE2_NO_UTF_CHECK);
1,507,268✔
1792
        if (!found_match) {
1,507,268✔
1793
            if (!sbc.sbc_pattern_locks.empty() && pat_index != -1) {
1,501,434✔
1794
                curr_fmt = -1;
1,994✔
1795
                pat_index = -1;
1,994✔
1796
            }
1797
            continue;
1,501,437✔
1798
        }
1799

1800
        auto ts = md[fpat->p_timestamp_field_index];
5,834✔
1801
        auto level_cap = md[fpat->p_level_field_index];
5,834✔
1802
        auto opid_cap = md[fpat->p_opid_field_index];
5,834✔
1803
        const char* last;
1804
        exttm log_time_tm;
5,834✔
1805
        timeval log_tv;
1806
        uint16_t opid = 0;
5,834✔
1807
        char combined_datetime_buf[512];
1808

1809
        if (fpat->p_time_field_index != -1) {
5,834✔
1810
            auto time_cap = md[fpat->p_time_field_index];
×
1811
            if (ts && time_cap) {
×
1812
                auto ts_str_len = snprintf(combined_datetime_buf,
×
1813
                                           sizeof(combined_datetime_buf),
1814
                                           "%.*sT%.*s",
1815
                                           ts->length(),
1816
                                           ts->data(),
1817
                                           time_cap->length(),
1818
                                           time_cap->data());
1819
                ts = string_fragment::from_bytes(combined_datetime_buf,
×
1820
                                                 ts_str_len);
×
1821
            }
1822
        }
1823

1824
        auto level = this->convert_level(
5,834✔
1825
            level_cap.value_or(string_fragment::invalid()), &sbc);
5,834✔
1826

1827
        if (!ts) {
5,834✔
1828
            level = log_level_t::LEVEL_INVALID;
×
1829
        } else if ((last
5,834✔
1830
                    = this->lf_date_time.scan(ts->data(),
11,668✔
1831
                                              ts->length(),
5,834✔
1832
                                              this->get_timestamp_formats(),
1833
                                              &log_time_tm,
1834
                                              log_tv))
1835
                   == nullptr)
5,834✔
1836
        {
1837
            auto ls = this->lf_date_time.unlock();
13✔
1838
            if ((last = this->lf_date_time.scan(ts->data(),
26✔
1839
                                                ts->length(),
13✔
1840
                                                this->get_timestamp_formats(),
1841
                                                &log_time_tm,
1842
                                                log_tv))
1843
                == nullptr)
13✔
1844
            {
1845
                this->lf_date_time.relock(ls);
2✔
1846
                continue;
3✔
1847
            }
1848
            if (last != nullptr) {
11✔
1849
                auto old_flags = this->lf_timestamp_flags & DATE_TIME_SET_FLAGS;
11✔
1850
                auto new_flags = log_time_tm.et_flags & DATE_TIME_SET_FLAGS;
11✔
1851

1852
                // It is unlikely a valid timestamp would lose much
1853
                // precision.
1854
                if (new_flags != old_flags) {
11✔
1855
                    continue;
1✔
1856
                }
1857
            }
1858

1859
            log_debug("%s:%zu: date-time re-locked to %d",
10✔
1860
                      lf.get_unique_path().c_str(),
1861
                      dst.size(),
1862
                      this->lf_date_time.dts_fmt_lock);
1863
        }
1864

1865
        this->lf_timestamp_flags = log_time_tm.et_flags;
5,831✔
1866

1867
        if (!(this->lf_timestamp_flags
11,662✔
1868
              & (ETF_MILLIS_SET | ETF_MICROS_SET | ETF_NANOS_SET))
5,831✔
1869
            && !dst.empty()
5,287✔
1870
            && dst.back().get_time<std::chrono::seconds>().count()
4,548✔
1871
                == log_tv.tv_sec
4,548✔
1872
            && dst.back()
14,554✔
1873
                    .get_subsecond_time<std::chrono::milliseconds>()
9,267✔
1874
                    .count()
3,436✔
1875
                != 0)
1876
        {
1877
            auto log_ms
1878
                = dst.back().get_subsecond_time<std::chrono::microseconds>();
×
1879

1880
            log_time_tm.et_nsec
1881
                = std::chrono::duration_cast<std::chrono::nanoseconds>(log_ms)
×
1882
                      .count();
×
1883
            log_tv.tv_usec
1884
                = std::chrono::duration_cast<std::chrono::microseconds>(log_ms)
×
1885
                      .count();
×
1886
        }
1887

1888
        if (!((log_time_tm.et_flags & ETF_DAY_SET)
5,831✔
1889
              && (log_time_tm.et_flags & ETF_MONTH_SET)
5,790✔
1890
              && (log_time_tm.et_flags & ETF_YEAR_SET)))
5,790✔
1891
        {
1892
            this->check_for_new_year(dst, log_time_tm, log_tv);
794✔
1893
        }
1894

1895
        auto log_us = to_us(log_tv);
5,831✔
1896
        if (!fpat->p_opid_description_field_indexes.empty()) {
5,831✔
1897
            hasher h;
4,504✔
1898
            for (auto& fidx : fpat->p_opid_description_field_indexes) {
13,009✔
1899
                auto desc_cap = md[fidx];
8,505✔
1900
                if (desc_cap) {
8,505✔
1901
                    h.update(desc_cap.value());
8,458✔
1902
                }
1903
            }
1904
            h.to_string(tmp_opid_buf);
4,504✔
1905
            opid_cap = string_fragment::from_bytes(tmp_opid_buf,
9,008✔
1906
                                                   sizeof(tmp_opid_buf) - 1);
4,504✔
1907
        }
1908

1909
        auto duration_cap = md[fpat->p_duration_field_index];
5,831✔
1910
        if (duration_cap && !opid_cap) {
5,831✔
1911
            hasher h;
86✔
1912
            h.update(line_sf);
86✔
1913
            h.to_string(tmp_opid_buf);
86✔
1914
            opid_cap = string_fragment::from_bytes(tmp_opid_buf,
172✔
1915
                                                   sizeof(tmp_opid_buf) - 1);
86✔
1916
        }
1917
        if (opid_cap && !opid_cap->empty()) {
5,831✔
1918
            auto duration = std::chrono::microseconds{0};
5,385✔
1919
            if (duration_cap) {
5,385✔
1920
                auto from_res
1921
                    = humanize::try_from<double>(duration_cap.value());
86✔
1922
                if (from_res) {
86✔
1923
                    duration = std::chrono::microseconds(
×
1924
                        static_cast<int64_t>(from_res.value() * 1000000));
86✔
1925
                }
1926
            }
1927
            auto opid_iter
1928
                = sbc.sbc_opids.insert_op(sbc.sbc_allocator,
10,770✔
1929
                                          opid_cap.value(),
5,385✔
1930
                                          log_us,
1931
                                          this->lf_timestamp_point_of_reference,
1932
                                          duration);
1933
            auto& otr = opid_iter->second;
5,385✔
1934

1935
            otr.otr_level_stats.update_msg_count(level);
5,385✔
1936
            if (fpat->p_subid_field_index != -1) {
5,385✔
1937
                auto subid_cap = md[fpat->p_subid_field_index];
64✔
1938
                if (subid_cap && !subid_cap->empty()) {
64✔
1939
                    auto* ostr = sbc.sbc_opids.sub_op_in_use(sbc.sbc_allocator,
192✔
1940
                                                             opid_iter,
1941
                                                             subid_cap.value(),
64✔
1942
                                                             log_us,
1943
                                                             level);
1944
                    if (ostr != nullptr && ostr->ostr_description.empty()) {
64✔
1945
                        log_op_description sub_desc;
43✔
1946
                        this->update_op_description(
43✔
1947
                            *this->lf_subid_description_def_vec,
43✔
1948
                            sub_desc,
1949
                            fpat,
1950
                            md);
1951
                        if (!sub_desc.lod_elements.empty()) {
43✔
1952
                            auto& sub_desc_def
1953
                                = this->lf_subid_description_def_vec->at(
39✔
1954
                                    sub_desc.lod_index.value());
39✔
1955
                            ostr->ostr_description = sub_desc_def->to_string(
78✔
1956
                                sub_desc.lod_elements);
39✔
1957
                        }
1958
                    }
43✔
1959
                }
1960
            }
1961
            this->update_op_description(*this->lf_opid_description_def_vec,
5,385✔
1962
                                        otr.otr_description,
5,385✔
1963
                                        fpat,
1964
                                        md);
1965
            opid = opid_cap->hash();
5,385✔
1966
        }
1967

1968
        for (const auto& ivd : fpat->p_value_by_index) {
66,109✔
1969
            if (!ivd.ivd_value_def->vd_meta.lvm_values_index) {
60,278✔
1970
                continue;
11,000✔
1971
            }
1972

1973
            ssize_t cap_size = md.capture_size(ivd.ivd_index);
49,278✔
1974
            auto& lvs = sbc.sbc_value_stats[ivd.ivd_value_def->vd_meta
49,278✔
1975
                                                .lvm_values_index.value()];
49,278✔
1976

1977
            if (cap_size > lvs.lvs_width) {
49,278✔
1978
                lvs.lvs_width = cap_size;
6,337✔
1979
            }
1980
        }
1981

1982
        for (auto value_index : fpat->p_numeric_value_indexes) {
10,678✔
1983
            const indexed_value_def& ivd = fpat->p_value_by_index[value_index];
4,847✔
1984
            const value_def& vd = *ivd.ivd_value_def;
4,847✔
1985
            auto num_cap = md[ivd.ivd_index];
4,847✔
1986

1987
            if (num_cap && num_cap->is_valid()) {
4,847✔
1988
                const struct scaling_factor* scaling = nullptr;
4,803✔
1989

1990
                if (ivd.ivd_unit_field_index >= 0) {
4,803✔
1991
                    auto unit_cap = md[ivd.ivd_unit_field_index];
80✔
1992

1993
                    if (unit_cap && unit_cap->is_valid()) {
80✔
1994
                        intern_string_t unit_val
1995
                            = intern_string::lookup(unit_cap.value());
80✔
1996

1997
                        auto unit_iter = vd.vd_unit_scaling.find(unit_val);
80✔
1998
                        if (unit_iter != vd.vd_unit_scaling.end()) {
80✔
1999
                            const auto& sf = unit_iter->second;
80✔
2000

2001
                            scaling = &sf;
80✔
2002
                        }
2003
                    }
2004
                }
2005

2006
                std::optional<double> dvalue_opt;
4,803✔
2007
                switch (vd.vd_meta.lvm_kind) {
4,803✔
2008
                    case value_kind_t::VALUE_INTEGER: {
4,637✔
2009
                        auto scan_res
2010
                            = scn::scan_int<int64_t>(num_cap->to_string_view());
4,637✔
2011
                        if (scan_res) {
4,637✔
2012
                            dvalue_opt = scan_res->value();
4,637✔
2013
                        }
2014
                        break;
4,637✔
2015
                    }
2016
                    case value_kind_t::VALUE_FLOAT: {
166✔
2017
                        auto scan_res = scn::scan_value<double>(
2018
                            num_cap->to_string_view());
166✔
2019
                        if (scan_res) {
166✔
2020
                            dvalue_opt = scan_res->value();
166✔
2021
                        }
2022
                        break;
166✔
2023
                    }
2024
                    default:
×
2025
                        break;
×
2026
                }
2027
                if (dvalue_opt) {
4,803✔
2028
                    auto dvalue = dvalue_opt.value();
4,803✔
2029
                    if (scaling != nullptr) {
4,803✔
2030
                        scaling->scale(dvalue);
80✔
2031
                    }
2032
                    sbc.sbc_value_stats[vd.vd_meta.lvm_values_index.value()]
4,803✔
2033
                        .add_value(dvalue);
4,803✔
2034
                }
2035
            }
2036
        }
2037

2038
        dst.emplace_back(li.li_file_range.fr_offset, log_us, level, opid);
5,831✔
2039

2040
        auto src_file_cap = md[fpat->p_src_file_field_index];
5,831✔
2041
        auto src_line_cap = md[fpat->p_src_line_field_index];
5,831✔
2042
        if (src_file_cap && src_line_cap) {
5,831✔
2043
            auto h = hasher();
94✔
2044
            h.update(this->get_name().c_str());
94✔
2045
            h.update(src_file_cap.value());
94✔
2046
            h.update(src_line_cap.value());
94✔
2047
            dst.back().set_schema(h.to_array());
94✔
2048
        }
2049
        auto thread_id_cap = md[fpat->p_thread_id_field_index];
5,831✔
2050
        if (thread_id_cap) {
5,831✔
2051
            auto tid_iter = sbc.sbc_tids.insert_tid(
1,994✔
2052
                sbc.sbc_allocator, thread_id_cap.value(), log_us);
997✔
2053
            tid_iter->second.titr_level_stats.update_msg_count(level);
997✔
2054
        } else {
2055
            auto tid_iter = sbc.sbc_tids.insert_tid(
4,834✔
2056
                sbc.sbc_allocator, string_fragment{}, log_us);
×
2057
            tid_iter->second.titr_level_stats.update_msg_count(level);
4,834✔
2058
        }
2059

2060
        if (orig_lock != curr_fmt) {
5,831✔
2061
            uint32_t lock_line;
2062

2063
            log_debug("%s:%zu: changing pattern lock %d -> (%d)%s",
601✔
2064
                      lf.get_unique_path().c_str(),
2065
                      dst.size() - 1,
2066
                      orig_lock,
2067
                      curr_fmt,
2068
                      this->elf_pattern_order[curr_fmt]->p_name.c_str());
2069
            if (sbc.sbc_pattern_locks.empty()) {
601✔
2070
                lock_line = 0;
575✔
2071
            } else {
2072
                lock_line = dst.size() - 1;
26✔
2073
            }
2074
            sbc.sbc_pattern_locks.pl_lines.emplace_back(lock_line, curr_fmt);
601✔
2075
        }
2076
        return scan_match{1000};
5,831✔
2077
    }
2078

2079
    if (this->lf_specialized && !this->lf_multiline) {
645,467✔
2080
        const auto& last_line = dst.back();
1✔
2081

2082
        log_debug("%s: invalid line %zu file_offset=%" PRIu64,
1✔
2083
                  lf.get_filename().c_str(),
2084
                  dst.size(),
2085
                  li.li_file_range.fr_offset);
2086
        dst.emplace_back(li.li_file_range.fr_offset,
1✔
2087
                         last_line.get_timeval(),
×
2088
                         log_level_t::LEVEL_INVALID);
1✔
2089

2090
        return scan_match{0};
1✔
2091
    }
2092

2093
    return scan_no_match{"no patterns matched"};
645,466✔
2094
}
2095

2096
void
2097
external_log_format::annotate(logfile* lf,
7,361✔
2098
                              uint64_t line_number,
2099
                              string_attrs_t& sa,
2100
                              logline_value_vector& values) const
2101
{
2102
    thread_local auto md = lnav::pcre2pp::match_data::unitialized();
7,361✔
2103

2104
    auto& line = values.lvv_sbr;
7,361✔
2105
    line_range lr;
7,361✔
2106

2107
    line.erase_ansi();
7,361✔
2108
    if (this->elf_type != elf_type_t::ELF_TYPE_TEXT) {
7,361✔
2109
        if (this->jlf_cached_opts.full_message) {
738✔
2110
            values = this->jlf_line_values;
319✔
2111
            sa = this->jlf_line_attrs;
319✔
2112
        } else {
2113
            values.lvv_sbr = this->jlf_line_values.lvv_sbr.clone();
419✔
2114
            for (const auto& llv : this->jlf_line_values.lvv_values) {
4,208✔
2115
                if (this->jlf_cached_sub_range.contains(llv.lv_origin)) {
3,789✔
2116
                    values.lvv_values.emplace_back(llv);
912✔
2117
                    values.lvv_values.back().lv_origin.shift(
912✔
2118
                        this->jlf_cached_sub_range.lr_start,
912✔
2119
                        -this->jlf_cached_sub_range.lr_start);
912✔
2120
                }
2121
            }
2122
            for (const auto& attr : this->jlf_line_attrs) {
2,279✔
2123
                if (this->jlf_cached_sub_range.contains(attr.sa_range)) {
1,860✔
2124
                    sa.emplace_back(attr);
669✔
2125
                    sa.back().sa_range.shift(
669✔
2126
                        this->jlf_cached_sub_range.lr_start,
669✔
2127
                        -this->jlf_cached_sub_range.lr_start);
669✔
2128
                }
2129
            }
2130
            values.lvv_opid_value = this->jlf_line_values.lvv_opid_value;
419✔
2131
            values.lvv_opid_provenance
2132
                = this->jlf_line_values.lvv_opid_provenance;
419✔
2133
            values.lvv_thread_id_value
2134
                = this->jlf_line_values.lvv_thread_id_value;
419✔
2135
        }
2136
        log_format::annotate(lf, line_number, sa, values);
738✔
2137
        return;
2,361✔
2138
    }
2139

2140
    if (line.empty()) {
6,623✔
2141
        return;
5✔
2142
    }
2143

2144
    values.lvv_values.reserve(this->elf_value_defs.size());
6,618✔
2145

2146
    auto lffs = lf->get_format_file_state();
6,618✔
2147
    int pat_index = lffs.lffs_pattern_locks.pattern_index_for_line(line_number);
6,618✔
2148
    const auto& pat = *this->elf_pattern_order[pat_index];
6,618✔
2149
    char tmp_opid_buf[hasher::STRING_SIZE];
2150

2151
    sa.reserve(pat.p_pcre.pp_value->get_capture_count());
6,618✔
2152
    auto match_res
2153
        = pat.p_pcre.pp_value->capture_from(line.to_string_fragment())
6,618✔
2154
              .into(md)
6,618✔
2155
              .matches(PCRE2_NO_UTF_CHECK)
13,236✔
2156
              .ignore_error();
6,618✔
2157
    if (!match_res) {
6,618✔
2158
        // A continued line still needs a body.
2159
        lr.lr_start = 0;
1,618✔
2160
        lr.lr_end = line.length();
1,618✔
2161
        sa.emplace_back(lr, SA_BODY.value());
1,618✔
2162
        if (!this->lf_multiline) {
1,618✔
2163
            auto len
2164
                = pat.p_pcre.pp_value->match_partial(line.to_string_fragment());
×
2165
            sa.emplace_back(
×
2166
                line_range{(int) len, -1},
×
2167
                SA_INVALID.value("Log line does not match any pattern"));
×
2168
        }
2169
        return;
1,618✔
2170
    }
2171

2172
    auto duration_cap = md[pat.p_duration_field_index];
5,000✔
2173

2174
    auto ts_cap = md[pat.p_timestamp_field_index];
5,000✔
2175
    if (ts_cap) {
5,000✔
2176
        sa.emplace_back(to_line_range(ts_cap.value()), L_TIMESTAMP.value());
5,000✔
2177
    }
2178

2179
    auto opid_cap = md[pat.p_opid_field_index];
5,000✔
2180

2181
    if (!pat.p_opid_description_field_indexes.empty()) {
5,000✔
2182
        hasher h;
4,203✔
2183
        for (auto& fidx : pat.p_opid_description_field_indexes) {
12,169✔
2184
            auto desc_cap = md[fidx];
7,966✔
2185
            if (desc_cap) {
7,966✔
2186
                h.update(desc_cap.value());
7,938✔
2187
            }
2188
        }
2189
        h.to_string(tmp_opid_buf);
4,203✔
2190
        opid_cap = string_fragment::from_bytes(tmp_opid_buf,
8,406✔
2191
                                               sizeof(tmp_opid_buf) - 1);
4,203✔
2192
    } else if (duration_cap && !opid_cap) {
797✔
2193
        hasher h;
3✔
2194
        h.update(line.to_string_fragment());
3✔
2195
        h.to_string(tmp_opid_buf);
3✔
2196
        opid_cap = string_fragment::from_bytes(tmp_opid_buf,
6✔
2197
                                               sizeof(tmp_opid_buf) - 1);
3✔
2198
    }
2199
    if (opid_cap && !opid_cap->empty()) {
5,000✔
2200
        sa.emplace_back(to_line_range(opid_cap.value()), L_OPID.value());
4,647✔
2201
        values.lvv_opid_value = opid_cap->to_string();
4,647✔
2202
        values.lvv_opid_provenance
2203
            = logline_value_vector::opid_provenance::file;
4,647✔
2204
    }
2205

2206
    auto body_cap = md[pat.p_body_field_index];
5,000✔
2207
    auto level_cap = md[pat.p_level_field_index];
5,000✔
2208
    auto src_file_cap = md[pat.p_src_file_field_index];
5,000✔
2209
    auto src_line_cap = md[pat.p_src_line_field_index];
5,000✔
2210
    auto thread_id_cap = md[pat.p_thread_id_field_index];
5,000✔
2211

2212
    if (level_cap
5,000✔
2213
        && (!body_cap
9,954✔
2214
            || (body_cap && level_cap->sf_begin != body_cap->sf_begin)))
9,954✔
2215
    {
2216
        sa.emplace_back(to_line_range(level_cap.value()), L_LEVEL.value());
4,565✔
2217
    }
2218

2219
    if (src_file_cap) {
5,000✔
2220
        sa.emplace_back(to_line_range(src_file_cap.value()),
105✔
2221
                        SA_SRC_FILE.value());
210✔
2222
    }
2223
    if (src_line_cap) {
5,000✔
2224
        sa.emplace_back(to_line_range(src_line_cap.value()),
105✔
2225
                        SA_SRC_LINE.value());
210✔
2226
    }
2227
    if (thread_id_cap) {
5,000✔
2228
        sa.emplace_back(to_line_range(thread_id_cap.value()),
590✔
2229
                        SA_THREAD_ID.value());
1,180✔
2230
        values.lvv_thread_id_value = thread_id_cap->to_string();
590✔
2231
    }
2232
    if (duration_cap) {
5,000✔
2233
        sa.emplace_back(to_line_range(duration_cap.value()),
3✔
2234
                        SA_DURATION.value());
6✔
2235
    }
2236

2237
    for (size_t lpc = 0; lpc < pat.p_value_by_index.size(); lpc++) {
57,308✔
2238
        const auto& ivd = pat.p_value_by_index[lpc];
52,308✔
2239
        const scaling_factor* scaling = nullptr;
52,308✔
2240
        auto cap = md[ivd.ivd_index];
52,308✔
2241
        const auto& vd = *ivd.ivd_value_def;
52,308✔
2242

2243
        if (ivd.ivd_unit_field_index >= 0) {
52,308✔
2244
            auto unit_cap = md[ivd.ivd_unit_field_index];
10✔
2245

2246
            if (unit_cap) {
10✔
2247
                intern_string_t unit_val
2248
                    = intern_string::lookup(unit_cap.value());
10✔
2249
                auto unit_iter = vd.vd_unit_scaling.find(unit_val);
10✔
2250
                if (unit_iter != vd.vd_unit_scaling.end()) {
10✔
2251
                    const auto& sf = unit_iter->second;
10✔
2252

2253
                    scaling = &sf;
10✔
2254
                }
2255
            }
2256
        }
2257

2258
        if (cap) {
52,308✔
2259
            values.lvv_values.emplace_back(
85,514✔
2260
                vd.vd_meta, line, to_line_range(cap.value()));
42,757✔
2261
            values.lvv_values.back().apply_scaling(scaling);
42,757✔
2262
        } else {
2263
            values.lvv_values.emplace_back(vd.vd_meta);
9,551✔
2264
        }
2265
    }
2266

2267
    if (body_cap && body_cap->is_valid()) {
5,000✔
2268
        lr = to_line_range(body_cap.value());
4,988✔
2269
    } else {
2270
        lr.lr_start = line.length();
12✔
2271
        lr.lr_end = line.length();
12✔
2272
    }
2273
    sa.emplace_back(lr, SA_BODY.value());
5,000✔
2274

2275
    log_format::annotate(lf, line_number, sa, values);
5,000✔
2276
}
2277

2278
void
2279
external_log_format::rewrite(exec_context& ec,
43✔
2280
                             shared_buffer_ref& line,
2281
                             string_attrs_t& sa,
2282
                             std::string& value_out)
2283
{
2284
    auto& values = *ec.ec_line_values;
43✔
2285

2286
    value_out.assign(line.get_data(), line.length());
43✔
2287

2288
    for (auto iter = values.lvv_values.begin(); iter != values.lvv_values.end();
259✔
2289
         ++iter)
216✔
2290
    {
2291
        if (!iter->lv_origin.is_valid()) {
216✔
2292
            log_debug("%d: not rewriting value with invalid origin -- %s",
24✔
2293
                      (int) ec.ec_top_line,
2294
                      iter->lv_meta.lvm_name.get());
2295
            continue;
184✔
2296
        }
2297

2298
        auto vd_iter = this->elf_value_defs.find(iter->lv_meta.lvm_name);
192✔
2299
        if (vd_iter == this->elf_value_defs.end()) {
192✔
2300
            log_debug("%d: not rewriting undefined value -- %s",
×
2301
                      (int) ec.ec_top_line,
2302
                      iter->lv_meta.lvm_name.get());
2303
            continue;
×
2304
        }
2305

2306
        const auto& vd = *vd_iter->second;
192✔
2307

2308
        if (vd.vd_rewriter.empty()) {
192✔
2309
            continue;
160✔
2310
        }
2311

2312
        auto _sg = ec.enter_source(
2313
            vd_iter->second->vd_rewrite_src_name, 1, vd.vd_rewriter);
32✔
2314
        std::string field_value;
32✔
2315

2316
        auto_mem<FILE> tmpout(fclose);
32✔
2317

2318
        tmpout = std::tmpfile();
32✔
2319
        if (!tmpout) {
32✔
2320
            log_error("unable to create temporary file");
×
2321
            return;
×
2322
        }
2323
        fcntl(fileno(tmpout), F_SETFD, FD_CLOEXEC);
32✔
2324
        auto fd_copy = auto_fd::dup_of(fileno(tmpout));
32✔
2325
        fd_copy.close_on_exec();
32✔
2326
        auto ec_out = std::make_pair(tmpout.release(), fclose);
32✔
2327
        {
2328
            exec_context::output_guard og(ec, "tmp", ec_out);
64✔
2329

2330
            auto exec_res = execute_any(ec, vd.vd_rewriter);
32✔
2331
            if (exec_res.isOk()) {
32✔
2332
                field_value = exec_res.unwrap();
32✔
2333
            } else {
2334
                field_value = exec_res.unwrapErr().to_attr_line().get_string();
×
2335
            }
2336
        }
32✔
2337
        struct stat st;
2338
        fstat(fd_copy.get(), &st);
32✔
2339
        if (st.st_size > 0) {
32✔
2340
            auto buf = auto_buffer::alloc(st.st_size);
2✔
2341

2342
            buf.resize(st.st_size);
2✔
2343
            pread(fd_copy.get(), buf.in(), st.st_size, 0);
2✔
2344
            field_value = buf.to_string();
2✔
2345
        }
2✔
2346
        value_out.erase(iter->lv_origin.lr_start, iter->lv_origin.length());
32✔
2347

2348
        int32_t shift_amount
2349
            = ((int32_t) field_value.length()) - iter->lv_origin.length();
32✔
2350
        auto orig_lr = iter->lv_origin;
32✔
2351
        value_out.insert(iter->lv_origin.lr_start, field_value);
32✔
2352
        for (auto shift_iter = values.lvv_values.begin();
32✔
2353
             shift_iter != values.lvv_values.end();
180✔
2354
             ++shift_iter)
148✔
2355
        {
2356
            shift_iter->lv_origin.shift_range(orig_lr, shift_amount);
148✔
2357
        }
2358
        shift_string_attrs(sa, orig_lr, shift_amount);
32✔
2359
    }
32✔
2360
}
2361

2362
static int
2363
read_json_field(yajlpp_parse_context* ypc,
150,745✔
2364
                const unsigned char* str,
2365
                size_t len,
2366
                yajl_string_props_t* props)
2367
{
2368
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
150,745✔
2369
    timeval tv_out;
2370
    const auto frag = string_fragment::from_bytes(str, len);
150,745✔
2371
    intern_string_t field_name;
150,745✔
2372
    const auto* vd = jlu->get_field_def(ypc);
150,745✔
2373

2374
    if (vd != nullptr) {
150,745✔
2375
        field_name = vd->vd_meta.lvm_name;
13,581✔
2376
    }
2377

2378
    if (field_name.empty()) {
150,745✔
2379
        if (!jlu->jlu_format->elf_opid_field.empty()) {
137,164✔
2380
            auto path_sf = ypc->get_path_as_string_fragment();
46,006✔
2381
            if (path_sf.startswith(jlu->jlu_format->elf_opid_field.c_str())) {
46,006✔
2382
                jlu->jlu_opid_hasher.update(path_sf);
8,901✔
2383
                jlu->jlu_opid_hasher.update(frag);
8,901✔
2384
            }
2385
        }
2386
    } else if (jlu->jlu_format->lf_timestamp_field == field_name) {
13,581✔
2387
        const auto* last = jlu->jlu_format->lf_date_time.scan(
3,012✔
2388
            (const char*) str,
2389
            len,
2390
            jlu->jlu_format->get_timestamp_formats(),
3,012✔
2391
            &jlu->jlu_exttm,
2392
            tv_out);
2393
        if (last == nullptr) {
3,012✔
2394
            auto ls = jlu->jlu_format->lf_date_time.unlock();
22✔
2395
            if ((last = jlu->jlu_format->lf_date_time.scan(
22✔
2396
                     (const char*) str,
2397
                     len,
2398
                     jlu->jlu_format->get_timestamp_formats(),
22✔
2399
                     &jlu->jlu_exttm,
2400
                     tv_out))
2401
                == nullptr)
22✔
2402
            {
2403
                jlu->jlu_format->lf_date_time.relock(ls);
×
2404
            }
2405
            if (last != nullptr) {
22✔
2406
                auto old_flags
22✔
2407
                    = jlu->jlu_format->lf_timestamp_flags & DATE_TIME_SET_FLAGS;
22✔
2408
                auto new_flags = jlu->jlu_exttm.et_flags & DATE_TIME_SET_FLAGS;
22✔
2409

2410
                // It is unlikely a valid timestamp would lose much
2411
                // precision.
2412
                if (new_flags != old_flags) {
22✔
2413
                    last = nullptr;
×
2414
                }
2415
            }
2416
        }
2417
        if (last != nullptr) {
3,012✔
2418
            jlu->jlu_format->lf_timestamp_flags = jlu->jlu_exttm.et_flags;
3,012✔
2419
            jlu->jlu_base_line->set_time(tv_out);
3,012✔
2420
        }
2421
    } else if (jlu->jlu_format->elf_level_pointer.pp_value != nullptr) {
10,569✔
2422
        if (jlu->jlu_format->elf_level_pointer.pp_value
230✔
2423
                ->find_in(field_name.to_string_fragment(), PCRE2_NO_UTF_CHECK)
230✔
2424
                .ignore_error()
230✔
2425
                .has_value())
115✔
2426
        {
2427
            jlu->jlu_base_line->set_level(
×
2428
                jlu->jlu_format->convert_level(frag, jlu->jlu_batch_context));
×
2429
        }
2430
    }
2431
    if (!field_name.empty() && jlu->jlu_format->elf_level_field == field_name) {
150,745✔
2432
        jlu->jlu_base_line->set_level(
3,354✔
2433
            jlu->jlu_format->convert_level(frag, jlu->jlu_batch_context));
3,354✔
2434
    }
2435
    if (!field_name.empty() && jlu->jlu_format->elf_opid_field == field_name) {
150,745✔
2436
        jlu->jlu_base_line->set_opid(frag.hash());
164✔
2437

2438
        auto& sbc = *jlu->jlu_batch_context;
164✔
2439
        auto opid_iter = sbc.sbc_opids.los_opid_ranges.find(frag);
164✔
2440
        if (opid_iter == sbc.sbc_opids.los_opid_ranges.end()) {
164✔
2441
            jlu->jlu_opid_frag = frag.to_owned(sbc.sbc_allocator);
141✔
2442
        } else {
2443
            jlu->jlu_opid_frag = opid_iter->first;
23✔
2444
        }
2445
    }
2446
    if (!field_name.empty()
150,745✔
2447
        && jlu->jlu_format->elf_thread_id_field == field_name)
150,745✔
2448
    {
2449
        auto& sbc = *jlu->jlu_batch_context;
×
2450
        auto tid_iter = sbc.sbc_tids.ltis_tid_ranges.find(frag);
×
2451
        if (tid_iter == sbc.sbc_tids.ltis_tid_ranges.end()) {
×
2452
            jlu->jlu_tid_frag = frag.to_owned(sbc.sbc_allocator);
×
2453
        } else {
2454
            jlu->jlu_tid_frag = tid_iter->first;
×
2455
        }
2456
    }
2457
    if (!jlu->jlu_format->elf_subid_field.empty()
150,745✔
2458
        && jlu->jlu_format->elf_subid_field == field_name)
150,745✔
2459
    {
2460
        jlu->jlu_subid = frag.to_string();
×
2461
    }
2462
    if (!field_name.empty()
150,745✔
2463
        && jlu->jlu_format->elf_duration_field == field_name)
150,745✔
2464
    {
2465
        auto from_res = humanize::try_from<double>(frag);
×
2466
        if (from_res) {
×
2467
            jlu->jlu_duration = std::chrono::microseconds(
×
2468
                static_cast<int64_t>(from_res.value() * 1000000));
2469
        }
2470
    }
2471

2472
    if (vd != nullptr && vd->vd_is_desc_field) {
150,745✔
2473
        auto frag_copy = frag.to_owned(jlu->jlu_format->lf_desc_allocator);
42✔
2474

2475
        jlu->jlu_format->lf_desc_captures.emplace(field_name, frag_copy);
42✔
2476
    }
2477

2478
    jlu->add_sub_lines_for(vd, ypc->is_level(1), std::nullopt, str, len, props);
150,745✔
2479

2480
    return 1;
150,745✔
2481
}
2482

2483
static int
2484
rewrite_json_field(yajlpp_parse_context* ypc,
69,343✔
2485
                   const unsigned char* str,
2486
                   size_t len,
2487
                   yajl_string_props_t* props)
2488
{
2489
    static const intern_string_t body_name = intern_string::lookup("body", -1);
69,343✔
2490
    auto* jlu = (json_log_userdata*) ypc->ypc_userdata;
69,343✔
2491
    intern_string_t field_name;
69,343✔
2492
    const auto* vd = jlu->get_field_def(ypc);
69,343✔
2493
    auto frag = string_fragment::from_bytes(str, len);
69,343✔
2494

2495
    if (!ypc->is_level(1) && vd == nullptr) {
69,343✔
2496
        if (!jlu->jlu_format->elf_opid_field.empty()) {
57,531✔
2497
            auto path_sf = ypc->get_path_as_string_fragment();
56,725✔
2498
            if (path_sf.startswith(jlu->jlu_format->elf_opid_field.c_str())) {
56,725✔
2499
                jlu->jlu_opid_hasher.update(path_sf);
43✔
2500
                jlu->jlu_opid_hasher.update(frag);
43✔
2501
            }
2502
        }
2503
        return 1;
57,531✔
2504
    }
2505
    if (vd != nullptr) {
11,812✔
2506
        field_name = vd->vd_meta.lvm_name;
11,338✔
2507
    } else {
2508
        field_name = ypc->get_path();
474✔
2509
    }
2510

2511
    if (jlu->jlu_format->elf_opid_field == field_name) {
11,812✔
2512
        jlu->jlu_format->jlf_line_values.lvv_opid_value = frag.to_string();
967✔
2513
        jlu->jlu_format->jlf_line_values.lvv_opid_provenance
967✔
2514
            = logline_value_vector::opid_provenance::file;
967✔
2515
    }
2516
    if (jlu->jlu_format->lf_timestamp_field == field_name) {
11,812✔
2517
        char time_buf[64];
2518

2519
        // TODO add a timeval kind to logline_value
2520
        if (jlu->jlu_line->is_time_skewed()
2,196✔
2521
            || (jlu->jlu_format->lf_timestamp_flags
4,392✔
2522
                & (ETF_MICROS_SET | ETF_NANOS_SET | ETF_ZONE_SET)))
2,196✔
2523
        {
2524
            timeval tv;
2525

2526
            const auto* last = jlu->jlu_format->lf_date_time.scan(
2,196✔
2527
                (const char*) str,
2528
                len,
2529
                jlu->jlu_format->get_timestamp_formats(),
2,196✔
2530
                &jlu->jlu_exttm,
2531
                tv);
2532
            if (last == nullptr) {
2,196✔
2533
                auto ls = jlu->jlu_format->lf_date_time.unlock();
58✔
2534
                if ((last = jlu->jlu_format->lf_date_time.scan(
58✔
2535
                         (const char*) str,
2536
                         len,
2537
                         jlu->jlu_format->get_timestamp_formats(),
58✔
2538
                         &jlu->jlu_exttm,
2539
                         tv))
2540
                    == nullptr)
58✔
2541
                {
2542
                    jlu->jlu_format->lf_date_time.relock(ls);
×
2543
                }
2544
            }
2545
            if (!jlu->jlu_subline_opts.hash_hack) {
2,196✔
2546
                if (jlu->jlu_exttm.et_flags & ETF_ZONE_SET
2,196✔
2547
                    && jlu->jlu_format->lf_date_time.dts_zoned_to_local)
2,196✔
2548
                {
2549
                    jlu->jlu_exttm.et_flags &= ~ETF_Z_IS_UTC;
2,196✔
2550
                }
2551
                jlu->jlu_exttm.et_gmtoff
2552
                    = jlu->jlu_format->lf_date_time.dts_local_offset_cache;
2,196✔
2553
            }
2554
            jlu->jlu_format->lf_date_time.ftime(
2,196✔
2555
                time_buf,
2556
                sizeof(time_buf),
2557
                jlu->jlu_format->get_timestamp_formats(),
2,196✔
2558
                jlu->jlu_exttm);
2,196✔
2559
        } else {
2560
            sql_strftime(
×
2561
                time_buf, sizeof(time_buf), jlu->jlu_line->get_timeval(), 'T');
×
2562
        }
2563
        jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
4,392✔
2564
            jlu->jlu_format->get_value_meta(field_name,
4,392✔
2565
                                            value_kind_t::VALUE_TEXT),
2566
            std::string{time_buf});
6,588✔
2567
    } else if (jlu->jlu_shared_buffer.contains((const char*) str)) {
9,616✔
2568
        auto str_offset = (int) ((const char*) str - jlu->jlu_line_value);
9,210✔
2569
        if (field_name == jlu->jlu_format->elf_body_field) {
9,210✔
2570
            jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
820✔
2571
                logline_value_meta(body_name,
1,640✔
2572
                                   value_kind_t::VALUE_TEXT,
2573
                                   logline_value_meta::internal_column{},
×
2574
                                   jlu->jlu_format),
820✔
2575
                string_fragment::from_byte_range(
1,640✔
2576
                    jlu->jlu_shared_buffer.get_data(),
820✔
2577
                    str_offset,
2578
                    str_offset + len));
820✔
2579
        }
2580

2581
        jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
9,210✔
2582
            jlu->jlu_format->get_value_meta(field_name,
18,420✔
2583
                                            value_kind_t::VALUE_TEXT),
2584
            string_fragment::from_byte_range(jlu->jlu_shared_buffer.get_data(),
18,420✔
2585
                                             str_offset,
2586
                                             str_offset + len));
9,210✔
2587
    } else {
2588
        if (field_name == jlu->jlu_format->elf_body_field) {
406✔
2589
            jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
315✔
2590
                logline_value_meta(body_name,
630✔
2591
                                   value_kind_t::VALUE_TEXT,
2592
                                   logline_value_meta::internal_column{},
×
2593
                                   jlu->jlu_format),
315✔
2594
                std::string{(const char*) str, len});
1,260✔
2595
        }
2596

2597
        jlu->jlu_format->jlf_line_values.lvv_values.emplace_back(
406✔
2598
            jlu->jlu_format->get_value_meta(ypc, vd, value_kind_t::VALUE_TEXT),
812✔
2599
            std::string{(const char*) str, len});
1,624✔
2600
    }
2601
    if (vd != nullptr && vd->vd_is_desc_field
11,338✔
2602
        && jlu->jlu_format->elf_opid_field.empty())
23,150✔
2603
    {
2604
        auto frag_copy = frag.to_owned(jlu->jlu_format->lf_desc_allocator);
62✔
2605

2606
        jlu->jlu_format->lf_desc_captures.emplace(field_name, frag_copy);
62✔
2607
    }
2608

2609
    return 1;
11,812✔
2610
}
2611

2612
void
2613
external_log_format::get_subline(const log_format_file_state& lffs,
23,507✔
2614
                                 const logline& ll,
2615
                                 shared_buffer_ref& sbr,
2616
                                 subline_options opts)
2617
{
2618
    if (this->elf_type == elf_type_t::ELF_TYPE_TEXT) {
23,507✔
2619
        return;
18,978✔
2620
    }
2621

2622
    if (this->jlf_cached_offset != ll.get_offset()
4,529✔
2623
        || this->jlf_cached_opts != opts)
4,529✔
2624
    {
2625
        auto& ypc = *(this->jlf_parse_context);
2,423✔
2626
        yajl_handle handle = this->jlf_yajl_handle.get();
2,423✔
2627
        json_log_userdata jlu(sbr, nullptr);
2,423✔
2628

2629
        jlu.jlu_subline_opts = opts;
2,423✔
2630

2631
        this->lf_desc_captures.clear();
2,423✔
2632
        this->lf_desc_allocator.reset();
2,423✔
2633
        this->jlf_share_manager.invalidate_refs();
2,423✔
2634
        this->jlf_cached_line.clear();
2,423✔
2635
        this->jlf_line_values.clear();
2,423✔
2636
        this->jlf_line_offsets.clear();
2,423✔
2637
        this->jlf_line_attrs.clear();
2,423✔
2638

2639
        auto line_frag = sbr.to_string_fragment();
2,423✔
2640

2641
        if (!line_frag.startswith("{")) {
2,423✔
2642
            this->jlf_cached_line.resize(line_frag.length());
72✔
2643
            memcpy(this->jlf_cached_line.data(),
72✔
2644
                   line_frag.data(),
72✔
2645
                   line_frag.length());
72✔
2646
            this->jlf_line_values.clear();
72✔
2647
            sbr.share(this->jlf_share_manager,
144✔
2648
                      &this->jlf_cached_line[0],
72✔
2649
                      this->jlf_cached_line.size());
2650
            this->jlf_line_values.lvv_sbr = sbr.clone();
72✔
2651
            this->jlf_line_attrs.emplace_back(
72✔
2652
                line_range{0, -1},
×
2653
                SA_INVALID.value(fmt::format(
144✔
2654
                    FMT_STRING("line at offset {} is not a JSON-line"),
144✔
2655
                    ll.get_offset())));
72✔
2656
            return;
72✔
2657
        }
2658

2659
        yajl_reset(handle);
2,351✔
2660
        ypc.set_static_handler(json_log_rewrite_handlers.jpc_children[0]);
2,351✔
2661
        ypc.ypc_userdata = &jlu;
2,351✔
2662
        ypc.ypc_ignore_unused = true;
2,351✔
2663
        ypc.ypc_alt_callbacks.yajl_start_array = json_array_start_const;
2,351✔
2664
        ypc.ypc_alt_callbacks.yajl_end_array = json_array_end;
2,351✔
2665
        ypc.ypc_alt_callbacks.yajl_start_map = json_array_start_const;
2,351✔
2666
        ypc.ypc_alt_callbacks.yajl_end_map = json_array_end;
2,351✔
2667
        jlu.jlu_format = this;
2,351✔
2668
        jlu.jlu_line = &ll;
2,351✔
2669
        jlu.jlu_handle = handle;
2,351✔
2670
        jlu.jlu_line_value = sbr.get_data();
2,351✔
2671
        jlu.jlu_format_hits.resize(this->jlf_line_format.size());
2,351✔
2672

2673
        yajl_status parse_status = yajl_parse(
4,702✔
2674
            handle, (const unsigned char*) sbr.get_data(), sbr.length());
2,351✔
2675
        if (parse_status != yajl_status_ok
2,351✔
2676
            || yajl_complete_parse(handle) != yajl_status_ok)
2,351✔
2677
        {
2678
            unsigned char* msg;
2679
            std::string full_msg;
14✔
2680

2681
            msg = yajl_get_error(
28✔
2682
                handle, 1, (const unsigned char*) sbr.get_data(), sbr.length());
14✔
2683
            if (msg != nullptr) {
14✔
2684
                full_msg = fmt::format(
14✔
2685
                    FMT_STRING("[offset: {}] {}\n{}"),
28✔
2686
                    ll.get_offset(),
14✔
2687
                    fmt::string_view{sbr.get_data(), sbr.length()},
14✔
2688
                    reinterpret_cast<char*>(msg));
28✔
2689
                yajl_free_error(handle, msg);
14✔
2690
            }
2691

2692
            this->jlf_cached_line.resize(full_msg.size());
14✔
2693
            memcpy(
14✔
2694
                this->jlf_cached_line.data(), full_msg.data(), full_msg.size());
14✔
2695
            this->jlf_line_values.clear();
14✔
2696
            this->jlf_line_attrs.emplace_back(
14✔
2697
                line_range{0, -1},
×
2698
                SA_INVALID.value("JSON line failed to parse"));
28✔
2699
        } else {
14✔
2700
            std::vector<logline_value>::iterator lv_iter;
2,337✔
2701
            bool used_values[this->jlf_line_values.lvv_values.size()];
4,674✔
2702
            struct line_range lr;
2,337✔
2703

2704
            memset(used_values, 0, sizeof(used_values));
2,337✔
2705
            for (lv_iter = this->jlf_line_values.lvv_values.begin();
2,337✔
2706
                 lv_iter != this->jlf_line_values.lvv_values.end();
20,790✔
2707
                 ++lv_iter)
18,453✔
2708
            {
2709
                lv_iter->lv_meta.lvm_format = this;
18,453✔
2710
            }
2711

2712
            if (jlu.jlu_tid_number) {
2,337✔
2713
                this->jlf_line_values.lvv_thread_id_value
2714
                    = fmt::to_string(jlu.jlu_tid_number.value());
114✔
2715
            } else if (jlu.jlu_tid_frag) {
2,223✔
2716
                this->jlf_line_values.lvv_thread_id_value
2717
                    = jlu.jlu_tid_frag->to_string();
×
2718
            }
2719

2720
            if (this->elf_opid_field.empty()
2,337✔
2721
                && this->lf_opid_source.value_or(
1,330✔
2722
                       opid_source_t::from_description)
1,330✔
2723
                    == opid_source_t::from_description
2724
                && this->lf_opid_description_def->size() == 1)
3,667✔
2725
            {
2726
                auto found_opid_desc = false;
217✔
2727
                const auto& od = this->lf_opid_description_def->begin()->second;
217✔
2728
                for (const auto& desc : *od.od_descriptors) {
651✔
2729
                    auto desc_iter
2730
                        = this->lf_desc_captures.find(desc.od_field.pp_value);
434✔
2731
                    if (desc_iter == this->lf_desc_captures.end()) {
434✔
2732
                        continue;
372✔
2733
                    }
2734
                    found_opid_desc = true;
62✔
2735
                    jlu.jlu_opid_hasher.update(desc_iter->second);
62✔
2736
                }
2737
                if (found_opid_desc) {
217✔
2738
                    this->jlf_line_values.lvv_opid_value
2739
                        = jlu.jlu_opid_hasher.to_string();
31✔
2740
                    this->jlf_line_values.lvv_opid_provenance
2741
                        = logline_value_vector::opid_provenance::file;
31✔
2742
                }
2743
            } else if (!jlu.jlu_opid_desc_frag && !jlu.jlu_opid_frag
4,212✔
2744
                       && jlu.jlu_duration > 0us)
4,212✔
2745
            {
2746
                jlu.jlu_opid_hasher.update(line_frag);
×
2747
                this->jlf_line_values.lvv_opid_value
2748
                    = jlu.jlu_opid_hasher.to_string();
×
2749
                this->jlf_line_values.lvv_opid_provenance
2750
                    = logline_value_vector::opid_provenance::file;
×
2751
            }
2752
            if (jlu.jlu_opid_desc_frag) {
2,337✔
2753
                this->jlf_line_values.lvv_opid_value
2754
                    = jlu.jlu_opid_hasher.to_string();
28✔
2755
                this->jlf_line_values.lvv_opid_provenance
2756
                    = logline_value_vector::opid_provenance::file;
28✔
2757
            }
2758
            if (jlu.jlu_opid_frag) {
2,337✔
2759
                this->jlf_line_values.lvv_opid_value
2760
                    = jlu.jlu_opid_frag->to_string();
×
2761
                this->jlf_line_values.lvv_opid_provenance
2762
                    = logline_value_vector::opid_provenance::file;
×
2763
            }
2764

2765
            int sub_offset = this->jlf_line_format_init_count;
2,337✔
2766
            for (const auto& jfe : this->jlf_line_format) {
30,258✔
2767
                static const intern_string_t ts_field
2768
                    = intern_string::lookup("__timestamp__", -1);
27,921✔
2769
                static const intern_string_t level_field
2770
                    = intern_string::lookup("__level__");
28,013✔
2771
                size_t begin_size = this->jlf_cached_line.size();
27,921✔
2772

2773
                switch (jfe.jfe_type) {
27,921✔
2774
                    case json_log_field::CONSTANT:
3,711✔
2775
                        this->json_append_to_cache(
3,711✔
2776
                            jfe.jfe_default_value.c_str(),
2777
                            jfe.jfe_default_value.size());
3,711✔
2778
                        break;
3,711✔
2779
                    case json_log_field::VARIABLE:
24,210✔
2780
                        lv_iter = find_if(
24,210✔
2781
                            this->jlf_line_values.lvv_values.begin(),
2782
                            this->jlf_line_values.lvv_values.end(),
2783
                            logline_value_name_cmp(&jfe.jfe_value.pp_value));
2784
                        if (lv_iter != this->jlf_line_values.lvv_values.end()) {
24,210✔
2785
                            auto str = lv_iter->to_string();
8,640✔
2786
                            value_def* vd = nullptr;
8,640✔
2787

2788
                            if (lv_iter->lv_meta.lvm_values_index) {
8,640✔
2789
                                vd = this->elf_value_def_order
2790
                                         [lv_iter->lv_meta.lvm_values_index
8,640✔
2791
                                              .value()]
8,640✔
2792
                                             .get();
8,640✔
2793
                            }
2794
                            while (endswith(str, "\n")) {
8,902✔
2795
                                str.pop_back();
262✔
2796
                            }
2797
                            size_t nl_pos = str.find('\n');
8,640✔
2798

2799
                            if (!jfe.jfe_prefix.empty()) {
8,640✔
2800
                                this->json_append_to_cache(jfe.jfe_prefix);
4,985✔
2801
                            }
2802
                            lr.lr_start = this->jlf_cached_line.size();
8,640✔
2803

2804
                            if ((int) str.size() > jfe.jfe_max_width) {
8,640✔
2805
                                switch (jfe.jfe_overflow) {
233✔
2806
                                    case json_format_element::overflow_t::
165✔
2807
                                        ABBREV: {
2808
                                        size_t new_size
2809
                                            = abbreviate_str(&str[0],
165✔
2810
                                                             str.size(),
2811
                                                             jfe.jfe_max_width);
165✔
2812
                                        str.resize(new_size);
165✔
2813
                                        this->json_append(lffs, jfe, vd, str);
165✔
2814
                                        break;
165✔
2815
                                    }
2816
                                    case json_format_element::overflow_t::
62✔
2817
                                        TRUNCATE: {
2818
                                        this->json_append_to_cache(
62✔
2819
                                            str.c_str(), jfe.jfe_max_width);
62✔
2820
                                        break;
62✔
2821
                                    }
2822
                                    case json_format_element::overflow_t::
6✔
2823
                                        DOTDOT: {
2824
                                        size_t middle
6✔
2825
                                            = (jfe.jfe_max_width / 2) - 1;
6✔
2826
                                        this->json_append_to_cache(str.c_str(),
6✔
2827
                                                                   middle);
2828
                                        this->json_append_to_cache("..", 2);
6✔
2829
                                        size_t rest
6✔
2830
                                            = (jfe.jfe_max_width - middle - 2);
6✔
2831
                                        this->json_append_to_cache(
12✔
2832
                                            str.c_str() + str.size() - rest,
6✔
2833
                                            rest);
2834
                                        break;
6✔
2835
                                    }
2836
                                    case json_format_element::overflow_t::
×
2837
                                        LASTWORD: {
2838
                                        size_t new_size
2839
                                            = last_word_str(&str[0],
×
2840
                                                            str.size(),
2841
                                                            jfe.jfe_max_width);
×
2842
                                        str.resize(new_size);
×
NEW
2843
                                        this->json_append(lffs, jfe, vd, str);
×
2844
                                        break;
×
2845
                                    }
2846
                                }
2847
                            } else {
2848
                                sub_offset
2849
                                    += std::count(str.begin(), str.end(), '\n');
8,407✔
2850
                                this->json_append(lffs, jfe, vd, str);
8,407✔
2851
                            }
2852

2853
                            if (nl_pos == std::string::npos
8,640✔
2854
                                || opts.full_message)
3✔
2855
                            {
2856
                                lr.lr_end = this->jlf_cached_line.size();
8,637✔
2857
                            } else {
2858
                                lr.lr_end = lr.lr_start + nl_pos;
3✔
2859
                            }
2860

2861
                            if (lv_iter->lv_meta.lvm_name
8,640✔
2862
                                == this->lf_timestamp_field)
8,640✔
2863
                            {
2864
                                this->jlf_line_attrs.emplace_back(
1,003✔
2865
                                    lr, L_TIMESTAMP.value());
2,006✔
2866
                            } else if (lv_iter->lv_meta.lvm_name
7,637✔
2867
                                       == this->elf_body_field)
7,637✔
2868
                            {
2869
                                this->jlf_line_attrs.emplace_back(
1,135✔
2870
                                    lr, SA_BODY.value());
2,270✔
2871
                            } else if (lv_iter->lv_meta.lvm_name
6,502✔
2872
                                       == this->elf_src_file_field)
6,502✔
2873
                            {
2874
                                this->jlf_line_attrs.emplace_back(
8✔
2875
                                    lr, SA_SRC_FILE.value());
16✔
2876
                            } else if (lv_iter->lv_meta.lvm_name
6,494✔
2877
                                       == this->elf_src_line_field)
6,494✔
2878
                            {
2879
                                this->jlf_line_attrs.emplace_back(
8✔
2880
                                    lr, SA_SRC_LINE.value());
16✔
2881
                            } else if (lv_iter->lv_meta.lvm_name
6,486✔
2882
                                       == this->elf_thread_id_field)
6,486✔
2883
                            {
2884
                                this->jlf_line_attrs.emplace_back(
60✔
2885
                                    lr, SA_THREAD_ID.value());
120✔
2886
                            } else if (lv_iter->lv_meta.lvm_name
6,426✔
2887
                                       == this->elf_duration_field)
6,426✔
2888
                            {
2889
                                this->jlf_line_attrs.emplace_back(
×
2890
                                    lr, SA_DURATION.value());
×
2891
                            } else if (lv_iter->lv_meta.lvm_name
6,426✔
2892
                                       == this->elf_level_field)
6,426✔
2893
                            {
2894
                                this->jlf_line_attrs.emplace_back(
1,077✔
2895
                                    lr, L_LEVEL.value());
2,154✔
2896
                            } else if (lv_iter->lv_meta.lvm_name
10,698✔
2897
                                           == this->elf_opid_field
5,349✔
2898
                                       && !lr.empty())
5,349✔
2899
                            {
2900
                                this->jlf_line_attrs.emplace_back(
967✔
2901
                                    lr, L_OPID.value());
1,934✔
2902
                            }
2903
                            lv_iter->lv_origin = lr;
8,640✔
2904
                            lv_iter->lv_sub_offset = sub_offset;
8,640✔
2905
                            used_values[std::distance(
8,640✔
2906
                                this->jlf_line_values.lvv_values.begin(),
2907
                                lv_iter)]
2908
                                = true;
8,640✔
2909

2910
                            if (!jfe.jfe_suffix.empty()) {
8,640✔
2911
                                this->json_append_to_cache(jfe.jfe_suffix);
1,322✔
2912
                            }
2913
                        } else if (jfe.jfe_value.pp_value == ts_field) {
24,210✔
2914
                            line_range lr;
1,479✔
2915
                            ssize_t ts_len;
2916
                            char ts[64];
2917
                            exttm et;
1,479✔
2918

2919
                            ll.to_exttm(et);
1,479✔
2920
                            et.et_nsec += jlu.jlu_exttm.et_nsec % 1000;
1,479✔
2921
                            et.et_gmtoff = jlu.jlu_exttm.et_gmtoff;
1,479✔
2922
                            et.et_flags |= jlu.jlu_exttm.et_flags;
1,479✔
2923
                            if (!jfe.jfe_prefix.empty()) {
1,479✔
2924
                                this->json_append_to_cache(jfe.jfe_prefix);
3✔
2925
                            }
2926
                            if (jfe.jfe_ts_format.empty()) {
1,479✔
2927
                                ts_len = this->lf_date_time.ftime(
1,320✔
2928
                                    ts,
2929
                                    sizeof(ts),
2930
                                    this->get_timestamp_formats(),
2931
                                    et);
2932
                            } else {
2933
                                ts_len = ftime_fmt(ts,
159✔
2934
                                                   sizeof(ts),
2935
                                                   jfe.jfe_ts_format.c_str(),
2936
                                                   et);
2937
                            }
2938
                            lr.lr_start = this->jlf_cached_line.size();
1,479✔
2939
                            this->json_append_to_cache(ts, ts_len);
1,479✔
2940
                            lr.lr_end = this->jlf_cached_line.size();
1,479✔
2941
                            this->jlf_line_attrs.emplace_back(
1,479✔
2942
                                lr, L_TIMESTAMP.value());
2,958✔
2943

2944
                            lv_iter = find_if(
1,479✔
2945
                                this->jlf_line_values.lvv_values.begin(),
2946
                                this->jlf_line_values.lvv_values.end(),
2947
                                logline_value_name_cmp(
2948
                                    &this->lf_timestamp_field));
1,479✔
2949
                            if (lv_iter
1,479✔
2950
                                != this->jlf_line_values.lvv_values.end())
1,479✔
2951
                            {
2952
                                used_values[distance(
1,479✔
2953
                                    this->jlf_line_values.lvv_values.begin(),
2954
                                    lv_iter)]
2955
                                    = true;
1,479✔
2956
                            }
2957
                            if (!jfe.jfe_suffix.empty()) {
1,479✔
2958
                                this->json_append_to_cache(jfe.jfe_suffix);
3✔
2959
                            }
2960
                        } else if (jfe.jfe_value.pp_value == level_field
14,091✔
2961
                                   || jfe.jfe_value.pp_value
28,088✔
2962
                                       == this->elf_level_field)
13,997✔
2963
                        {
2964
                            auto level_name = ll.get_level_name();
94✔
2965
                            lr.lr_start = this->jlf_cached_line.size();
94✔
2966
                            this->json_append(lffs, jfe, nullptr, level_name);
94✔
2967
                            if (jfe.jfe_auto_width) {
94✔
2968
                                this->json_append_to_cache(
60✔
2969
                                    MAX_LEVEL_NAME_LEN - level_name.length());
60✔
2970
                            }
2971
                            lr.lr_end = this->jlf_cached_line.size();
94✔
2972
                            this->jlf_line_attrs.emplace_back(lr,
94✔
2973
                                                              L_LEVEL.value());
188✔
2974
                        } else if (!jfe.jfe_default_value.empty()) {
13,997✔
2975
                            if (!jfe.jfe_prefix.empty()) {
124✔
2976
                                this->json_append_to_cache(jfe.jfe_prefix);
×
2977
                            }
2978
                            this->json_append(
124✔
2979
                                lffs, jfe, nullptr, jfe.jfe_default_value);
124✔
2980
                            if (!jfe.jfe_suffix.empty()) {
124✔
2981
                                this->json_append_to_cache(jfe.jfe_suffix);
×
2982
                            }
2983
                        }
2984

2985
                        switch (jfe.jfe_text_transform) {
24,210✔
2986
                            case json_format_element::transform_t::NONE:
24,116✔
2987
                                break;
24,116✔
2988
                            case json_format_element::transform_t::UPPERCASE:
94✔
2989
                                for (size_t cindex = begin_size;
94✔
2990
                                     cindex < this->jlf_cached_line.size();
713✔
2991
                                     cindex++)
2992
                                {
2993
                                    this->jlf_cached_line[cindex] = toupper(
619✔
2994
                                        this->jlf_cached_line[cindex]);
619✔
2995
                                }
2996
                                break;
94✔
NEW
2997
                            case json_format_element::transform_t::LOWERCASE:
×
UNCOV
2998
                                for (size_t cindex = begin_size;
×
2999
                                     cindex < this->jlf_cached_line.size();
×
3000
                                     cindex++)
3001
                                {
3002
                                    this->jlf_cached_line[cindex] = tolower(
×
3003
                                        this->jlf_cached_line[cindex]);
×
3004
                                }
3005
                                break;
×
NEW
3006
                            case json_format_element::transform_t::CAPITALIZE:
×
UNCOV
3007
                                for (size_t cindex = begin_size;
×
3008
                                     cindex < begin_size + 1;
×
3009
                                     cindex++)
3010
                                {
3011
                                    this->jlf_cached_line[cindex] = toupper(
×
3012
                                        this->jlf_cached_line[cindex]);
×
3013
                                }
3014
                                for (size_t cindex = begin_size + 1;
×
3015
                                     cindex < this->jlf_cached_line.size();
×
3016
                                     cindex++)
3017
                                {
3018
                                    this->jlf_cached_line[cindex] = tolower(
×
3019
                                        this->jlf_cached_line[cindex]);
×
3020
                                }
3021
                                break;
×
3022
                        }
3023
                        break;
24,210✔
3024
                }
3025
            }
3026
            this->json_append_to_cache("\n", 1);
2,337✔
3027
            sub_offset += 1;
2,337✔
3028

3029
            for (size_t lpc = 0; lpc < this->jlf_line_values.lvv_values.size();
20,790✔
3030
                 lpc++)
3031
            {
3032
                static const intern_string_t body_name
3033
                    = intern_string::lookup("body", -1);
18,453✔
3034
                auto& lv = this->jlf_line_values.lvv_values[lpc];
18,453✔
3035

3036
                if (lv.lv_meta.is_hidden() || used_values[lpc]
28,289✔
3037
                    || body_name == lv.lv_meta.lvm_name)
28,289✔
3038
                {
3039
                    continue;
17,373✔
3040
                }
3041

3042
                auto str = lv.to_string();
1,080✔
3043
                while (endswith(str, "\n")) {
1,080✔
3044
                    str.pop_back();
×
3045
                }
3046

3047
                lv.lv_sub_offset = sub_offset;
1,080✔
3048
                lv.lv_origin.lr_start = this->jlf_cached_line.size() + 2
1,080✔
3049
                    + lv.lv_meta.lvm_name.size() + 2;
1,080✔
3050
                auto frag = string_fragment::from_str(str);
1,080✔
3051
                while (true) {
3052
                    auto utf_scan_res = is_utf8(frag, '\n');
1,086✔
3053

3054
                    this->json_append_to_cache("  ", 2);
1,086✔
3055
                    this->json_append_to_cache(
1,086✔
3056
                        lv.lv_meta.lvm_name.to_string_fragment());
1,086✔
3057
                    this->json_append_to_cache(": ", 2);
1,086✔
3058
                    lr.lr_start = this->jlf_cached_line.size();
1,086✔
3059
                    this->json_append_to_cache(utf_scan_res.usr_valid_frag);
1,086✔
3060
                    lr.lr_end = this->jlf_cached_line.size();
1,086✔
3061
                    if (lv.lv_meta.lvm_name == this->elf_body_field) {
1,086✔
3062
                        this->jlf_line_attrs.emplace_back(lr, SA_BODY.value());
×
3063
                    } else {
3064
                        this->jlf_line_attrs.emplace_back(
1,086✔
3065
                            lr, SA_EXTRA_CONTENT.value());
2,172✔
3066
                    }
3067
                    this->json_append_to_cache("\n", 1);
1,086✔
3068
                    sub_offset += 1;
1,086✔
3069
                    if (utf_scan_res.usr_remaining) {
1,086✔
3070
                        frag = utf_scan_res.usr_remaining.value();
6✔
3071
                    } else {
3072
                        break;
1,080✔
3073
                    }
3074
                }
6✔
3075
                lv.lv_origin.lr_end = this->jlf_cached_line.size() - 1;
1,080✔
3076
                if (lv.lv_meta.lvm_name == this->elf_opid_field
1,080✔
3077
                    && !lv.lv_origin.empty())
1,080✔
3078
                {
3079
                    this->jlf_line_attrs.emplace_back(lv.lv_origin,
×
3080
                                                      L_OPID.value());
×
3081
                }
3082
            }
1,080✔
3083
        }
2,337✔
3084

3085
        this->jlf_line_offsets.push_back(0);
2,351✔
3086
        for (size_t lpc = 0; lpc < this->jlf_cached_line.size(); lpc++) {
263,684✔
3087
            if (this->jlf_cached_line[lpc] == '\n') {
261,333✔
3088
                this->jlf_line_offsets.push_back(lpc + 1);
4,217✔
3089
            }
3090
        }
3091
        this->jlf_line_offsets.push_back(this->jlf_cached_line.size());
2,351✔
3092
        this->jlf_cached_offset = ll.get_offset();
2,351✔
3093
        this->jlf_cached_opts = opts;
2,351✔
3094
    }
2,423✔
3095

3096
    off_t this_off = 0, next_off = 0;
4,457✔
3097

3098
    if (!this->jlf_line_offsets.empty()
4,457✔
3099
        && ll.get_sub_offset() < this->jlf_line_offsets.size())
4,457✔
3100
    {
3101
        require(ll.get_sub_offset() < this->jlf_line_offsets.size());
4,457✔
3102

3103
        this_off = this->jlf_line_offsets[ll.get_sub_offset()];
4,457✔
3104
        if ((ll.get_sub_offset() + 1) < (int) this->jlf_line_offsets.size()) {
4,457✔
3105
            next_off = this->jlf_line_offsets[ll.get_sub_offset() + 1];
4,457✔
3106
        } else {
3107
            next_off = this->jlf_cached_line.size();
×
3108
        }
3109
        if (next_off > 0 && this->jlf_cached_line[next_off - 1] == '\n'
4,457✔
3110
            && this_off != next_off)
8,914✔
3111
        {
3112
            next_off -= 1;
4,457✔
3113
        }
3114
    }
3115

3116
    if (opts.full_message) {
4,457✔
3117
        sbr.share(this->jlf_share_manager,
644✔
3118
                  &this->jlf_cached_line[0],
322✔
3119
                  this->jlf_cached_line.size());
3120
    } else {
3121
        sbr.share(this->jlf_share_manager,
8,270✔
3122
                  this->jlf_cached_line.data() + this_off,
4,135✔
3123
                  next_off - this_off);
4,135✔
3124
    }
3125
    sbr.get_metadata().m_valid_utf = ll.is_valid_utf();
4,457✔
3126
    sbr.get_metadata().m_has_ansi = ll.has_ansi();
4,457✔
3127
    this->jlf_cached_sub_range.lr_start = this_off;
4,457✔
3128
    this->jlf_cached_sub_range.lr_end = next_off;
4,457✔
3129
    this->jlf_line_values.lvv_sbr = sbr.clone();
4,457✔
3130
}
3131

3132
struct compiled_header_expr {
3133
    auto_mem<sqlite3_stmt> che_stmt{sqlite3_finalize};
3134
    bool che_enabled{true};
3135
};
3136

3137
struct format_header_expressions : public lnav_config_listener {
3138
    format_header_expressions() : lnav_config_listener(__FILE__) {}
1,160✔
3139

3140
    auto_sqlite3 e_db;
3141
    std::map<intern_string_t, std::map<std::string, compiled_header_expr>>
3142
        e_header_exprs;
3143
};
3144

3145
using safe_format_header_expressions = safe::Safe<format_header_expressions>;
3146

3147
static safe_format_header_expressions format_header_exprs;
3148

3149
std::optional<external_file_format>
3150
detect_mime_type(const std::filesystem::path& filename)
609✔
3151
{
3152
    uint8_t buffer[1024];
3153
    size_t buffer_size = 0;
609✔
3154

3155
    {
3156
        auto_fd fd;
609✔
3157

3158
        if ((fd = lnav::filesystem::openp(filename, O_RDONLY)) == -1) {
609✔
3159
            return std::nullopt;
×
3160
        }
3161

3162
        ssize_t rc;
3163

3164
        if ((rc = read(fd, buffer, sizeof(buffer))) == -1) {
609✔
3165
            return std::nullopt;
×
3166
        }
3167
        buffer_size = rc;
609✔
3168
    }
609✔
3169

3170
    auto hexbuf = auto_buffer::alloc(buffer_size * 2);
609✔
3171

3172
    for (size_t lpc = 0; lpc < buffer_size; lpc++) {
324,715✔
3173
        fmt::format_to(
324,106✔
3174
            std::back_inserter(hexbuf), FMT_STRING("{:02x}"), buffer[lpc]);
1,296,424✔
3175
    }
3176

3177
    safe::WriteAccess<safe_format_header_expressions> in(format_header_exprs);
609✔
3178

3179
    for (const auto& format : log_format::get_root_formats()) {
44,354✔
3180
        auto elf = std::dynamic_pointer_cast<external_log_format>(format);
43,745✔
3181
        if (elf == nullptr) {
43,745✔
3182
            continue;
3,045✔
3183
        }
3184

3185
        if (elf->elf_converter.c_header.h_exprs.he_exprs.empty()) {
40,700✔
3186
            continue;
40,091✔
3187
        }
3188

3189
        if (buffer_size < elf->elf_converter.c_header.h_size) {
609✔
3190
            log_debug(
21✔
3191
                "%s: file content too small (%zu) for header detection: %s",
3192
                filename.c_str(),
3193
                buffer_size,
3194
                elf->get_name().get());
3195
            continue;
21✔
3196
        }
3197
        for (const auto& hpair : elf->elf_converter.c_header.h_exprs.he_exprs) {
2,940✔
3198
            auto& he = in->e_header_exprs[elf->get_name()][hpair.first];
2,352✔
3199

3200
            if (!he.che_enabled) {
2,352✔
3201
                continue;
×
3202
            }
3203

3204
            auto* stmt = he.che_stmt.in();
2,352✔
3205

3206
            if (stmt == nullptr) {
2,352✔
3207
                continue;
×
3208
            }
3209
            sqlite3_reset(stmt);
2,352✔
3210
            auto count = sqlite3_bind_parameter_count(stmt);
2,352✔
3211
            for (int lpc = 0; lpc < count; lpc++) {
4,704✔
3212
                const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
2,352✔
3213

3214
                if (name[0] == '$') {
2,352✔
3215
                    const char* env_value;
3216

3217
                    if ((env_value = getenv(&name[1])) != nullptr) {
×
3218
                        sqlite3_bind_text(
×
3219
                            stmt, lpc + 1, env_value, -1, SQLITE_STATIC);
3220
                    }
3221
                    continue;
×
3222
                }
3223
                if (strcmp(name, ":header") == 0) {
2,352✔
3224
                    sqlite3_bind_text(stmt,
2,352✔
3225
                                      lpc + 1,
3226
                                      hexbuf.in(),
2,352✔
3227
                                      hexbuf.size(),
2,352✔
3228
                                      SQLITE_STATIC);
3229
                    continue;
2,352✔
3230
                }
3231
                if (strcmp(name, ":filepath") == 0) {
×
3232
                    sqlite3_bind_text(
×
3233
                        stmt, lpc + 1, filename.c_str(), -1, SQLITE_STATIC);
3234
                    continue;
×
3235
                }
3236
            }
3237

3238
            auto step_res = sqlite3_step(stmt);
2,352✔
3239

3240
            switch (step_res) {
2,352✔
3241
                case SQLITE_OK:
2,352✔
3242
                case SQLITE_DONE:
3243
                    continue;
2,352✔
3244
                case SQLITE_ROW:
×
3245
                    break;
×
3246
                default: {
×
3247
                    log_error(
×
3248
                        "failed to execute file-format header expression: "
3249
                        "%s:%s -- %s",
3250
                        elf->get_name().get(),
3251
                        hpair.first.c_str(),
3252
                        sqlite3_errmsg(in->e_db));
3253
                    he.che_enabled = false;
×
3254
                    continue;
×
3255
                }
3256
            }
3257

3258
            log_info("detected format for: %s -- %s (header-expr: %s)",
×
3259
                     filename.c_str(),
3260
                     elf->get_name().get(),
3261
                     hpair.first.c_str());
3262
            return external_file_format{
×
3263
                elf->get_name().to_string(),
×
3264
                elf->elf_converter.c_command.pp_value,
×
3265
                elf->elf_converter.c_command.pp_location.sl_source.to_string(),
×
3266
            };
3267
        }
3268
    }
43,745✔
3269

3270
    return std::nullopt;
609✔
3271
}
609✔
3272

3273
log_format::scan_result_t
3274
log_format::test_line(sample_t& sample,
×
3275
                      std::vector<lnav::console::user_message>& msgs)
3276
{
3277
    return scan_no_match{};
×
3278
}
3279

3280
log_format::scan_result_t
3281
external_log_format::test_line(sample_t& sample,
180,602✔
3282
                               std::vector<lnav::console::user_message>& msgs)
3283
{
3284
    auto lines
3285
        = string_fragment::from_str(sample.s_line.pp_value).split_lines();
180,602✔
3286

3287
    if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
180,602✔
3288
        auto alloc = ArenaAlloc::Alloc<char>{};
2,222✔
3289
        pattern_locks pats;
2,222✔
3290
        auto sbc = scan_batch_context{
2,222✔
3291
            alloc,
3292
            pats,
3293
        };
2,222✔
3294
        sbc.sbc_value_stats.resize(this->elf_value_defs.size());
2,222✔
3295
        std::vector<logline> dst;
2,222✔
3296
        auto li = line_info{
2,222✔
3297
            {0, lines[0].length()},
2,222✔
3298
        };
2,222✔
3299
        shared_buffer sb;
2,222✔
3300
        shared_buffer_ref sbr;
2,222✔
3301
        sbr.share(sb, lines[0].data(), (size_t) lines[0].length());
2,222✔
3302

3303
        return this->scan_json(dst, li, sbr, sbc);
2,222✔
3304
    }
2,222✔
3305

3306
    scan_result_t retval = scan_no_match{"no patterns matched"};
178,380✔
3307
    auto found = false;
178,380✔
3308

3309
    for (auto pat_iter = this->elf_pattern_order.begin();
178,380✔
3310
         pat_iter != this->elf_pattern_order.end();
1,354,490✔
3311
         ++pat_iter)
1,176,110✔
3312
    {
3313
        auto& pat = *(*pat_iter);
1,176,110✔
3314

3315
        if (!pat.p_pcre.pp_value) {
1,176,110✔
3316
            continue;
997,732✔
3317
        }
3318

3319
        auto md = pat.p_pcre.pp_value->create_match_data();
1,176,110✔
3320
        auto match_res = pat.p_pcre.pp_value->capture_from(lines[0])
1,176,110✔
3321
                             .into(md)
1,176,110✔
3322
                             .matches(PCRE2_NO_UTF_CHECK)
2,352,220✔
3323
                             .ignore_error();
1,176,110✔
3324
        if (!match_res) {
1,176,110✔
3325
            continue;
997,732✔
3326
        }
3327
        retval = scan_match{1000};
178,378✔
3328
        found = true;
178,378✔
3329

3330
        sample.s_matched_regexes.insert(pat.p_name.to_string());
178,378✔
3331

3332
        const auto ts_cap = md[pat.p_timestamp_field_index];
178,378✔
3333
        const auto level_cap = md[pat.p_level_field_index];
178,378✔
3334
        const char* const* custom_formats = this->get_timestamp_formats();
178,378✔
3335
        date_time_scanner dts;
178,378✔
3336
        timeval tv;
3337
        exttm tm;
178,378✔
3338

3339
        if (ts_cap && ts_cap->sf_begin == 0) {
178,378✔
3340
            pat.p_timestamp_end = ts_cap->sf_end;
108,429✔
3341
        }
3342
        const char* dts_scan_res = nullptr;
178,378✔
3343

3344
        if (ts_cap) {
178,378✔
3345
            dts_scan_res = dts.scan(
178,376✔
3346
                ts_cap->data(), ts_cap->length(), custom_formats, &tm, tv);
178,376✔
3347
        }
3348
        if (dts_scan_res != nullptr) {
178,378✔
3349
            if (dts_scan_res != ts_cap->data() + ts_cap->length()) {
178,375✔
3350
                auto match_len = dts_scan_res - ts_cap->data();
×
3351
                auto notes = attr_line_t("the used timestamp format: ");
×
3352
                if (custom_formats == nullptr) {
×
3353
                    notes.append(PTIMEC_FORMATS[dts.dts_fmt_lock].pf_fmt);
×
3354
                } else {
3355
                    notes.append(custom_formats[dts.dts_fmt_lock]);
×
3356
                }
3357
                notes.append("\n  ")
×
3358
                    .append(ts_cap.value())
×
3359
                    .append("\n")
×
3360
                    .append(2 + match_len, ' ')
×
3361
                    .append("^ matched up to here"_snippet_border);
×
3362
                auto um = lnav::console::user_message::warning(
×
3363
                              attr_line_t("timestamp was not fully matched: ")
×
3364
                                  .append_quoted(ts_cap.value()))
×
3365
                              .with_snippet(sample.s_line.to_snippet())
×
3366
                              .with_note(notes)
×
3367
                              .move();
×
3368

3369
                msgs.emplace_back(um);
×
3370
            }
3371
        } else if (!ts_cap) {
3✔
3372
            msgs.emplace_back(
2✔
3373
                lnav::console::user_message::error(
×
3374
                    attr_line_t("invalid sample log message: ")
4✔
3375
                        .append(lnav::to_json(sample.s_line.pp_value)))
4✔
3376
                    .with_reason(attr_line_t("timestamp was not captured"))
4✔
3377
                    .with_snippet(sample.s_line.to_snippet())
4✔
3378
                    .with_help(attr_line_t(
4✔
3379
                        "A timestamp needs to be captured in order for a "
3380
                        "line to be recognized as a log message")));
3381
        } else {
3382
            attr_line_t notes;
1✔
3383

3384
            if (custom_formats == nullptr) {
1✔
3385
                notes.append("the following built-in formats were tried:");
×
3386
                for (int lpc = 0; PTIMEC_FORMATS[lpc].pf_fmt != nullptr; lpc++)
×
3387
                {
3388
                    off_t off = 0;
×
3389

3390
                    PTIMEC_FORMATS[lpc].pf_func(
×
3391
                        &tm, ts_cap->data(), off, ts_cap->length());
×
3392
                    notes.append("\n  ")
×
3393
                        .append(ts_cap.value())
×
3394
                        .append("\n")
×
3395
                        .append(2 + off, ' ')
×
3396
                        .append("^ "_snippet_border)
×
3397
                        .append_quoted(
×
3398
                            lnav::roles::symbol(PTIMEC_FORMATS[lpc].pf_fmt))
×
3399
                        .append(" matched up to here"_snippet_border);
×
3400
                }
3401
            } else {
3402
                notes.append("the following custom formats were tried:");
1✔
3403
                for (int lpc = 0; custom_formats[lpc] != nullptr; lpc++) {
2✔
3404
                    off_t off = 0;
1✔
3405

3406
                    ptime_fmt(custom_formats[lpc],
1✔
3407
                              &tm,
3408
                              ts_cap->data(),
3409
                              off,
3410
                              ts_cap->length());
1✔
3411
                    notes.append("\n  ")
1✔
3412
                        .append(ts_cap.value())
1✔
3413
                        .append("\n")
1✔
3414
                        .append(2 + off, ' ')
1✔
3415
                        .append("^ "_snippet_border)
1✔
3416
                        .append_quoted(lnav::roles::symbol(custom_formats[lpc]))
2✔
3417
                        .append(" matched up to here"_snippet_border);
1✔
3418
                }
3419
            }
3420

3421
            msgs.emplace_back(
1✔
3422
                lnav::console::user_message::error(
×
3423
                    attr_line_t("invalid sample log message: ")
1✔
3424
                        .append(lnav::to_json(sample.s_line.pp_value)))
2✔
3425
                    .with_reason(attr_line_t("unrecognized timestamp -- ")
2✔
3426
                                     .append(ts_cap.value()))
1✔
3427
                    .with_snippet(sample.s_line.to_snippet())
2✔
3428
                    .with_note(notes)
1✔
3429
                    .with_help(attr_line_t("If the timestamp format is not "
2✔
3430
                                           "supported by default, you can "
3431
                                           "add a custom format with the ")
3432
                                   .append_quoted("timestamp-format"_symbol)
1✔
3433
                                   .append(" property")));
1✔
3434
        }
1✔
3435

3436
        auto level = this->convert_level(
178,378✔
3437
            level_cap.value_or(string_fragment::invalid()), nullptr);
178,378✔
3438

3439
        if (sample.s_level != LEVEL_UNKNOWN && sample.s_level != level) {
178,378✔
3440
            attr_line_t note_al;
1✔
3441

3442
            note_al.append("matched regex = ")
1✔
3443
                .append(lnav::roles::symbol(pat.p_name.to_string()))
2✔
3444
                .append("\n")
1✔
3445
                .append("captured level = ")
1✔
3446
                .append_quoted(level_cap->to_string());
1✔
3447
            if (level_cap && !this->elf_level_patterns.empty()) {
1✔
3448
                thread_local auto md = lnav::pcre2pp::match_data::unitialized();
1✔
3449

3450
                note_al.append("\nlevel regular expression match results:");
1✔
3451
                for (const auto& level_pattern : this->elf_level_patterns) {
3✔
3452
                    attr_line_t regex_al
3453
                        = level_pattern.second.lp_pcre.pp_value->get_pattern();
2✔
3454
                    lnav::snippets::regex_highlighter(
2✔
3455
                        regex_al, -1, line_range{0, (int) regex_al.length()});
2✔
3456
                    note_al.append("\n  ")
2✔
3457
                        .append(lnav::roles::symbol(
4✔
3458
                            level_pattern.second.lp_pcre.pp_path.to_string()))
4✔
3459
                        .append(" = ")
2✔
3460
                        .append(regex_al)
2✔
3461
                        .append("\n    ");
2✔
3462
                    auto match_res = level_pattern.second.lp_pcre.pp_value
2✔
3463
                                         ->capture_from(level_cap.value())
2✔
3464
                                         .into(md)
2✔
3465
                                         .matches(PCRE2_NO_UTF_CHECK)
4✔
3466
                                         .ignore_error();
2✔
3467
                    if (!match_res) {
2✔
3468
                        note_al.append(lnav::roles::warning("no match"));
1✔
3469
                        continue;
1✔
3470
                    }
3471

3472
                    note_al.append(level_cap.value())
1✔
3473
                        .append("\n    ")
1✔
3474
                        .append(md.leading().length(), ' ')
1✔
3475
                        .append("^"_snippet_border);
1✔
3476
                    if (match_res->f_all.length() > 2) {
1✔
3477
                        note_al.append(lnav::roles::snippet_border(
1✔
3478
                            std::string(match_res->f_all.length() - 2, '-')));
3✔
3479
                    }
3480
                    if (match_res->f_all.length() > 1) {
1✔
3481
                        note_al.append("^"_snippet_border);
1✔
3482
                    }
3483
                }
2✔
3484
            }
3485
            auto um
3486
                = lnav::console::user_message::error(
×
3487
                      attr_line_t("invalid sample log message: ")
1✔
3488
                          .append(lnav::to_json(sample.s_line.pp_value)))
2✔
3489
                      .with_reason(attr_line_t()
2✔
3490
                                       .append_quoted(lnav::roles::symbol(
2✔
3491
                                           level_names[level]))
1✔
3492
                                       .append(" does not match the expected "
1✔
3493
                                               "level of ")
3494
                                       .append_quoted(lnav::roles::symbol(
2✔
3495
                                           level_names[sample.s_level])))
1✔
3496
                      .with_snippet(sample.s_line.to_snippet())
2✔
3497
                      .with_note(note_al)
1✔
3498
                      .move();
1✔
3499
            if (!this->elf_level_patterns.empty()) {
1✔
3500
                um.with_help(
1✔
3501
                    attr_line_t("Level regexes are not anchored to the "
2✔
3502
                                "start/end of the string.  Prepend ")
3503
                        .append_quoted("^"_symbol)
1✔
3504
                        .append(" to the expression to match from the "
1✔
3505
                                "start of the string and append ")
3506
                        .append_quoted("$"_symbol)
1✔
3507
                        .append(" to match up to the end of the string."));
1✔
3508
            }
3509
            msgs.emplace_back(um);
1✔
3510
        }
1✔
3511

3512
        {
3513
            auto full_match_res
3514
                = pat.p_pcre.pp_value->capture_from(sample.s_line.pp_value)
178,378✔
3515
                      .into(md)
178,378✔
3516
                      .matches()
356,756✔
3517
                      .ignore_error();
178,378✔
3518
            if (!full_match_res) {
178,378✔
3519
                attr_line_t regex_al = pat.p_pcre.pp_value->get_pattern();
1✔
3520
                lnav::snippets::regex_highlighter(
1✔
3521
                    regex_al, -1, line_range{0, (int) regex_al.length()});
1✔
3522
                msgs.emplace_back(
1✔
3523
                    lnav::console::user_message::error(
×
3524
                        attr_line_t("invalid pattern: ")
1✔
3525
                            .append_quoted(
1✔
3526
                                lnav::roles::symbol(pat.p_name.to_string())))
2✔
3527
                        .with_reason("pattern does not match entire "
2✔
3528
                                     "multiline sample message")
3529
                        .with_snippet(sample.s_line.to_snippet())
2✔
3530
                        .with_note(attr_line_t()
2✔
3531
                                       .append(lnav::roles::symbol(
1✔
3532
                                           pat.p_name.to_string()))
2✔
3533
                                       .append(" = ")
1✔
3534
                                       .append(regex_al))
1✔
3535
                        .with_help(
3536
                            attr_line_t("use ").append_quoted(".*").append(
2✔
3537
                                " to match new-lines")));
3538
            } else if (static_cast<size_t>(full_match_res->f_all.length())
178,378✔
3539
                       != sample.s_line.pp_value.length())
178,377✔
3540
            {
3541
                attr_line_t regex_al = pat.p_pcre.pp_value->get_pattern();
1✔
3542
                lnav::snippets::regex_highlighter(
1✔
3543
                    regex_al, -1, line_range{0, (int) regex_al.length()});
1✔
3544
                auto match_length
3545
                    = static_cast<size_t>(full_match_res->f_all.length());
1✔
3546
                attr_line_t sample_al = sample.s_line.pp_value;
1✔
3547
                sample_al.append("\n")
1✔
3548
                    .append(match_length, ' ')
1✔
3549
                    .append("^ matched up to here"_error)
1✔
3550
                    .with_attr_for_all(VC_ROLE.value(role_t::VCR_QUOTED_CODE));
1✔
3551
                auto sample_snippet = lnav::console::snippet::from(
3552
                    sample.s_line.pp_location, sample_al);
1✔
3553
                msgs.emplace_back(
1✔
3554
                    lnav::console::user_message::error(
×
3555
                        attr_line_t("invalid pattern: ")
1✔
3556
                            .append_quoted(
1✔
3557
                                lnav::roles::symbol(pat.p_name.to_string())))
2✔
3558
                        .with_reason("pattern does not match entire "
2✔
3559
                                     "message")
3560
                        .with_snippet(sample_snippet)
1✔
3561
                        .with_note(attr_line_t()
3✔
3562
                                       .append(lnav::roles::symbol(
2✔
3563
                                           pat.p_name.to_string()))
2✔
3564
                                       .append(" = ")
1✔
3565
                                       .append(regex_al))
1✔
3566
                        .with_help("update the regular expression to fully "
3567
                                   "capture the sample message"));
3568
            }
1✔
3569
        }
3570
    }
1,176,110✔
3571

3572
    if (!found && !this->elf_pattern_order.empty()) {
178,380✔
3573
        std::vector<std::pair<ssize_t, intern_string_t>> partial_indexes;
2✔
3574
        attr_line_t notes;
2✔
3575
        size_t max_name_width = 0;
2✔
3576

3577
        for (const auto& pat_iter : this->elf_pattern_order) {
10✔
3578
            auto& pat = *pat_iter;
8✔
3579

3580
            if (!pat.p_pcre.pp_value) {
8✔
3581
                continue;
×
3582
            }
3583

3584
            partial_indexes.emplace_back(
8✔
3585
                pat.p_pcre.pp_value->match_partial(lines[0]), pat.p_name);
8✔
3586
            max_name_width = std::max(max_name_width, pat.p_name.size());
8✔
3587
        }
3588
        for (const auto& line_frag : lines) {
4✔
3589
            auto src_line = attr_line_t(line_frag.to_string());
2✔
3590
            if (!line_frag.endswith("\n")) {
2✔
3591
                src_line.append("\n");
2✔
3592
            }
3593
            src_line.with_attr_for_all(VC_ROLE.value(role_t::VCR_QUOTED_CODE));
2✔
3594
            notes.append("   ").append(src_line);
2✔
3595
            for (auto& part_pair : partial_indexes) {
10✔
3596
                if (part_pair.first >= 0
16✔
3597
                    && part_pair.first < line_frag.length())
8✔
3598
                {
3599
                    notes.append("   ")
8✔
3600
                        .append(part_pair.first, ' ')
8✔
3601
                        .append("^ "_snippet_border)
8✔
3602
                        .append(
8✔
3603
                            lnav::roles::symbol(part_pair.second.to_string()))
16✔
3604
                        .append(" matched up to here"_snippet_border)
8✔
3605
                        .append("\n");
8✔
3606
                }
3607
                part_pair.first -= line_frag.length();
8✔
3608
            }
3609
        }
2✔
3610
        notes.add_header(
2✔
3611
            "the following shows how each pattern matched this sample:\n");
3612

3613
        attr_line_t regex_note;
2✔
3614
        for (const auto& pat_iter : this->elf_pattern_order) {
10✔
3615
            if (!pat_iter->p_pcre.pp_value) {
8✔
3616
                regex_note
3617
                    .append(lnav::roles::symbol(fmt::format(
×
3618
                        FMT_STRING("{:{}}"), pat_iter->p_name, max_name_width)))
×
3619
                    .append(" is invalid");
×
3620
                continue;
×
3621
            }
3622

3623
            attr_line_t regex_al = pat_iter->p_pcre.pp_value->get_pattern();
8✔
3624
            lnav::snippets::regex_highlighter(
8✔
3625
                regex_al, -1, line_range{0, (int) regex_al.length()});
8✔
3626

3627
            regex_note
3628
                .append(lnav::roles::symbol(fmt::format(
16✔
3629
                    FMT_STRING("{:{}}"), pat_iter->p_name, max_name_width)))
24✔
3630
                .append(" = ")
8✔
3631
                .append_quoted(regex_al)
16✔
3632
                .append("\n");
8✔
3633
        }
8✔
3634

3635
        msgs.emplace_back(
2✔
3636
            lnav::console::user_message::error(
×
3637
                attr_line_t("invalid sample log message: ")
2✔
3638
                    .append(lnav::to_json(sample.s_line.pp_value)))
4✔
3639
                .with_reason("sample does not match any patterns")
4✔
3640
                .with_snippet(sample.s_line.to_snippet())
4✔
3641
                .with_note(notes.rtrim())
4✔
3642
                .with_note(regex_note));
3643
    }
2✔
3644

3645
    return retval;
178,380✔
3646
}
180,602✔
3647

3648
void
3649
external_log_format::build(std::vector<lnav::console::user_message>& errors)
49,906✔
3650
{
3651
    auto& vc = view_colors::singleton();
49,906✔
3652

3653
    if (!this->lf_timestamp_field.empty()) {
49,906✔
3654
        auto& vd = this->elf_value_defs[this->lf_timestamp_field];
49,906✔
3655
        if (vd.get() == nullptr) {
49,906✔
3656
            vd = std::make_shared<value_def>(
35,750✔
3657
                this->lf_timestamp_field,
35,750✔
3658
                value_kind_t::VALUE_TEXT,
×
3659
                logline_value_meta::internal_column{},
×
3660
                this);
35,750✔
3661
            if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
35,750✔
3662
                this->elf_value_def_order.emplace_back(vd);
4,922✔
3663
            }
3664
        }
3665
        vd->vd_meta.lvm_name = this->lf_timestamp_field;
49,906✔
3666
        vd->vd_meta.lvm_kind = value_kind_t::VALUE_TEXT;
49,906✔
3667
        vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
49,906✔
3668
        vd->vd_internal = true;
49,906✔
3669

3670
        this->elf_value_defs[LOG_TIME_STR] = vd;
49,906✔
3671
    }
3672

3673
    if (!this->lf_subsecond_field.empty()) {
49,906✔
3674
        if (!this->lf_subsecond_unit.has_value()) {
97✔
3675
            errors.emplace_back(
1✔
3676
                lnav::console::user_message::error(
×
3677
                    attr_line_t()
2✔
3678
                        .append_quoted(
1✔
3679
                            lnav::roles::symbol(this->elf_name.to_string()))
2✔
3680
                        .append(" is not a valid log format"))
1✔
3681
                    .with_reason(attr_line_t()
2✔
3682
                                     .append_quoted("subsecond-units"_symbol)
1✔
3683
                                     .append(" must be set when ")
1✔
3684
                                     .append_quoted("subsecond-field"_symbol)
1✔
3685
                                     .append(" is used"))
1✔
3686
                    .with_snippets(this->get_snippets()));
2✔
3687
        } else {
3688
            auto& vd = this->elf_value_defs[this->lf_subsecond_field];
96✔
3689
            if (vd.get() == nullptr) {
96✔
3690
                vd = std::make_shared<value_def>(
96✔
3691
                    this->lf_subsecond_field,
96✔
3692
                    value_kind_t::VALUE_INTEGER,
×
3693
                    logline_value_meta::internal_column{},
×
3694
                    this);
96✔
3695
                if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
96✔
3696
                    this->elf_value_def_order.emplace_back(vd);
96✔
3697
                }
3698
            }
3699
            vd->vd_meta.lvm_name = this->lf_subsecond_field;
96✔
3700
            vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
96✔
3701
            vd->vd_meta.lvm_hidden = true;
96✔
3702
            vd->vd_internal = true;
96✔
3703
        }
3704
    }
3705

3706
    if (startswith(this->elf_level_field.get(), "/")) {
49,906✔
3707
        this->elf_level_field
3708
            = intern_string::lookup(this->elf_level_field.get() + 1);
192✔
3709
    }
3710
    if (!this->elf_level_field.empty()) {
49,906✔
3711
        auto level_iter = this->elf_value_defs.find(this->elf_level_field);
49,906✔
3712
        if (level_iter == this->elf_value_defs.end()) {
49,906✔
3713
            auto& vd = this->elf_value_defs[this->elf_level_field];
25,293✔
3714
            vd = std::make_shared<value_def>(
25,293✔
3715
                this->elf_level_field,
25,293✔
3716
                value_kind_t::VALUE_TEXT,
×
3717
                logline_value_meta::internal_column{},
×
3718
                this);
25,293✔
3719
            if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
25,293✔
3720
                this->elf_value_def_order.emplace_back(vd);
2,606✔
3721
            }
3722
            vd->vd_meta.lvm_name = this->elf_level_field;
25,293✔
3723
            vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
25,293✔
3724
            vd->vd_internal = true;
25,293✔
3725

3726
            if (this->elf_level_field != this->elf_body_field) {
25,293✔
3727
                this->elf_value_defs[LOG_LEVEL_STR] = vd;
24,457✔
3728
            }
3729
        } else {
3730
            if (level_iter->second->vd_meta.lvm_kind
24,613✔
3731
                != value_kind_t::VALUE_TEXT)
24,613✔
3732
            {
3733
                this->lf_level_hideable = false;
5,276✔
3734
            }
3735
            this->elf_value_defs[LOG_LEVEL_STR] = level_iter->second;
24,613✔
3736
        }
3737
    }
3738

3739
    auto opid_field_iter = this->elf_value_defs.find(LOG_OPID_STR);
49,906✔
3740
    if (opid_field_iter == this->elf_value_defs.end()) {
49,906✔
3741
        auto vd
3742
            = std::make_shared<value_def>(this->elf_opid_field,
49,906✔
3743
                                          value_kind_t::VALUE_TEXT,
×
3744
                                          logline_value_meta::internal_column{},
×
3745
                                          this);
49,906✔
3746
        if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
49,906✔
3747
            this->elf_value_def_order.emplace_back(vd);
8,718✔
3748
        }
3749
        vd->vd_meta.lvm_name = LOG_OPID_STR;
49,906✔
3750
        vd->vd_meta.lvm_kind = value_kind_t::VALUE_TEXT;
49,906✔
3751
        vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
49,906✔
3752
        vd->vd_internal = true;
49,906✔
3753

3754
        this->elf_value_defs[LOG_OPID_STR] = vd;
49,906✔
3755
    }
49,906✔
3756

3757
    if (!this->elf_body_field.empty()) {
49,906✔
3758
        auto& vd = this->elf_value_defs[this->elf_body_field];
49,906✔
3759
        if (vd.get() == nullptr) {
49,906✔
3760
            vd = std::make_shared<value_def>(
40,833✔
3761
                this->elf_body_field,
40,833✔
3762
                value_kind_t::VALUE_TEXT,
×
3763
                logline_value_meta::internal_column{},
×
3764
                this);
40,833✔
3765
            if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
40,833✔
3766
                this->elf_value_def_order.emplace_back(vd);
5,661✔
3767
            }
3768
        }
3769
        vd->vd_meta.lvm_name = this->elf_body_field;
49,906✔
3770
        vd->vd_meta.lvm_kind = value_kind_t::VALUE_TEXT;
49,906✔
3771
        vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
49,906✔
3772
        vd->vd_internal = true;
49,906✔
3773
    }
3774

3775
    if (!this->elf_src_file_field.empty()) {
49,906✔
3776
        auto& vd = this->elf_value_defs[this->elf_src_file_field];
7,401✔
3777
        if (vd.get() == nullptr) {
7,401✔
3778
            vd = std::make_shared<value_def>(
1✔
3779
                this->elf_src_file_field,
1✔
3780
                value_kind_t::VALUE_TEXT,
×
3781
                logline_value_meta::internal_column{},
×
3782
                this);
2✔
3783
        }
3784
        vd->vd_meta.lvm_name = this->elf_src_file_field;
7,401✔
3785
        vd->vd_meta.lvm_kind = value_kind_t::VALUE_TEXT;
7,401✔
3786
        vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
7,401✔
3787
    }
3788

3789
    if (!this->elf_src_line_field.empty()) {
49,906✔
3790
        auto& vd = this->elf_value_defs[this->elf_src_line_field];
7,401✔
3791
        if (vd.get() == nullptr) {
7,401✔
3792
            vd = std::make_shared<value_def>(
1✔
3793
                this->elf_src_line_field,
1✔
3794
                value_kind_t::VALUE_INTEGER,
×
3795
                logline_value_meta::internal_column{},
×
3796
                this);
2✔
3797
        }
3798
        vd->vd_meta.lvm_name = this->elf_src_line_field;
7,401✔
3799
        vd->vd_meta.lvm_kind = value_kind_t::VALUE_INTEGER;
7,401✔
3800
        vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
7,401✔
3801
    }
3802

3803
    if (!this->elf_thread_id_field.empty()) {
49,906✔
3804
        auto& vd = this->elf_value_defs[this->elf_thread_id_field];
16,281✔
3805
        if (vd.get() == nullptr) {
16,281✔
3806
            vd = std::make_shared<value_def>(
1✔
3807
                this->elf_thread_id_field,
1✔
3808
                value_kind_t::VALUE_TEXT,
×
3809
                logline_value_meta::internal_column{},
×
3810
                this);
2✔
3811
        }
3812
        vd->vd_meta.lvm_name = this->elf_thread_id_field;
16,281✔
3813
        vd->vd_meta.lvm_kind = value_kind_t::VALUE_TEXT;
16,281✔
3814
        vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
16,281✔
3815
    }
3816

3817
    if (!this->elf_duration_field.empty()) {
49,906✔
3818
        auto& vd = this->elf_value_defs[this->elf_duration_field];
2,220✔
3819
        if (vd.get() == nullptr) {
2,220✔
3820
            vd = std::make_shared<value_def>(
×
3821
                this->elf_duration_field,
×
3822
                value_kind_t::VALUE_FLOAT,
×
3823
                logline_value_meta::internal_column{},
×
3824
                this);
×
3825
        }
3826
        vd->vd_meta.lvm_name = this->elf_duration_field;
2,220✔
3827
        vd->vd_meta.lvm_kind = value_kind_t::VALUE_FLOAT;
2,220✔
3828
        vd->vd_meta.lvm_column = logline_value_meta::internal_column{};
2,220✔
3829
    }
3830

3831
    for (auto& od_pair : *this->lf_opid_description_def) {
71,368✔
3832
        od_pair.second.od_index = this->lf_opid_description_def_vec->size();
21,462✔
3833
        this->lf_opid_description_def_vec->emplace_back(&od_pair.second);
21,462✔
3834
    }
3835

3836
    for (auto& od_pair : *this->lf_subid_description_def) {
50,646✔
3837
        od_pair.second.od_index = this->lf_subid_description_def_vec->size();
740✔
3838
        this->lf_subid_description_def_vec->emplace_back(&od_pair.second);
740✔
3839
    }
3840

3841
    if (!this->lf_timestamp_format.empty()) {
49,906✔
3842
        this->lf_timestamp_format.push_back(nullptr);
5,280✔
3843
    }
3844
    auto src_file_found = 0;
49,906✔
3845
    auto src_line_found = 0;
49,906✔
3846
    auto thread_id_found = 0;
49,906✔
3847
    auto duration_found = 0;
49,906✔
3848
    for (auto& elf_pattern : this->elf_patterns) {
145,214✔
3849
        auto& pat = *elf_pattern.second;
95,308✔
3850

3851
        if (pat.p_pcre.pp_value == nullptr) {
95,308✔
3852
            continue;
1✔
3853
        }
3854

3855
        if (pat.p_opid_field_index == -1
190,614✔
3856
            && this->lf_opid_source.value_or(opid_source_t::from_description)
95,307✔
3857
                == opid_source_t::from_description
3858
            && this->lf_opid_description_def->size() == 1)
190,614✔
3859
        {
3860
            const auto& opid_def
3861
                = this->lf_opid_description_def->begin()->second;
24,420✔
3862
            for (const auto& desc : *opid_def.od_descriptors) {
54,760✔
3863
                for (auto named_cap : pat.p_pcre.pp_value->get_named_captures())
346,320✔
3864
                {
3865
                    const intern_string_t name
3866
                        = intern_string::lookup(named_cap.get_name());
315,980✔
3867

3868
                    if (name == desc.od_field.pp_value) {
315,980✔
3869
                        pat.p_opid_description_field_indexes.emplace_back(
56,240✔
3870
                            named_cap.get_index());
28,120✔
3871
                    }
3872
                }
3873
            }
3874
        }
3875

3876
        for (auto named_cap : pat.p_pcre.pp_value->get_named_captures()) {
821,710✔
3877
            const intern_string_t name
3878
                = intern_string::lookup(named_cap.get_name());
726,403✔
3879

3880
            if (name == this->lf_timestamp_field) {
726,403✔
3881
                pat.p_timestamp_field_index = named_cap.get_index();
95,305✔
3882
            }
3883
            if (name == this->lf_time_field) {
726,403✔
3884
                pat.p_time_field_index = named_cap.get_index();
740✔
3885
            }
3886
            if (name == this->elf_level_field) {
726,403✔
3887
                pat.p_level_field_index = named_cap.get_index();
72,166✔
3888
            }
3889
            if (name == this->elf_opid_field) {
726,403✔
3890
                pat.p_opid_field_index = named_cap.get_index();
19,980✔
3891
            }
3892
            if (name == this->elf_subid_field) {
726,403✔
3893
                pat.p_subid_field_index = named_cap.get_index();
11,100✔
3894
            }
3895
            if (name == this->elf_body_field) {
726,403✔
3896
                pat.p_body_field_index = named_cap.get_index();
81,985✔
3897
            }
3898
            if (name == this->elf_src_file_field) {
726,403✔
3899
                pat.p_src_file_field_index = named_cap.get_index();
11,840✔
3900
                src_file_found += 1;
11,840✔
3901
            }
3902
            if (name == this->elf_src_line_field) {
726,403✔
3903
                pat.p_src_line_field_index = named_cap.get_index();
13,320✔
3904
                src_line_found += 1;
13,320✔
3905
            }
3906
            if (name == this->elf_thread_id_field) {
726,403✔
3907
                pat.p_thread_id_field_index = named_cap.get_index();
41,440✔
3908
                thread_id_found += 1;
41,440✔
3909
            }
3910
            if (name == this->elf_duration_field) {
726,403✔
3911
                pat.p_duration_field_index = named_cap.get_index();
4,440✔
3912
                duration_found += 1;
4,440✔
3913
            }
3914

3915
            auto value_iter = this->elf_value_defs.find(name);
726,403✔
3916
            if (value_iter != this->elf_value_defs.end()) {
726,403✔
3917
                auto vd = value_iter->second;
715,205✔
3918
                indexed_value_def ivd;
715,205✔
3919

3920
                ivd.ivd_index = named_cap.get_index();
715,205✔
3921
                if (!vd->vd_unit_field.empty()) {
715,205✔
3922
                    ivd.ivd_unit_field_index = pat.p_pcre.pp_value->name_index(
1,480✔
3923
                        vd->vd_unit_field.get());
740✔
3924
                } else {
3925
                    ivd.ivd_unit_field_index = -1;
714,465✔
3926
                }
3927
                if (!vd->vd_internal
715,205✔
3928
                    && !vd->vd_meta.lvm_column
1,229,151✔
3929
                            .is<logline_value_meta::table_column>())
513,946✔
3930
                {
3931
                    vd->vd_meta.lvm_column = logline_value_meta::table_column{
285,286✔
3932
                        this->elf_column_count++};
285,286✔
3933
                }
3934
                ivd.ivd_value_def = vd;
715,205✔
3935
                pat.p_value_by_index.push_back(ivd);
715,205✔
3936
            }
715,205✔
3937
            pat.p_value_name_to_index[name] = named_cap.get_index();
726,403✔
3938
        }
3939

3940
        stable_sort(pat.p_value_by_index.begin(), pat.p_value_by_index.end());
95,307✔
3941

3942
        for (int lpc = 0; lpc < (int) pat.p_value_by_index.size(); lpc++) {
810,512✔
3943
            auto& ivd = pat.p_value_by_index[lpc];
715,205✔
3944
            auto vd = ivd.ivd_value_def;
715,205✔
3945

3946
            if (!vd->vd_meta.lvm_foreign_key && !vd->vd_meta.lvm_identifier) {
715,205✔
3947
                switch (vd->vd_meta.lvm_kind) {
363,512✔
3948
                    case value_kind_t::VALUE_INTEGER:
50,416✔
3949
                    case value_kind_t::VALUE_FLOAT:
3950
                        pat.p_numeric_value_indexes.push_back(lpc);
50,416✔
3951
                        break;
50,416✔
3952
                    default:
313,096✔
3953
                        break;
313,096✔
3954
                }
3955
            }
3956
        }
715,205✔
3957

3958
        if (pat.p_timestamp_field_index == -1) {
95,307✔
3959
            errors.emplace_back(
2✔
3960
                lnav::console::user_message::error(
×
3961
                    attr_line_t("invalid pattern: ")
4✔
3962
                        .append_quoted(lnav::roles::symbol(pat.p_config_path)))
4✔
3963
                    .with_reason("no timestamp capture found in the pattern")
4✔
3964
                    .with_snippets(this->get_snippets())
4✔
3965
                    .with_help("all log messages need a timestamp"));
3966
        }
3967

3968
        if (!this->elf_level_field.empty() && pat.p_level_field_index == -1) {
95,307✔
3969
            log_warning("%s:level field '%s' not found in pattern",
23,141✔
3970
                        pat.p_config_path.c_str(),
3971
                        this->elf_level_field.get());
3972
        }
3973
        if (!this->elf_body_field.empty() && pat.p_body_field_index == -1) {
95,307✔
3974
            log_warning("%s:body field '%s' not found in pattern",
13,322✔
3975
                        pat.p_config_path.c_str(),
3976
                        this->elf_body_field.get());
3977
        }
3978

3979
        this->elf_pattern_order.push_back(elf_pattern.second);
95,307✔
3980
    }
3981
    if (this->elf_type == elf_type_t::ELF_TYPE_TEXT
99,812✔
3982
        && !this->elf_src_file_field.empty() && src_file_found == 0)
49,906✔
3983
    {
3984
        errors.emplace_back(
1✔
3985
            lnav::console::user_message::error(
×
3986
                attr_line_t("invalid pattern: ")
2✔
3987
                    .append_quoted(
1✔
3988
                        lnav::roles::symbol(this->elf_name.to_string())))
2✔
3989
                .with_reason("no source file capture found in the pattern")
2✔
3990
                .with_snippets(this->get_snippets())
2✔
3991
                .with_help(attr_line_t("at least one pattern needs a source "
2✔
3992
                                       "file capture named ")
3993
                               .append_quoted(this->elf_src_file_field.get())));
1✔
3994
    }
3995
    if (this->elf_type == elf_type_t::ELF_TYPE_TEXT
99,812✔
3996
        && !this->elf_src_line_field.empty() && src_line_found == 0)
49,906✔
3997
    {
3998
        errors.emplace_back(
1✔
3999
            lnav::console::user_message::error(
×
4000
                attr_line_t("invalid pattern: ")
2✔
4001
                    .append_quoted(
1✔
4002
                        lnav::roles::symbol(this->elf_name.to_string())))
2✔
4003
                .with_reason("no source line capture found in the pattern")
2✔
4004
                .with_snippets(this->get_snippets())
2✔
4005
                .with_help(attr_line_t("at least one pattern needs a source "
2✔
4006
                                       "line capture named ")
4007
                               .append_quoted(this->elf_src_line_field.get())));
1✔
4008
    }
4009
    if (this->elf_type == elf_type_t::ELF_TYPE_TEXT
99,812✔
4010
        && !this->elf_thread_id_field.empty() && thread_id_found == 0)
49,906✔
4011
    {
4012
        errors.emplace_back(
1✔
4013
            lnav::console::user_message::error(
×
4014
                attr_line_t("invalid pattern: ")
2✔
4015
                    .append_quoted(
1✔
4016
                        lnav::roles::symbol(this->elf_name.to_string())))
2✔
4017
                .with_reason("no thread ID capture found in the pattern")
2✔
4018
                .with_snippets(this->get_snippets())
2✔
4019
                .with_help(
4020
                    attr_line_t(
2✔
4021
                        "at least one pattern needs a thread ID capture named ")
4022
                        .append_quoted(this->elf_thread_id_field.get())));
1✔
4023
    }
4024
    if (this->elf_type == elf_type_t::ELF_TYPE_TEXT
99,812✔
4025
        && !this->elf_duration_field.empty() && duration_found == 0)
49,906✔
4026
    {
4027
        errors.emplace_back(
×
4028
            lnav::console::user_message::error(
×
4029
                attr_line_t("invalid pattern: ")
×
4030
                    .append_quoted(
×
4031
                        lnav::roles::symbol(this->elf_name.to_string())))
×
4032
                .with_reason("no duration capture found in the pattern")
×
4033
                .with_snippets(this->get_snippets())
×
4034
                .with_help(
4035
                    attr_line_t(
×
4036
                        "at least one pattern needs a duration capture named ")
4037
                        .append_quoted(this->elf_duration_field.get())));
×
4038
    }
4039

4040
    if (this->elf_type != elf_type_t::ELF_TYPE_TEXT) {
49,906✔
4041
        if (!this->elf_patterns.empty()) {
8,718✔
4042
            errors.emplace_back(
1✔
4043
                lnav::console::user_message::error(
×
4044
                    attr_line_t()
2✔
4045
                        .append_quoted(
1✔
4046
                            lnav::roles::symbol(this->elf_name.to_string()))
2✔
4047
                        .append(" is not a valid log format"))
1✔
4048
                    .with_reason("structured logs cannot have regexes")
2✔
4049
                    .with_snippets(this->get_snippets()));
2✔
4050
        }
4051
        if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
8,718✔
4052
            this->lf_multiline = true;
8,718✔
4053
            this->lf_structured = true;
8,718✔
4054
            this->lf_formatted_lines = true;
8,718✔
4055
            this->jlf_parse_context
4056
                = std::make_shared<yajlpp_parse_context>(this->elf_name);
8,718✔
4057
            this->jlf_yajl_handle.reset(
8,718✔
4058
                yajl_alloc(&this->jlf_parse_context->ypc_callbacks,
8,718✔
4059
                           nullptr,
4060
                           this->jlf_parse_context.get()),
8,718✔
4061
                yajl_handle_deleter());
4062
            yajl_config(
8,718✔
4063
                this->jlf_yajl_handle.get(), yajl_dont_validate_strings, 1);
4064
        }
4065
    } else {
4066
        if (this->elf_patterns.empty()) {
41,188✔
4067
            errors.emplace_back(lnav::console::user_message::error(
2✔
4068
                                    attr_line_t()
4✔
4069
                                        .append_quoted(lnav::roles::symbol(
4✔
4070
                                            this->elf_name.to_string()))
4✔
4071
                                        .append(" is not a valid log format"))
2✔
4072
                                    .with_reason("no regexes specified")
4✔
4073
                                    .with_snippets(this->get_snippets()));
4✔
4074
        }
4075
    }
4076

4077
    stable_sort(this->elf_level_pairs.begin(), this->elf_level_pairs.end());
49,906✔
4078

4079
    {
4080
        safe::WriteAccess<safe_format_header_expressions> hexprs(
4081
            format_header_exprs);
49,906✔
4082

4083
        if (hexprs->e_db.in() == nullptr) {
49,906✔
4084
            if (sqlite3_open(":memory:", hexprs->e_db.out()) != SQLITE_OK) {
740✔
4085
                log_error("unable to open memory DB");
×
4086
                return;
×
4087
            }
4088
            register_sqlite_funcs(hexprs->e_db.in(), sqlite_registration_funcs);
740✔
4089
        }
4090

4091
        for (const auto& hpair : this->elf_converter.c_header.h_exprs.he_exprs)
52,867✔
4092
        {
4093
            auto stmt_str
4094
                = fmt::format(FMT_STRING("SELECT 1 WHERE {}"), hpair.second);
8,883✔
4095
            compiled_header_expr che;
2,961✔
4096

4097
            log_info("preparing file-format header expression: %s",
2,961✔
4098
                     stmt_str.c_str());
4099
            auto retcode = sqlite3_prepare_v2(hexprs->e_db.in(),
5,922✔
4100
                                              stmt_str.c_str(),
4101
                                              stmt_str.size(),
2,961✔
4102
                                              che.che_stmt.out(),
4103
                                              nullptr);
4104
            if (retcode != SQLITE_OK) {
2,961✔
4105
                auto sql_al = attr_line_t(hpair.second)
2✔
4106
                                  .with_attr_for_all(SA_PREFORMATTED.value())
2✔
4107
                                  .with_attr_for_all(
1✔
4108
                                      VC_ROLE.value(role_t::VCR_QUOTED_CODE))
2✔
4109
                                  .move();
1✔
4110
                readline_sql_highlighter(
1✔
4111
                    sql_al, lnav::sql::dialect::sqlite, std::nullopt);
4112
                intern_string_t watch_expr_path = intern_string::lookup(
4113
                    fmt::format(FMT_STRING("/{}/converter/header/expr/{}"),
3✔
4114
                                this->elf_name,
1✔
4115
                                hpair.first));
2✔
4116
                auto snippet = lnav::console::snippet::from(
4117
                    source_location(watch_expr_path), sql_al);
1✔
4118

4119
                auto um = lnav::console::user_message::error(
2✔
4120
                              "SQL expression is invalid")
4121
                              .with_reason(sqlite3_errmsg(hexprs->e_db.in()))
2✔
4122
                              .with_snippet(snippet)
1✔
4123
                              .move();
1✔
4124

4125
                errors.emplace_back(um);
1✔
4126
                continue;
1✔
4127
            }
1✔
4128

4129
            hexprs->e_header_exprs[this->elf_name][hpair.first]
2,960✔
4130
                = std::move(che);
5,920✔
4131
        }
2,962✔
4132

4133
        if (!this->elf_converter.c_header.h_exprs.he_exprs.empty()
49,906✔
4134
            && this->elf_converter.c_command.pp_value.empty())
49,906✔
4135
        {
4136
            auto um = lnav::console::user_message::error(
2✔
4137
                          "A command is required when a converter is defined")
4138
                          .with_help(
2✔
4139
                              "The converter command transforms the file "
4140
                              "into a format that can be consumed by lnav")
4141
                          .with_snippets(this->get_snippets())
2✔
4142
                          .move();
1✔
4143
            errors.emplace_back(um);
1✔
4144
        }
1✔
4145
    }
49,906✔
4146

4147
    for (auto& vd : this->elf_value_def_order) {
490,579✔
4148
        std::vector<std::string>::iterator act_iter;
440,673✔
4149

4150
        if (!vd->vd_internal
440,673✔
4151
            && log_vtab_impl::RESERVED_COLUMNS.count(
836,950✔
4152
                vd->vd_meta.lvm_name.to_string_fragment()))
836,950✔
4153
        {
4154
            auto um = lnav::console::user_message::error(
×
4155
                          attr_line_t("value name ")
1✔
4156
                              .append_quoted(lnav::roles::symbol(
2✔
4157
                                  fmt::format(FMT_STRING("/{}/value/{}"),
4✔
4158
                                              this->elf_name,
1✔
4159
                                              vd->vd_meta.lvm_name)))
1✔
4160
                              .append(" is reserved and cannot be used"))
1✔
4161
                          .with_reason(
2✔
4162
                              "lnav automatically defines several columns in "
4163
                              "the log virtual table")
4164
                          .with_snippets(this->get_snippets())
2✔
4165
                          .with_help("Choose another name")
2✔
4166
                          .move();
1✔
4167
            errors.emplace_back(um);
1✔
4168
        }
1✔
4169

4170
        vd->vd_meta.lvm_format = this;
440,673✔
4171
        if (!vd->vd_internal
440,673✔
4172
            && !vd->vd_meta.lvm_column.is<logline_value_meta::table_column>())
440,673✔
4173
        {
4174
            vd->vd_meta.lvm_column
110,991✔
4175
                = logline_value_meta::table_column{this->elf_column_count++};
110,991✔
4176
        }
4177

4178
        if (vd->vd_meta.lvm_kind == value_kind_t::VALUE_UNKNOWN) {
440,673✔
4179
            vd->vd_meta.lvm_kind = value_kind_t::VALUE_TEXT;
×
4180
        }
4181

4182
        if (this->elf_type == elf_type_t::ELF_TYPE_TEXT) {
440,673✔
4183
            std::set<std::string> available_captures;
300,828✔
4184

4185
            bool found_in_pattern = false;
300,828✔
4186
            for (const auto& pat : this->elf_patterns) {
511,732✔
4187
                if (pat.second->p_pcre.pp_value == nullptr) {
511,730✔
4188
                    continue;
×
4189
                }
4190

4191
                auto cap_index = pat.second->p_pcre.pp_value->name_index(
1,023,460✔
4192
                    vd->vd_meta.lvm_name.get());
511,730✔
4193
                if (cap_index >= 0) {
511,730✔
4194
                    found_in_pattern = true;
300,826✔
4195
                    break;
300,826✔
4196
                }
4197

4198
                for (auto named_cap :
210,904✔
4199
                     pat.second->p_pcre.pp_value->get_named_captures())
1,924,758✔
4200
                {
4201
                    available_captures.insert(named_cap.get_name().to_string());
1,502,950✔
4202
                }
4203
            }
4204
            if (!found_in_pattern) {
300,828✔
4205
                auto notes
4206
                    = attr_line_t("the following captures are available:\n  ")
2✔
4207
                          .join(available_captures,
2✔
4208
                                VC_ROLE.value(role_t::VCR_SYMBOL),
4✔
4209
                                ", ")
4210
                          .move();
2✔
4211
                errors.emplace_back(
2✔
4212
                    lnav::console::user_message::warning(
×
4213
                        attr_line_t("invalid value ")
2✔
4214
                            .append_quoted(lnav::roles::symbol(
4✔
4215
                                fmt::format(FMT_STRING("/{}/value/{}"),
8✔
4216
                                            this->elf_name,
2✔
4217
                                            vd->vd_meta.lvm_name.get()))))
4✔
4218
                        .with_reason(
4✔
4219
                            attr_line_t("no patterns have a capture named ")
4✔
4220
                                .append_quoted(vd->vd_meta.lvm_name.get()))
2✔
4221
                        .with_note(notes)
2✔
4222
                        .with_snippets(this->get_snippets())
4✔
4223
                        .with_help("values are populated from captures in "
4224
                                   "patterns, so at least one pattern must "
4225
                                   "have a capture with this value name"));
4226
            }
2✔
4227
        }
300,828✔
4228

4229
        for (act_iter = vd->vd_action_list.begin();
440,673✔
4230
             act_iter != vd->vd_action_list.end();
441,413✔
4231
             ++act_iter)
740✔
4232
        {
4233
            if (this->lf_action_defs.find(*act_iter)
740✔
4234
                == this->lf_action_defs.end())
1,480✔
4235
            {
4236
#if 0
4237
                errors.push_back("error:" + this->elf_name.to_string() + ":"
4238
                                 + vd->vd_meta.lvm_name.get()
4239
                                 + ": cannot find action -- " + (*act_iter));
4240
#endif
4241
            }
4242
        }
4243

4244
        vd->set_rewrite_src_name();
440,673✔
4245
    }
4246

4247
    if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
49,906✔
4248
        for (const auto& vd : this->elf_value_def_order) {
148,563✔
4249
            this->elf_value_def_frag_map[vd->vd_meta.lvm_name
139,845✔
4250
                                             .to_string_fragment()]
139,845✔
4251
                = vd.get();
279,690✔
4252
        }
4253
    }
4254

4255
    for (const auto& td_pair : this->lf_tag_defs) {
52,225✔
4256
        const auto& td = td_pair.second;
2,319✔
4257

4258
        if (td->ftd_pattern.pp_value == nullptr
2,319✔
4259
            || td->ftd_pattern.pp_value->get_pattern().empty())
2,319✔
4260
        {
4261
            errors.emplace_back(
3✔
4262
                lnav::console::user_message::error(
×
4263
                    attr_line_t("invalid tag definition ")
6✔
4264
                        .append_quoted(lnav::roles::symbol(
6✔
4265
                            fmt::format(FMT_STRING("/{}/tags/{}"),
12✔
4266
                                        this->elf_name,
3✔
4267
                                        td_pair.first))))
3✔
4268
                    .with_reason(
6✔
4269
                        "tag definitions must have a non-empty pattern")
4270
                    .with_snippets(this->get_snippets()));
6✔
4271
        }
4272
    }
4273

4274
    if (this->elf_opid_field.empty()
49,906✔
4275
        && this->lf_opid_description_def->size() > 1)
49,906✔
4276
    {
4277
        errors.emplace_back(
1✔
4278
            lnav::console::user_message::error(
×
4279
                attr_line_t("too many opid descriptions")
2✔
4280
                    .append_quoted(lnav::roles::symbol(fmt::format(
2✔
4281
                        FMT_STRING("/{}/opid/description"), this->elf_name))))
4✔
4282
                .with_reason(attr_line_t("when no ")
2✔
4283
                                 .append("opid-field"_symbol)
1✔
4284
                                 .append(" is specified, only a single "
1✔
4285
                                         "description is supported"))
4286
                .with_snippets(this->get_snippets()));
2✔
4287
    }
4288

4289
    for (const auto& opid_desc_pair : *this->lf_opid_description_def) {
71,368✔
4290
        for (const auto& opid_desc : *opid_desc_pair.second.od_descriptors) {
51,064✔
4291
            auto iter = this->elf_value_defs.find(opid_desc.od_field.pp_value);
29,602✔
4292
            if (iter == this->elf_value_defs.end()) {
29,602✔
4293
                errors.emplace_back(
2✔
4294
                    lnav::console::user_message::error(
×
4295
                        attr_line_t("invalid opid description field ")
4✔
4296
                            .append_quoted(lnav::roles::symbol(
4✔
4297
                                opid_desc.od_field.pp_path.to_string())))
4✔
4298
                        .with_reason(
4✔
4299
                            attr_line_t("unknown value name ")
4✔
4300
                                .append_quoted(opid_desc.od_field.pp_value))
2✔
4301
                        .with_snippets(this->get_snippets()));
4✔
4302
            } else {
4303
                this->lf_desc_fields.insert(iter->first);
29,600✔
4304
                iter->second->vd_is_desc_field = true;
29,600✔
4305
            }
4306
        }
4307
    }
4308

4309
    for (const auto& subid_desc_pair : *this->lf_subid_description_def) {
50,646✔
4310
        for (const auto& subid_desc : *subid_desc_pair.second.od_descriptors) {
1,480✔
4311
            auto iter = this->elf_value_defs.find(subid_desc.od_field.pp_value);
740✔
4312
            if (iter == this->elf_value_defs.end()) {
740✔
4313
                errors.emplace_back(
×
4314
                    lnav::console::user_message::error(
×
4315
                        attr_line_t("invalid subid description field ")
×
4316
                            .append_quoted(lnav::roles::symbol(
×
4317
                                subid_desc.od_field.pp_path.to_string())))
×
4318
                        .with_reason(
×
4319
                            attr_line_t("unknown value name ")
×
4320
                                .append_quoted(subid_desc.od_field.pp_value))
×
4321
                        .with_snippets(this->get_snippets()));
×
4322
            } else {
4323
                this->lf_desc_fields.insert(iter->first);
740✔
4324
                iter->second->vd_is_desc_field = true;
740✔
4325
            }
4326
        }
4327
    }
4328

4329
    if (this->elf_type == elf_type_t::ELF_TYPE_TEXT
99,812✔
4330
        && this->elf_samples.empty())
49,906✔
4331
    {
4332
        errors.emplace_back(
3✔
4333
            lnav::console::user_message::error(
×
4334
                attr_line_t()
6✔
4335
                    .append_quoted(
3✔
4336
                        lnav::roles::symbol(this->elf_name.to_string()))
6✔
4337
                    .append(" is not a valid log format"))
3✔
4338
                .with_reason("log message samples must be included in a format "
6✔
4339
                             "definition")
4340
                .with_snippets(this->get_snippets()));
6✔
4341
    }
4342

4343
    for (const auto& pat : this->elf_pattern_order) {
145,213✔
4344
        if (this->elf_type != elf_type_t::ELF_TYPE_TEXT) {
95,307✔
4345
            continue;
1✔
4346
        }
4347
        if (pat->p_pcre.pp_value->name_index(this->lf_timestamp_field.get())
95,306✔
4348
            < 0)
95,306✔
4349
        {
4350
            attr_line_t notes;
1✔
4351
            bool first_note = true;
1✔
4352

4353
            if (pat->p_pcre.pp_value->get_capture_count() > 0) {
1✔
4354
                notes.append("the following captures are available:\n  ");
1✔
4355
            }
4356
            for (auto named_cap : pat->p_pcre.pp_value->get_named_captures()) {
4✔
4357
                if (!first_note) {
3✔
4358
                    notes.append(", ");
2✔
4359
                }
4360
                notes.append(
3✔
4361
                    lnav::roles::symbol(named_cap.get_name().to_string()));
6✔
4362
                first_note = false;
3✔
4363
            }
4364
            errors.emplace_back(
1✔
4365
                lnav::console::user_message::error(
×
4366
                    attr_line_t("invalid value for property ")
1✔
4367
                        .append_quoted(lnav::roles::symbol(
2✔
4368
                            fmt::format(FMT_STRING("/{}/timestamp-field"),
4✔
4369
                                        this->elf_name))))
1✔
4370
                    .with_reason(
2✔
4371
                        attr_line_t()
2✔
4372
                            .append_quoted(this->lf_timestamp_field)
1✔
4373
                            .append(" was not found in the pattern at ")
1✔
4374
                            .append(lnav::roles::symbol(pat->p_config_path)))
2✔
4375
                    .with_note(notes)
1✔
4376
                    .with_snippets(this->get_snippets()));
2✔
4377
        }
1✔
4378
    }
4379

4380
    for (size_t sample_index = 0; sample_index < this->elf_samples.size();
230,504✔
4381
         sample_index += 1)
180,598✔
4382
    {
4383
        auto& elf_sample = this->elf_samples[sample_index];
180,598✔
4384
        auto sample_lines
4385
            = string_fragment(elf_sample.s_line.pp_value).split_lines();
180,598✔
4386

4387
        if (this->test_line(elf_sample, errors).is<scan_match>()) {
180,598✔
4388
            for (const auto& pat_name : elf_sample.s_matched_regexes) {
358,974✔
4389
                this->elf_patterns[pat_name]->p_matched_samples.emplace(
178,377✔
4390
                    sample_index);
4391
            }
4392
        }
4393
    }
180,598✔
4394

4395
    if (!this->elf_samples.empty()) {
49,906✔
4396
        for (const auto& elf_sample : this->elf_samples) {
222,523✔
4397
            if (elf_sample.s_matched_regexes.size() <= 1) {
180,598✔
4398
                continue;
180,598✔
4399
            }
4400

4401
            errors.emplace_back(
×
4402
                lnav::console::user_message::warning(
×
4403
                    attr_line_t("invalid log format: ")
×
4404
                        .append_quoted(
×
4405
                            lnav::roles::symbol(this->elf_name.to_string())))
×
4406
                    .with_reason(
×
4407
                        attr_line_t(
×
4408
                            "sample is matched by more than one regex: ")
4409
                            .join(elf_sample.s_matched_regexes,
×
4410
                                  VC_ROLE.value(role_t::VCR_SYMBOL),
×
4411
                                  ", "))
4412
                    .with_snippet(lnav::console::snippet::from(
×
4413
                        elf_sample.s_line.pp_location,
4414
                        attr_line_t().append(lnav::roles::quoted_code(
×
4415
                            elf_sample.s_line.pp_value))))
×
4416
                    .with_help("log format regexes must match a single type "
4417
                               "of log message"));
4418
        }
4419

4420
        for (const auto& pat : this->elf_pattern_order) {
137,229✔
4421
            if (pat->p_matched_samples.empty()) {
95,304✔
4422
                errors.emplace_back(
2✔
4423
                    lnav::console::user_message::warning(
×
4424
                        attr_line_t("invalid pattern: ")
4✔
4425
                            .append_quoted(
2✔
4426
                                lnav::roles::symbol(pat->p_config_path)))
4✔
4427
                        .with_reason("pattern does not match any samples")
4✔
4428
                        .with_snippet(lnav::console::snippet::from(
6✔
4429
                            pat->p_pcre.pp_location, ""))
2✔
4430
                        .with_help(
4431
                            "every pattern should have at least one sample "
4432
                            "that it matches"));
4433
            }
4434
        }
4435
    }
4436

4437
    size_t value_def_index = 0;
49,906✔
4438
    for (auto& elf_value_def : this->elf_value_def_order) {
490,579✔
4439
        elf_value_def->vd_meta.lvm_values_index
440,673✔
4440
            = std::make_optional(value_def_index++);
440,673✔
4441

4442
        if (elf_value_def->vd_meta.lvm_foreign_key
440,673✔
4443
            || elf_value_def->vd_meta.lvm_identifier)
440,673✔
4444
        {
4445
            continue;
233,609✔
4446
        }
4447

4448
        switch (elf_value_def->vd_meta.lvm_kind) {
207,064✔
4449
            case value_kind_t::VALUE_INTEGER:
52,184✔
4450
            case value_kind_t::VALUE_FLOAT:
4451
                this->elf_numeric_value_defs.push_back(elf_value_def);
52,184✔
4452
                break;
52,184✔
4453
            default:
154,880✔
4454
                break;
154,880✔
4455
        }
4456
    }
4457

4458
    int format_index = 0;
49,906✔
4459
    for (auto iter = this->jlf_line_format.begin();
49,906✔
4460
         iter != this->jlf_line_format.end();
153,841✔
4461
         ++iter, format_index++)
103,935✔
4462
    {
4463
        static const intern_string_t ts
4464
            = intern_string::lookup("__timestamp__");
105,415✔
4465
        static const intern_string_t level_field
4466
            = intern_string::lookup("__level__");
105,415✔
4467
        auto& jfe = *iter;
103,935✔
4468

4469
        if (startswith(jfe.jfe_value.pp_value.get(), "/")) {
103,935✔
4470
            jfe.jfe_value.pp_value
4471
                = intern_string::lookup(jfe.jfe_value.pp_value.get() + 1);
192✔
4472
        }
4473
        if (!jfe.jfe_ts_format.empty()) {
103,935✔
4474
            if (!jfe.jfe_value.pp_value.empty() && jfe.jfe_value.pp_value != ts)
836✔
4475
            {
4476
                log_warning(
96✔
4477
                    "%s:line-format[%d]:ignoring field '%s' since "
4478
                    "timestamp-format was used",
4479
                    this->elf_name.get(),
4480
                    format_index,
4481
                    jfe.jfe_value.pp_value.get());
4482
            }
4483
            jfe.jfe_value.pp_value = ts;
836✔
4484
        }
4485

4486
        switch (jfe.jfe_type) {
103,935✔
4487
            case json_log_field::VARIABLE: {
68,167✔
4488
                auto vd_iter
4489
                    = this->elf_value_defs.find(jfe.jfe_value.pp_value);
68,167✔
4490
                if (jfe.jfe_value.pp_value == ts) {
68,167✔
4491
                    this->elf_value_defs[this->lf_timestamp_field]
6,949✔
4492
                        ->vd_meta.lvm_hidden
6,949✔
4493
                        = true;
6,949✔
4494
                } else if (jfe.jfe_value.pp_value == level_field) {
61,218✔
4495
                    this->elf_value_defs[this->elf_level_field]
2,960✔
4496
                        ->vd_meta.lvm_hidden
2,960✔
4497
                        = true;
2,960✔
4498
                } else if (vd_iter == this->elf_value_defs.end()) {
58,258✔
4499
                    errors.emplace_back(
2✔
4500
                        lnav::console::user_message::error(
×
4501
                            attr_line_t("invalid line format element ")
4✔
4502
                                .append_quoted(lnav::roles::symbol(fmt::format(
4✔
4503
                                    FMT_STRING("/{}/line-format/{}/field"),
6✔
4504
                                    this->elf_name,
2✔
4505
                                    format_index))))
4506
                            .with_reason(
4✔
4507
                                attr_line_t()
4✔
4508
                                    .append_quoted(jfe.jfe_value.pp_value)
2✔
4509
                                    .append(" is not a defined value"))
2✔
4510
                            .with_snippet(jfe.jfe_value.to_snippet()));
4✔
4511
                } else {
4512
                    vd_iter->second->vd_line_format_index = format_index;
58,256✔
4513
                    switch (vd_iter->second->vd_meta.lvm_kind) {
58,256✔
4514
                        case value_kind_t::VALUE_INTEGER:
9,812✔
4515
                        case value_kind_t::VALUE_FLOAT:
4516
                            if (jfe.jfe_align
9,812✔
4517
                                == json_format_element::align_t::NONE)
4518
                            {
4519
                                jfe.jfe_align
4520
                                    = json_format_element::align_t::RIGHT;
9,072✔
4521
                            }
4522
                            break;
9,812✔
4523
                        default:
48,444✔
4524
                            break;
48,444✔
4525
                    }
4526
                }
4527
                break;
68,167✔
4528
            }
4529
            case json_log_field::CONSTANT:
35,768✔
4530
                this->jlf_line_format_init_count
35,768✔
4531
                    += std::count(jfe.jfe_default_value.begin(),
35,768✔
4532
                                  jfe.jfe_default_value.end(),
4533
                                  '\n');
35,768✔
4534
                break;
35,768✔
4535
            default:
×
4536
                break;
×
4537
        }
4538
    }
4539

4540
    for (auto& hd_pair : this->elf_highlighter_patterns) {
50,292✔
4541
        auto& hd = hd_pair.second;
386✔
4542
        text_attrs attrs;
386✔
4543

4544
        if (!hd.hd_color.pp_value.empty()) {
386✔
4545
            attrs.ta_fg_color = vc.match_color(
385✔
4546
                styling::color_unit::from_str(hd.hd_color.pp_value)
770✔
4547
                    .unwrapOrElse([&](const auto& msg) {
1✔
4548
                        errors.emplace_back(
1✔
4549
                            lnav::console::user_message::error(
4550
                                attr_line_t()
1✔
4551
                                    .append_quoted(hd.hd_color.pp_value)
2✔
4552
                                    .append(" is not a valid color value for "
1✔
4553
                                            "property ")
4554
                                    .append_quoted(lnav::roles::symbol(
2✔
4555
                                        hd.hd_color.pp_path.to_string())))
1✔
4556
                                .with_reason(msg)
2✔
4557
                                .with_snippet(hd.hd_color.to_snippet()));
1✔
4558
                        return styling::color_unit::EMPTY;
1✔
4559
                    }));
4560
        }
4561

4562
        if (!hd.hd_background_color.pp_value.empty()) {
386✔
4563
            attrs.ta_bg_color = vc.match_color(
1✔
4564
                styling::color_unit::from_str(hd.hd_background_color.pp_value)
2✔
4565
                    .unwrapOrElse([&](const auto& msg) {
1✔
4566
                        errors.emplace_back(
1✔
4567
                            lnav::console::user_message::error(
4568
                                attr_line_t()
1✔
4569
                                    .append_quoted(
2✔
4570
                                        hd.hd_background_color.pp_value)
1✔
4571
                                    .append(" is not a valid color value for "
1✔
4572
                                            "property ")
4573
                                    .append_quoted(lnav::roles::symbol(
2✔
4574
                                        hd.hd_background_color.pp_path
1✔
4575
                                            .to_string())))
4576
                                .with_reason(msg)
2✔
4577
                                .with_snippet(
4578
                                    hd.hd_background_color.to_snippet()));
1✔
4579
                        return styling::color_unit::EMPTY;
1✔
4580
                    }));
4581
        }
4582

4583
        if (hd.hd_underline) {
386✔
4584
            attrs |= text_attrs::style::underline;
96✔
4585
        }
4586
        if (hd.hd_blink) {
386✔
4587
            attrs |= text_attrs::style::blink;
×
4588
        }
4589

4590
        if (hd.hd_pattern.pp_value != nullptr) {
386✔
4591
            this->lf_highlighters.emplace_back(hd.hd_pattern.pp_value);
384✔
4592
            this->lf_highlighters.back()
384✔
4593
                .with_name(hd_pair.first.to_string())
768✔
4594
                .with_format_name(this->elf_name)
384✔
4595
                .with_attrs(attrs)
384✔
4596
                .with_nestable(hd.hd_nestable);
384✔
4597
        }
4598
    }
4599
}
4600

4601
void
4602
external_log_format::register_vtabs(
41,876✔
4603
    log_vtab_manager* vtab_manager,
4604
    std::vector<lnav::console::user_message>& errors)
4605
{
4606
    for (auto& elf_search_table : this->elf_search_tables) {
50,015✔
4607
        if (elf_search_table.second.std_pattern.pp_value == nullptr) {
8,139✔
4608
            continue;
1✔
4609
        }
4610

4611
        auto lst = std::make_shared<log_search_table>(
4612
            elf_search_table.second.std_pattern.pp_value,
8,138✔
4613
            elf_search_table.first);
8,138✔
4614
        lst->lst_format = this;
8,138✔
4615
        lst->lst_log_path_glob = elf_search_table.second.std_glob;
8,138✔
4616
        if (elf_search_table.second.std_level != LEVEL_UNKNOWN) {
8,138✔
4617
            lst->lst_log_level = elf_search_table.second.std_level;
3,130✔
4618
        }
4619
        auto errmsg = vtab_manager->register_vtab(lst);
8,138✔
4620
        if (!errmsg.empty()) {
8,138✔
4621
#if 0
4622
            errors.push_back("error:" + this->elf_name.to_string() + ":"
4623
                             + search_iter->first.to_string()
4624
                             + ":unable to register table -- " + errmsg);
4625
#endif
4626
        }
4627
    }
8,138✔
4628
}
41,876✔
4629

4630
bool
4631
external_log_format::match_samples(const std::vector<sample_t>& samples) const
3,325,930✔
4632
{
4633
    for (const auto& sample_iter : samples) {
15,204,689✔
4634
        for (const auto& pat_iter : this->elf_pattern_order) {
33,168,742✔
4635
            auto& pat = *pat_iter;
21,289,983✔
4636

4637
            if (!pat.p_pcre.pp_value) {
21,289,983✔
4638
                continue;
×
4639
            }
4640

4641
            if (pat.p_pcre.pp_value
42,579,966✔
4642
                    ->find_in(sample_iter.s_line.pp_value, PCRE2_NO_UTF_CHECK)
42,579,966✔
4643
                    .ignore_error())
42,579,966✔
4644
            {
4645
                return true;
15,424✔
4646
            }
4647
        }
4648
    }
4649

4650
    return false;
3,310,506✔
4651
}
4652

4653
class external_log_table : public log_format_vtab_impl {
4654
public:
4655
    explicit external_log_table(const external_log_format& elf)
41,876✔
4656
        : log_format_vtab_impl(elf), elt_format(elf)
41,876✔
4657
    {
4658
    }
41,876✔
4659

4660
    void get_columns(std::vector<vtab_column>& cols) const override
42,268✔
4661
    {
4662
        const auto& elf = this->elt_format;
42,268✔
4663

4664
        cols.resize(elf.elf_column_count);
42,268✔
4665
        for (const auto& vd : elf.elf_value_def_order) {
417,436✔
4666
            auto type_pair = logline_value_to_sqlite_type(vd->vd_meta.lvm_kind);
375,168✔
4667

4668
            if (!vd->vd_meta.lvm_column.is<logline_value_meta::table_column>())
375,168✔
4669
            {
4670
                continue;
36,967✔
4671
            }
4672

4673
            auto col
4674
                = vd->vd_meta.lvm_column.get<logline_value_meta::table_column>()
338,201✔
4675
                      .value;
338,201✔
4676
            require(0 <= col && col < elf.elf_column_count);
338,201✔
4677

4678
            cols[col].vc_name = vd->vd_meta.lvm_name.get();
338,201✔
4679
            cols[col].vc_type = type_pair.first;
338,201✔
4680
            cols[col].vc_subtype = type_pair.second;
338,201✔
4681
            cols[col].vc_collator = vd->vd_collate;
338,201✔
4682
            cols[col].vc_comment = vd->vd_description;
338,201✔
4683
        }
4684
    }
42,268✔
4685

4686
    void get_foreign_keys(
34,386✔
4687
        std::unordered_set<std::string>& keys_inout) const override
4688
    {
4689
        log_vtab_impl::get_foreign_keys(keys_inout);
34,386✔
4690

4691
        for (const auto& elf_value_def : this->elt_format.elf_value_defs) {
496,462✔
4692
            if (elf_value_def.second->vd_meta.lvm_foreign_key
462,076✔
4693
                || elf_value_def.second->vd_meta.lvm_identifier)
462,076✔
4694
            {
4695
                keys_inout.emplace(elf_value_def.first.to_string());
167,210✔
4696
            }
4697
        }
4698
    }
34,386✔
4699

4700
    bool next(log_cursor& lc, logfile_sub_source& lss) override
3,476✔
4701
    {
4702
        if (lc.is_eof()) {
3,476✔
4703
            return true;
×
4704
        }
4705

4706
        content_line_t cl(lss.at(lc.lc_curr_line));
3,476✔
4707
        auto* lf = lss.find_file_ptr(cl);
3,476✔
4708
        auto lf_iter = lf->begin() + cl;
3,476✔
4709
        if (lf_iter->is_continued()) {
3,476✔
UNCOV
4710
            return false;
×
4711
        }
4712

4713
        if (lf->get_format_name() == this->lfvi_format.get_name()) {
3,476✔
4714
            return true;
3,470✔
4715
        }
4716

4717
        return false;
6✔
4718
    }
4719

4720
    void extract(logfile* lf,
3,385✔
4721
                 uint64_t line_number,
4722
                 string_attrs_t& sa,
4723
                 logline_value_vector& values) override
4724
    {
4725
        auto format = lf->get_format();
3,385✔
4726

4727
        sa.clear();
3,385✔
4728
        format->annotate(lf, line_number, sa, values);
3,385✔
4729
    }
3,385✔
4730

4731
    const external_log_format& elt_format;
4732
    line_range elt_container_body;
4733
};
4734

4735
std::shared_ptr<log_vtab_impl>
4736
external_log_format::get_vtab_impl() const
41,876✔
4737
{
4738
    return std::make_shared<external_log_table>(*this);
41,876✔
4739
}
4740

4741
std::shared_ptr<log_format>
4742
external_log_format::specialized(int fmt_lock)
506✔
4743
{
4744
    auto retval = std::make_shared<external_log_format>(*this);
506✔
4745

4746
    retval->lf_specialized = true;
506✔
4747
    if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
506✔
4748
        this->jlf_parse_context
4749
            = std::make_shared<yajlpp_parse_context>(this->elf_name);
46✔
4750
        this->jlf_yajl_handle.reset(
46✔
4751
            yajl_alloc(&this->jlf_parse_context->ypc_callbacks,
46✔
4752
                       nullptr,
4753
                       this->jlf_parse_context.get()),
46✔
4754
            yajl_handle_deleter());
4755
        yajl_config(this->jlf_yajl_handle.get(), yajl_dont_validate_strings, 1);
46✔
4756
        this->jlf_cached_line.reserve(16 * 1024);
46✔
4757
    }
4758

4759
    this->elf_specialized_value_defs_state = *this->elf_value_defs_state;
506✔
4760

4761
    return retval;
1,012✔
4762
}
506✔
4763

4764
log_format::match_name_result
4765
external_log_format::match_name(const std::string& filename)
777,299✔
4766
{
4767
    if (this->elf_filename_pcre.pp_value == nullptr) {
777,299✔
4768
        return name_matched{};
775,920✔
4769
    }
4770

4771
    if (this->elf_filename_pcre.pp_value->find_in(filename)
2,758✔
4772
            .ignore_error()
2,758✔
4773
            .has_value())
1,379✔
4774
    {
4775
        return name_matched{};
238✔
4776
    }
4777

4778
    return name_mismatched{
2,282✔
4779
        this->elf_filename_pcre.pp_value->match_partial(filename),
2,282✔
4780
        this->elf_filename_pcre.pp_value->get_pattern(),
1,141✔
4781
    };
1,141✔
4782
}
4783

4784
auto
4785
external_log_format::value_line_count(scan_batch_context& sbc,
205,179✔
4786
                                      const value_def* vd,
4787
                                      bool top_level,
4788
                                      std::optional<double> val,
4789
                                      const unsigned char* str,
4790
                                      ssize_t len,
4791
                                      yajl_string_props_t* props)
4792
    -> value_line_count_result
4793
{
4794
    value_line_count_result retval;
205,179✔
4795

4796
    if (vd == nullptr) {
205,179✔
4797
        if (this->jlf_hide_extra || !top_level) {
190,119✔
4798
            retval.vlcr_count = 0;
176,868✔
4799
        }
4800

4801
        return retval;
190,119✔
4802
    }
4803

4804
    if (str != nullptr && props != nullptr && !val) {
15,060✔
4805
        auto frag = string_fragment::from_bytes(str, len);
13,581✔
4806
        while (frag.endswith("\n")) {
13,750✔
4807
            frag.pop_back();
169✔
4808
            props->line_feeds -= 1;
169✔
4809
        }
4810
        retval.vlcr_has_ansi |= props->has_ansi;
13,581✔
4811
        retval.vlcr_count += props->line_feeds;
13,581✔
4812
    }
4813

4814
    if (vd->vd_meta.lvm_values_index) {
15,060✔
4815
        auto& lvs = sbc.sbc_value_stats[vd->vd_meta.lvm_values_index.value()];
6,180✔
4816
        if (len > lvs.lvs_width) {
6,180✔
4817
            lvs.lvs_width = len;
3,160✔
4818
        }
4819
        if (val) {
6,180✔
4820
            lvs.add_value(val.value());
151✔
4821
        }
4822
    }
4823

4824
    if (vd->vd_line_format_index) {
15,060✔
4825
        retval.vlcr_line_format_count += 1;
3,058✔
4826
        retval.vlcr_count -= 1;
3,058✔
4827
        retval.vlcr_line_format_index = vd->vd_line_format_index;
3,058✔
4828
    }
4829
    if (vd->vd_meta.is_hidden()) {
15,060✔
4830
        retval.vlcr_count = 0;
2,911✔
4831
        return retval;
2,911✔
4832
    }
4833

4834
    return retval;
12,149✔
4835
}
4836

4837
log_level_t
4838
external_log_format::convert_level(string_fragment sf,
187,752✔
4839
                                   scan_batch_context* sbc) const
4840
{
4841
    auto retval = LEVEL_INFO;
187,752✔
4842

4843
    if (sf.is_valid()) {
187,752✔
4844
        if (sbc != nullptr) {
146,637✔
4845
            auto ssm_res = sbc->sbc_level_cache.lookup(sf);
9,156✔
4846
            if (ssm_res.has_value()) {
9,156✔
4847
                return static_cast<log_level_t>(ssm_res.value());
4,088✔
4848
            }
4849
        }
4850

4851
        if (this->elf_level_patterns.empty()) {
142,549✔
4852
            retval = string2level(sf.data(), sf.length());
29,933✔
4853
        } else {
4854
            for (const auto& elf_level_pattern : this->elf_level_patterns) {
303,495✔
4855
                if (elf_level_pattern.second.lp_pcre.pp_value
555,530✔
4856
                        ->find_in(sf, PCRE2_NO_UTF_CHECK)
555,530✔
4857
                        .ignore_error()
555,530✔
4858
                        .has_value())
277,765✔
4859
                {
4860
                    retval = elf_level_pattern.first;
86,886✔
4861
                    break;
86,886✔
4862
                }
4863
            }
4864
        }
4865

4866
        if (sbc != nullptr
142,549✔
4867
            && sf.length() <= lnav::small_string_map::MAX_KEY_SIZE)
142,549✔
4868
        {
4869
            sbc->sbc_level_cache.insert(sf, retval);
4,421✔
4870
        }
4871
    }
4872

4873
    return retval;
183,664✔
4874
}
4875

4876
logline_value_meta
4877
external_log_format::get_value_meta(intern_string_t field_name,
15,035✔
4878
                                    value_kind_t kind)
4879
{
4880
    const auto iter = this->elf_value_defs.find(field_name);
15,035✔
4881
    if (iter == this->elf_value_defs.end()) {
15,035✔
4882
        auto retval = logline_value_meta(
4883
            field_name, kind, logline_value_meta::external_column{}, this);
879✔
4884

4885
        retval.lvm_hidden = this->jlf_hide_extra;
879✔
4886
        return retval;
879✔
4887
    }
879✔
4888

4889
    auto lvm = iter->second->vd_meta;
14,156✔
4890

4891
    lvm.lvm_kind = kind;
14,156✔
4892
    return lvm;
14,156✔
4893
}
14,156✔
4894

4895
logline_value_meta
4896
external_log_format::get_value_meta(yajlpp_parse_context* ypc,
2,311✔
4897
                                    const value_def* vd,
4898
                                    value_kind_t kind)
4899
{
4900
    if (vd == nullptr) {
2,311✔
4901
        auto retval = logline_value_meta(
4902
            ypc->get_path(), kind, logline_value_meta::external_column{}, this);
57✔
4903

4904
        retval.lvm_hidden = this->jlf_hide_extra;
57✔
4905
        return retval;
57✔
4906
    }
57✔
4907

4908
    auto lvm = vd->vd_meta;
2,254✔
4909

4910
    lvm.lvm_kind = kind;
2,254✔
4911
    return lvm;
2,254✔
4912
}
2,254✔
4913

4914
void
4915
external_log_format::json_append(const log_format_file_state& lffs,
8,790✔
4916
                                 const json_format_element& jfe,
4917
                                 const value_def* vd,
4918
                                 const string_fragment& sf)
4919
{
4920
    if (jfe.jfe_align == json_format_element::align_t::RIGHT) {
8,790✔
4921
        auto sf_width = sf.column_width();
343✔
4922
        if (sf_width < jfe.jfe_min_width) {
343✔
4923
            this->json_append_to_cache(jfe.jfe_min_width - sf_width);
×
4924
        } else if (jfe.jfe_auto_width && vd != nullptr
32✔
4925
                   && sf_width
375✔
4926
                       < lffs.lffs_value_stats[vd->vd_meta.lvm_values_index
32✔
4927
                                                   .value()]
32✔
4928
                             .lvs_width)
32✔
4929
        {
4930
            this->json_append_to_cache(
8✔
4931
                lffs.lffs_value_stats[vd->vd_meta.lvm_values_index.value()]
8✔
4932
                    .lvs_width
8✔
4933
                - sf_width);
8✔
4934
        }
4935
    }
4936
    this->json_append_to_cache(sf.data(), sf.length());
8,790✔
4937
    if ((jfe.jfe_align == json_format_element::align_t::LEFT
8,790✔
4938
         || jfe.jfe_align == json_format_element::align_t::NONE)
8,732✔
4939
        && (jfe.jfe_min_width > 0 || jfe.jfe_auto_width))
8,447✔
4940
    {
4941
        auto sf_width = sf.column_width();
1,198✔
4942
        if (sf_width < jfe.jfe_min_width) {
1,198✔
4943
            this->json_append_to_cache(jfe.jfe_min_width - sf_width);
369✔
4944
        } else if (jfe.jfe_auto_width && vd != nullptr
704✔
4945
                   && sf_width
1,533✔
4946
                       < lffs.lffs_value_stats[vd->vd_meta.lvm_values_index
644✔
4947
                                                   .value()]
644✔
4948
                             .lvs_width)
644✔
4949
        {
UNCOV
4950
            this->json_append_to_cache(
×
NEW
4951
                lffs.lffs_value_stats[vd->vd_meta.lvm_values_index.value()]
×
UNCOV
4952
                    .lvs_width
×
UNCOV
4953
                - sf_width);
×
4954
        }
4955
    }
4956
}
8,790✔
4957

4958
intern_string_t
4959
external_log_format::get_pattern_name(const pattern_locks& pl,
50✔
4960
                                      uint64_t line_number) const
4961
{
4962
    if (this->elf_type != elf_type_t::ELF_TYPE_TEXT) {
50✔
4963
        static auto structured = intern_string::lookup("structured");
4964

4965
        return structured;
×
4966
    }
4967
    auto pat_index = pl.pattern_index_for_line(line_number);
50✔
4968
    return this->elf_pattern_order[pat_index]->p_name;
50✔
4969
}
4970

4971
std::string
NEW
4972
log_format::get_pattern_path(const pattern_locks& pl,
×
4973
                             uint64_t line_number) const
4974
{
NEW
4975
    auto pat_index = pl.pattern_index_for_line(line_number);
×
4976
    return fmt::format(FMT_STRING("builtin ({})"), pat_index);
×
4977
}
4978

4979
intern_string_t
4980
log_format::get_pattern_name(const pattern_locks& pl,
6✔
4981
                             uint64_t line_number) const
4982
{
4983
    char pat_str[128];
4984

4985
    auto pat_index = pl.pattern_index_for_line(line_number);
6✔
4986
    auto to_n_res = fmt::format_to_n(
×
4987
        pat_str, sizeof(pat_str) - 1, FMT_STRING("builtin ({})"), pat_index);
18✔
4988
    pat_str[to_n_res.size] = '\0';
6✔
4989
    return intern_string::lookup(pat_str);
12✔
4990
}
4991

4992
std::shared_ptr<log_format>
4993
log_format::find_root_format(const char* name)
1,262✔
4994
{
4995
    auto& fmts = get_root_formats();
1,262✔
4996
    for (auto& lf : fmts) {
47,663✔
4997
        if (lf->get_name() == name) {
47,663✔
4998
            return lf;
1,262✔
4999
        }
5000
    }
5001
    return nullptr;
×
5002
}
5003

5004
exttm
5005
log_format::tm_for_display(logfile::iterator ll, string_fragment sf)
383✔
5006
{
5007
    auto adjusted_time = ll->get_timeval();
383✔
5008
    exttm retval;
383✔
5009

5010
    retval.et_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(
383✔
5011
                         std::chrono::microseconds{adjusted_time.tv_usec})
×
5012
                         .count();
383✔
5013
    if (this->lf_timestamp_flags & ETF_NANOS_SET) {
383✔
5014
        timeval actual_tv;
5015
        exttm tm;
×
5016
        if (this->lf_date_time.scan(sf.data(),
×
5017
                                    sf.length(),
×
5018
                                    this->get_timestamp_formats(),
5019
                                    &tm,
5020
                                    actual_tv,
5021
                                    false))
5022
        {
5023
            adjusted_time.tv_usec = actual_tv.tv_usec;
×
5024
            retval.et_nsec = tm.et_nsec;
×
5025
        }
5026
    }
5027
    gmtime_r(&adjusted_time.tv_sec, &retval.et_tm);
383✔
5028
    retval.et_flags = this->lf_timestamp_flags;
383✔
5029
    if (this->lf_timestamp_flags & ETF_ZONE_SET
383✔
5030
        && this->lf_date_time.dts_zoned_to_local)
293✔
5031
    {
5032
        retval.et_flags &= ~ETF_Z_IS_UTC;
293✔
5033
    }
5034
    retval.et_gmtoff = this->lf_date_time.dts_local_offset_cache;
383✔
5035

5036
    return retval;
766✔
5037
}
5038

5039
pattern_for_lines::pattern_for_lines(uint32_t pfl_line, uint32_t pfl_pat_index)
2,855✔
5040
    : pfl_line(pfl_line), pfl_pat_index(pfl_pat_index)
2,855✔
5041
{
5042
}
2,855✔
5043

5044
void
5045
logline_value_stats::merge(const logline_value_stats& other)
15,014✔
5046
{
5047
    if (other.lvs_count == 0) {
15,014✔
5048
        return;
13,895✔
5049
    }
5050

5051
    require(other.lvs_min_value <= other.lvs_max_value);
1,119✔
5052

5053
    if (other.lvs_width > this->lvs_width) {
1,119✔
5054
        this->lvs_width = other.lvs_width;
761✔
5055
    }
5056
    if (other.lvs_min_value < this->lvs_min_value) {
1,119✔
5057
        this->lvs_min_value = other.lvs_min_value;
773✔
5058
    }
5059
    if (other.lvs_max_value > this->lvs_max_value) {
1,119✔
5060
        this->lvs_max_value = other.lvs_max_value;
1,050✔
5061
    }
5062
    this->lvs_count += other.lvs_count;
1,119✔
5063
    this->lvs_total += other.lvs_total;
1,119✔
5064

5065
    ensure(this->lvs_count >= 0);
1,119✔
5066
    ensure(this->lvs_min_value <= this->lvs_max_value);
1,119✔
5067
}
5068

5069
void
5070
logline_value_stats::add_value(double value)
28,680✔
5071
{
5072
    if (value < this->lvs_min_value) {
28,680✔
5073
        this->lvs_min_value = value;
1,262✔
5074
    }
5075
    if (value > this->lvs_max_value) {
28,680✔
5076
        this->lvs_max_value = value;
1,918✔
5077
    }
5078
    this->lvs_count += 1;
28,680✔
5079
    this->lvs_total += value;
28,680✔
5080
}
28,680✔
5081

5082
std::vector<logline_value_meta>
5083
external_log_format::get_value_metadata() const
8,138✔
5084
{
5085
    std::vector<logline_value_meta> retval;
8,138✔
5086

5087
    for (const auto& vd : this->elf_value_def_order) {
82,632✔
5088
        retval.emplace_back(vd->vd_meta);
74,494✔
5089
    }
5090

5091
    return retval;
8,138✔
5092
}
×
5093

5094
std::optional<size_t>
5095
external_log_format::stats_index_for_value(const intern_string_t& name) const
71✔
5096
{
5097
    const auto iter = this->elf_value_defs.find(name);
71✔
5098
    if (iter != this->elf_value_defs.end()
71✔
5099
        && iter->second->vd_meta.lvm_values_index)
71✔
5100
    {
5101
        return iter->second->vd_meta.lvm_values_index.value();
71✔
5102
    }
5103

NEW
5104
    return std::nullopt;
×
5105
}
5106

5107
std::string
5108
external_log_format::get_pattern_regex(const pattern_locks& pl,
9✔
5109
                                       uint64_t line_number) const
5110
{
5111
    if (this->elf_type != elf_type_t::ELF_TYPE_TEXT) {
9✔
5112
        return "";
2✔
5113
    }
5114
    auto pat_index = pl.pattern_index_for_line(line_number);
8✔
5115
    return this->elf_pattern_order[pat_index]->p_pcre.pp_value->get_pattern();
8✔
5116
}
5117

5118
bool
5119
external_log_format::hide_field(const intern_string_t field_name, bool val)
12✔
5120
{
5121
    const auto vd_iter = this->elf_value_defs.find(field_name);
12✔
5122
    if (vd_iter == this->elf_value_defs.end()) {
12✔
5123
        log_warning("field to hide not found: %s.%s",
1✔
5124
                    this->elf_name.c_str(),
5125
                    field_name.c_str());
5126
        return false;
1✔
5127
    }
5128

5129
    vd_iter->second->vd_meta.lvm_user_hidden = val;
11✔
5130
    if (this->elf_type == elf_type_t::ELF_TYPE_JSON) {
11✔
5131
        bool found = false;
2✔
5132

5133
        if (!field_name.to_string_fragment().find('#')) {
2✔
5134
            for (const auto& jfe : this->jlf_line_format) {
18✔
5135
                if (jfe.jfe_value.pp_value == field_name) {
17✔
5136
                    log_debug(
×
5137
                        "hide-field not triggering rebuild since it is in "
5138
                        "line-format: %s.%s",
5139
                        this->elf_name.c_str(),
5140
                        field_name.c_str());
5141
                    found = true;
×
5142
                    break;
×
5143
                }
5144
            }
5145
        }
5146
        if (!found) {
2✔
5147
            log_info("format field %s.%s changed, rebuilding",
2✔
5148
                     this->elf_name.get(),
5149
                     field_name.get());
5150
            this->elf_value_defs_state->vds_generation += 1;
2✔
5151
        }
5152
    }
5153
    return true;
11✔
5154
}
5155

5156
bool
5157
external_log_format::format_changed()
2,564✔
5158
{
5159
    if (this->elf_specialized_value_defs_state.vds_generation
5,128✔
5160
        != this->elf_value_defs_state->vds_generation)
2,564✔
5161
    {
5162
        this->elf_specialized_value_defs_state = *this->elf_value_defs_state;
2✔
5163
        this->jlf_cached_offset = -1;
2✔
5164
        return true;
2✔
5165
    }
5166

5167
    return false;
2,562✔
5168
}
5169

5170
bool
5171
format_tag_def::path_restriction::matches(const char* fn) const
5✔
5172
{
5173
    return fnmatch(this->p_glob.c_str(), fn, 0) == 0;
5✔
5174
}
5175

5176
bool
5177
format_partition_def::path_restriction::matches(const char* fn) const
5✔
5178
{
5179
    return fnmatch(this->p_glob.c_str(), fn, 0) == 0;
5✔
5180
}
5181

5182
int
5183
pattern_locks::pattern_index_for_line(uint64_t line_number) const
6,773✔
5184
{
5185
    if (this->pl_lines.empty()) {
6,773✔
NEW
5186
        return -1;
×
5187
    }
5188

5189
    auto iter
5190
        = std::lower_bound(this->pl_lines.cbegin(),
6,773✔
5191
                           this->pl_lines.cend(),
5192
                           line_number,
5193
                           [](const pattern_for_lines& pfl, uint32_t line) {
6,832✔
5194
                               return pfl.pfl_line < line;
6,832✔
5195
                           });
5196

5197
    if (iter == this->pl_lines.end() || iter->pfl_line != line_number) {
6,773✔
5198
        --iter;
6,146✔
5199
    }
5200

5201
    return iter->pfl_pat_index;
6,773✔
5202
}
5203

5204
/* XXX */
5205
#include "log_format_impls.cc"
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc