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

tstack / lnav / 17743879018-2517

15 Sep 2025 07:09PM UTC coverage: 64.914% (-0.04%) from 64.951%
17743879018-2517

push

github

tstack
[build] missed adding file

45705 of 70409 relevant lines covered (64.91%)

406498.12 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,318,774✔
81
{
82
    const intern_string_t name = ypc.get_substr_i(0);
3,318,774✔
83
    auto* formats = ud->ud_format_names;
3,318,774✔
84
    auto* retval = LOG_FORMATS[name].get();
3,318,774✔
85
    if (retval == nullptr) {
3,318,774✔
86
        LOG_FORMATS[name] = std::make_shared<external_log_format>(name);
46,917✔
87
        retval = LOG_FORMATS[name].get();
46,917✔
88
        log_debug("Loading format -- %s", name.get());
46,917✔
89
    }
90
    retval->elf_source_path.insert(ud->ud_format_path.parent_path().string());
3,318,774✔
91

92
    if (find(formats->begin(), formats->end(), name) == formats->end()) {
3,318,774✔
93
        formats->push_back(name);
47,011✔
94
    }
95

96
    if (!ud->ud_format_path.empty()) {
3,318,774✔
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,318,774✔
108
        retval->elf_builtin_format = true;
3,279,010✔
109
    }
110

111
    return retval;
3,318,774✔
112
}
113

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

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

124
    if (pat->p_config_path.empty()) {
274,863✔
125
        pat->p_name = intern_string::lookup(regex_name);
90,926✔
126
        pat->p_config_path = fmt::format(
181,852✔
127
            FMT_STRING("/{}/regex/{}"), elf->get_name(), regex_name);
454,630✔
128
    }
129

130
    return pat.get();
549,726✔
131
}
274,863✔
132

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

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

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

153
    return retval.get();
3,049,660✔
154
}
1,524,830✔
155

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

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

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

173
    return retval.get();
24,216✔
174
}
12,108✔
175

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

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

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

193
    return retval.get();
13,262✔
194
}
6,631✔
195

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

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

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

211
    return elf->jlf_line_format[index];
166,362✔
212
}
213

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

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

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

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

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

239
    return 1;
3,349✔
240
}
3,349✔
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✔
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)
789✔
269
{
270
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
789✔
271
    auto field_name = ypc->get_path_fragment(1);
789✔
272

273
    if (field_name == "timestamp-divisor") {
789✔
274
        if (val <= 0) {
789✔
275
            ypc->report_error(
×
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;
789✔
287
    }
288

289
    return 1;
789✔
290
}
789✔
291

292
static int
293
read_format_field(yajlpp_parse_context* ypc,
88,111✔
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();
88,111✔
299
    auto leading_slash = len > 0 && str[0] == '/';
88,111✔
300
    auto value = std::string((const char*) (leading_slash ? str + 1 : str),
301
                             leading_slash ? len - 1 : len);
88,111✔
302
    auto field_name = ypc->get_path_fragment(1);
88,111✔
303

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

311
    return 1;
88,111✔
312
}
88,111✔
313

314
static int
315
read_levels(yajlpp_parse_context* ypc,
78,108✔
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();
78,108✔
321
    auto regex = std::string((const char*) str, len);
78,108✔
322
    auto level_name_or_number = ypc->get_path_fragment(2);
78,108✔
323
    log_level_t level = string2level(level_name_or_number.c_str());
78,108✔
324
    auto value_frag = string_fragment::from_bytes(str, len);
78,108✔
325

326
    elf->elf_level_patterns[level].lp_pcre.pp_path = ypc->get_full_path();
78,108✔
327
    auto compile_res = lnav::pcre2pp::code::from(value_frag);
78,108✔
328
    if (compile_res.isErr()) {
78,108✔
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
78,107✔
338
            = compile_res.unwrap().to_shared();
156,214✔
339
    }
340

341
    return 1;
78,108✔
342
}
78,108✔
343

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

351
    elf->elf_level_pairs.emplace_back(val, level);
12,003✔
352

353
    return 1;
12,003✔
354
}
12,003✔
355

356
static int
357
read_action_def(yajlpp_parse_context* ypc,
695✔
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();
695✔
363
    auto action_name = ypc->get_path_fragment(2);
695✔
364
    auto field_name = ypc->get_path_fragment(3);
695✔
365
    auto val = std::string((const char*) str, len);
695✔
366

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

372
    return 1;
695✔
373
}
695✔
374

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

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

384
    return 1;
695✔
385
}
695✔
386

