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

tstack / lnav / 17589970077-2502

09 Sep 2025 05:00PM UTC coverage: 65.196% (-5.0%) from 70.225%
17589970077-2502

push

github

tstack
[format] add fields for source file/line

Knowing the source file/line context in a log
message can help find log messages when using
log2src.

56 of 70 new or added lines in 2 files covered. (80.0%)

13954 existing lines in 210 files now uncovered.

45516 of 69814 relevant lines covered (65.2%)

404154.37 hits per line

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

92.34
/src/log_format_loader.cc
1
/**
2
 * Copyright (c) 2013-2016, 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
 * @file log_format_loader.cc
30
 */
31

32
#include <map>
33
#include <string>
34

35
#include "log_format_loader.hh"
36

37
#include <glob.h>
38
#include <libgen.h>
39
#include <sys/stat.h>
40

41
#include "base/auto_fd.hh"
42
#include "base/from_trait.hh"
43
#include "base/fs_util.hh"
44
#include "base/paths.hh"
45
#include "base/string_util.hh"
46
#include "bin2c.hh"
47
#include "builtin-scripts.h"
48
#include "builtin-sh-scripts.h"
49
#include "config.h"
50
#include "default-formats.h"
51
#include "file_format.hh"
52
#include "fmt/format.h"
53
#include "format.scripts.hh"
54
#include "lnav_config.hh"
55
#include "log_format_ext.hh"
56
#include "sql_execute.hh"
57
#include "sql_util.hh"
58
#include "yajlpp/yajlpp.hh"
59
#include "yajlpp/yajlpp_def.hh"
60

61
static void extract_metadata(string_fragment, struct script_metadata& meta_out);
62

63
using log_formats_map_t
64
    = std::map<intern_string_t, std::shared_ptr<external_log_format>>;
65

66
using namespace lnav::roles::literals;
67

68
static auto intern_lifetime = intern_string::get_table_lifetime();
69
static log_formats_map_t LOG_FORMATS;
70

71
struct loader_userdata {
72
    yajlpp_parse_context* ud_parse_context{nullptr};
73
    std::string ud_file_schema;
74
    std::filesystem::path ud_format_path;
75
    std::vector<intern_string_t>* ud_format_names{nullptr};
76
    std::vector<lnav::console::user_message>* ud_errors{nullptr};
77
};
78

79
static external_log_format*
80
ensure_format(const yajlpp_provider_context& ypc, loader_userdata* ud)
3,293,399✔
81
{
82
    const intern_string_t name = ypc.get_substr_i(0);
3,293,399✔
83
    auto* formats = ud->ud_format_names;
3,293,399✔
84
    auto* retval = LOG_FORMATS[name].get();
3,293,399✔
85
    if (retval == nullptr) {
3,293,399✔
86
        LOG_FORMATS[name] = std::make_shared<external_log_format>(name);
46,785✔
87
        retval = LOG_FORMATS[name].get();
46,785✔
88
        log_debug("Loading format -- %s", name.get());
46,785✔
89
    }
90
    retval->elf_source_path.insert(ud->ud_format_path.parent_path().string());
3,293,399✔
91

92
    if (find(formats->begin(), formats->end(), name) == formats->end()) {
3,293,399✔
93
        formats->push_back(name);
46,879✔
94
    }
95

96
    if (!ud->ud_format_path.empty()) {
3,293,399✔
97
        const intern_string_t i_src_path
98
            = intern_string::lookup(ud->ud_format_path.string());
39,764✔
99
        auto srcs_iter = retval->elf_format_sources.find(i_src_path);
39,764✔
100
        if (srcs_iter == retval->elf_format_sources.end()) {
39,764✔
101
            retval->elf_format_source_order.emplace_back(ud->ud_format_path);
1,141✔
102
            retval->elf_format_sources[i_src_path]
2,282✔
103
                = ud->ud_parse_context->get_line_number();
1,141✔
104
        }
105
    }
106

107
    if (ud->ud_format_path.empty()) {
3,293,399✔
108
        retval->elf_builtin_format = true;
3,253,635✔
109
    }
110

111
    return retval;
3,293,399✔
112
}
113

114
static external_log_format::pattern*
115
pattern_provider(const yajlpp_provider_context& ypc, external_log_format* elf)
274,077✔
116
{
117
    auto regex_name = ypc.get_substr(0);
274,077✔
118
    auto& pat = elf->elf_patterns[regex_name];
274,077✔
119

120
    if (pat.get() == nullptr) {
274,077✔
121
        pat = std::make_shared<external_log_format::pattern>();
90,666✔
122
    }
123

124
    if (pat->p_config_path.empty()) {
274,077✔
125
        pat->p_name = intern_string::lookup(regex_name);
90,666✔
126
        pat->p_config_path = fmt::format(
181,332✔
127
            FMT_STRING("/{}/regex/{}"), elf->get_name(), regex_name);
453,330✔
128
    }
129

130
    return pat.get();
548,154✔
131
}
274,077✔
132

133
static external_log_format::value_def*
134
value_def_provider(const yajlpp_provider_context& ypc, external_log_format* elf)
1,514,928✔
135
{
136
    const intern_string_t value_name = ypc.get_substr_i(0);
1,514,928✔
137

138
    auto iter = elf->elf_value_defs.find(value_name);
1,514,928✔
139
    std::shared_ptr<external_log_format::value_def> retval;
1,514,928✔
140

141
    if (iter == elf->elf_value_defs.end()) {
1,514,928✔
142
        retval = std::make_shared<external_log_format::value_def>(
388,751✔
143
            value_name,
144
            value_kind_t::VALUE_TEXT,
388,751✔
145
            logline_value_meta::external_column{},
×
146
            elf);
388,751✔
147
        elf->elf_value_defs[value_name] = retval;
388,751✔
148
        elf->elf_value_def_order.emplace_back(retval);
388,751✔
149
    } else {
150
        retval = iter->second;
1,126,177✔
151
    }
152

153
    return retval.get();
3,029,856✔
154
}
1,514,928✔
155

156
static format_tag_def*
157
format_tag_def_provider(const yajlpp_provider_context& ypc,
12,074✔
158
                        external_log_format* elf)
159
{
160
    const intern_string_t tag_name = ypc.get_substr_i(0);
12,074✔
161

162
    auto iter = elf->lf_tag_defs.find(tag_name);
12,074✔
163
    std::shared_ptr<format_tag_def> retval;
12,074✔
164

165
    if (iter == elf->lf_tag_defs.end()) {
12,074✔
166
        auto tag_with_hash = fmt::format(FMT_STRING("#{}"), tag_name);
6,528✔
167
        retval = std::make_shared<format_tag_def>(tag_with_hash);
2,176✔
168
        elf->lf_tag_defs[tag_name] = retval;
2,176✔
169
    } else {
2,176✔
170
        retval = iter->second;
9,898✔
171
    }
172

173
    return retval.get();
24,148✔
174
}
12,074✔
175

176
static format_partition_def*
177
format_partition_def_provider(const yajlpp_provider_context& ypc,
6,613✔
178
                              external_log_format* elf)
179
{
180
    const intern_string_t partition_name = ypc.get_substr_i(0);
6,613✔
181

182
    auto iter = elf->lf_partition_defs.find(partition_name);
6,613✔
183
    std::shared_ptr<format_partition_def> retval;
6,613✔
184

185
    if (iter == elf->lf_partition_defs.end()) {
6,613✔
186
        retval = std::make_shared<format_partition_def>(
1,574✔
187
            partition_name.to_string());
2,361✔
188
        elf->lf_partition_defs[partition_name] = retval;
787✔
189
    } else {
190
        retval = iter->second;
5,826✔
191
    }
192

193
    return retval.get();
13,226✔
194
}
6,613✔
195

196
static scaling_factor*
197
scaling_factor_provider(const yajlpp_provider_context& ypc,
5,544✔
198
                        external_log_format::value_def* value_def)
199
{
200
    auto scale_name = ypc.get_substr_i(0);
5,544✔
201
    auto& retval = value_def->vd_unit_scaling[scale_name];
5,544✔
202

203
    return &retval;
5,544✔
204
}
205

206
static external_log_format::json_format_element&
207
ensure_json_format_element(external_log_format* elf, int index)
165,898✔
208
{
209
    elf->jlf_line_format.resize(index + 1);
165,898✔
210

211
    return elf->jlf_line_format[index];
165,898✔
212
}
213

214
static external_log_format::json_format_element*
215
line_format_provider(const yajlpp_provider_context& ypc,
134,398✔
216
                     external_log_format* elf)
217
{
218
    auto& jfe = ensure_json_format_element(elf, ypc.ypc_index);
134,398✔
219

220
    jfe.jfe_type = external_log_format::json_log_field::VARIABLE;
134,398✔
221

222
    return &jfe;
134,398✔
223
}
224

225
static int
226
read_format_bool(yajlpp_parse_context* ypc, int val)
3,341✔
227
{
228
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
3,341✔
229
    auto field_name = ypc->get_path_fragment(1);
3,341✔
230

231
    if (field_name == "convert-to-local-time") {
3,341✔
UNCOV
232
        elf->lf_date_time.dts_local_time = val;
×
233
    } else if (field_name == "json") {
3,341✔
234
        if (val) {
3,341✔
235
            elf->elf_type = external_log_format::elf_type_t::ELF_TYPE_JSON;
2,648✔
236
        }
237
    }
238

239
    return 1;
3,341✔
240
}
3,341✔
241