387
static int
388
read_action_cmd(yajlpp_parse_context* ypc,
695✔
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();
695✔
394
    auto action_name = ypc->get_path_fragment(2);
695✔
395
    auto field_name = ypc->get_path_fragment(3);
695✔
396
    auto val = std::string((const char*) str, len);
695✔
397

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

401
    return 1;
695✔
402
}
695✔
403

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

409
    return elf->elf_samples[index];
183,136✔
410
}
411

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

417
    return &sample;
183,136✔
418
}
419

420
static int
421
read_json_constant(yajlpp_parse_context* ypc,
31,586✔
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,586✔
427
    auto elf = (external_log_format*) ypc->ypc_obj_stack.top();
31,586✔
428

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

434
    return 1;
31,586✔
435
}
31,586✔
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,888✔
880
                auto* retval = &(root->elf_search_tables[ypc.get_substr_i(0)]);
29,888✔
881

882
                return retval;
29,888✔
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("thread-id-field")
1042
        .with_description(
1043
            "The name of the thread ID field in the log message pattern")
1044
        .for_field(&external_log_format::elf_thread_id_field),
1045
    json_path_handler("src-file-field")
1046
        .with_description(
1047
            "The name of the source file field in the log message pattern")
1048
        .for_field(&external_log_format::elf_src_file_field),
1049
    json_path_handler("src-line-field")
1050
        .with_description(
1051
            "The name of the source line field in the log message pattern")
1052
        .for_field(&external_log_format::elf_src_line_field),
1053
    json_path_handler("url",
1054
                      lnav::pcre2pp::code::from_const("^url#?").to_shared())
1055
        .add_cb(read_format_field)
1056
        .with_description("A URL with more information about this log format"),
1057
    json_path_handler("title", read_format_field)
1058
        .with_description("The human-readable name for this log format"),
1059
    json_path_handler("description")
1060
        .with_description("A longer description of this log format")
1061
        .for_field(&external_log_format::lf_description),
1062
    json_path_handler("timestamp-format#", read_format_field)
1063
        .with_description("An array of strptime(3)-like timestamp formats"),
1064
    json_path_handler("module-field", read_format_field)
1065
        .with_description(
1066
            "The name of the module field in the log message pattern"),
1067
    json_path_handler("opid-field")
1068
        .with_description(
1069
            "The name of the operation-id field in the log message pattern")
1070
        .for_field(&external_log_format::elf_opid_field),
1071
    yajlpp::property_handler("opid")
1072
        .with_description("Definitions related to operations found in logs")
1073
        .with_children(opid_handlers),
1074
    yajlpp::property_handler("ordered-by-time")
1075
        .with_synopsis("<bool>")
1076
        .with_description(
1077
            "Indicates that the order of messages in the file is time-based.")
1078
        .for_field(&log_format::lf_time_ordered),
1079
    yajlpp::property_handler("level")
1080
        .with_description(
1081
            "The map of level names to patterns or integer values")
1082
        .with_children(level_handlers),
1083

1084
    yajlpp::property_handler("value")
1085
        .with_description("The set of value definitions")
1086
        .with_children(value_handlers),
1087

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

1092
    yajlpp::property_handler("partitions")
1093
        .with_description(
1094
            "The partitions to automatically apply to log messages")
1095
        .with_children(partition_handlers),
1096

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

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

1114
    yajlpp::property_handler("highlights")
1115
        .with_description("The set of highlight definitions")
1116
        .with_children(highlight_handlers),
1117

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

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

1132
static int
1133
read_id(yajlpp_parse_context* ypc,
46,917✔
1134
        const unsigned char* str,
1135
        size_t len,
1136
        yajl_string_props_t*)
1137
{
1138
    auto* ud = static_cast<loader_userdata*>(ypc->ypc_userdata);
46,917✔
1139
    auto file_id = std::string((const char*) str, len);
46,917✔
1140

1141
    ud->ud_file_schema = file_id;
46,917✔
1142
    if (SUPPORTED_FORMAT_SCHEMAS.find(file_id)
46,917✔
1143
        == SUPPORTED_FORMAT_SCHEMAS.end())
93,834✔
1144
    {
1145
        const auto* handler = ypc->ypc_current_handler;
1✔
1146
        attr_line_t notes{"expecting one of the following $schema values:"};
1✔
1147

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

1162
    return 1;
46,917✔
1163
}
46,917✔
1164

1165
const struct json_path_container root_format_handler = json_path_container{
1166
    json_path_handler("$schema", read_id)
1167
        .with_synopsis("The URI of the schema for this file")
1168
        .with_description("Specifies the type of this file"),
1169

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

1177
static void
1178
write_sample_file()
695✔
1179
{
1180
    const auto dstdir = lnav::paths::dotlnav();
695✔
1181
    for (const auto& bsf : lnav_format_json) {
46,565✔
1182
        auto sample_path = dstdir
1183
            / fmt::format(FMT_STRING("formats/default/{}.sample"),
137,610✔
1184
                          bsf.get_name());
91,740✔
1185

1186
        auto stat_res = lnav::filesystem::stat_file(sample_path);
45,870✔
1187
        if (stat_res.isOk()) {
45,870✔
1188
            auto st = stat_res.unwrap();
39,138✔
1189
            if (st.st_mtime >= lnav::filesystem::self_mtime()) {
39,138✔
1190
                log_debug("skipping writing sample: %s (mtimes %d >= %d)",
39,138✔
1191
                          bsf.get_name(),
1192
                          st.st_mtime,
1193
                          lnav::filesystem::self_mtime());
1194
                continue;
39,138✔
1195
            }
1196
            log_debug("sample file needs to be updated: %s", bsf.get_name());
×
1197
        } else {
1198
            log_debug("sample file does not exist: %s", bsf.get_name());
6,732✔
1199
        }
1200

1201
        auto sfp = bsf.to_string_fragment_producer();
6,732✔
1202
        auto write_res = lnav::filesystem::write_file(
1203
            sample_path,
1204
            *sfp,
6,732✔
1205
            {lnav::filesystem::write_file_options::read_only});
20,196✔
1206

1207
        if (write_res.isErr()) {
6,732✔
1208
            auto msg = write_res.unwrapErr();
4,488✔
1209
            fprintf(stderr,
4,488✔
1210
                    "error:unable to write default format file: %s -- %s\n",
1211
                    sample_path.c_str(),
1212
                    msg.c_str());
1213
        }
4,488✔
1214
    }
85,008✔
1215

1216
    for (const auto& bsf : lnav_sh_scripts) {
3,475✔
1217
        auto sh_path = dstdir
1218
            / fmt::format(FMT_STRING("formats/default/{}"), bsf.get_name());
11,120✔
1219
        auto stat_res = lnav::filesystem::stat_file(sh_path);
2,780✔
1220
        if (stat_res.isOk()) {
2,780✔
1221
            auto st = stat_res.unwrap();
2,372✔
1222
            if (st.st_mtime >= lnav::filesystem::self_mtime()) {
2,372✔
1223
                continue;
2,372✔
1224
            }
1225
        }
1226

1227
        auto sfp = bsf.to_string_fragment_producer();
408✔
1228
        auto write_res = lnav::filesystem::write_file(
1229
            sh_path,
1230
            *sfp,
408✔
1231
            {
1232
                lnav::filesystem::write_file_options::executable,
1233
                lnav::filesystem::write_file_options::read_only,
1234
            });
1,224✔
1235

1236
        if (write_res.isErr()) {
408✔
1237
            auto msg = write_res.unwrapErr();
272✔
1238
            fprintf(stderr,
272✔
1239
                    "error:unable to write default text file: %s -- %s\n",
1240
                    sh_path.c_str(),
1241
                    msg.c_str());
1242
        }
272✔
1243
    }
5,152✔
1244

1245
    for (const auto& bsf : lnav_scripts) {
12,510✔
1246
        script_metadata meta;
11,815✔
1247
        auto sf = bsf.to_string_fragment_producer()->to_string();
11,815✔
1248

1249
        meta.sm_name = bsf.get_name();
11,815✔
1250
        extract_metadata(sf, meta);
11,815✔
1251
        auto path
1252
            = fmt::format(FMT_STRING("formats/default/{}.lnav"), meta.sm_name);
35,445✔
1253
        auto script_path = dstdir / path;
11,815✔
1254
        auto stat_res = lnav::filesystem::stat_file(script_path);
11,815✔
1255
        if (stat_res.isOk()) {
11,815✔
1256
            auto st = stat_res.unwrap();
10,115✔
1257
            if (st.st_mtime >= lnav::filesystem::self_mtime()) {
10,115✔
1258
                continue;
10,115✔
1259
            }
1260
        }
1261

1262
        auto write_res = lnav::filesystem::write_file(
1263
            script_path,
1264
            sf,
1265
            {
1266
                lnav::filesystem::write_file_options::executable,
1267
                lnav::filesystem::write_file_options::read_only,
1268
            });
3,400✔
1269
        if (write_res.isErr()) {
1,700✔
1270
            fprintf(stderr,
1,156✔
1271
                    "error:unable to write default script file: %s -- %s\n",
1272
                    script_path.c_str(),
1273
                    strerror(errno));
1,156✔
1274
        }
1275
    }
52,275✔
1276
}
695✔
1277

1278
static void
1279
format_error_reporter(const yajlpp_parse_context& ypc,
16✔
1280
                      const lnav::console::user_message& msg)
1281
{
1282
    auto* ud = (loader_userdata*) ypc.ypc_userdata;
16✔
1283

1284
    ud->ud_errors->emplace_back(msg);
16✔
1285
}
16✔
1286

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

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

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

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

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

1373
    return retval;
2,098✔
1374
}
1,049✔
1375

1376
static void
1377
load_from_path(const std::filesystem::path& path,
1,839✔
1378
               std::vector<lnav::console::user_message>& errors)
1379
{
1380
    auto format_path = path / "formats/*/*.json";
1,839✔
1381
    static_root_mem<glob_t, globfree> gl;
1,839✔
1382

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

1388
            if (startswith(filepath.filename().string(), "config.")) {
1,046✔
1389
                log_info("  not loading config as format: %s",
×
1390
                         filepath.c_str());
1391
                continue;
×
1392
            }
1393

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

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

1418
    write_sample_file();
695✔
1419

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

1439
    for (const auto& extra_path : extra_paths) {
2,534✔
1440
        load_from_path(extra_path, errors);
1,839✔
1441
    }
1442

1443
    uint8_t mod_counter = 0;
695✔
1444

1445
    std::vector<std::shared_ptr<external_log_format>> alpha_ordered_formats;
695✔
1446
    for (auto iter = LOG_FORMATS.begin(); iter != LOG_FORMATS.end(); ++iter) {
47,609✔
1447
        auto& elf = iter->second;
46,914✔
1448
        elf->build(errors);
46,914✔
1449

1450
        if (elf->elf_has_module_format) {
46,914✔
1451
            mod_counter += 1;
2,085✔
1452
            elf->lf_mod_index = mod_counter;
2,085✔
1453
        }
1454

1455
        for (auto& check_iter : LOG_FORMATS) {
3,223,584✔
1456
            if (iter->first == check_iter.first) {
3,176,670✔
1457
                continue;
46,914✔
1458
            }
1459

1460
            auto& check_elf = check_iter.second;
3,129,756✔
1461
            if (elf->match_samples(check_elf->elf_samples)) {
3,129,756✔
1462
                log_warning(
15,142✔
1463
                    "Format collision, format '%s' matches sample from '%s'",
1464
                    elf->get_name().get(),
1465
                    check_elf->get_name().get());
1466
                elf->elf_collision.push_back(check_elf->get_name());
15,142✔
1467
            }
1468
        }
1469

1470
        alpha_ordered_formats.push_back(elf);
46,914✔
1471
    }
1472

1473
    auto& graph_ordered_formats = external_log_format::GRAPH_ORDERED_FORMATS;
695✔
1474

1475
    while (!alpha_ordered_formats.empty()) {
8,911✔
1476
        std::vector<intern_string_t> popped_formats;
8,216✔
1477

1478
        for (auto iter = alpha_ordered_formats.begin();
8,216✔
1479
             iter != alpha_ordered_formats.end();)
90,574✔
1480
        {
1481
            auto elf = *iter;
82,358✔
1482
            if (elf->elf_collision.empty()) {
82,358✔
1483
                iter = alpha_ordered_formats.erase(iter);
46,914✔
1484
                popped_formats.push_back(elf->get_name());
46,914✔
1485
                graph_ordered_formats.push_back(elf);
46,914✔
1486
            } else {
1487
                ++iter;
35,444✔
1488
            }
1489
        }
82,358✔
1490

1491
        if (popped_formats.empty() && !alpha_ordered_formats.empty()) {
8,216✔
1492
            bool broke_cycle = false;
2,370✔
1493

1494
            log_warning("Detected a cycle...");
2,370✔
1495
            for (const auto& elf : alpha_ordered_formats) {
13,833✔
1496
                if (elf->elf_builtin_format) {
11,748✔
1497
                    log_warning("  Skipping builtin format -- %s",
11,463✔
1498
                                elf->get_name().get());
1499
                } else {
1500
                    log_warning("  Breaking cycle by picking -- %s",
285✔
1501
                                elf->get_name().get());
1502
                    elf->elf_collision.clear();
285✔
1503
                    broke_cycle = true;
285✔
1504
                    break;
285✔
1505
                }
1506
            }
1507
            if (!broke_cycle) {
2,370✔
1508
                alpha_ordered_formats.front()->elf_collision.clear();
2,085✔
1509
            }
1510
        }
1511

1512
        for (const auto& elf : alpha_ordered_formats) {
43,660✔
1513
            for (auto& popped_format : popped_formats) {
404,249✔
1514
                elf->elf_collision.remove(popped_format);
368,805✔
1515
            }
1516
        }
1517
    }
8,216✔
1518

1519
    log_info("Format order:") for (auto& graph_ordered_format :
1,390✔
1520
                                   graph_ordered_formats)
48,304✔
1521
    {
1522
        log_info("  %s", graph_ordered_format->get_name().get());
46,914✔
1523
    }
1524

1525
    auto& roots = log_format::get_root_formats();
695✔
1526
    auto iter = std::find_if(roots.begin(), roots.end(), [](const auto& elem) {
695✔
1527
        return elem->get_name() == "generic_log";
3,475✔
1528
    });
1529
    roots.insert(
695✔
1530
        iter, graph_ordered_formats.begin(), graph_ordered_formats.end());
1531
}
695✔
1532

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

1542
    log_info("executing SQL files in path: %s", format_path.c_str());
1,793✔
1543
    if (glob(format_path.c_str(), 0, nullptr, gl.inout()) == 0) {
1,793✔
1544
        for (int lpc = 0; lpc < (int) gl->gl_pathc; lpc++) {
1,261✔
1545
            auto filename = std::filesystem::path(gl->gl_pathv[lpc]);
631✔
1546
            auto read_res = lnav::filesystem::read_file(filename);
631✔
1547

1548
            if (read_res.isOk()) {
631✔
1549
                log_info("Executing SQL file: %s", filename.c_str());
631✔
1550
                auto content = read_res.unwrap();
631✔
1551

1552
                sql_execute_script(
631✔
1553
                    db, global_vars, filename.c_str(), content.c_str(), errors);
1554
            } else {
631✔
1555
                errors.emplace_back(
×
1556
                    lnav::console::user_message::error(
×
1557
                        attr_line_t("unable to read format file: ")
×
1558
                            .append(lnav::roles::file(filename.string())))
×
1559
                        .with_reason(read_res.unwrapErr()));
×
1560
            }
1561
        }
631✔
1562
    }
1563
}
1,793✔
1564