242
static int
243
read_format_double(yajlpp_parse_context* ypc, double val)
1✔
244
{
245
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
1✔
246
    auto field_name = ypc->get_path_fragment(1);
1✔
247

248
    if (field_name == "timestamp-divisor") {
1✔
249
        if (val <= 0) {
1✔
250
            ypc->report_error(
2✔
UNCOV
251
                lnav::console::user_message::error(
×
252
                    attr_line_t()
2✔
253
                        .append_quoted(fmt::to_string(val))
2✔
254
                        .append(" is not a valid value for ")
1✔
255
                        .append_quoted(lnav::roles::symbol(
2✔
256
                            ypc->get_full_path().to_string())))
2✔
257
                    .with_reason("value cannot be less than or equal to zero")
2✔
258
                    .with_snippet(ypc->get_snippet())
2✔
259
                    .with_help(ypc->ypc_current_handler->get_help_text(ypc)));
1✔
260
        }
261
        elf->elf_timestamp_divisor = val;
1✔
262
    }
263

264
    return 1;
1✔
265
}
1✔
266

267
static int
268
read_format_int(yajlpp_parse_context* ypc, long long val)
787✔
269
{
270
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
787✔
271
    auto field_name = ypc->get_path_fragment(1);
787✔
272

273
    if (field_name == "timestamp-divisor") {
787✔
274
        if (val <= 0) {
787✔
UNCOV
275
            ypc->report_error(
×
UNCOV
276
                lnav::console::user_message::error(
×
277
                    attr_line_t()
×
278
                        .append_quoted(fmt::to_string(val))
×
279
                        .append(" is not a valid value for ")
×
280
                        .append_quoted(lnav::roles::symbol(
×
281
                            ypc->get_full_path().to_string())))
×
282
                    .with_reason("value cannot be less than or equal to zero")
×
283
                    .with_snippet(ypc->get_snippet())
×
284
                    .with_help(ypc->ypc_current_handler->get_help_text(ypc)));
×
285
        }
286
        elf->elf_timestamp_divisor = val;
787✔
287
    }
288

289
    return 1;
787✔
290
}
787✔
291

292
static int
293
read_format_field(yajlpp_parse_context* ypc,
87,861✔
294
                  const unsigned char* str,
295
                  size_t len,
296
                  yajl_string_props_t*)
297
{
298
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
87,861✔
299
    auto leading_slash = len > 0 && str[0] == '/';
87,861✔
300
    auto value = std::string((const char*) (leading_slash ? str + 1 : str),
301
                             leading_slash ? len - 1 : len);
87,861✔
302
    auto field_name = ypc->get_path_fragment(1);
87,861✔
303

304
    if (field_name == "timestamp-format") {
87,861✔
305
        elf->lf_timestamp_format.push_back(intern_string::lookup(value)->get());
8,508✔
306
    } else if (field_name == "module-field") {
79,353✔
307
        elf->elf_module_id_field = intern_string::lookup(value);
1,387✔
308
        elf->elf_container = true;
1,387✔
309
    }
310

311
    return 1;
87,861✔
312
}
87,861✔
313

314
static int
315
read_levels(yajlpp_parse_context* ypc,
77,890✔
316
            const unsigned char* str,
317
            size_t len,
318
            yajl_string_props_t*)
319
{
320
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
77,890✔
321
    auto regex = std::string((const char*) str, len);
77,890✔
322
    auto level_name_or_number = ypc->get_path_fragment(2);
77,890✔
323
    log_level_t level = string2level(level_name_or_number.c_str());
77,890✔
324
    auto value_frag = string_fragment::from_bytes(str, len);
77,890✔
325

326
    elf->elf_level_patterns[level].lp_pcre.pp_path = ypc->get_full_path();
77,890✔
327
    auto compile_res = lnav::pcre2pp::code::from(value_frag);
77,890✔
328
    if (compile_res.isErr()) {
77,890✔
329
        static const intern_string_t PATTERN_SRC
330
            = intern_string::lookup("pattern");
3✔
331
        auto ce = compile_res.unwrapErr();
1✔
332
        ypc->ypc_current_handler->report_error(
1✔
333
            ypc,
334
            value_frag.to_string(),
2✔
335
            lnav::console::to_user_message(PATTERN_SRC, ce));
2✔
336
    } else {
1✔
337
        elf->elf_level_patterns[level].lp_pcre.pp_value
77,889✔
338
            = compile_res.unwrap().to_shared();
155,778✔
339
    }
340

341
    return 1;
77,890✔
342
}
77,890✔
343

344
static int
345
read_level_int(yajlpp_parse_context* ypc, long long val)
11,969✔
346
{
347
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
11,969✔
348
    auto level_name_or_number = ypc->get_path_fragment(2);
11,969✔
349
    log_level_t level = string2level(level_name_or_number.c_str());
11,969✔
350

351
    elf->elf_level_pairs.emplace_back(val, level);
11,969✔
352

353
    return 1;
11,969✔
354
}
11,969✔
355

356
static int
357
read_action_def(yajlpp_parse_context* ypc,
693✔
358
                const unsigned char* str,
359
                size_t len,
360
                yajl_string_props_t*)
361
{
362
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
693✔
363
    auto action_name = ypc->get_path_fragment(2);
693✔
364
    auto field_name = ypc->get_path_fragment(3);
693✔
365
    auto val = std::string((const char*) str, len);
693✔
366

367
    elf->lf_action_defs[action_name].ad_name = action_name;
693✔
368
    if (field_name == "label") {
693✔
369
        elf->lf_action_defs[action_name].ad_label = val;
693✔
370
    }
371

372
    return 1;
693✔
373
}
693✔
374

375
static int
376
read_action_bool(yajlpp_parse_context* ypc, int val)
693✔
377
{
378
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
693✔
379
    auto action_name = ypc->get_path_fragment(2);
693✔
380
    auto field_name = ypc->get_path_fragment(3);
693✔
381

382
    elf->lf_action_defs[action_name].ad_capture_output = val;
693✔
383

384
    return 1;
693✔
385
}
693✔
386

387
static int
388
read_action_cmd(yajlpp_parse_context* ypc,
693✔
389
                const unsigned char* str,
390
                size_t len,
391
                yajl_string_props_t*)
392
{
393
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
693✔
394
    auto action_name = ypc->get_path_fragment(2);
693✔
395
    auto field_name = ypc->get_path_fragment(3);
693✔
396
    auto val = std::string((const char*) str, len);
693✔
397

398
    elf->lf_action_defs[action_name].ad_name = action_name;
693✔
399
    elf->lf_action_defs[action_name].ad_cmdline.push_back(val);
693✔
400

401
    return 1;
693✔
402
}
693✔
403

404
static external_log_format::sample_t&
405
ensure_sample(external_log_format* elf, int index)
182,612✔
406
{
407
    elf->elf_samples.resize(index + 1);
182,612✔
408

409
    return elf->elf_samples[index];
182,612✔
410
}
411

412
static external_log_format::sample_t*
413
sample_provider(const yajlpp_provider_context& ypc, external_log_format* elf)
182,612✔
414
{
415
    auto& sample = ensure_sample(elf, ypc.ypc_index);
182,612✔
416

417
    return &sample;
182,612✔
418
}
419

420
static int
421
read_json_constant(yajlpp_parse_context* ypc,
31,500✔
422
                   const unsigned char* str,
423
                   size_t len,
424
                   yajl_string_props_t*)
425
{
426
    auto val = std::string((const char*) str, len);
31,500✔
427
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
31,500✔
428

429
    ypc->ypc_array_index.back() += 1;
31,500✔
430
    auto& jfe = ensure_json_format_element(elf, ypc->ypc_array_index.back());
31,500✔
431
    jfe.jfe_type = external_log_format::json_log_field::CONSTANT;
31,500✔
432
    jfe.jfe_default_value = val;
31,500✔
433

434
    return 1;
31,500✔
435
}
31,500✔
436

437
static const struct json_path_container pattern_handlers = {
438
    yajlpp::property_handler("pattern")
439
        .with_synopsis("<message-regex>")
440
        .with_description(
441
            "The regular expression to match a log message and capture fields.")
442
        .with_min_length(1)
443
        .for_field(&external_log_format::pattern::p_pcre),
444
    yajlpp::property_handler("module-format")
445
        .with_synopsis("<bool>")
446
        .with_description(
447
            "If true, this pattern will only be used to parse message bodies "
448
            "of container formats, like syslog")
449
        .for_field(&external_log_format::pattern::p_module_format),
450
};
451

452
static constexpr json_path_handler_base::enum_value_t SUBSECOND_UNIT_ENUM[] = {
453
    {"milli"_frag, log_format::subsecond_unit::milli},
454
    {"micro"_frag, log_format::subsecond_unit::micro},
455
    {"nano"_frag, log_format::subsecond_unit::nano},
456

457
    json_path_handler_base::ENUM_TERMINATOR,
458
};
459

460
static constexpr json_path_handler_base::enum_value_t ALIGN_ENUM[] = {
461
    {"left"_frag, external_log_format::json_format_element::align_t::LEFT},
462
    {"right"_frag, external_log_format::json_format_element::align_t::RIGHT},
463

464
    json_path_handler_base::ENUM_TERMINATOR,
465
};
466

467
static constexpr json_path_handler_base::enum_value_t OVERFLOW_ENUM[] = {
468
    {"abbrev"_frag,
469
     external_log_format::json_format_element::overflow_t::ABBREV},
470
    {"truncate"_frag,
471
     external_log_format::json_format_element::overflow_t::TRUNCATE},
472
    {"dot-dot"_frag,
473
     external_log_format::json_format_element::overflow_t::DOTDOT},
474
    {"last-word"_frag,
475
     external_log_format::json_format_element::overflow_t::LASTWORD},
476

477
    json_path_handler_base::ENUM_TERMINATOR,
478
};
479

480
static constexpr json_path_handler_base::enum_value_t TRANSFORM_ENUM[] = {
481
    {"none"_frag, external_log_format::json_format_element::transform_t::NONE},
482
    {"uppercase"_frag,
483
     external_log_format::json_format_element::transform_t::UPPERCASE},
484
    {"lowercase"_frag,
485
     external_log_format::json_format_element::transform_t::LOWERCASE},
486
    {"capitalize"_frag,
487
     external_log_format::json_format_element::transform_t::CAPITALIZE},
488

489
    json_path_handler_base::ENUM_TERMINATOR,
490
};
491

492
static const json_path_container line_format_handlers = {
493
    yajlpp::property_handler("field")
494
        .with_synopsis("<field-name>")
495
        .with_description(
496
            "The name of the field to substitute at this position")
497
        .for_field(&external_log_format::json_format_element::jfe_value),
498

499
    yajlpp::property_handler("default-value")
500
        .with_synopsis("<string>")
501
        .with_description(
502
            "The default value for this position if the field is null")
503
        .for_field(
504
            &external_log_format::json_format_element::jfe_default_value),
505

506
    yajlpp::property_handler("timestamp-format")
507
        .with_synopsis("<string>")
508
        .with_min_length(1)
509
        .with_description("The strftime(3) format for this field")
510
        .for_field(&external_log_format::json_format_element::jfe_ts_format),
511

512
    yajlpp::property_handler("min-width")
513
        .with_min_value(0)
514
        .with_synopsis("<size>")
515
        .with_description("The minimum width of the field")
516
        .for_field(&external_log_format::json_format_element::jfe_min_width),
517

518
    yajlpp::property_handler("auto-width")
519
        .with_description("Automatically detect the necessary width of the "
520
                          "field based on the observed values")
521
        .for_field(&external_log_format::json_format_element::jfe_auto_width),
522

523
    yajlpp::property_handler("max-width")
524
        .with_min_value(0)
525
        .with_synopsis("<size>")
526
        .with_description("The maximum width of the field")
527
        .for_field(&external_log_format::json_format_element::jfe_max_width),
528

529
    yajlpp::property_handler("align")
530
        .with_synopsis("left|right")
531
        .with_description(
532
            "Align the text in the column to the left or right side")
533
        .with_enum_values(ALIGN_ENUM)
534
        .for_field(&external_log_format::json_format_element::jfe_align),
535

536
    yajlpp::property_handler("overflow")
537
        .with_synopsis("abbrev|truncate|dot-dot")
538
        .with_description("Overflow style")
539
        .with_enum_values(OVERFLOW_ENUM)
540
        .for_field(&external_log_format::json_format_element::jfe_overflow),
541

542
    yajlpp::property_handler("text-transform")
543
        .with_synopsis("none|uppercase|lowercase|capitalize")
544
        .with_description("Text transformation")
545
        .with_enum_values(TRANSFORM_ENUM)
546
        .for_field(
547
            &external_log_format::json_format_element::jfe_text_transform),
548

549
    yajlpp::property_handler("prefix")
550
        .with_synopsis("<str>")
551
        .with_description("Text to prepend to the value")
552
        .for_field(&external_log_format::json_format_element::jfe_prefix),
553

554
    yajlpp::property_handler("suffix")
555
        .with_synopsis("<str>")
556
        .with_description("Text to append to the value")
557
        .for_field(&external_log_format::json_format_element::jfe_suffix),
558
};
559

560
static constexpr json_path_handler_base::enum_value_t KIND_ENUM[] = {
561
    {"string"_frag, value_kind_t::VALUE_TEXT},
562
    {"integer"_frag, value_kind_t::VALUE_INTEGER},
563
    {"float"_frag, value_kind_t::VALUE_FLOAT},
564
    {"boolean"_frag, value_kind_t::VALUE_BOOLEAN},
565
    {"json"_frag, value_kind_t::VALUE_JSON},
566
    {"struct"_frag, value_kind_t::VALUE_STRUCT},
567
    {"quoted"_frag, value_kind_t::VALUE_QUOTED},
568
    {"xml"_frag, value_kind_t::VALUE_XML},
569

570
    json_path_handler_base::ENUM_TERMINATOR,
571
};
572

573
static constexpr json_path_handler_base::enum_value_t SCALE_OP_ENUM[] = {
574
    {"identity"_frag, scale_op_t::SO_IDENTITY},
575
    {"multiply"_frag, scale_op_t::SO_MULTIPLY},
576
    {"divide"_frag, scale_op_t::SO_DIVIDE},
577

578
    json_path_handler_base::ENUM_TERMINATOR,
579
};
580

581
static const struct json_path_container scaling_factor_handlers = {
582
    yajlpp::property_handler("op")
583
        .with_enum_values(SCALE_OP_ENUM)
584
        .for_field(&scaling_factor::sf_op),
585

586
    yajlpp::property_handler("value").for_field(&scaling_factor::sf_value),
587
};
588

589
static const struct json_path_container scale_handlers = {
590
    yajlpp::pattern_property_handler("(?<scale>[^/]+)")
591
        .with_obj_provider(scaling_factor_provider)
592
        .with_children(scaling_factor_handlers),
593
};
594

595
static const struct json_path_container unit_handlers = {
596
    yajlpp::property_handler("field")
597
        .with_synopsis("<field-name>")
598
        .with_description(
599
            "The name of the field that contains the units for this field")
600
        .for_field(&external_log_format::value_def::vd_unit_field),
601

602
    yajlpp::property_handler("scaling-factor")
603
        .with_description("Transforms the numeric value by the given factor")
604
        .with_children(scale_handlers),
605
};
606

607
static const struct json_path_container value_def_handlers = {
608
    yajlpp::property_handler("kind")
609
        .with_synopsis("<data-type>")
610
        .with_description("The type of data in the field")
611
        .with_enum_values(KIND_ENUM)
612
        .for_field(&external_log_format::value_def::vd_meta,
613
                   &logline_value_meta::lvm_kind),
614

615
    yajlpp::property_handler("collate")
616
        .with_synopsis("<function>")
617
        .with_description("The collating function to use for this column")
618
        .for_field(&external_log_format::value_def::vd_collate),
619

620
    yajlpp::property_handler("unit")
621
        .with_description("Unit definitions for this field")
622
        .with_children(unit_handlers),
623

624
    yajlpp::property_handler("identifier")
625
        .with_synopsis("<bool>")
626
        .with_description("Indicates whether or not this field contains an "
627
                          "identifier that should be highlighted")
628
        .for_field(&external_log_format::value_def::vd_meta,
629
                   &logline_value_meta::lvm_identifier),
630

631
    yajlpp::property_handler("foreign-key")
632
        .with_synopsis("<bool>")
633
        .with_description("Indicates whether or not this field should be "
634
                          "treated as a foreign key for row in another table")
635
        .for_field(&external_log_format::value_def::vd_meta,
636
                   &logline_value_meta::lvm_foreign_key),
637

638
    yajlpp::property_handler("hidden")
639
        .with_synopsis("<bool>")
640
        .with_description(
641
            "Indicates whether or not this field should be hidden")
642
        .for_field(&external_log_format::value_def::vd_meta,
643
                   &logline_value_meta::lvm_hidden),
644

645
    yajlpp::property_handler("action-list#")
646
        .with_synopsis("<string>")
647
        .with_description("Actions to execute when this field is clicked on")
648
        .for_field(&external_log_format::value_def::vd_action_list),
649

650
    yajlpp::property_handler("rewriter")
651
        .with_synopsis("<command>")
652
        .with_description(
653
            "A command that will rewrite this field when pretty-printing")
654
        .for_field(&external_log_format::value_def::vd_rewriter)
655
        .with_example(";SELECT :sc_status || ' (' || (SELECT message FROM "
656
                      "http_status_codes WHERE status = :sc_status) || ') '"),
657

658
    yajlpp::property_handler("description")
659
        .with_synopsis("<string>")
660
        .with_description("A description of the field")
661
        .for_field(&external_log_format::value_def::vd_description),
662
};
663

664
static const struct json_path_container highlighter_def_handlers = {
665
    yajlpp::property_handler("pattern")
666
        .with_synopsis("<regex>")
667
        .with_description(
668
            "A regular expression to highlight in logs of this format.")
669
        .for_field(&external_log_format::highlighter_def::hd_pattern),
670

671
    yajlpp::property_handler("color")
672
        .with_synopsis("#<hex>|<name>")
673
        .with_description("The color to use when highlighting this pattern.")
674
        .for_field(&external_log_format::highlighter_def::hd_color),
675

676
    yajlpp::property_handler("background-color")
677
        .with_synopsis("#<hex>|<name>")
678
        .with_description(
679
            "The background color to use when highlighting this pattern.")
680
        .for_field(&external_log_format::highlighter_def::hd_background_color),
681

682
    yajlpp::property_handler("underline")
683
        .with_synopsis("<enabled>")
684
        .with_description("Highlight this pattern with an underline.")
685
        .for_field(&external_log_format::highlighter_def::hd_underline),
686

687
    yajlpp::property_handler("blink")
688
        .with_synopsis("<enabled>")
689
        .with_description("Highlight this pattern by blinking.")
690
        .for_field(&external_log_format::highlighter_def::hd_blink),
691
};
692

693
static const json_path_handler_base::enum_value_t LEVEL_ENUM[] = {
694
    {level_names[LEVEL_TRACE], LEVEL_TRACE},
695
    {level_names[LEVEL_DEBUG5], LEVEL_DEBUG5},
696
    {level_names[LEVEL_DEBUG4], LEVEL_DEBUG4},
697
    {level_names[LEVEL_DEBUG3], LEVEL_DEBUG3},
698
    {level_names[LEVEL_DEBUG2], LEVEL_DEBUG2},
699
    {level_names[LEVEL_DEBUG], LEVEL_DEBUG},
700
    {level_names[LEVEL_INFO], LEVEL_INFO},
701
    {level_names[LEVEL_STATS], LEVEL_STATS},
702
    {level_names[LEVEL_NOTICE], LEVEL_NOTICE},
703
    {level_names[LEVEL_WARNING], LEVEL_WARNING},
704
    {level_names[LEVEL_ERROR], LEVEL_ERROR},
705
    {level_names[LEVEL_CRITICAL], LEVEL_CRITICAL},
706
    {level_names[LEVEL_FATAL], LEVEL_FATAL},
707

708
    json_path_handler_base::ENUM_TERMINATOR,
709
};
710

711
static const struct json_path_container sample_handlers = {
712
    yajlpp::property_handler("description")
713
        .with_synopsis("<text>")
714
        .with_description("A description of this sample.")
715
        .for_field(&external_log_format::sample_t::s_description),
716
    yajlpp::property_handler("line")
717
        .with_synopsis("<log-line>")
718
        .with_description(
719
            "A sample log line that should match a pattern in this format.")
720
        .for_field(&external_log_format::sample_t::s_line),
721

722
    yajlpp::property_handler("level")
723
        .with_enum_values(LEVEL_ENUM)
724
        .with_description("The expected level for this sample log line.")
725
        .for_field(&external_log_format::sample_t::s_level),
726
};
727

728
static constexpr json_path_handler_base::enum_value_t TYPE_ENUM[] = {
729
    {"text"_frag, external_log_format::elf_type_t::ELF_TYPE_TEXT},
730
    {"json"_frag, external_log_format::elf_type_t::ELF_TYPE_JSON},
731
    {"csv"_frag, external_log_format::elf_type_t::ELF_TYPE_CSV},
732

733
    json_path_handler_base::ENUM_TERMINATOR,
734
};
735

736
static const struct json_path_container regex_handlers = {
737
    yajlpp::pattern_property_handler(R"((?<pattern_name>[^/]+))")
738
        .with_description("The set of patterns used to match log messages")
739
        .with_obj_provider(pattern_provider)
740
        .with_children(pattern_handlers),
741
};
742

743
static const struct json_path_container level_handlers = {
744
    yajlpp::pattern_property_handler("(?<level>trace|debug[2345]?|info|stats|"
745
                                     "notice|warning|error|critical|fatal)")
746
        .add_cb(read_levels)
747
        .add_cb(read_level_int)
748
        .with_synopsis("<pattern|integer>")
749
        .with_description("The regular expression used to match the log text "
750
                          "for this level.  "
751
                          "For JSON logs with numeric levels, this should be "
752
                          "the number for the corresponding level."),
753
};
754

755
static const struct json_path_container value_handlers = {
756
    yajlpp::pattern_property_handler("(?<value_name>[^/]+)")
757
        .with_description(
758
            "The set of values captured by the log message patterns")
759
        .with_obj_provider(value_def_provider)
760
        .with_children(value_def_handlers),
761
};
762

763
static const struct json_path_container tag_path_handlers = {
764
    yajlpp::property_handler("glob")
765
        .with_synopsis("<glob>")
766
        .with_description("The glob to match against file paths")
767
        .with_example("*/system.log*")
768
        .for_field(&format_tag_def::path_restriction::p_glob),
769
};
770

771
static const struct json_path_container format_tag_def_handlers = {
772
    yajlpp::property_handler("paths#")
773
        .with_description("Restrict tagging to the given paths")
774
        .for_field(&format_tag_def::ftd_paths)
775
        .with_children(tag_path_handlers),
776
    yajlpp::property_handler("pattern")
777
        .with_synopsis("<regex>")
778
        .with_description("The regular expression to match against the body of "
779
                          "the log message")
780
        .with_example("\\w+ is down")
781
        .for_field(&format_tag_def::ftd_pattern),
782
    yajlpp::property_handler("description")
783
        .with_synopsis("<string>")
784
        .with_description("A description of this tag")
785
        .for_field(&format_tag_def::ftd_description),
786
    json_path_handler("level")
787
        .with_synopsis("<log-level>")
788
        .with_description("Constrain hits to log messages with this level")
789
        .with_enum_values(LEVEL_ENUM)
790
        .for_field(&format_tag_def::ftd_level),
791
};
792

793
static const struct json_path_container tag_handlers = {
794
    yajlpp::pattern_property_handler(R"((?<tag_name>[\w:;\._\-]+))")
795
        .with_description("The name of the tag to apply")
796
        .with_obj_provider(format_tag_def_provider)
797
        .with_children(format_tag_def_handlers),
798
};
799

800
static const struct json_path_container format_partition_def_handlers = {
801
    yajlpp::property_handler("paths#")
802
        .with_description("Restrict partitioning to the given paths")
803
        .for_field(&format_partition_def::fpd_paths)
804
        .with_children(tag_path_handlers),
805
    yajlpp::property_handler("pattern")
806
        .with_synopsis("<regex>")
807
        .with_description("The regular expression to match against the body of "
808
                          "the log message")
809
        .with_example("\\w+ is down")
810
        .for_field(&format_partition_def::fpd_pattern),
811
    yajlpp::property_handler("description")
812
        .with_synopsis("<string>")
813
        .with_description("A description of this partition")
814
        .for_field(&format_partition_def::fpd_description),
815
    json_path_handler("level")
816
        .with_synopsis("<log-level>")
817
        .with_description("Constrain hits to log messages with this level")
818
        .with_enum_values(LEVEL_ENUM)
819
        .for_field(&format_partition_def::fpd_level),
820
};
821

822
static const struct json_path_container partition_handlers = {
823
    yajlpp::pattern_property_handler(R"((?<partition_type>[\w:;\._\-]+))")
824
        .with_description("The type of partition to apply")
825
        .with_obj_provider(format_partition_def_provider)
826
        .with_children(format_partition_def_handlers),
827
};
828

829
static const struct json_path_container highlight_handlers = {
830
    yajlpp::pattern_property_handler(R"((?<highlight_name>[^/]+))")
831
        .with_description("The definition of a highlight")
832
        .with_obj_provider<external_log_format::highlighter_def,
833
                           external_log_format>(
834
            [](const yajlpp_provider_context& ypc, external_log_format* root) {
1,605✔
835
                auto* retval
836
                    = &(root->elf_highlighter_patterns[ypc.get_substr_i(0)]);
1,605✔
837

838
                return retval;
1,605✔
839
            })
840
        .with_children(highlighter_def_handlers),
841
};
842

843
static const struct json_path_container action_def_handlers = {
844
    json_path_handler("label", read_action_def),
845
    json_path_handler("capture-output", read_action_bool),
846
    json_path_handler("cmd#", read_action_cmd),
847
};
848

849
static const struct json_path_container action_handlers = {
850
    json_path_handler(
851
        lnav::pcre2pp::code::from_const("(?<action_name>\\w+)").to_shared(),
852
        read_action_def)
853
        .with_children(action_def_handlers),
854
};
855

856
static const struct json_path_container search_table_def_handlers = {
857
    json_path_handler("pattern")
858
        .with_synopsis("<regex>")
859
        .with_description("The regular expression for this search table.")
860
        .for_field(&external_log_format::search_table_def::std_pattern),
861
    json_path_handler("glob")
862
        .with_synopsis("<glob>")
863
        .with_description("Glob pattern used to constrain hits to messages "
864
                          "that match the given pattern.")
865
        .for_field(&external_log_format::search_table_def::std_glob),
866
    json_path_handler("level")
867
        .with_synopsis("<log-level>")
868
        .with_description("Constrain hits to log messages with this level")
869
        .with_enum_values(LEVEL_ENUM)
870
        .for_field(&external_log_format::search_table_def::std_level),
871
};
872

873
static const struct json_path_container search_table_handlers = {
874
    yajlpp::pattern_property_handler("(?<table_name>\\w+)")
875
        .with_description(
876
            "The set of search tables to be automatically defined")
877
        .with_obj_provider<external_log_format::search_table_def,
878
                           external_log_format>(
879
            [](const yajlpp_provider_context& ypc, external_log_format* root) {
29,802✔
880
                auto* retval = &(root->elf_search_tables[ypc.get_substr_i(0)]);
29,802✔
881

882
                return retval;
29,802✔
883
            })
884
        .with_children(search_table_def_handlers),
885
};
886

887
static const struct json_path_container header_expr_handlers = {
888
    yajlpp::pattern_property_handler(R"((?<header_expr_name>\w+))")
889
        .with_description("SQLite expression")
890
        .for_field(&external_log_format::header_exprs::he_exprs),
891
};
892

893
static const struct json_path_container header_handlers = {
894
    yajlpp::property_handler("expr")
895
        .with_description("The expressions used to check if a file header "
896
                          "matches this file format")
897
        .for_child(&external_log_format::header::h_exprs)
898
        .with_children(header_expr_handlers),
899
    yajlpp::property_handler("size")
900
        .with_description("The minimum size required for this header type")
901
        .for_field(&external_log_format::header::h_size),
902
};
903

904
static const struct json_path_container converter_handlers = {
905
    yajlpp::property_handler("type")
906
        .with_description("The MIME type")
907
        .for_field(&external_log_format::converter::c_type),
908
    yajlpp::property_handler("header")
909
        .with_description("File header detection definitions")
910
        .for_child(&external_log_format::converter::c_header)
911
        .with_children(header_handlers),
912
    yajlpp::property_handler("command")
913
        .with_description("The script used to convert the file")
914
        .with_pattern(R"([\w\.\-]+)")
915
        .for_field(&external_log_format::converter::c_command),
916
};
917

918
static const struct json_path_container opid_descriptor_handlers = {
919
    yajlpp::property_handler("field")
920
        .with_synopsis("<name>")
921
        .with_description("The field to include in the operation description")
922
        .for_field(&log_format::opid_descriptor::od_field),
923
    yajlpp::property_handler("extractor")
924
        .with_synopsis("<regex>")
925
        .with_description(
926
            "The regex used to extract content for the operation description")
927
        .for_field(&log_format::opid_descriptor::od_extractor),
928
    yajlpp::property_handler("prefix")
929
        .with_description(
930
            "A string to prepend to this field in the description")
931
        .for_field(&log_format::opid_descriptor::od_prefix),
932
    yajlpp::property_handler("suffix")
933
        .with_description("A string to append to this field in the description")
934
        .for_field(&log_format::opid_descriptor::od_suffix),
935
    yajlpp::property_handler("joiner")
936
        .with_description("A string to insert between instances of this field "
937
                          "when the field is found more than once")
938
        .for_field(&log_format::opid_descriptor::od_joiner),
939
};
940

941
static const struct json_path_container opid_description_format_handlers = {
942
    yajlpp::property_handler("format#")
943
        .with_description("Defines the elements of this operation description")
944
        .for_field(&log_format::opid_descriptors::od_descriptors)
945
        .with_children(opid_descriptor_handlers),
946
};
947

948
static const struct json_path_container opid_description_handlers = {
949
    yajlpp::pattern_property_handler(R"((?<opid_descriptor>[\w\.\-]+))")
950
        .with_description("A type of description for this operation")
951
        .for_field(&log_format::lf_opid_description_def)
952
        .with_children(opid_description_format_handlers),
953
};
954

955
static const struct json_path_container subid_description_handlers = {
956
    yajlpp::pattern_property_handler(R"((?<subid_descriptor>[\w\.\-]+))")
957
        .with_description("A type of description for this sub-operation")
958
        .for_field(&log_format::lf_subid_description_def)
959
        .with_children(opid_description_format_handlers),
960
};
961

962
static const struct json_path_container opid_handlers = {
963
    yajlpp::property_handler("subid")
964
        .with_description("The field that holds the ID for a sub-operation")
965
        .for_field(&external_log_format::elf_subid_field),
966
    yajlpp::property_handler("description")
967
        .with_description(
968
            "Define how to construct a description of an operation")
969
        .with_children(opid_description_handlers),
970
    yajlpp::property_handler("sub-description")
971
        .with_description(
972
            "Define how to construct a description of a sub-operation")
973
        .with_children(subid_description_handlers),
974
};
975

976
const struct json_path_container format_handlers = {
977
    yajlpp::property_handler("regex")
978
        .with_description(
979
            "The set of regular expressions used to match log messages")
980
        .with_children(regex_handlers),
981

982
    json_path_handler("json", read_format_bool)
983
        .with_description(
984
            R"(Indicates that log files are JSON-encoded (deprecated, use "file-type": "json"))"),
985
    json_path_handler("convert-to-local-time")
986
        .with_description("Indicates that displayed timestamps should "
987
                          "automatically be converted to local time")
988
        .for_field(&external_log_format::lf_date_time,
989
                   &date_time_scanner::dts_local_time),
990
    json_path_handler("hide-extra")
991
        .with_description(
992
            "Specifies whether extra values in JSON logs should be displayed")
993
        .for_field(&external_log_format::jlf_hide_extra),
994
    json_path_handler("multiline")
995
        .with_description("Indicates that log messages can span multiple lines")
996
        .for_field(&log_format::lf_multiline),
997
    json_path_handler("timestamp-divisor", read_format_double)
998
        .add_cb(read_format_int)
999
        .with_synopsis("<number>")
1000
        .with_description(
1001
            "The value to divide a numeric timestamp by in a JSON log."),
1002
    json_path_handler("file-pattern")
1003
        .with_description("A regular expression that restricts this format to "
1004
                          "log files with a matching name")
1005
        .for_field(&external_log_format::elf_filename_pcre),
1006
    json_path_handler("converter")
1007
        .with_description("Describes how the file format can be detected and "
1008
                          "converted to a log that can be understood by lnav")
1009
        .for_child(&external_log_format::elf_converter)
1010
        .with_children(converter_handlers),
1011
    json_path_handler("level-field")
1012
        .with_description(
1013
            "The name of the level field in the log message pattern")
1014
        .for_field(&external_log_format::elf_level_field),
1015
    json_path_handler("level-pointer")
1016
        .with_description("A regular-expression that matches the JSON-pointer "
1017
                          "of the level property")
1018
        .for_field(&external_log_format::elf_level_pointer),
1019
    json_path_handler("timestamp-field")
1020
        .with_description(
1021
            "The name of the timestamp field in the log message pattern")
1022
        .for_field(&log_format::lf_timestamp_field),
1023
    json_path_handler("subsecond-field")
1024
        .with_description("The path to the property in a JSON-lines log "
1025
                          "message that contains the sub-second time value")
1026
        .for_field(&log_format::lf_subsecond_field),
1027
    json_path_handler("subsecond-units")
1028
        .with_description("The units of the subsecond-field property value")
1029
        .with_enum_values(SUBSECOND_UNIT_ENUM)
1030
        .for_field(&log_format::lf_subsecond_unit),
1031
    json_path_handler("time-field")
1032
        .with_description(
1033
            "The name of the time field in the log message pattern.  This "
1034
            "field should only be specified if the timestamp field only "
1035
            "contains a date.")
1036
        .for_field(&log_format::lf_time_field),
1037
    json_path_handler("body-field")
1038
        .with_description(
1039
            "The name of the body field in the log message pattern")
1040
        .for_field(&external_log_format::elf_body_field),
1041
    json_path_handler("src-file-field")
1042
        .with_description(
1043
            "The name of the source file field in the log message pattern")
1044
        .for_field(&external_log_format::elf_src_file_field),
1045
    json_path_handler("src-line-field")
1046
        .with_description(
1047
            "The name of the source line field in the log message pattern")
1048
        .for_field(&external_log_format::elf_src_line_field),
1049
    json_path_handler("url",
1050
                      lnav::pcre2pp::code::from_const("^url#?").to_shared())
1051
        .add_cb(read_format_field)
1052
        .with_description("A URL with more information about this log format"),
1053
    json_path_handler("title", read_format_field)
1054
        .with_description("The human-readable name for this log format"),
1055
    json_path_handler("description")
1056
        .with_description("A longer description of this log format")
1057
        .for_field(&external_log_format::lf_description),
1058
    json_path_handler("timestamp-format#", read_format_field)
1059
        .with_description("An array of strptime(3)-like timestamp formats"),
1060
    json_path_handler("module-field", read_format_field)
1061
        .with_description(
1062
            "The name of the module field in the log message pattern"),
1063
    json_path_handler("opid-field")
1064
        .with_description(
1065
            "The name of the operation-id field in the log message pattern")
1066
        .for_field(&external_log_format::elf_opid_field),
1067
    yajlpp::property_handler("opid")
1068
        .with_description("Definitions related to operations found in logs")
1069
        .with_children(opid_handlers),
1070
    yajlpp::property_handler("ordered-by-time")
1071
        .with_synopsis("<bool>")
1072
        .with_description(
1073
            "Indicates that the order of messages in the file is time-based.")
1074
        .for_field(&log_format::lf_time_ordered),
1075
    yajlpp::property_handler("level")
1076
        .with_description(
1077
            "The map of level names to patterns or integer values")
1078
        .with_children(level_handlers),
1079

1080
    yajlpp::property_handler("value")
1081
        .with_description("The set of value definitions")
1082
        .with_children(value_handlers),
1083

1084
    yajlpp::property_handler("tags")
1085
        .with_description("The tags to automatically apply to log messages")
1086
        .with_children(tag_handlers),
1087

1088
    yajlpp::property_handler("partitions")
1089
        .with_description(
1090
            "The partitions to automatically apply to log messages")
1091
        .with_children(partition_handlers),
1092

1093
    yajlpp::property_handler("action").with_children(action_handlers),
1094
    yajlpp::property_handler("sample#")
1095
        .with_description("An array of sample log messages to be tested "
1096
                          "against the log message patterns")
1097
        .with_obj_provider(sample_provider)
1098
        .with_children(sample_handlers),
1099

1100
    yajlpp::property_handler("line-format#")
1101
        .with_description("The display format for JSON-encoded log messages")
1102
        .with_obj_provider(line_format_provider)
1103
        .add_cb(read_json_constant)
1104
        .with_children(line_format_handlers),
1105
    json_path_handler("search-table")
1106
        .with_description(
1107
            "Search tables to automatically define for this log format")
1108
        .with_children(search_table_handlers),
1109

1110
    yajlpp::property_handler("highlights")
1111
        .with_description("The set of highlight definitions")
1112
        .with_children(highlight_handlers),
1113

1114
    yajlpp::property_handler("file-type")
1115
        .with_synopsis("text|json|csv")
1116
        .with_description("The type of file that contains the log messages")
1117
        .with_enum_values(TYPE_ENUM)
1118
        .for_field(&external_log_format::elf_type),
1119

1120
    yajlpp::property_handler("max-unrecognized-lines")
1121
        .with_synopsis("<lines>")
1122
        .with_description("The maximum number of lines in a file to use when "
1123
                          "detecting the format")
1124
        .with_min_value(1)
1125
        .for_field(&log_format::lf_max_unrecognized_lines),
1126
};
1127

1128
static int
1129
read_id(yajlpp_parse_context* ypc,
46,785✔
1130
        const unsigned char* str,
1131
        size_t len,
1132
        yajl_string_props_t*)
1133
{
1134
    auto* ud = static_cast<loader_userdata*>(ypc->ypc_userdata);
46,785✔
1135
    auto file_id = std::string((const char*) str, len);
46,785✔
1136

1137
    ud->ud_file_schema = file_id;
46,785✔
1138
    if (SUPPORTED_FORMAT_SCHEMAS.find(file_id)
46,785✔
1139
        == SUPPORTED_FORMAT_SCHEMAS.end())
93,570✔
1140
    {
1141
        const auto* handler = ypc->ypc_current_handler;
1✔
1142
        attr_line_t notes{"expecting one of the following $schema values:"};
1✔
1143

1144
        for (const auto& schema : SUPPORTED_FORMAT_SCHEMAS) {
2✔
1145
            notes.append("\n").append(
2✔
1146
                lnav::roles::symbol(fmt::format(FMT_STRING("  {}"), schema)));
5✔
1147
        }
1148
        ypc->report_error(
2✔
UNCOV
1149
            lnav::console::user_message::error(
×
1150
                attr_line_t("'")
1✔
1151
                    .append(lnav::roles::symbol(file_id))
2✔
1152
                    .append("' is not a supported log format $schema version"))
1✔
1153
                .with_snippet(ypc->get_snippet())
2✔
1154
                .with_note(notes)
1✔
1155
                .with_help(handler->get_help_text(ypc)));
1✔
1156
    }
1✔
1157

1158
    return 1;
46,785✔
1159
}
46,785✔
1160

1161
const struct json_path_container root_format_handler = json_path_container{
1162
    json_path_handler("$schema", read_id)
1163
        .with_synopsis("The URI of the schema for this file")
1164
        .with_description("Specifies the type of this file"),
1165

1166
    yajlpp::pattern_property_handler("(?<format_name>\\w+)")
1167
        .with_description("The definition of a log file format.")
1168
        .with_obj_provider(ensure_format)
1169
        .with_children(format_handlers),
1170
}
1171
    .with_schema_id(DEFAULT_FORMAT_SCHEMA);
1172

1173
static void
1174
write_sample_file()
693✔
1175
{
1176
    const auto dstdir = lnav::paths::dotlnav();
693✔
1177
    for (const auto& bsf : lnav_format_json) {
46,431✔
1178
        auto sample_path = dstdir
1179
            / fmt::format(FMT_STRING("formats/default/{}.sample"),
137,214✔
1180
                          bsf.get_name());
91,476✔
1181

1182
        auto stat_res = lnav::filesystem::stat_file(sample_path);
45,738✔
1183
        if (stat_res.isOk()) {
45,738✔
1184
            auto st = stat_res.unwrap();
39,072✔
1185
            if (st.st_mtime >= lnav::filesystem::self_mtime()) {
39,072✔
1186
                log_debug("skipping writing sample: %s (mtimes %d >= %d)",
39,072✔
1187
                          bsf.get_name(),
1188
                          st.st_mtime,
1189
                          lnav::filesystem::self_mtime());
1190
                continue;
39,072✔
1191
            }
UNCOV
1192
            log_debug("sample file needs to be updated: %s", bsf.get_name());
×
1193
        } else {
1194
            log_debug("sample file does not exist: %s", bsf.get_name());
6,666✔
1195
        }
1196

1197
        auto sfp = bsf.to_string_fragment_producer();
6,666✔
1198
        auto write_res = lnav::filesystem::write_file(
1199
            sample_path,
1200
            *sfp,
6,666✔
1201
            {lnav::filesystem::write_file_options::read_only});
19,998✔
1202

1203
        if (write_res.isErr()) {
6,666✔
1204
            auto msg = write_res.unwrapErr();
4,488✔
1205
            fprintf(stderr,
4,488✔
1206
                    "error:unable to write default format file: %s -- %s\n",
1207
                    sample_path.c_str(),
1208
                    msg.c_str());
1209
        }
4,488✔
1210
    }
84,810✔
1211

1212
    for (const auto& bsf : lnav_sh_scripts) {
3,465✔
1213
        auto sh_path = dstdir
1214
            / fmt::format(FMT_STRING("formats/default/{}"), bsf.get_name());
11,088✔
1215
        auto stat_res = lnav::filesystem::stat_file(sh_path);
2,772✔
1216
        if (stat_res.isOk()) {
2,772✔
1217
            auto st = stat_res.unwrap();
2,368✔
1218
            if (st.st_mtime >= lnav::filesystem::self_mtime()) {
2,368✔
1219
                continue;
2,368✔
1220
            }
1221
        }
1222

1223
        auto sfp = bsf.to_string_fragment_producer();
404✔
1224
        auto write_res = lnav::filesystem::write_file(
1225
            sh_path,
1226
            *sfp,
404✔
1227
            {
1228
                lnav::filesystem::write_file_options::executable,
1229
                lnav::filesystem::write_file_options::read_only,
1230
            });
1,212✔
1231

1232
        if (write_res.isErr()) {
404✔
1233
            auto msg = write_res.unwrapErr();
272✔
1234
            fprintf(stderr,
272✔
1235
                    "error:unable to write default text file: %s -- %s\n",
1236
                    sh_path.c_str(),
1237
                    msg.c_str());
1238
        }
272✔
1239
    }
5,140✔
1240

1241
    for (const auto& bsf : lnav_scripts) {
11,088✔
1242
        script_metadata meta;
10,395✔
1243
        auto sf = bsf.to_string_fragment_producer()->to_string();
10,395✔
1244

1245
        meta.sm_name = bsf.get_name();
10,395✔
1246
        extract_metadata(sf, meta);
10,395✔
1247
        auto path
1248
            = fmt::format(FMT_STRING("formats/default/{}.lnav"), meta.sm_name);
31,185✔
1249
        auto script_path = dstdir / path;
10,395✔
1250
        auto stat_res = lnav::filesystem::stat_file(script_path);
10,395✔
1251
        if (stat_res.isOk()) {
10,395✔
1252
            auto st = stat_res.unwrap();
8,913✔
1253
            if (st.st_mtime >= lnav::filesystem::self_mtime()) {
8,913✔
1254
                continue;
8,913✔
1255
            }
1256
        }
1257

1258
        auto write_res = lnav::filesystem::write_file(
1259
            script_path,
1260
            sf,
1261
            {
1262
                lnav::filesystem::write_file_options::executable,
1263
                lnav::filesystem::write_file_options::read_only,
1264
            });
2,964✔
1265
        if (write_res.isErr()) {
1,482✔
1266
            fprintf(stderr,
1,020✔
1267
                    "error:unable to write default script file: %s -- %s\n",
1268
                    script_path.c_str(),
1269
                    strerror(errno));
1,020✔
1270
        }
1271
    }
46,047✔
1272
}
693✔
1273

1274
static void
1275
format_error_reporter(const yajlpp_parse_context& ypc,
16✔
1276
                      const lnav::console::user_message& msg)
1277
{
1278
    auto* ud = (loader_userdata*) ypc.ypc_userdata;
16✔
1279

1280
    ud->ud_errors->emplace_back(msg);
16✔
1281
}
16✔
1282

1283
std::vector<intern_string_t>
1284
load_format_file(const std::filesystem::path& filename,
1,049✔
1285
                 std::vector<lnav::console::user_message>& errors)
1286
{
1287
    std::vector<intern_string_t> retval;
1,049✔
1288
    loader_userdata ud;
1,049✔
1289
    auto_fd fd;
1,049✔
1290

1291
    log_info("loading formats from file: %s", filename.c_str());
1,049✔
1292
    yajlpp_parse_context ypc(intern_string::lookup(filename.string()),
2,098✔
1293
                             &root_format_handler);
1,049✔
1294
    ud.ud_parse_context = &ypc;
1,049✔
1295
    ud.ud_format_path = filename;
1,049✔
1296
    ud.ud_format_names = &retval;
1,049✔
1297
    ud.ud_errors = &errors;
1,049✔
1298
    ypc.ypc_userdata = &ud;
1,049✔
1299
    ypc.with_obj(ud);
1,049✔
1300
    if ((fd = lnav::filesystem::openp(filename, O_RDONLY)) == -1) {
1,049✔
UNCOV
1301
        errors.emplace_back(
×
UNCOV
1302
            lnav::console::user_message::error(
×
UNCOV
1303
                attr_line_t("unable to open format file: ")
×
UNCOV
1304
                    .append(lnav::roles::file(filename.string())))
×
1305
                .with_errno_reason());
1306
    } else {
1307
        auto_mem<yajl_handle_t> handle(yajl_free);
1,049✔
1308
        char buffer[2048];
1309
        off_t offset = 0;
1,049✔
1310
        ssize_t rc = -1;
1,049✔
1311

1312
        handle = yajl_alloc(&ypc.ypc_callbacks, nullptr, &ypc);
1,049✔
1313
        ypc.with_handle(handle).with_error_reporter(format_error_reporter);
1,049✔
1314
        yajl_config(handle, yajl_allow_comments, 1);
1,049✔
1315
        while (true) {
1316
            rc = read(fd, buffer, sizeof(buffer));
2,285✔
1317
            if (rc == 0) {
2,285✔
1318
                break;
1,048✔
1319
            }
1320
            if (rc == -1) {
1,237✔
UNCOV
1321
                errors.emplace_back(
×
UNCOV
1322
                    lnav::console::user_message::error(
×
UNCOV
1323
                        attr_line_t("unable to read format file: ")
×
UNCOV
1324
                            .append(lnav::roles::file(filename.string())))
×
1325
                        .with_errno_reason());
UNCOV
1326
                break;
×
1327
            }
1328
            if (offset == 0 && (rc > 2) && (buffer[0] == '#')
1,237✔
UNCOV
1329
                && (buffer[1] == '!'))
×
1330
            {
1331
                // Turn it into a JavaScript comment.
UNCOV
1332
                buffer[0] = buffer[1] = '/';
×
1333
            }
1334
            if (ypc.parse((const unsigned char*) buffer, rc) != yajl_status_ok)
1,237✔
1335
            {
1336
                break;
1✔
1337
            }
1338
            offset += rc;
1,236✔
1339
        }
1340
        if (rc == 0) {
1,049✔
1341
            ypc.complete_parse();
1,048✔
1342
        }
1343

1344
        if (ud.ud_file_schema.empty()) {
1,049✔
1345
            static const auto SCHEMA_LINE
1346
                = attr_line_t()
2✔
1347
                      .append(
4✔
1348
                          fmt::format(FMT_STRING("    \"$schema\": \"{}\","),
8✔
1349
                                      *SUPPORTED_FORMAT_SCHEMAS.begin()))
4✔
1350
                      .with_attr_for_all(VC_ROLE.value(role_t::VCR_QUOTED_CODE))
4✔
1351
                      .move();
4✔
1352

1353
            errors.emplace_back(
2✔
1354
                lnav::console::user_message::warning(
×
1355
                    attr_line_t("format file is missing ")
4✔
1356
                        .append_quoted("$schema"_symbol)
2✔
1357
                        .append(" property"))
2✔
1358
                    .with_snippet(lnav::console::snippet::from(
6✔
1359
                        intern_string::lookup(filename.string()), ""))
4✔
1360
                    .with_note("the schema specifies the supported format "
4✔
1361
                               "version and can be used with editors to "
1362
                               "automatically validate the file")
1363
                    .with_help(attr_line_t("add the following property to the "
4✔
1364
                                           "top-level JSON object:\n")
1365
                                   .append(SCHEMA_LINE)));
2✔
1366
        }
1367
    }
1,049✔
1368

1369
    return retval;
2,098✔
1370
}
1,049✔
1371

1372
static void
1373
load_from_path(const std::filesystem::path& path,
1,833✔
1374
               std::vector<lnav::console::user_message>& errors)
1375
{
1376
    auto format_path = path / "formats/*/*.json";
1,833✔
1377
    static_root_mem<glob_t, globfree> gl;
1,833✔
1378

1379
    log_info("loading formats from path: %s", format_path.c_str());
1,833✔
1380
    if (glob(format_path.c_str(), 0, nullptr, gl.inout()) == 0) {
1,833✔
1381
        for (int lpc = 0; lpc < (int) gl->gl_pathc; lpc++) {
1,142✔
1382
            auto filepath = std::filesystem::path(gl->gl_pathv[lpc]);
1,046✔
1383

1384
            if (startswith(filepath.filename().string(), "config.")) {
1,046✔
UNCOV
1385
                log_info("  not loading config as format: %s",
×
1386
                         filepath.c_str());
UNCOV
1387
                continue;
×
1388
            }
1389

1390
            auto format_list = load_format_file(filepath, errors);
1,046✔
1391
            if (format_list.empty()) {
1,046✔
1392
                log_warning("Empty format file: %s", filepath.c_str());
2✔
1393
            } else {
1394
                log_info("contents of format file '%s':", filepath.c_str());
1,044✔
1395
                for (auto iter = format_list.begin(); iter != format_list.end();
2,182✔
1396
                     ++iter)
1,138✔
1397
                {
1398
                    log_info("  found format: %s", iter->get());
1,138✔
1399
                }
1400
            }
1401
        }
1,046✔
1402
    }
1403
}
1,833✔
1404

1405
void
1406
load_formats(const std::vector<std::filesystem::path>& extra_paths,
693✔
1407
             std::vector<lnav::console::user_message>& errors)
1408
{
1409
    auto default_source = lnav::paths::dotlnav() / "default";
693✔
1410
    std::vector<intern_string_t> retval;
693✔
1411
    struct loader_userdata ud;
693✔
1412
    yajl_handle handle;
1413

1414
    write_sample_file();
693✔
1415

1416
    log_debug("Loading default formats");
693✔
1417
    for (const auto& bsf : lnav_format_json) {
46,431✔
1418
        yajlpp_parse_context ypc_builtin(intern_string::lookup(bsf.get_name()),
1419
                                         &root_format_handler);
45,738✔
1420
        handle = yajl_alloc(&ypc_builtin.ypc_callbacks, nullptr, &ypc_builtin);
45,738✔
1421
        ud.ud_parse_context = &ypc_builtin;
45,738✔
1422
        ud.ud_format_names = &retval;
45,738✔
1423
        ud.ud_errors = &errors;
45,738✔
1424
        ypc_builtin.with_obj(ud)
91,476✔
1425
            .with_handle(handle)
45,738✔
1426
            .with_error_reporter(format_error_reporter)
45,738✔
1427
            .ypc_userdata
1428
            = &ud;
91,476✔
1429
        yajl_config(handle, yajl_allow_comments, 1);
45,738✔
1430
        auto sf = bsf.to_string_fragment_producer();
45,738✔
1431
        ypc_builtin.parse(*sf);
45,738✔
1432
        yajl_free(handle);
45,738✔
1433
    }
45,738✔
1434

1435
    for (const auto& extra_path : extra_paths) {
2,526✔
1436
        load_from_path(extra_path, errors);
1,833✔
1437
    }
1438

1439
    uint8_t mod_counter = 0;
693✔
1440

1441
    std::vector<std::shared_ptr<external_log_format>> alpha_ordered_formats;
693✔
1442
    for (auto iter = LOG_FORMATS.begin(); iter != LOG_FORMATS.end(); ++iter) {
47,475✔
1443
        auto& elf = iter->second;
46,782✔
1444
        elf->build(errors);
46,782✔
1445

1446
        if (elf->elf_has_module_format) {
46,782✔
1447
            mod_counter += 1;
2,079✔
1448
            elf->lf_mod_index = mod_counter;
2,079✔
1449
        }
1450

1451
        for (auto& check_iter : LOG_FORMATS) {
3,214,740✔
1452
            if (iter->first == check_iter.first) {
3,167,958✔
1453
                continue;
46,782✔
1454
            }
1455

1456
            auto& check_elf = check_iter.second;
3,121,176✔
1457
            if (elf->match_samples(check_elf->elf_samples)) {
3,121,176✔
1458
                log_warning(
15,102✔
1459
                    "Format collision, format '%s' matches sample from '%s'",
1460
                    elf->get_name().get(),
1461
                    check_elf->get_name().get());
1462
                elf->elf_collision.push_back(check_elf->get_name());
15,102✔
1463
            }
1464
        }
1465

1466
        alpha_ordered_formats.push_back(elf);
46,782✔
1467
    }
1468

1469
    auto& graph_ordered_formats = external_log_format::GRAPH_ORDERED_FORMATS;
693✔
1470

1471
    while (!alpha_ordered_formats.empty()) {
8,887✔
1472
        std::vector<intern_string_t> popped_formats;
8,194✔
1473

1474
        for (auto iter = alpha_ordered_formats.begin();
8,194✔
1475
             iter != alpha_ordered_formats.end();)
90,332✔
1476
        {
1477
            auto elf = *iter;
82,138✔
1478
            if (elf->elf_collision.empty()) {
82,138✔
1479
                iter = alpha_ordered_formats.erase(iter);
46,782✔
1480
                popped_formats.push_back(elf->get_name());
46,782✔
1481
                graph_ordered_formats.push_back(elf);
46,782✔
1482
            } else {
1483
                ++iter;
35,356✔
1484
            }
1485
        }
82,138✔
1486

1487
        if (popped_formats.empty() && !alpha_ordered_formats.empty()) {
8,194✔
1488
            bool broke_cycle = false;
2,364✔
1489

1490
            log_warning("Detected a cycle...");
2,364✔
1491
            for (const auto& elf : alpha_ordered_formats) {
13,797✔
1492
                if (elf->elf_builtin_format) {
11,718✔
1493
                    log_warning("  Skipping builtin format -- %s",
11,433✔
1494
                                elf->get_name().get());
1495
                } else {
1496
                    log_warning("  Breaking cycle by picking -- %s",
285✔
1497
                                elf->get_name().get());
1498
                    elf->elf_collision.clear();
285✔
1499
                    broke_cycle = true;
285✔
1500
                    break;
285✔
1501
                }
1502
            }
1503
            if (!broke_cycle) {
2,364✔
1504
                alpha_ordered_formats.front()->elf_collision.clear();
2,079✔
1505
            }
1506
        }
1507

1508
        for (const auto& elf : alpha_ordered_formats) {
43,550✔
1509
            for (auto& popped_format : popped_formats) {
403,179✔
1510
                elf->elf_collision.remove(popped_format);
367,823✔
1511
            }
1512
        }
1513
    }
8,194✔
1514

1515
    log_info("Format order:") for (auto& graph_ordered_format :
1,386✔
1516
                                   graph_ordered_formats)
48,168✔
1517
    {
1518
        log_info("  %s", graph_ordered_format->get_name().get());
46,782✔
1519
    }
1520

1521
    auto& roots = log_format::get_root_formats();
693✔
1522
    auto iter = std::find_if(roots.begin(), roots.end(), [](const auto& elem) {
693✔
1523
        return elem->get_name() == "generic_log";
3,465✔
1524
    });
1525
    roots.insert(
693✔
1526
        iter, graph_ordered_formats.begin(), graph_ordered_formats.end());
1527
}
693✔
1528