1565
void
1566
load_format_extra(sqlite3* db,
581✔
1567
                  const std::map<std::string, scoped_value_t>& global_vars,
1568
                  const std::vector<std::filesystem::path>& extra_paths,
1569
                  std::vector<lnav::console::user_message>& errors)
1570
{
1571
    for (const auto& extra_path : extra_paths) {
2,374✔
1572
        exec_sql_in_path(db, global_vars, extra_path, errors);
1,793✔
1573
    }
1574
}
581✔
1575

1576
static void
1577
extract_metadata(string_fragment contents, script_metadata& meta_out)
12,444✔
1578
{
1579
    static const auto SYNO_RE = lnav::pcre2pp::code::from_const(
1580
        "^#\\s+@synopsis:(.*)$", PCRE2_MULTILINE);
12,444✔
1581
    static const auto DESC_RE = lnav::pcre2pp::code::from_const(
1582
        "^#\\s+@description:(.*)$", PCRE2_MULTILINE);
12,444✔
1583
    static const auto OUTPUT_FORMAT_RE = lnav::pcre2pp::code::from_const(
1584
        "^#\\s+@output-format:\\s+(.*)$", PCRE2_MULTILINE);
12,444✔
1585

1586
    auto syno_md = SYNO_RE.create_match_data();
12,444✔
1587
    auto syno_match_res
1588
        = SYNO_RE.capture_from(contents).into(syno_md).matches().ignore_error();
12,444✔
1589
    if (syno_match_res) {
12,444✔
1590
        meta_out.sm_synopsis = syno_md[1]->trim().to_string();
12,413✔
1591
    }
1592
    auto desc_md = DESC_RE.create_match_data();
12,444✔
1593
    auto desc_match_res
1594
        = DESC_RE.capture_from(contents).into(desc_md).matches().ignore_error();
12,444✔
1595
    if (desc_match_res) {
12,444✔
1596
        meta_out.sm_description = desc_md[1]->trim().to_string();
12,413✔
1597
    }
1598

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

1622
    if (!meta_out.sm_synopsis.empty()) {
12,444✔
1623
        size_t space = meta_out.sm_synopsis.find(' ');
12,413✔
1624

1625
        if (space == std::string::npos) {
12,413✔
1626
            space = meta_out.sm_synopsis.size();
8,790✔
1627
        }
1628
        meta_out.sm_name = meta_out.sm_synopsis.substr(0, space);
12,413✔
1629
    }
1630
}
12,444✔
1631

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