1529
static void
1530
exec_sql_in_path(sqlite3* db,
1,787✔
1531
                 const std::map<std::string, scoped_value_t>& global_vars,
1532
                 const std::filesystem::path& path,
1533
                 std::vector<lnav::console::user_message>& errors)
1534
{
1535
    auto format_path = path / "formats/*/*.sql";
1,787✔
1536
    static_root_mem<glob_t, globfree> gl;
1,787✔
1537

1538
    log_info("executing SQL files in path: %s", format_path.c_str());
1,787✔
1539
    if (glob(format_path.c_str(), 0, nullptr, gl.inout()) == 0) {
1,787✔
1540
        for (int lpc = 0; lpc < (int) gl->gl_pathc; lpc++) {
1,257✔
1541
            auto filename = std::filesystem::path(gl->gl_pathv[lpc]);
629✔
1542
            auto read_res = lnav::filesystem::read_file(filename);
629✔
1543

1544
            if (read_res.isOk()) {
629✔
1545
                log_info("Executing SQL file: %s", filename.c_str());
629✔
1546
                auto content = read_res.unwrap();
629✔
1547

1548
                sql_execute_script(
629✔
1549
                    db, global_vars, filename.c_str(), content.c_str(), errors);
1550
            } else {
629✔
UNCOV
1551
                errors.emplace_back(
×
UNCOV
1552
                    lnav::console::user_message::error(
×
UNCOV
1553
                        attr_line_t("unable to read format file: ")
×
UNCOV
1554
                            .append(lnav::roles::file(filename.string())))
×
UNCOV
1555
                        .with_reason(read_res.unwrapErr()));
×
1556
            }
1557
        }
629✔
1558
    }
1559
}
1,787✔
1560

1561
void
1562
load_format_extra(sqlite3* db,
579✔
1563
                  const std::map<std::string, scoped_value_t>& global_vars,
1564
                  const std::vector<std::filesystem::path>& extra_paths,
1565
                  std::vector<lnav::console::user_message>& errors)
1566
{
1567
    for (const auto& extra_path : extra_paths) {
2,366✔
1568
        exec_sql_in_path(db, global_vars, extra_path, errors);
1,787✔
1569
    }
1570
}
579✔
1571

1572
static void
1573
extract_metadata(string_fragment contents, script_metadata& meta_out)
10,936✔
1574
{
1575
    static const auto SYNO_RE = lnav::pcre2pp::code::from_const(
1576
        "^#\\s+@synopsis:(.*)$", PCRE2_MULTILINE);
10,936✔
1577
    static const auto DESC_RE = lnav::pcre2pp::code::from_const(
1578
        "^#\\s+@description:(.*)$", PCRE2_MULTILINE);
10,936✔
1579
    static const auto OUTPUT_FORMAT_RE = lnav::pcre2pp::code::from_const(
1580
        "^#\\s+@output-format:\\s+(.*)$", PCRE2_MULTILINE);
10,936✔
1581

1582
    auto syno_md = SYNO_RE.create_match_data();
10,936✔
1583
    auto syno_match_res
1584
        = SYNO_RE.capture_from(contents).into(syno_md).matches().ignore_error();
10,936✔
1585
    if (syno_match_res) {
10,936✔
1586
        meta_out.sm_synopsis = syno_md[1]->trim().to_string();
10,905✔
1587
    }
1588
    auto desc_md = DESC_RE.create_match_data();
10,936✔
1589
    auto desc_match_res
1590
        = DESC_RE.capture_from(contents).into(desc_md).matches().ignore_error();
10,936✔
1591
    if (desc_match_res) {
10,936✔
1592
        meta_out.sm_description = desc_md[1]->trim().to_string();
10,905✔
1593
    }
1594

1595
    auto out_format_md = OUTPUT_FORMAT_RE.create_match_data();
10,936✔
1596
    auto out_format_res = OUTPUT_FORMAT_RE.capture_from(contents)
10,936✔
1597
                              .into(out_format_md)
10,936✔
1598
                              .matches()
21,872✔
1599
                              .ignore_error();
10,936✔
1600
    if (out_format_res) {
10,936✔
1601
        auto out_format_frag = out_format_md[1]->trim();
729✔
1602
        auto from_res = from<text_format_t>(out_format_frag);
729✔
1603
        if (from_res.isErr()) {
729✔
UNCOV
1604
            log_error("%s (%s): invalid @output-format '%.*s'",
×
1605
                      meta_out.sm_name.c_str(),
1606
                      meta_out.sm_path.c_str(),
1607
                      out_format_frag.length(),
1608
                      out_format_frag.data());
1609
        } else {
1610
            meta_out.sm_output_format = from_res.unwrap();
729✔
1611
            log_info("%s (%s): setting output format to %d",
729✔
1612
                     meta_out.sm_name.c_str(),
1613
                     meta_out.sm_path.c_str(),
1614
                     meta_out.sm_output_format);
1615
        }
1616
    }
729✔
1617

1618
    if (!meta_out.sm_synopsis.empty()) {
10,936✔
1619
        size_t space = meta_out.sm_synopsis.find(' ');
10,905✔
1620

1621
        if (space == std::string::npos) {
10,905✔
1622
            space = meta_out.sm_synopsis.size();
8,754✔
1623
        }
1624
        meta_out.sm_name = meta_out.sm_synopsis.substr(0, space);
10,905✔
1625
    }
1626
}
10,936✔
1627

1628
void
1629
extract_metadata_from_file(struct script_metadata& meta_inout)
541✔
1630
{
1631
    auto stat_res = lnav::filesystem::stat_file(meta_inout.sm_path);
541✔
1632
    if (stat_res.isErr()) {
541✔
UNCOV
1633
        log_warning("unable to open script: %s -- %s",
×
1634
                    meta_inout.sm_path.c_str(),
1635
                    stat_res.unwrapErr().c_str());
UNCOV
1636
        return;
×
1637
    }
1638

1639
    auto st = stat_res.unwrap();
541✔
1640
    if (!S_ISREG(st.st_mode)) {
541✔
UNCOV
1641
        log_warning("script is not a regular file -- %s",
×
1642
                    meta_inout.sm_path.c_str());
UNCOV
1643
        return;
×
1644
    }
1645

1646
    auto open_res = lnav::filesystem::open_file(meta_inout.sm_path, O_RDONLY);
541✔
1647
    if (open_res.isErr()) {
541✔
UNCOV
1648
        log_warning("unable to open script file: %s -- %s",
×
1649
                    meta_inout.sm_path.c_str(),
1650
                    open_res.unwrapErr().c_str());
UNCOV
1651
        return;
×
1652
    }
1653

1654
    auto fd = open_res.unwrap();
541✔
1655
    char buffer[8 * 1024];
1656
    auto rc = read(fd, buffer, sizeof(buffer));
541✔
1657
    if (rc > 0) {
541✔
1658
        extract_metadata(string_fragment::from_bytes(buffer, rc), meta_inout);
541✔
1659
    }
1660
}
541✔
1661

1662
static void
1663
find_format_in_path(const std::filesystem::path& path,
114✔
1664
                    available_scripts& scripts)
1665
{
1666
    for (const auto& format_path :
342✔
1667
         {path / "formats/*/*.lnav", path / "configs/*/*.lnav"})
684✔
1668
    {
1669
        static_root_mem<glob_t, globfree> gl;
228✔
1670

1671
        log_debug("Searching for script in path: %s", format_path.c_str());
228✔
1672
        if (glob(format_path.c_str(), 0, nullptr, gl.inout()) == 0) {
228✔
1673
            for (size_t lpc = 0; lpc < gl->gl_pathc; lpc++) {
582✔
1674
                const char* filename = basename(gl->gl_pathv[lpc]);
534✔
1675
                auto script_name = std::string(filename, strlen(filename) - 5);
534✔
1676
                struct script_metadata meta;
534✔
1677

1678
                meta.sm_path = gl->gl_pathv[lpc];
534✔
1679
                meta.sm_name = script_name;
534✔
1680
                extract_metadata_from_file(meta);
534✔
1681
                scripts.as_scripts[script_name].push_back(meta);
534✔
1682

1683
                log_info("  found script: %s", meta.sm_path.c_str());
534✔
1684
            }
534✔
1685
        }
1686
    }
570✔
1687
}
114✔
1688

1689
available_scripts
1690
find_format_scripts(const std::vector<std::filesystem::path>& extra_paths)
36✔
1691
{
1692
    available_scripts retval;
36✔
1693
    for (const auto& extra_path : extra_paths) {
150✔
1694
        find_format_in_path(extra_path, retval);
114✔
1695
    }
1696
    return retval;
36✔
UNCOV
1697
}
×
1698

1699
void
1700
load_format_vtabs(log_vtab_manager* vtab_manager,
579✔
1701
                  std::vector<lnav::console::user_message>& errors)
1702
{
1703
    auto& root_formats = LOG_FORMATS;
579✔
1704

1705
    for (auto& root_format : root_formats) {
39,331✔
1706
        root_format.second->register_vtabs(vtab_manager, errors);
38,752✔
1707
    }
1708
}
579✔
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