1643
    auto st = stat_res.unwrap();
629✔
1644
    if (!S_ISREG(st.st_mode)) {
629✔
1645
        log_warning("script is not a regular file -- %s",
×
1646
                    meta_inout.sm_path.c_str());
1647
        return;
×
1648
    }
1649

1650
    auto open_res = lnav::filesystem::open_file(meta_inout.sm_path, O_RDONLY);
629✔
1651
    if (open_res.isErr()) {
629✔
1652
        log_warning("unable to open script file: %s -- %s",
×
1653
                    meta_inout.sm_path.c_str(),
1654
                    open_res.unwrapErr().c_str());
1655
        return;
×
1656
    }
1657

1658
    auto fd = open_res.unwrap();
629✔
1659
    char buffer[8 * 1024];
1660
    auto rc = read(fd, buffer, sizeof(buffer));
629✔
1661
    if (rc > 0) {
629✔
1662
        extract_metadata(string_fragment::from_bytes(buffer, rc), meta_inout);
629✔
1663
    }
1664
}
629✔
1665

1666
static void
1667
find_format_in_path(const std::filesystem::path& path,
117✔
1668
                    available_scripts& scripts)
1669
{
1670
    for (const auto& format_path :
351✔
1671
         {path / "formats/*/*.lnav", path / "configs/*/*.lnav"})
702✔
1672
    {
1673
        static_root_mem<glob_t, globfree> gl;
234✔
1674

1675
        log_debug("Searching for script in path: %s", format_path.c_str());
234✔
1676
        if (glob(format_path.c_str(), 0, nullptr, gl.inout()) == 0) {
234✔
1677
            for (size_t lpc = 0; lpc < gl->gl_pathc; lpc++) {
671✔
1678
                const char* filename = basename(gl->gl_pathv[lpc]);
622✔
1679
                auto script_name = std::string(filename, strlen(filename) - 5);
622✔
1680
                struct script_metadata meta;
622✔
1681

1682
                meta.sm_path = gl->gl_pathv[lpc];
622✔
1683
                meta.sm_name = script_name;
622✔
1684
                extract_metadata_from_file(meta);
622✔
1685
                scripts.as_scripts[script_name].push_back(meta);
622✔
1686

1687
                log_info("  found script: %s", meta.sm_path.c_str());
622✔
1688
            }
622✔
1689
        }
1690
    }
585✔
1691
}
117✔
1692

1693
available_scripts
1694
find_format_scripts(const std::vector<std::filesystem::path>& extra_paths)
37✔
1695
{
1696
    available_scripts retval;
37✔
1697
    for (const auto& extra_path : extra_paths) {
154✔
1698
        find_format_in_path(extra_path, retval);
117✔
1699
    }
1700
    return retval;
37✔
1701
}
×
1702

1703
void
1704
load_format_vtabs(log_vtab_manager* vtab_manager,
581✔
1705
                  std::vector<lnav::console::user_message>& errors)
1706
{
1707
    auto& root_formats = LOG_FORMATS;
581✔
1708

1709
    for (auto& root_format : root_formats) {
39,465✔
1710
        root_format.second->register_vtabs(vtab_manager, errors);
38,884✔
1711
    }
1712
}
581✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc