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

tstack / lnav / 23836075406-2909

01 Apr 2026 06:51AM UTC coverage: 69.084% (+0.007%) from 69.077%
23836075406-2909

push

github

tstack
[tags] only save user-provided tags in the session, not format-provided

60 of 76 new or added lines in 14 files covered. (78.95%)

2 existing lines in 2 files now uncovered.

53287 of 77134 relevant lines covered (69.08%)

535481.86 hits per line

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

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

30
#include <algorithm>
31
#include <chrono>
32
#include <functional>
33
#include <future>
34
#include <optional>
35
#include <string>
36
#include <unordered_set>
37
#include <vector>
38

39
#include "logfile_sub_source.hh"
40

41
#include <sqlite3.h>
42

43
#include "base/ansi_scrubber.hh"
44
#include "base/ansi_vars.hh"
45
#include "base/distributed_slice.hh"
46
#include "base/injector.hh"
47
#include "base/itertools.hh"
48
#include "base/string_util.hh"
49
#include "bookmarks.hh"
50
#include "bookmarks.json.hh"
51
#include "command_executor.hh"
52
#include "config.h"
53
#include "field_overlay_source.hh"
54
#include "file_collection.hh"
55
#include "hasher.hh"
56
#include "k_merge_tree.h"
57
#include "lnav_util.hh"
58
#include "log_accel.hh"
59
#include "logfile_sub_source.cfg.hh"
60
#include "logline_window.hh"
61
#include "md2attr_line.hh"
62
#include "ptimec.hh"
63
#include "scn/scan.h"
64
#include "shlex.hh"
65
#include "sql_util.hh"
66
#include "sqlitepp.client.hh"
67
#include "sqlitepp.hh"
68
#include "tlx/container/btree_set.hpp"
69
#include "vtab_module.hh"
70
#include "yajlpp/yajlpp.hh"
71
#include "yajlpp/yajlpp_def.hh"
72

73
using namespace std::chrono_literals;
74
using namespace std::string_literals;
75
using namespace lnav::roles::literals;
76

77
const DIST_SLICE(bm_types) bookmark_type_t logfile_sub_source::BM_FILES("file");
78

79
static int
80
pretty_sql_callback(exec_context& ec, sqlite3_stmt* stmt)
69✔
81
{
82
    if (!sqlite3_stmt_busy(stmt)) {
69✔
83
        return 0;
30✔
84
    }
85

86
    const auto ncols = sqlite3_column_count(stmt);
39✔
87

88
    for (int lpc = 0; lpc < ncols; lpc++) {
78✔
89
        if (!ec.ec_accumulator->empty()) {
39✔
90
            ec.ec_accumulator->append(", ");
10✔
91
        }
92

93
        const char* res = (const char*) sqlite3_column_text(stmt, lpc);
39✔
94
        if (res == nullptr) {
39✔
95
            continue;
×
96
        }
97

98
        ec.ec_accumulator->append(res);
39✔
99
    }
100

101
    for (int lpc = 0; lpc < ncols; lpc++) {
78✔
102
        const auto* colname = sqlite3_column_name(stmt, lpc);
39✔
103
        auto* raw_value = sqlite3_column_value(stmt, lpc);
39✔
104
        auto value_type = sqlite3_value_type(raw_value);
39✔
105
        scoped_value_t value;
39✔
106

107
        switch (value_type) {
39✔
108
            case SQLITE_INTEGER:
×
109
                value = (int64_t) sqlite3_value_int64(raw_value);
×
110
                break;
×
111
            case SQLITE_FLOAT:
×
112
                value = sqlite3_value_double(raw_value);
×
113
                break;
×
114
            case SQLITE_NULL:
×
115
                value = null_value_t{};
×
116
                break;
×
117
            default:
39✔
118
                value = string_fragment::from_bytes(
39✔
119
                    sqlite3_value_text(raw_value),
120
                    sqlite3_value_bytes(raw_value));
78✔
121
                break;
39✔
122
        }
123
        if (!ec.ec_local_vars.empty() && !ec.ec_dry_run) {
39✔
124
            if (sql_ident_needs_quote(colname)) {
39✔
125
                continue;
25✔
126
            }
127
            auto& vars = ec.ec_local_vars.top();
14✔
128

129
            if (vars.find(colname) != vars.end()) {
42✔
130
                continue;
10✔
131
            }
132

133
            if (value.is<string_fragment>()) {
4✔
134
                value = value.get<string_fragment>().to_string();
4✔
135
            }
136
            vars[colname] = value;
8✔
137
        }
138
    }
39✔
139
    return 0;
39✔
140
}
141

142
static std::future<std::string>
143
pretty_pipe_callback(exec_context& ec, const std::string& cmdline, auto_fd& fd)
×
144
{
145
    auto retval = std::async(std::launch::async, [&]() {
×
146
        char buffer[1024];
147
        std::ostringstream ss;
×
148
        ssize_t rc;
149

150
        while ((rc = read(fd, buffer, sizeof(buffer))) > 0) {
×
151
            ss.write(buffer, rc);
×
152
        }
153

154
        auto retval = ss.str();
×
155

156
        if (endswith(retval, "\n")) {
×
157
            retval.resize(retval.length() - 1);
×
158
        }
159

160
        return retval;
×
161
    });
×
162

163
    return retval;
×
164
}
165

166
logfile_sub_source::logfile_sub_source()
1,232✔
167
    : text_sub_source(1), lnav_config_listener(__FILE__),
168
      lss_meta_grepper(*this), lss_location_history(*this)
1,232✔
169
{
170
    this->tss_supports_filtering = true;
1,232✔
171
    this->clear_line_size_cache();
1,232✔
172
    this->clear_min_max_row_times();
1,232✔
173
}
1,232✔
174

175
std::shared_ptr<logfile>
176
logfile_sub_source::find(const char* fn, content_line_t& line_base)
29✔
177
{
178
    std::shared_ptr<logfile> retval = nullptr;
29✔
179

180
    line_base = content_line_t(0);
29✔
181
    for (auto iter = this->lss_files.begin();
29✔
182
         iter != this->lss_files.end() && retval == nullptr;
59✔
183
         ++iter)
30✔
184
    {
185
        auto& ld = *(*iter);
30✔
186
        auto* lf = ld.get_file_ptr();
30✔
187

188
        if (lf == nullptr) {
30✔
189
            continue;
×
190
        }
191
        if (strcmp(lf->get_filename_as_string().c_str(), fn) == 0) {
30✔
192
            retval = ld.get_file();
29✔
193
        } else {
194
            line_base += content_line_t(MAX_LINES_PER_FILE);
1✔
195
        }
196
    }
197

198
    return retval;
29✔
199
}
×
200

201
struct filtered_logline_cmp {
202
    filtered_logline_cmp(const logfile_sub_source& lc) : llss_controller(lc) {}
453✔
203

204
    bool operator()(const uint32_t& lhs, const uint32_t& rhs) const
205
    {
206
        auto cl_lhs = llss_controller.lss_index[lhs].value();
207
        auto cl_rhs = llss_controller.lss_index[rhs].value();
208
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
209
        const auto* ll_rhs = this->llss_controller.find_line(cl_rhs);
210

211
        if (ll_lhs == nullptr) {
212
            return true;
213
        }
214
        if (ll_rhs == nullptr) {
215
            return false;
216
        }
217
        return (*ll_lhs) < (*ll_rhs);
218
    }
219

220
    bool operator()(const uint32_t& lhs, const timeval& rhs) const
1,434✔
221
    {
222
        const auto cl_lhs = llss_controller.lss_index[lhs].value();
1,434✔
223
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
1,434✔
224

225
        if (ll_lhs == nullptr) {
1,434✔
226
            return true;
×
227
        }
228
        return (*ll_lhs) < rhs;
1,434✔
229
    }
230

231
    const logfile_sub_source& llss_controller;
232
};
233

234
std::optional<vis_line_t>
235
logfile_sub_source::find_from_time(const timeval& start) const
129✔
236
{
237
    const auto lb = std::lower_bound(this->lss_filtered_index.begin(),
129✔
238
                                     this->lss_filtered_index.end(),
239
                                     start,
240
                                     filtered_logline_cmp(*this));
241
    if (lb != this->lss_filtered_index.end()) {
129✔
242
        auto retval = std::distance(this->lss_filtered_index.begin(), lb);
98✔
243
        return vis_line_t(retval);
98✔
244
    }
245

246
    return std::nullopt;
31✔
247
}
248

249
line_info
250
logfile_sub_source::text_value_for_line(textview_curses& tc,
4,561✔
251
                                        int row,
252
                                        std::string& value_out,
253
                                        line_flags_t flags)
254
{
255
    if (this->lss_indexing_in_progress) {
4,561✔
256
        value_out = "";
×
257
        this->lss_token_al.al_attrs.clear();
×
258
        return {};
×
259
    }
260

261
    line_info retval;
4,561✔
262
    content_line_t line(0);
4,561✔
263

264
    require_ge(row, 0);
4,561✔
265
    require_lt((size_t) row, this->lss_filtered_index.size());
4,561✔
266

267
    line = this->at(vis_line_t(row));
4,561✔
268

269
    if (flags & RF_RAW) {
4,561✔
270
        auto lf = this->find(line);
32✔
271
        auto ll = lf->begin() + line;
32✔
272
        retval.li_file_range = lf->get_file_range(ll, false);
32✔
273
        retval.li_level = ll->get_msg_level();
32✔
274
        // retval.li_timestamp = ll->get_timeval();
275
        retval.li_partial = false;
32✔
276
        retval.li_utf8_scan_result.usr_has_ansi = ll->has_ansi();
32✔
277
        retval.li_utf8_scan_result.usr_message = ll->is_valid_utf() ? nullptr
32✔
278
                                                                    : "bad";
279
        // timeval start_time, end_time;
280
        // gettimeofday(&start_time, NULL);
281
        value_out = lf->read_line(lf->begin() + line)
64✔
282
                        .map([](auto sbr) { return to_string(sbr); })
64✔
283
                        .unwrapOr({});
32✔
284
        // gettimeofday(&end_time, NULL);
285
        // timeval diff = end_time - start_time;
286
        // log_debug("read time %d.%06d %s:%d", diff.tv_sec, diff.tv_usec,
287
        // lf->get_filename().c_str(), line);
288
        return retval;
32✔
289
    }
32✔
290

291
    require_false(this->lss_in_value_for_line);
4,529✔
292

293
    this->lss_in_value_for_line = true;
4,529✔
294
    this->lss_token_flags = flags;
4,529✔
295
    this->lss_token_file_data = this->find_data(line);
4,529✔
296
    this->lss_token_file = (*this->lss_token_file_data)->get_file();
4,529✔
297
    this->lss_token_line = this->lss_token_file->begin() + line;
4,529✔
298

299
    this->lss_token_al.clear();
4,529✔
300
    this->lss_token_values.clear();
4,529✔
301
    this->lss_share_manager.invalidate_refs();
4,529✔
302
    if (flags & RF_FULL) {
4,529✔
303
        shared_buffer_ref sbr;
48✔
304

305
        this->lss_token_file->read_full_message(this->lss_token_line, sbr);
48✔
306
        this->lss_token_al.al_string = to_string(sbr);
48✔
307
        if (sbr.get_metadata().m_has_ansi) {
48✔
308
            scrub_ansi_string(this->lss_token_al.al_string,
17✔
309
                              &this->lss_token_al.al_attrs);
310
            sbr.get_metadata().m_has_ansi = false;
17✔
311
        }
312
    } else {
48✔
313
        auto sub_opts = subline_options{};
4,481✔
314
        sub_opts.scrub_invalid_utf8 = false;
4,481✔
315
        this->lss_token_al.al_string
316
            = this->lss_token_file->read_line(this->lss_token_line, sub_opts)
8,962✔
317
                  .map([](auto sbr) { return to_string(sbr); })
8,962✔
318
                  .unwrapOr({});
4,481✔
319
        if (this->lss_token_line->has_ansi()) {
4,481✔
320
            scrub_ansi_string(this->lss_token_al.al_string,
24✔
321
                              &this->lss_token_al.al_attrs);
322
        }
323
    }
324
    this->lss_token_shifts.clear();
4,529✔
325

326
    auto format = this->lss_token_file->get_format_ptr();
4,529✔
327

328
    auto& sbr = this->lss_token_values.lvv_sbr;
4,529✔
329

330
    sbr.share(this->lss_share_manager,
4,529✔
331
              (char*) this->lss_token_al.al_string.c_str(),
332
              this->lss_token_al.al_string.size());
333
    format->annotate(this->lss_token_file.get(),
4,529✔
334
                     line,
335
                     this->lss_token_al.al_attrs,
4,529✔
336
                     this->lss_token_values);
4,529✔
337

338
    auto src_file_attr
339
        = find_string_attr(this->lss_token_al.al_attrs, &SA_SRC_FILE);
4,529✔
340
    if (src_file_attr != this->lss_token_al.al_attrs.end()) {
4,529✔
341
        auto lr = src_file_attr->sa_range;
34✔
342
        lr.lr_end = lr.lr_start + 1;
34✔
343
        auto break_ta = text_attrs::with_underline();
34✔
344
        this->lss_token_al.with_attr({lr, VC_STYLE.value(break_ta)})
68✔
345
            .with_attr({lr,
34✔
346
                        VC_COMMAND.value(ui_command{
136✔
347
                            source_location{},
348
                            "|lnav-src-loc-handler $mouse_button",
349
                        })});
350
        if (!this->lss_breakpoints.empty()) {
34✔
351
            if (this->lss_token_values.lvv_src_line_value) {
6✔
352
                auto h = hasher();
6✔
353
                h.update(format->get_name().to_string_fragment());
6✔
354
                h.update(this->lss_token_values.lvv_src_file_value.value());
6✔
355
                h.update(this->lss_token_values.lvv_src_line_value.value());
6✔
356

357
                auto schema = h.to_string();
6✔
358
                auto& breakpoints = this->lss_breakpoints;
6✔
359
                auto it = breakpoints.find(schema);
6✔
360
                if (it != breakpoints.end()) {
6✔
361
                    lr.lr_end = lr.lr_start;
2✔
362
                    this->lss_token_al.insert(
2✔
363
                        lr.lr_start,
2✔
364
                        it->second.bp_enabled ? ui_icon_t::breakpoint
2✔
365
                                              : ui_icon_t::disabled_breakpoint);
366
                    lr.lr_end += 2;
2✔
367
                    this->lss_token_al.with_attr(
2✔
368
                        {lr,
369
                         VC_COMMAND.value(ui_command{
6✔
370
                             source_location{},
371
                             "|lnav-breakpoint-handler $mouse_button",
372
                         })});
373
                    this->lss_token_values.shift_origins_by(lr, 2);
2✔
374
                }
375
            }
6✔
376
        }
377
    }
378

379
    value_out = this->lss_token_al.al_string;
4,529✔
380

381
    for (const auto& hl : format->lf_highlighters) {
5,556✔
382
        auto hl_range = line_range{0, -1};
1,027✔
383
        auto value_iter = this->lss_token_values.lvv_values.end();
1,027✔
384
        if (!hl.h_field.empty()) {
1,027✔
385
            value_iter = std::find_if(this->lss_token_values.lvv_values.begin(),
215✔
386
                                      this->lss_token_values.lvv_values.end(),
387
                                      logline_value_name_cmp(&hl.h_field));
388
            if (value_iter == this->lss_token_values.lvv_values.end()) {
215✔
389
                continue;
204✔
390
            }
391
            hl_range = value_iter->lv_origin;
11✔
392
        }
393
        if (hl.annotate(this->lss_token_al, hl_range)
823✔
394
            && value_iter != this->lss_token_values.lvv_values.end())
823✔
395
        {
396
            value_iter->lv_highlighted = true;
7✔
397
        }
398
    }
399

400
    for (const auto& hl : this->lss_highlighters) {
4,546✔
401
        auto hl_range = line_range{0, -1};
17✔
402
        auto value_iter = this->lss_token_values.lvv_values.end();
17✔
403
        if (!hl.h_field.empty()) {
17✔
404
            value_iter = std::find_if(this->lss_token_values.lvv_values.begin(),
17✔
405
                                      this->lss_token_values.lvv_values.end(),
406
                                      logline_value_name_cmp(&hl.h_field));
407
            if (value_iter == this->lss_token_values.lvv_values.end()) {
17✔
408
                continue;
3✔
409
            }
410
            hl_range = value_iter->lv_origin;
14✔
411
        }
412
        if (hl.annotate(this->lss_token_al, hl_range)
14✔
413
            && value_iter != this->lss_token_values.lvv_values.end())
14✔
414
        {
415
            value_iter->lv_highlighted = true;
9✔
416
        }
417
    }
418

419
    if (flags & RF_REWRITE) {
4,529✔
420
        exec_context ec(
421
            &this->lss_token_values, pretty_sql_callback, pretty_pipe_callback);
48✔
422
        std::string rewritten_line;
48✔
423
        db_label_source rewrite_label_source;
48✔
424

425
        ec.with_perms(exec_context::perm_t::READ_ONLY);
48✔
426
        ec.ec_local_vars.push(std::map<std::string, scoped_value_t>());
48✔
427
        ec.ec_top_line = vis_line_t(row);
48✔
428
        ec.ec_label_source_stack.push_back(&rewrite_label_source);
48✔
429
        add_ansi_vars(ec.ec_global_vars);
48✔
430
        add_global_vars(ec);
48✔
431
        format->rewrite(ec, sbr, this->lss_token_al.al_attrs, rewritten_line);
48✔
432
        this->lss_token_al.al_string.assign(rewritten_line);
48✔
433
        value_out = this->lss_token_al.al_string;
48✔
434
    }
48✔
435

436
    {
437
        auto lr = line_range{0, (int) this->lss_token_al.al_string.length()};
4,529✔
438
        this->lss_token_al.al_attrs.emplace_back(lr, SA_ORIGINAL_LINE.value());
4,529✔
439
    }
440

441
    // Replace VALUE_TIMESTAMP fields right-to-left so that origins
442
    // for earlier fields remain valid.
443
    if (format->lf_date_time.dts_fmt_lock != -1) {
4,529✔
444
        for (auto lv_iter = this->lss_token_values.lvv_values.rbegin();
4,456✔
445
             lv_iter != this->lss_token_values.lvv_values.rend();
46,444✔
446
             ++lv_iter)
41,988✔
447
        {
448
            if (lv_iter->lv_meta.lvm_kind != value_kind_t::VALUE_TIMESTAMP
41,988✔
449
                || !lv_iter->lv_origin.is_valid())
41,988✔
450
            {
451
                continue;
41,337✔
452
            }
453

454
            auto ts_str = lv_iter->to_string();
651✔
455
            value_out.replace(lv_iter->lv_origin.lr_start,
651✔
456
                              lv_iter->lv_origin.length(),
651✔
457
                              ts_str);
458
            auto shift = (int) (ts_str.size() - lv_iter->lv_origin.length());
651✔
459
            if (shift != 0) {
651✔
460
                this->lss_token_shifts.emplace_back(lv_iter->lv_origin.lr_start,
2✔
461
                                                    shift);
462
            }
463
        }
651✔
464
    }
465

466
    std::optional<exttm> adjusted_tm;
4,529✔
467
    auto time_attr
468
        = find_string_attr(this->lss_token_al.al_attrs, &L_TIMESTAMP);
4,529✔
469
    if (!this->lss_token_line->is_continued() && !format->lf_formatted_lines
7,157✔
470
        && (this->lss_token_file->is_time_adjusted()
2,383✔
471
            || ((format->lf_timestamp_flags & ETF_ZONE_SET
2,368✔
472
                 || format->lf_date_time.dts_default_zone != nullptr)
1,428✔
473
                && format->lf_date_time.dts_zoned_to_local)
1,024✔
474
            || format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
1,344✔
475
            || !(format->lf_timestamp_flags & ETF_DAY_SET)
1,344✔
476
            || !(format->lf_timestamp_flags & ETF_MONTH_SET))
1,323✔
477
        && format->lf_date_time.dts_fmt_lock != -1)
7,157✔
478
    {
479
        if (time_attr != this->lss_token_al.al_attrs.end()) {
1,005✔
480
            const auto time_range = time_attr->sa_range;
1,004✔
481
            const auto time_sf
482
                = string_fragment::from_str_range(this->lss_token_al.al_string,
1,004✔
483
                                                  time_range.lr_start,
1,004✔
484
                                                  time_range.lr_end);
1,004✔
485
            adjusted_tm = format->tm_for_display(this->lss_token_line, time_sf);
1,004✔
486

487
            char buffer[128];
488
            const char* fmt;
489
            ssize_t len;
490

491
            if (format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
1,004✔
492
                || !(format->lf_timestamp_flags & ETF_DAY_SET)
370✔
493
                || !(format->lf_timestamp_flags & ETF_MONTH_SET))
364✔
494
            {
495
                if (format->lf_timestamp_flags & ETF_NANOS_SET) {
640✔
496
                    fmt = "%Y-%m-%d %H:%M:%S.%N";
×
497
                } else if (format->lf_timestamp_flags & ETF_MICROS_SET) {
640✔
498
                    fmt = "%Y-%m-%d %H:%M:%S.%f";
635✔
499
                } else if (format->lf_timestamp_flags & ETF_MILLIS_SET) {
5✔
500
                    fmt = "%Y-%m-%d %H:%M:%S.%L";
2✔
501
                } else {
502
                    fmt = "%Y-%m-%d %H:%M:%S";
3✔
503
                }
504
                len = ftime_fmt(
640✔
505
                    buffer, sizeof(buffer), fmt, adjusted_tm.value());
640✔
506
            } else {
507
                len = format->lf_date_time.ftime(
364✔
508
                    buffer,
509
                    sizeof(buffer),
510
                    format->get_timestamp_formats(),
511
                    adjusted_tm.value());
364✔
512
            }
513

514
            value_out.replace(
2,008✔
515
                time_range.lr_start, time_range.length(), buffer, len);
1,004✔
516
            this->lss_token_shifts.emplace_back(
2,008✔
517
                time_range.lr_start, (int) (len - time_range.length()));
1,004✔
518
        }
519
    }
520

521
    // Insert space for the file/search-hit markers.
522
    value_out.insert(0, 1, ' ');
4,529✔
523
    this->lss_time_column_size = 0;
4,529✔
524
    if (this->lss_line_context == line_context_t::time_column) {
4,529✔
525
        if (time_attr != this->lss_token_al.al_attrs.end()) {
×
526
            const char* fmt;
527
            if (this->lss_all_timestamp_flags
×
528
                & (ETF_MICROS_SET | ETF_NANOS_SET))
×
529
            {
530
                fmt = "%H:%M:%S.%f";
×
531
            } else if (this->lss_all_timestamp_flags & ETF_MILLIS_SET) {
×
532
                fmt = "%H:%M:%S.%L";
×
533
            } else {
534
                fmt = "%H:%M:%S";
×
535
            }
536
            if (!adjusted_tm) {
×
537
                const auto time_range = time_attr->sa_range;
×
538
                const auto time_sf = string_fragment::from_str_range(
×
539
                    this->lss_token_al.al_string,
×
540
                    time_range.lr_start,
×
541
                    time_range.lr_end);
×
542
                adjusted_tm
543
                    = format->tm_for_display(this->lss_token_line, time_sf);
×
544
            }
545
            adjusted_tm->et_flags |= this->lss_all_timestamp_flags
×
546
                & (ETF_MILLIS_SET | ETF_MICROS_SET | ETF_NANOS_SET);
×
547
            char buffer[128];
548
            this->lss_time_column_size
549
                = ftime_fmt(buffer, sizeof(buffer), fmt, adjusted_tm.value());
×
550
            if (this->tss_view->is_selectable()
×
551
                && this->tss_view->get_selection() == row)
×
552
            {
553
                buffer[this->lss_time_column_size] = ' ';
×
554
                buffer[this->lss_time_column_size + 1] = ' ';
×
555
                this->lss_time_column_size += 2;
×
556
            } else {
557
                constexpr char block[] = "\u258c ";
×
558

559
                strcpy(&buffer[this->lss_time_column_size], block);
×
560
                this->lss_time_column_size += sizeof(block) - 1;
×
561
            }
562
            if (time_attr->sa_range.lr_start != 0) {
×
563
                buffer[this->lss_time_column_size] = ' ';
×
564
                this->lss_time_column_size += 1;
×
565
                this->lss_time_column_padding = 1;
×
566
            } else {
567
                this->lss_time_column_padding = 0;
×
568
            }
569
            value_out.insert(1, buffer, this->lss_time_column_size);
×
570
            this->lss_token_al.al_attrs.emplace_back(time_attr->sa_range,
×
571
                                                     SA_REPLACED.value());
×
572
        }
573
        if (format->lf_level_hideable) {
×
574
            auto level_attr
575
                = find_string_attr(this->lss_token_al.al_attrs, &L_LEVEL);
×
576
            if (level_attr != this->lss_token_al.al_attrs.end()) {
×
577
                this->lss_token_al.al_attrs.emplace_back(level_attr->sa_range,
×
578
                                                         SA_REPLACED.value());
×
579
            }
580
        }
581
    } else if (this->lss_line_context < line_context_t::none) {
4,529✔
582
        size_t file_offset_end;
583
        std::string name;
×
584
        if (this->lss_line_context == line_context_t::filename) {
×
585
            file_offset_end = this->lss_filename_width;
×
586
            name = fmt::to_string(this->lss_token_file->get_filename());
×
587
            if (file_offset_end < name.size()) {
×
588
                file_offset_end = name.size();
×
589
                this->lss_filename_width = name.size();
×
590
            }
591
        } else {
592
            file_offset_end = this->lss_basename_width;
×
593
            name = fmt::to_string(this->lss_token_file->get_unique_path());
×
594
            if (file_offset_end < name.size()) {
×
595
                file_offset_end = name.size();
×
596
                this->lss_basename_width = name.size();
×
597
            }
598
        }
599
        value_out.insert(0, file_offset_end - name.size(), ' ');
×
600
        value_out.insert(0, name);
×
601
    }
602

603
    if (this->tas_display_time_offset) {
4,529✔
604
        auto row_vl = vis_line_t(row);
210✔
605
        auto relstr = this->get_time_offset_for_line(tc, row_vl);
210✔
606
        value_out = fmt::format(FMT_STRING("{: >12}|{}"), relstr, value_out);
840✔
607
    }
210✔
608

609
    this->lss_in_value_for_line = false;
4,529✔
610

611
    return retval;
4,529✔
612
}
613

614
void
615
logfile_sub_source::text_attrs_for_line(textview_curses& lv,
4,529✔
616
                                        int row,
617
                                        string_attrs_t& value_out)
618
{
619
    if (this->lss_indexing_in_progress) {
4,529✔
620
        return;
×
621
    }
622

623
    auto& vc = view_colors::singleton();
4,529✔
624
    logline* next_line = nullptr;
4,529✔
625
    line_range lr;
4,529✔
626
    int time_offset_end = 0;
4,529✔
627
    text_attrs attrs;
4,529✔
628
    auto* format = this->lss_token_file->get_format_ptr();
4,529✔
629

630
    if ((row + 1) < (int) this->lss_filtered_index.size()) {
4,529✔
631
        next_line = this->find_line(this->at(vis_line_t(row + 1)));
4,325✔
632
    }
633

634
    if (next_line != nullptr
4,529✔
635
        && (day_num(next_line->get_time<std::chrono::seconds>().count())
8,854✔
636
            > day_num(this->lss_token_line->get_time<std::chrono::seconds>()
8,854✔
637
                          .count())))
638
    {
639
        attrs |= text_attrs::style::underline;
17✔
640
    }
641

642
    const auto& line_values = this->lss_token_values;
4,529✔
643

644
    lr.lr_start = 0;
4,529✔
645
    lr.lr_end = -1;
4,529✔
646
    this->lss_token_al.al_attrs.emplace_back(
4,529✔
647
        lr, SA_LEVEL.value(this->lss_token_line->get_msg_level()));
9,058✔
648

649
    lr.lr_start = time_offset_end;
4,529✔
650
    lr.lr_end = -1;
4,529✔
651

652
    if (!attrs.empty()) {
4,529✔
653
        this->lss_token_al.al_attrs.emplace_back(lr, VC_STYLE.value(attrs));
17✔
654
    }
655

656
    if (this->lss_token_line->get_msg_level() == log_level_t::LEVEL_INVALID) {
4,529✔
657
        for (auto& token_attr : this->lss_token_al.al_attrs) {
82✔
658
            if (token_attr.sa_type != &SA_INVALID) {
59✔
659
                continue;
52✔
660
            }
661

662
            this->lss_token_al.al_attrs.emplace_back(
14✔
663
                token_attr.sa_range, VC_ROLE.value(role_t::VCR_INVALID_MSG));
7✔
664
        }
665
    }
666

667
    for (const auto& line_value : line_values.lvv_values) {
46,639✔
668
        if ((!(this->lss_token_flags & RF_FULL)
94,233✔
669
             && line_value.lv_sub_offset
83,792✔
670
                 != this->lss_token_line->get_sub_offset())
41,896✔
671
            || !line_value.lv_origin.is_valid())
84,006✔
672
        {
673
            continue;
10,013✔
674
        }
675

676
        if (line_value.lv_meta.is_hidden()) {
32,097✔
677
            this->lss_token_al.al_attrs.emplace_back(
324✔
678
                line_value.lv_origin, SA_HIDDEN.value(ui_icon_t::hidden));
162✔
679
        }
680

681
        if (!line_value.lv_meta.lvm_identifier
82,477✔
682
            || !line_value.lv_origin.is_valid())
32,097✔
683
        {
684
            continue;
18,283✔
685
        }
686

687
        if (line_value.lv_highlighted) {
13,814✔
688
            continue;
7✔
689
        }
690

691
        this->lss_token_al.al_attrs.emplace_back(
27,614✔
692
            line_value.lv_origin, VC_ROLE.value(role_t::VCR_IDENTIFIER));
13,807✔
693
    }
694

695
    // Apply shifts right-to-left so positions remain valid.
696
    std::stable_sort(
4,529✔
697
        this->lss_token_shifts.begin(),
698
        this->lss_token_shifts.end(),
699
        [](const auto& a, const auto& b) { return a.first > b.first; });
2✔
700
    for (const auto& shift : this->lss_token_shifts) {
5,535✔
701
        shift_string_attrs(
1,006✔
702
            this->lss_token_al.al_attrs, shift.first + 1, shift.second);
1,006✔
703
    }
704

705
    shift_string_attrs(this->lss_token_al.al_attrs, 0, 1);
4,529✔
706

707
    lr.lr_start = 0;
4,529✔
708
    lr.lr_end = 1;
4,529✔
709
    {
710
        auto& bm = lv.get_bookmarks();
4,529✔
711
        const auto& bv = bm[&BM_FILES];
4,529✔
712
        bool is_first_for_file = bv.bv_tree.exists(vis_line_t(row));
4,529✔
713
        bool is_last_for_file = bv.bv_tree.exists(vis_line_t(row + 1));
4,529✔
714
        auto graph = NCACS_VLINE;
4,529✔
715
        if (is_first_for_file) {
4,529✔
716
            if (is_last_for_file) {
245✔
717
                graph = NCACS_HLINE;
8✔
718
            } else {
719
                graph = NCACS_ULCORNER;
237✔
720
            }
721
        } else if (is_last_for_file) {
4,284✔
722
            graph = NCACS_LLCORNER;
23✔
723
        }
724
        this->lss_token_al.al_attrs.emplace_back(lr, VC_GRAPHIC.value(graph));
4,529✔
725

726
        if (!(this->lss_token_flags & RF_FULL)) {
4,529✔
727
            const auto& bv_search = bm[&textview_curses::BM_SEARCH];
4,481✔
728

729
            if (bv_search.bv_tree.exists(vis_line_t(row))) {
4,481✔
730
                lr.lr_start = 0;
9✔
731
                lr.lr_end = 1;
9✔
732
                this->lss_token_al.al_attrs.emplace_back(
9✔
733
                    lr, VC_STYLE.value(text_attrs::with_reverse()));
18✔
734
            }
735
        }
736
    }
737

738
    this->lss_token_al.al_attrs.emplace_back(
4,529✔
739
        lr,
740
        VC_STYLE.value(
9,058✔
741
            vc.attrs_for_ident(this->lss_token_file->get_filename())));
9,058✔
742

743
    if (this->lss_line_context < line_context_t::none) {
4,529✔
744
        size_t file_offset_end
×
745
            = (this->lss_line_context == line_context_t::filename)
×
746
            ? this->lss_filename_width
×
747
            : this->lss_basename_width;
748

749
        shift_string_attrs(this->lss_token_al.al_attrs, 0, file_offset_end);
×
750

751
        lr.lr_start = 0;
×
752
        lr.lr_end = file_offset_end + 1;
×
753
        this->lss_token_al.al_attrs.emplace_back(
×
754
            lr,
755
            VC_STYLE.value(
×
756
                vc.attrs_for_ident(this->lss_token_file->get_filename())));
×
757
    } else if (this->lss_time_column_size > 0) {
4,529✔
758
        shift_string_attrs(
×
759
            this->lss_token_al.al_attrs, 1, this->lss_time_column_size);
×
760

761
        ui_icon_t icon;
762
        switch (this->lss_token_line->get_msg_level()) {
×
763
            case LEVEL_TRACE:
×
764
                icon = ui_icon_t::log_level_trace;
×
765
                break;
×
766
            case LEVEL_DEBUG:
×
767
            case LEVEL_DEBUG2:
768
            case LEVEL_DEBUG3:
769
            case LEVEL_DEBUG4:
770
            case LEVEL_DEBUG5:
771
                icon = ui_icon_t::log_level_debug;
×
772
                break;
×
773
            case LEVEL_INFO:
×
774
                icon = ui_icon_t::log_level_info;
×
775
                break;
×
776
            case LEVEL_STATS:
×
777
                icon = ui_icon_t::log_level_stats;
×
778
                break;
×
779
            case LEVEL_NOTICE:
×
780
                icon = ui_icon_t::log_level_notice;
×
781
                break;
×
782
            case LEVEL_WARNING:
×
783
                icon = ui_icon_t::log_level_warning;
×
784
                break;
×
785
            case LEVEL_ERROR:
×
786
                icon = ui_icon_t::log_level_error;
×
787
                break;
×
788
            case LEVEL_CRITICAL:
×
789
                icon = ui_icon_t::log_level_critical;
×
790
                break;
×
791
            case LEVEL_FATAL:
×
792
                icon = ui_icon_t::log_level_fatal;
×
793
                break;
×
794
            default:
×
795
                icon = ui_icon_t::hidden;
×
796
                break;
×
797
        }
798
        auto extra_space_size = this->lss_time_column_padding;
×
799
        lr.lr_start = 1 + this->lss_time_column_size - 1 - extra_space_size;
×
800
        lr.lr_end = 1 + this->lss_time_column_size - extra_space_size;
×
801
        this->lss_token_al.al_attrs.emplace_back(lr, VC_ICON.value(icon));
×
802
        if (this->tss_view->is_selectable()
×
803
            && this->tss_view->get_selection() != row)
×
804
        {
805
            lr.lr_start = 1;
×
806
            lr.lr_end = 1 + this->lss_time_column_size - 2 - extra_space_size;
×
807
            this->lss_token_al.al_attrs.emplace_back(
×
808
                lr, VC_ROLE.value(role_t::VCR_TIME_COLUMN));
×
809
            if (this->lss_token_line->is_time_skewed()) {
×
810
                this->lss_token_al.al_attrs.emplace_back(
×
811
                    lr, VC_ROLE.value(role_t::VCR_SKEWED_TIME));
×
812
            }
813
            lr.lr_start = 1 + this->lss_time_column_size - 2 - extra_space_size;
×
814
            lr.lr_end = 1 + this->lss_time_column_size - 1 - extra_space_size;
×
815
            this->lss_token_al.al_attrs.emplace_back(
×
816
                lr, VC_ROLE.value(role_t::VCR_TIME_COLUMN_TO_TEXT));
×
817
        }
818
    }
819

820
    if (this->tas_display_time_offset) {
4,529✔
821
        time_offset_end = 13;
210✔
822
        lr.lr_start = 0;
210✔
823
        lr.lr_end = time_offset_end;
210✔
824

825
        shift_string_attrs(this->lss_token_al.al_attrs, 0, time_offset_end);
210✔
826

827
        this->lss_token_al.al_attrs.emplace_back(
210✔
828
            lr, VC_ROLE.value(role_t::VCR_OFFSET_TIME));
420✔
829
        this->lss_token_al.al_attrs.emplace_back(line_range(12, 13),
210✔
830
                                                 VC_GRAPHIC.value(NCACS_VLINE));
420✔
831

832
        auto bar_role = role_t::VCR_NONE;
210✔
833

834
        switch (this->get_line_accel_direction(vis_line_t(row))) {
210✔
835
            case log_accel::direction_t::A_STEADY:
126✔
836
                break;
126✔
837
            case log_accel::direction_t::A_DECEL:
42✔
838
                bar_role = role_t::VCR_DIFF_DELETE;
42✔
839
                break;
42✔
840
            case log_accel::direction_t::A_ACCEL:
42✔
841
                bar_role = role_t::VCR_DIFF_ADD;
42✔
842
                break;
42✔
843
        }
844
        if (bar_role != role_t::VCR_NONE) {
210✔
845
            this->lss_token_al.al_attrs.emplace_back(line_range(12, 13),
84✔
846
                                                     VC_ROLE.value(bar_role));
168✔
847
        }
848
    }
849

850
    lr.lr_start = 0;
4,529✔
851
    lr.lr_end = -1;
4,529✔
852
    this->lss_token_al.al_attrs.emplace_back(
4,529✔
853
        lr, L_FILE.value(this->lss_token_file));
9,058✔
854
    this->lss_token_al.al_attrs.emplace_back(
4,529✔
855
        lr, SA_FORMAT.value(format->get_name()));
9,058✔
856

857
    {
858
        auto line_meta_context = this->get_bookmark_metadata_context(
4,529✔
859
            vis_line_t(row + 1), bookmark_metadata::categories::partition);
860
        if (line_meta_context.bmc_current_metadata) {
4,529✔
861
            lr.lr_start = 0;
9✔
862
            lr.lr_end = -1;
9✔
863
            this->lss_token_al.al_attrs.emplace_back(
9✔
864
                lr,
865
                L_PARTITION.value(
18✔
866
                    line_meta_context.bmc_current_metadata.value()));
867
        }
868

869
        auto line_meta_opt = this->find_bookmark_metadata(vis_line_t(row));
4,529✔
870

871
        if (line_meta_opt) {
4,529✔
872
            lr.lr_start = 0;
26✔
873
            lr.lr_end = -1;
26✔
874
            this->lss_token_al.al_attrs.emplace_back(
26✔
875
                lr, L_META.value(line_meta_opt.value()));
52✔
876
        }
877
    }
878

879
    if (this->lss_time_column_size == 0) {
4,529✔
880
        if (this->lss_token_file->is_time_adjusted()) {
4,529✔
881
            auto time_range = find_string_attr_range(
17✔
882
                this->lss_token_al.al_attrs, &L_TIMESTAMP);
17✔
883

884
            if (time_range.lr_end != -1) {
17✔
885
                this->lss_token_al.al_attrs.emplace_back(
15✔
886
                    time_range, VC_ROLE.value(role_t::VCR_ADJUSTED_TIME));
30✔
887
            }
888
        } else if (this->lss_token_line->is_time_skewed()) {
4,512✔
889
            auto time_range = find_string_attr_range(
8✔
890
                this->lss_token_al.al_attrs, &L_TIMESTAMP);
8✔
891

892
            if (time_range.lr_end != -1) {
8✔
893
                this->lss_token_al.al_attrs.emplace_back(
8✔
894
                    time_range, VC_ROLE.value(role_t::VCR_SKEWED_TIME));
16✔
895
            }
896
        }
897
    }
898

899
    if (this->tss_preview_min_log_level
4,529✔
900
        && this->lss_token_line->get_msg_level()
4,529✔
901
            < this->tss_preview_min_log_level)
4,529✔
902
    {
903
        auto color = styling::color_unit::from_palette(
×
904
            lnav::enums::to_underlying(ansi_color::red));
×
905
        this->lss_token_al.al_attrs.emplace_back(line_range{0, 1},
×
906
                                                 VC_BACKGROUND.value(color));
×
907
    }
908
    if (this->ttt_preview_min_time
4,529✔
909
        && this->lss_token_line->get_timeval() < this->ttt_preview_min_time)
4,529✔
910
    {
911
        auto color = styling::color_unit::from_palette(
×
912
            lnav::enums::to_underlying(ansi_color::red));
×
913
        this->lss_token_al.al_attrs.emplace_back(line_range{0, 1},
×
914
                                                 VC_BACKGROUND.value(color));
×
915
    }
916
    if (this->ttt_preview_max_time
4,529✔
917
        && this->ttt_preview_max_time < this->lss_token_line->get_timeval())
4,529✔
918
    {
919
        auto color = styling::color_unit::from_palette(
×
920
            lnav::enums::to_underlying(ansi_color::red));
×
921
        this->lss_token_al.al_attrs.emplace_back(line_range{0, 1},
×
922
                                                 VC_BACKGROUND.value(color));
×
923
    }
924
    if (!this->lss_token_line->is_continued()) {
4,529✔
925
        if (this->lss_preview_filter_stmt != nullptr) {
2,628✔
926
            auto color = styling::color_unit::EMPTY;
×
927
            auto eval_res
928
                = this->eval_sql_filter(this->lss_preview_filter_stmt.in(),
929
                                        this->lss_token_file_data,
930
                                        this->lss_token_line);
×
931
            if (eval_res.isErr()) {
×
932
                color = palette_color{
×
933
                    lnav::enums::to_underlying(ansi_color::yellow)};
×
934
                this->lss_token_al.al_attrs.emplace_back(
×
935
                    line_range{0, -1},
×
936
                    SA_ERROR.value(
×
937
                        eval_res.unwrapErr().to_attr_line().get_string()));
×
938
            } else {
939
                auto matched = eval_res.unwrap();
×
940

941
                if (matched) {
×
942
                    color = palette_color{
×
943
                        lnav::enums::to_underlying(ansi_color::green)};
×
944
                } else {
945
                    color = palette_color{
×
946
                        lnav::enums::to_underlying(ansi_color::red)};
×
947
                    this->lss_token_al.al_attrs.emplace_back(
×
948
                        line_range{0, 1},
×
949
                        VC_STYLE.value(text_attrs::with_blink()));
×
950
                }
951
            }
952
            this->lss_token_al.al_attrs.emplace_back(
×
953
                line_range{0, 1}, VC_BACKGROUND.value(color));
×
954
        }
955

956
        auto sql_filter_opt = this->get_sql_filter();
2,628✔
957
        if (sql_filter_opt) {
2,628✔
958
            auto* sf = (sql_filter*) sql_filter_opt.value().get();
36✔
959
            auto eval_res = this->eval_sql_filter(sf->sf_filter_stmt.in(),
960
                                                  this->lss_token_file_data,
961
                                                  this->lss_token_line);
36✔
962
            if (eval_res.isErr()) {
36✔
963
                auto msg = fmt::format(
964
                    FMT_STRING(
×
965
                        "filter expression evaluation failed with -- {}"),
966
                    eval_res.unwrapErr().to_attr_line().get_string());
×
967
                auto cu = styling::color_unit::from_palette(palette_color{
×
968
                    lnav::enums::to_underlying(ansi_color::yellow)});
969
                this->lss_token_al.al_attrs.emplace_back(line_range{0, -1},
×
970
                                                         SA_ERROR.value(msg));
×
971
                this->lss_token_al.al_attrs.emplace_back(
×
972
                    line_range{0, 1}, VC_BACKGROUND.value(cu));
×
973
            }
974
        }
36✔
975
    }
2,628✔
976

977
    value_out = std::move(this->lss_token_al.al_attrs);
4,529✔
978
}
979

980
struct logline_cmp {
981
    explicit logline_cmp(logfile_sub_source& lc) : llss_controller(lc) {}
1,256✔
982

983
    bool operator()(const logfile_sub_source::indexed_content& lhs,
106,951✔
984
                    const logfile_sub_source::indexed_content& rhs) const
985
    {
986
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
106,951✔
987
        const auto* ll_rhs = this->llss_controller.find_line(rhs.value());
106,951✔
988

989
        return (*ll_lhs) < (*ll_rhs);
106,951✔
990
    }
991

992
    bool operator()(const logfile_sub_source::indexed_content& lhs,
×
993
                    const timeval& rhs) const
994
    {
995
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
×
996

997
        return *ll_lhs < rhs;
×
998
    }
999

1000
    logfile_sub_source& llss_controller;
1001
};
1002

1003
logfile_sub_source::rebuild_result
1004
logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
4,801✔
1005
{
1006
    if (this->tss_view == nullptr) {
4,801✔
1007
        return rebuild_result::rr_no_change;
124✔
1008
    }
1009

1010
    this->lss_indexing_in_progress = true;
4,677✔
1011
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
4,677✔
1012

1013
    iterator iter;
4,677✔
1014
    size_t total_lines = 0;
4,677✔
1015
    size_t est_remaining_lines = 0;
4,677✔
1016
    auto all_time_ordered_formats = true;
4,677✔
1017
    auto full_sort = this->lss_index.empty();
4,677✔
1018
    int file_count = 0;
4,677✔
1019
    auto force = std::exchange(this->lss_force_rebuild, false);
4,677✔
1020
    auto retval = rebuild_result::rr_no_change;
4,677✔
1021
    std::optional<timeval> lowest_tv = std::nullopt;
4,677✔
1022
    auto search_start = 0_vl;
4,677✔
1023

1024
    if (force) {
4,677✔
1025
        log_debug("forced to full rebuild");
534✔
1026
        retval = rebuild_result::rr_full_rebuild;
534✔
1027
        full_sort = true;
534✔
1028
        this->tss_level_filtered_count = 0;
534✔
1029
        this->lss_index.clear();
534✔
1030
    }
1031

1032
    std::vector<size_t> file_order(this->lss_files.size());
4,677✔
1033

1034
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
8,036✔
1035
        file_order[lpc] = lpc;
3,359✔
1036
    }
1037
    if (!this->lss_index.empty()) {
4,677✔
1038
        std::stable_sort(file_order.begin(),
2,544✔
1039
                         file_order.end(),
1040
                         [this](const auto& left, const auto& right) {
250✔
1041
                             const auto& left_ld = this->lss_files[left];
250✔
1042
                             const auto& right_ld = this->lss_files[right];
250✔
1043

1044
                             if (left_ld->get_file_ptr() == nullptr) {
250✔
1045
                                 return true;
×
1046
                             }
1047
                             if (right_ld->get_file_ptr() == nullptr) {
250✔
1048
                                 return false;
3✔
1049
                             }
1050

1051
                             return left_ld->get_file_ptr()->back()
247✔
1052
                                 < right_ld->get_file_ptr()->back();
494✔
1053
                         });
1054
    }
1055

1056
    bool time_left = true;
4,677✔
1057
    this->lss_all_timestamp_flags = 0;
4,677✔
1058
    for (const auto file_index : file_order) {
8,036✔
1059
        auto& ld = *(this->lss_files[file_index]);
3,359✔
1060
        auto* lf = ld.get_file_ptr();
3,359✔
1061

1062
        if (lf == nullptr) {
3,359✔
1063
            if (ld.ld_lines_indexed > 0) {
4✔
1064
                log_debug("%zu: file closed, doing full rebuild",
1✔
1065
                          ld.ld_file_index);
1066
                force = true;
1✔
1067
                retval = rebuild_result::rr_full_rebuild;
1✔
1068
                full_sort = true;
1✔
1069
            }
1070
        } else {
1071
            if (!lf->get_format_ptr()->lf_time_ordered) {
3,355✔
1072
                all_time_ordered_formats = false;
183✔
1073
            }
1074
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
3,355✔
1075
                log_debug("no time left, skipping %s",
2✔
1076
                          lf->get_filename_as_string().c_str());
1077
                time_left = false;
2✔
1078
            }
1079
            this->lss_all_timestamp_flags
3,355✔
1080
                |= lf->get_format_ptr()->lf_timestamp_flags;
3,355✔
1081

1082
            if (!this->tss_view->is_paused() && time_left) {
3,355✔
1083
                auto log_rebuild_res = lf->rebuild_index(deadline);
3,353✔
1084

1085
                if (ld.ld_lines_indexed < lf->size()
3,353✔
1086
                    && log_rebuild_res
3,353✔
1087
                        == logfile::rebuild_result_t::NO_NEW_LINES)
1088
                {
1089
                    // This is a bit awkward... if the logfile indexing was
1090
                    // complete before being added to us, we need to adjust
1091
                    // the rebuild result to make it look like new lines
1092
                    // were added.
1093
                    log_rebuild_res = logfile::rebuild_result_t::NEW_LINES;
51✔
1094
                }
1095
                switch (log_rebuild_res) {
3,353✔
1096
                    case logfile::rebuild_result_t::NO_NEW_LINES:
2,755✔
1097
                        break;
2,755✔
1098
                    case logfile::rebuild_result_t::NEW_LINES:
539✔
1099
                        if (retval == rebuild_result::rr_no_change) {
539✔
1100
                            retval = rebuild_result::rr_appended_lines;
488✔
1101
                        }
1102
                        log_debug("new lines for %s:%zu",
539✔
1103
                                  lf->get_filename_as_string().c_str(),
1104
                                  lf->size());
1105
                        if (!this->lss_index.empty()
539✔
1106
                            && lf->size() > ld.ld_lines_indexed)
539✔
1107
                        {
1108
                            auto& new_file_line = (*lf)[ld.ld_lines_indexed];
×
1109
                            auto cl = this->lss_index.back().value();
×
1110
                            auto* last_indexed_line = this->find_line(cl);
×
1111

1112
                            // If there are new lines that are older than what
1113
                            // we have in the index, we need to resort.
1114
                            if (last_indexed_line == nullptr
×
1115
                                || new_file_line
×
1116
                                    < last_indexed_line->get_timeval())
×
1117
                            {
1118
                                log_debug(
×
1119
                                    "%s:%ld: found older lines, full "
1120
                                    "rebuild: %p  %lld < %lld",
1121
                                    lf->get_filename().c_str(),
1122
                                    ld.ld_lines_indexed,
1123
                                    last_indexed_line,
1124
                                    new_file_line
1125
                                        .get_time<std::chrono::microseconds>()
1126
                                        .count(),
1127
                                    last_indexed_line == nullptr
1128
                                        ? (uint64_t) -1
1129
                                        : last_indexed_line
1130
                                              ->get_time<
1131
                                                  std::chrono::microseconds>()
1132
                                              .count());
1133
                                if (retval <= rebuild_result::rr_partial_rebuild
×
1134
                                    && all_time_ordered_formats)
×
1135
                                {
1136
                                    retval = rebuild_result::rr_partial_rebuild;
×
1137
                                    if (!lowest_tv
×
1138
                                        || new_file_line.get_timeval()
×
1139
                                            < lowest_tv.value())
×
1140
                                    {
1141
                                        lowest_tv = new_file_line.get_timeval();
×
1142
                                    }
1143
                                } else {
1144
                                    log_debug(
×
1145
                                        "already doing full rebuild, doing "
1146
                                        "full_sort as well");
1147
                                    force = true;
×
1148
                                    full_sort = true;
×
1149
                                }
1150
                            }
1151
                        }
1152
                        break;
539✔
1153
                    case logfile::rebuild_result_t::INVALID:
59✔
1154
                    case logfile::rebuild_result_t::NEW_ORDER:
1155
                        log_debug("%s: log file has a new order, full rebuild",
59✔
1156
                                  lf->get_filename().c_str());
1157
                        retval = rebuild_result::rr_full_rebuild;
59✔
1158
                        force = true;
59✔
1159
                        full_sort = true;
59✔
1160
                        break;
59✔
1161
                }
1162
            }
1163
            file_count += 1;
3,355✔
1164
            total_lines += lf->size();
3,355✔
1165

1166
            est_remaining_lines += lf->estimated_remaining_lines();
3,355✔
1167
        }
1168
    }
1169

1170
    if (!all_time_ordered_formats
4,677✔
1171
        && retval == rebuild_result::rr_partial_rebuild)
177✔
1172
    {
1173
        force = true;
×
1174
        full_sort = true;
×
1175
        retval = rebuild_result::rr_full_rebuild;
×
1176
    }
1177

1178
    if (this->lss_index.reserve(total_lines + est_remaining_lines)) {
4,677✔
1179
        // The index array was reallocated, just do a full sort/rebuild since
1180
        // it's been cleared out.
1181
        log_debug("expanding index capacity %zu", this->lss_index.ba_capacity);
696✔
1182
        force = true;
696✔
1183
        retval = rebuild_result::rr_full_rebuild;
696✔
1184
        full_sort = true;
696✔
1185
        this->tss_level_filtered_count = 0;
696✔
1186
    }
1187

1188
    auto& vis_bm = this->tss_view->get_bookmarks();
4,677✔
1189

1190
    if (force) {
4,677✔
1191
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,837✔
1192
             iter++)
592✔
1193
        {
1194
            (*iter)->ld_lines_indexed = 0;
592✔
1195
        }
1196

1197
        this->lss_index.clear();
1,245✔
1198
        this->lss_filtered_index.clear();
1,245✔
1199
        this->tss_level_filtered_count = 0;
1,245✔
1200
        this->lss_longest_line = 0;
1,245✔
1201
        this->lss_basename_width = 0;
1,245✔
1202
        this->lss_filename_width = 0;
1,245✔
1203
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,245✔
1204
        if (this->lss_index_delegate) {
1,245✔
1205
            this->lss_index_delegate->index_start(*this);
1,245✔
1206
        }
1207
    } else if (retval == rebuild_result::rr_partial_rebuild) {
3,432✔
1208
        size_t remaining = 0;
×
1209

1210
        log_debug("partial rebuild with lowest time: %ld",
×
1211
                  lowest_tv.value().tv_sec);
1212
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1213
             iter++)
×
1214
        {
1215
            logfile_data& ld = *(*iter);
×
1216
            auto* lf = ld.get_file_ptr();
×
1217

1218
            if (lf == nullptr) {
×
1219
                continue;
×
1220
            }
1221

1222
            require(lf->get_format_ptr()->lf_time_ordered);
×
1223

1224
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1225

1226
            if (line_iter) {
×
1227
                log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
×
1228
                          line_iter.value()->get_timeval().tv_sec,
1229
                          std::distance(lf->cbegin(), line_iter.value()),
1230
                          lf->size(),
1231
                          lf->get_filename_as_string().c_str());
1232
            }
1233
            ld.ld_lines_indexed
1234
                = std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
×
1235
            remaining += lf->size() - ld.ld_lines_indexed;
×
1236
        }
1237

1238
        auto* row_iter = std::lower_bound(this->lss_index.begin(),
×
1239
                                          this->lss_index.end(),
1240
                                          lowest_tv.value(),
×
1241
                                          logline_cmp(*this));
1242
        this->lss_index.shrink_to(
×
1243
            std::distance(this->lss_index.begin(), row_iter));
×
1244
        log_debug("new index size %ld/%ld; remain %ld",
×
1245
                  this->lss_index.ba_size,
1246
                  this->lss_index.ba_capacity,
1247
                  remaining);
1248
        auto filt_row_iter = std::lower_bound(this->lss_filtered_index.begin(),
×
1249
                                              this->lss_filtered_index.end(),
1250
                                              lowest_tv.value(),
×
1251
                                              filtered_logline_cmp(*this));
1252
        this->lss_filtered_index.resize(
×
1253
            std::distance(this->lss_filtered_index.begin(), filt_row_iter));
×
1254
        search_start = vis_line_t(this->lss_filtered_index.size());
×
1255

1256
        if (this->lss_index_delegate) {
×
1257
            this->lss_index_delegate->index_start(*this);
×
1258
            for (const auto row_in_full_index : this->lss_filtered_index) {
×
1259
                auto cl = this->lss_index[row_in_full_index].value();
×
1260
                uint64_t line_number;
1261
                auto ld_iter = this->find_data(cl, line_number);
×
1262
                auto& ld = *ld_iter;
×
1263
                auto line_iter = ld->get_file_ptr()->begin() + line_number;
×
1264

1265
                this->lss_index_delegate->index_line(
×
1266
                    *this, ld->get_file_ptr(), line_iter);
1267
            }
1268
        }
1269
    }
1270

1271
    if (this->lss_index.empty() && !time_left) {
4,677✔
1272
        log_info("ran out of time, skipping rebuild");
×
1273
        // need to make sure we rebuild in case no new data comes in
1274
        this->lss_force_rebuild = true;
×
1275
        return rebuild_result::rr_appended_lines;
×
1276
    }
1277

1278
    if (retval != rebuild_result::rr_no_change || force) {
4,677✔
1279
        size_t index_size = 0, start_size = this->lss_index.size();
1,256✔
1280
        logline_cmp line_cmper(*this);
1,256✔
1281

1282
        for (auto& ld : this->lss_files) {
1,864✔
1283
            auto* lf = ld->get_file_ptr();
608✔
1284

1285
            if (lf == nullptr) {
608✔
1286
                continue;
1✔
1287
            }
1288
            this->lss_longest_line = std::max(
1,214✔
1289
                this->lss_longest_line, lf->get_longest_line_length() + 1);
607✔
1290
            this->lss_basename_width
1291
                = std::max(this->lss_basename_width,
1,214✔
1292
                           lf->get_unique_path().native().size());
607✔
1293
            this->lss_filename_width = std::max(
1,214✔
1294
                this->lss_filename_width, lf->get_filename().native().size());
607✔
1295
        }
1296

1297
        if (full_sort) {
1,256✔
1298
            log_trace("rebuild_index full sort");
1,256✔
1299
            for (auto& ld : this->lss_files) {
1,864✔
1300
                auto* lf = ld->get_file_ptr();
608✔
1301

1302
                if (lf == nullptr) {
608✔
1303
                    continue;
1✔
1304
                }
1305

1306
                for (size_t line_index = 0; line_index < lf->size();
15,384✔
1307
                     line_index++)
1308
                {
1309
                    const auto lf_iter
1310
                        = ld->get_file_ptr()->begin() + line_index;
14,777✔
1311
                    if (lf_iter->is_ignored()) {
14,777✔
1312
                        continue;
414✔
1313
                    }
1314

1315
                    content_line_t con_line(
1316
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
14,363✔
1317

1318
                    if (lf_iter->is_meta_marked()) {
14,363✔
1319
                        auto start_iter = lf_iter;
12✔
1320
                        while (start_iter->is_continued()) {
12✔
1321
                            --start_iter;
×
1322
                        }
1323
                        int start_index
1324
                            = start_iter - ld->get_file_ptr()->begin();
12✔
1325
                        content_line_t start_con_line(ld->ld_file_index
12✔
1326
                                                          * MAX_LINES_PER_FILE
12✔
1327
                                                      + start_index);
12✔
1328

1329
                        auto& line_meta
1330
                            = ld->get_file_ptr()
1331
                                  ->get_bookmark_metadata()[start_index];
12✔
1332
                        if (line_meta.has(bookmark_metadata::categories::notes))
12✔
1333
                        {
1334
                            this->lss_user_marks[&textview_curses::BM_META]
4✔
1335
                                .insert_once(start_con_line);
4✔
1336
                        }
1337
                        if (line_meta.has(
12✔
1338
                                bookmark_metadata::categories::partition))
1339
                        {
1340
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
8✔
1341
                                .insert_once(start_con_line);
8✔
1342
                        }
1343
                    }
1344
                    this->lss_index.push_back(
14,363✔
1345
                        indexed_content{con_line, lf_iter});
28,726✔
1346
                }
1347
            }
1348

1349
            // XXX get rid of this full sort on the initial run, it's not
1350
            // needed unless the file is not in time-order
1351
            if (this->lss_sorting_observer) {
1,256✔
1352
                this->lss_sorting_observer(*this, 0, this->lss_index.size());
3✔
1353
            }
1354
            std::sort(
1,256✔
1355
                this->lss_index.begin(), this->lss_index.end(), line_cmper);
1356
            if (this->lss_sorting_observer) {
1,256✔
1357
                this->lss_sorting_observer(
6✔
1358
                    *this, this->lss_index.size(), this->lss_index.size());
3✔
1359
            }
1360
        } else {
1361
            kmerge_tree_c<logline, logfile_data, logfile::iterator> merge(
1362
                file_count);
×
1363

1364
            for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1365
                 iter++)
×
1366
            {
1367
                auto* ld = iter->get();
×
1368
                auto* lf = ld->get_file_ptr();
×
1369
                if (lf == nullptr) {
×
1370
                    continue;
×
1371
                }
1372

1373
                merge.add(ld, lf->begin() + ld->ld_lines_indexed, lf->end());
×
1374
                index_size += lf->size();
×
1375
            }
1376

1377
            file_off_t index_off = 0;
×
1378
            merge.execute();
×
1379
            if (this->lss_sorting_observer) {
×
1380
                this->lss_sorting_observer(*this, index_off, index_size);
×
1381
            }
1382
            log_trace("k-way merge");
×
1383
            for (;;) {
1384
                logfile::iterator lf_iter;
×
1385
                logfile_data* ld;
1386

1387
                if (!merge.get_top(ld, lf_iter)) {
×
1388
                    break;
×
1389
                }
1390

1391
                if (!lf_iter->is_ignored()) {
×
1392
                    int file_index = ld->ld_file_index;
×
1393
                    int line_index = lf_iter - ld->get_file_ptr()->begin();
×
1394

1395
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
1396
                                            + line_index);
×
1397

1398
                    if (lf_iter->is_meta_marked()) {
×
1399
                        auto start_iter = lf_iter;
×
1400
                        while (start_iter->is_continued()) {
×
1401
                            --start_iter;
×
1402
                        }
1403
                        int start_index
1404
                            = start_iter - ld->get_file_ptr()->begin();
×
1405
                        content_line_t start_con_line(
1406
                            file_index * MAX_LINES_PER_FILE + start_index);
×
1407

1408
                        auto& line_meta
1409
                            = ld->get_file_ptr()
1410
                                  ->get_bookmark_metadata()[start_index];
×
1411
                        if (line_meta.has(bookmark_metadata::categories::notes))
×
1412
                        {
1413
                            this->lss_user_marks[&textview_curses::BM_META]
×
1414
                                .insert_once(start_con_line);
×
1415
                        }
1416
                        if (line_meta.has(
×
1417
                                bookmark_metadata::categories::partition))
1418
                        {
1419
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
×
1420
                                .insert_once(start_con_line);
×
1421
                        }
1422
                    }
1423
                    this->lss_index.push_back(
×
1424
                        indexed_content{con_line, lf_iter});
×
1425
                }
1426

1427
                merge.next();
×
1428
                index_off += 1;
×
1429
                if (index_off % 100000 == 0 && this->lss_sorting_observer) {
×
1430
                    this->lss_sorting_observer(*this, index_off, index_size);
×
1431
                }
1432
            }
1433
            if (this->lss_sorting_observer) {
×
1434
                this->lss_sorting_observer(*this, index_size, index_size);
×
1435
            }
1436
        }
1437

1438
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,864✔
1439
             ++iter)
608✔
1440
        {
1441
            auto* lf = (*iter)->get_file_ptr();
608✔
1442

1443
            if (lf == nullptr) {
608✔
1444
                continue;
1✔
1445
            }
1446

1447
            (*iter)->ld_lines_indexed = lf->size();
607✔
1448
        }
1449

1450
        this->lss_filtered_index.reserve(this->lss_index.size());
1,256✔
1451

1452
        uint32_t filter_in_mask, filter_out_mask;
1453
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,256✔
1454

1455
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,256✔
1456
            this->lss_index_delegate->index_start(*this);
1,256✔
1457
        }
1458

1459
        log_trace("filtered index");
1,256✔
1460
        for (size_t index_index = start_size;
15,619✔
1461
             index_index < this->lss_index.size();
15,619✔
1462
             index_index++)
1463
        {
1464
            const auto cl = this->lss_index[index_index].value();
14,363✔
1465
            uint64_t line_number;
1466
            auto ld = this->find_data(cl, line_number);
14,363✔
1467

1468
            if (!(*ld)->is_visible()) {
14,363✔
1469
                continue;
×
1470
            }
1471

1472
            auto* lf = (*ld)->get_file_ptr();
14,363✔
1473
            auto line_iter = lf->begin() + line_number;
14,363✔
1474

1475
            if (line_iter->is_ignored()) {
14,363✔
1476
                continue;
×
1477
            }
1478

1479
            if (!this->tss_apply_filters
28,726✔
1480
                || (!(*ld)->ld_filter_state.excluded(
28,675✔
1481
                        filter_in_mask, filter_out_mask, line_number)
1482
                    && this->check_extra_filters(ld, line_iter)))
14,312✔
1483
            {
1484
                auto eval_res = this->eval_sql_filter(
1485
                    this->lss_marker_stmt.in(), ld, line_iter);
14,312✔
1486
                if (eval_res.isErr()) {
14,312✔
1487
                    line_iter->set_expr_mark(false);
×
1488
                } else {
1489
                    auto matched = eval_res.unwrap();
14,312✔
1490

1491
                    line_iter->set_expr_mark(matched);
14,312✔
1492
                    if (matched) {
14,312✔
1493
                        vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1494
                            vis_line_t(this->lss_filtered_index.size()));
×
1495
                        this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1496
                            .insert_once(cl);
×
1497
                    }
1498
                }
1499
                this->lss_filtered_index.push_back(index_index);
14,312✔
1500
                if (this->lss_index_delegate != nullptr) {
14,312✔
1501
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
14,312✔
1502
                }
1503
            }
14,312✔
1504
        }
1505

1506
        this->lss_indexing_in_progress = false;
1,256✔
1507

1508
        if (this->lss_index_delegate != nullptr) {
1,256✔
1509
            this->lss_index_delegate->index_complete(*this);
1,256✔
1510
        }
1511
    }
1512

1513
    switch (retval) {
4,677✔
1514
        case rebuild_result::rr_no_change:
3,421✔
1515
            break;
3,421✔
1516
        case rebuild_result::rr_full_rebuild:
1,245✔
1517
            log_debug("redoing search");
1,245✔
1518
            this->lss_index_generation += 1;
1,245✔
1519
            this->tss_view->reload_data();
1,245✔
1520
            this->tss_view->redo_search();
1,245✔
1521
            break;
1,245✔
1522
        case rebuild_result::rr_partial_rebuild:
×
1523
            log_debug("redoing search from: %d", (int) search_start);
×
1524
            this->lss_index_generation += 1;
×
1525
            this->tss_view->reload_data();
×
1526
            this->tss_view->search_new_data(search_start);
×
1527
            break;
×
1528
        case rebuild_result::rr_appended_lines:
11✔
1529
            this->tss_view->reload_data();
11✔
1530
            this->tss_view->search_new_data();
11✔
1531
            break;
11✔
1532
    }
1533

1534
    return retval;
4,677✔
1535
}
4,677✔
1536

1537
void
1538
logfile_sub_source::text_update_marks(vis_bookmarks& bm)
2,445✔
1539
{
1540
    logfile* last_file = nullptr;
2,445✔
1541
    vis_line_t vl;
2,445✔
1542

1543
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,445✔
1544
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,445✔
1545
    auto& bm_files = bm[&BM_FILES];
2,445✔
1546

1547
    bm_warnings.clear();
2,445✔
1548
    bm_errors.clear();
2,445✔
1549
    bm_files.clear();
2,445✔
1550

1551
    std::vector<const bookmark_type_t*> used_marks;
2,445✔
1552
    for (const auto* bmt :
14,670✔
1553
         {
1554
             &textview_curses::BM_USER,
1555
             &textview_curses::BM_USER_EXPR,
1556
             &textview_curses::BM_PARTITION,
1557
             &textview_curses::BM_META,
1558
             &textview_curses::BM_STICKY,
1559
         })
17,115✔
1560
    {
1561
        bm[bmt].clear();
12,225✔
1562
        if (!this->lss_user_marks[bmt].empty()) {
12,225✔
1563
            used_marks.emplace_back(bmt);
156✔
1564
        }
1565
    }
1566

1567
    for (; vl < (int) this->lss_filtered_index.size(); ++vl) {
20,735✔
1568
        const auto& orig_ic = this->lss_index[this->lss_filtered_index[vl]];
18,290✔
1569
        auto cl = orig_ic.value();
18,290✔
1570
        auto* lf = this->find_file_ptr(cl);
18,290✔
1571

1572
        for (const auto& bmt : used_marks) {
20,809✔
1573
            auto& user_mark = this->lss_user_marks[bmt];
2,519✔
1574
            if (user_mark.bv_tree.exists(orig_ic.value())) {
2,519✔
1575
                bm[bmt].insert_once(vl);
192✔
1576
            }
1577
        }
1578

1579
        if (lf != last_file) {
18,290✔
1580
            bm_files.insert_once(vl);
1,066✔
1581
        }
1582

1583
        switch (orig_ic.level()) {
18,290✔
1584
            case indexed_content::level_t::warning:
133✔
1585
                bm_warnings.insert_once(vl);
133✔
1586
                break;
133✔
1587

1588
            case indexed_content::level_t::error:
3,529✔
1589
                bm_errors.insert_once(vl);
3,529✔
1590
                break;
3,529✔
1591

1592
            default:
14,628✔
1593
                break;
14,628✔
1594
        }
1595

1596
        last_file = lf;
18,290✔
1597
    }
1598
}
2,445✔
1599

1600
void
1601
logfile_sub_source::text_filters_changed()
195✔
1602
{
1603
    static auto op = lnav_operation{"text_filters_changed"};
195✔
1604

1605
    auto op_guard = lnav_opid_guard::internal(op);
195✔
1606
    this->lss_index_generation += 1;
195✔
1607
    this->tss_level_filtered_count = 0;
195✔
1608

1609
    if (this->lss_line_meta_changed) {
195✔
1610
        this->invalidate_sql_filter();
47✔
1611
        this->lss_line_meta_changed = false;
47✔
1612
    }
1613

1614
    log_debug("filtering files");
195✔
1615
    for (auto& ld : *this) {
339✔
1616
        auto* lf = ld->get_file_ptr();
144✔
1617

1618
        if (lf != nullptr) {
144✔
1619
            ld->ld_filter_state.clear_deleted_filter_state();
144✔
1620
            lf->reobserve_from(lf->begin()
144✔
1621
                               + ld->ld_filter_state.get_min_count(lf->size()));
144✔
1622
        }
1623
    }
1624

1625
    if (this->lss_force_rebuild) {
195✔
1626
        log_debug("skipping update since in the middle of force rebuild");
×
1627
        return;
×
1628
    }
1629

1630
    auto& vis_bm = this->tss_view->get_bookmarks();
195✔
1631
    uint32_t filtered_in_mask, filtered_out_mask;
1632

1633
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
195✔
1634

1635
    if (this->lss_index_delegate != nullptr) {
195✔
1636
        this->lss_index_delegate->index_start(*this);
195✔
1637
    }
1638
    vis_bm[&textview_curses::BM_USER_EXPR].clear();
195✔
1639

1640
    this->lss_filtered_index.clear();
195✔
1641
    for (size_t index_index = 0; index_index < this->lss_index.size();
1,835✔
1642
         index_index++)
1643
    {
1644
        auto cl = this->lss_index[index_index].value();
1,640✔
1645
        uint64_t line_number;
1646
        auto ld = this->find_data(cl, line_number);
1,640✔
1647

1648
        if (!(*ld)->is_visible()) {
1,640✔
1649
            continue;
213✔
1650
        }
1651

1652
        auto lf = (*ld)->get_file_ptr();
1,427✔
1653
        auto line_iter = lf->begin() + line_number;
1,427✔
1654

1655
        if (!this->tss_apply_filters
2,854✔
1656
            || (!(*ld)->ld_filter_state.excluded(
2,689✔
1657
                    filtered_in_mask, filtered_out_mask, line_number)
1658
                && this->check_extra_filters(ld, line_iter)))
1,262✔
1659
        {
1660
            auto eval_res = this->eval_sql_filter(
1661
                this->lss_marker_stmt.in(), ld, line_iter);
1,220✔
1662
            if (eval_res.isErr()) {
1,220✔
1663
                line_iter->set_expr_mark(false);
×
1664
            } else {
1665
                auto matched = eval_res.unwrap();
1,220✔
1666

1667
                line_iter->set_expr_mark(matched);
1,220✔
1668
                if (matched) {
1,220✔
1669
                    vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1670
                        vis_line_t(this->lss_filtered_index.size()));
×
1671
                    this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1672
                        .insert_once(cl);
×
1673
                }
1674
            }
1675
            this->lss_filtered_index.push_back(index_index);
1,220✔
1676
            if (this->lss_index_delegate != nullptr) {
1,220✔
1677
                this->lss_index_delegate->index_line(*this, lf, line_iter);
1,220✔
1678
            }
1679
        }
1,220✔
1680
    }
1681

1682
    if (this->lss_index_delegate != nullptr) {
195✔
1683
        this->lss_index_delegate->index_complete(*this);
195✔
1684
    }
1685

1686
    if (this->tss_view != nullptr) {
195✔
1687
        log_debug("reloading view");
195✔
1688
        this->tss_view->reload_data();
195✔
1689
        this->tss_view->redo_search();
195✔
1690
    }
1691
    log_debug("finished filter update");
195✔
1692
}
195✔
1693

1694
std::optional<json_string>
1695
logfile_sub_source::text_row_details(const textview_curses& tc)
29✔
1696
{
1697
    if (this->lss_index.empty()) {
29✔
1698
        log_trace("logfile_sub_source::text_row_details empty");
1✔
1699
        return std::nullopt;
1✔
1700
    }
1701

1702
    auto ov_sel = tc.get_overlay_selection();
28✔
1703
    if (ov_sel.has_value()) {
28✔
1704
        auto* fos
1705
            = dynamic_cast<field_overlay_source*>(tc.get_overlay_source());
×
1706
        auto iter = fos->fos_row_to_field_meta.find(ov_sel.value());
×
1707
        if (iter != fos->fos_row_to_field_meta.end()) {
×
1708
            auto find_res = this->find_line_with_file(tc.get_top());
×
1709
            if (find_res) {
×
1710
                yajlpp_gen gen;
×
1711

1712
                {
1713
                    yajlpp_map root(gen);
×
1714

1715
                    root.gen("value");
×
1716
                    root.gen(iter->second.ri_value);
×
1717
                }
1718

1719
                return json_string(gen);
×
1720
            }
1721
        }
1722
    }
1723

1724
    return std::nullopt;
28✔
1725
}
1726

1727
bool
1728
logfile_sub_source::list_input_handle_key(listview_curses& lv,
×
1729
                                          const ncinput& ch)
1730
{
1731
    switch (ch.eff_text[0]) {
×
1732
        case ' ': {
×
1733
            auto ov_vl = lv.get_overlay_selection();
×
1734
            if (ov_vl) {
×
1735
                auto* fos = dynamic_cast<field_overlay_source*>(
×
1736
                    lv.get_overlay_source());
×
1737
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
1738
                if (iter != fos->fos_row_to_field_meta.end()
×
1739
                    && iter->second.ri_meta)
×
1740
                {
1741
                    auto find_res = this->find_line_with_file(lv.get_top());
×
1742
                    if (find_res) {
×
1743
                        auto file_and_line = find_res.value();
×
1744
                        auto* format = file_and_line.first->get_format_ptr();
×
1745
                        auto fstates = format->get_field_states();
×
1746
                        auto state_iter
1747
                            = fstates.find(iter->second.ri_meta->lvm_name);
×
1748
                        if (state_iter != fstates.end()) {
×
1749
                            format->hide_field(iter->second.ri_meta->lvm_name,
×
1750
                                               !state_iter->second.is_hidden());
×
1751
                            lv.set_needs_update();
×
1752
                        }
1753
                    }
1754
                }
1755
                return true;
×
1756
            }
1757
            return false;
×
1758
        }
1759
        case '#': {
×
1760
            auto ov_vl = lv.get_overlay_selection();
×
1761
            if (ov_vl) {
×
1762
                auto* fos = dynamic_cast<field_overlay_source*>(
×
1763
                    lv.get_overlay_source());
×
1764
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
1765
                if (iter != fos->fos_row_to_field_meta.end()
×
1766
                    && iter->second.ri_meta)
×
1767
                {
1768
                    const auto& meta = iter->second.ri_meta.value();
×
1769
                    std::string cmd;
×
1770

1771
                    switch (meta.to_chart_type()) {
×
1772
                        case chart_type_t::none:
×
1773
                            break;
×
1774
                        case chart_type_t::hist: {
×
1775
                            auto prql = fmt::format(
1776
                                FMT_STRING(
×
1777
                                    "from {} | stats.hist {} slice:'1h'"),
1778
                                meta.lvm_format.value()->get_name(),
×
1779
                                meta.lvm_name);
×
1780
                            cmd = fmt::format(FMT_STRING(":prompt sql ; '{}'"),
×
1781
                                              shlex::escape(prql));
×
1782
                            break;
×
1783
                        }
1784
                        case chart_type_t::spectro:
×
1785
                            cmd = fmt::format(FMT_STRING(":spectrogram {}"),
×
1786
                                              meta.lvm_name);
×
1787
                            break;
×
1788
                    }
1789
                    if (!cmd.empty()) {
×
1790
                        this->lss_exec_context
×
1791
                            ->with_provenance(exec_context::mouse_input{})
×
1792
                            ->execute(INTERNAL_SRC_LOC, cmd);
×
1793
                    }
1794
                }
1795
                return true;
×
1796
            }
1797
            return false;
×
1798
        }
1799
        case 'h':
×
1800
        case 'H':
1801
        case NCKEY_LEFT:
1802
            if (lv.get_left() == 0) {
×
1803
                this->increase_line_context();
×
1804
                lv.set_needs_update();
×
1805
                return true;
×
1806
            }
1807
            break;
×
1808
        case 'l':
×
1809
        case 'L':
1810
        case NCKEY_RIGHT:
1811
            if (this->decrease_line_context()) {
×
1812
                lv.set_needs_update();
×
1813
                return true;
×
1814
            }
1815
            break;
×
1816
    }
1817
    return false;
×
1818
}
1819

1820
std::optional<
1821
    std::pair<grep_proc_source<vis_line_t>*, grep_proc_sink<vis_line_t>*>>
1822
logfile_sub_source::get_grepper()
9✔
1823
{
1824
    return std::make_pair(
18✔
1825
        (grep_proc_source<vis_line_t>*) &this->lss_meta_grepper,
×
1826
        (grep_proc_sink<vis_line_t>*) &this->lss_meta_grepper);
9✔
1827
}
1828

1829
/**
1830
 * Functor for comparing the ld_file field of the logfile_data struct.
1831
 */
1832
struct logfile_data_eq {
1833
    explicit logfile_data_eq(std::shared_ptr<logfile> lf)
1,148✔
1834
        : lde_file(std::move(lf))
1,148✔
1835
    {
1836
    }
1,148✔
1837

1838
    bool operator()(
692✔
1839
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1840
    {
1841
        return this->lde_file == ld->get_file();
692✔
1842
    }
1843

1844
    std::shared_ptr<logfile> lde_file;
1845
};
1846

1847
bool
1848
logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
574✔
1849
{
1850
    iterator existing;
574✔
1851

1852
    require_lt(lf->size(), MAX_LINES_PER_FILE);
574✔
1853

1854
    existing = std::find_if(this->lss_files.begin(),
574✔
1855
                            this->lss_files.end(),
1856
                            logfile_data_eq(nullptr));
1,148✔
1857
    if (existing == this->lss_files.end()) {
574✔
1858
        if (this->lss_files.size() >= MAX_FILES) {
574✔
1859
            return false;
×
1860
        }
1861

1862
        auto ld = std::make_unique<logfile_data>(
1863
            this->lss_files.size(), this->get_filters(), lf);
574✔
1864
        ld->set_visibility(lf->get_open_options().loo_is_visible);
574✔
1865
        this->lss_files.push_back(std::move(ld));
574✔
1866
    } else {
574✔
1867
        (*existing)->set_file(lf);
×
1868
    }
1869

1870
    return true;
574✔
1871
}
1872

1873
Result<void, lnav::console::user_message>
1874
logfile_sub_source::set_sql_filter(std::string stmt_str, sqlite3_stmt* stmt)
837✔
1875
{
1876
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
837✔
1877
        auto top_cl = this->at(0_vl);
10✔
1878
        auto ld = this->find_data(top_cl);
10✔
1879
        auto eval_res
1880
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
10✔
1881

1882
        if (eval_res.isErr()) {
10✔
1883
            sqlite3_finalize(stmt);
1✔
1884
            return Err(eval_res.unwrapErr());
1✔
1885
        }
1886
    }
10✔
1887

1888
    for (auto& ld : *this) {
855✔
1889
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
19✔
1890
    }
1891

1892
    auto old_filter_iter = this->tss_filters.find(0);
836✔
1893
    if (stmt != nullptr) {
836✔
1894
        auto new_filter
1895
            = std::make_shared<sql_filter>(*this, std::move(stmt_str), stmt);
9✔
1896

1897
        if (old_filter_iter != this->tss_filters.end()) {
9✔
1898
            *old_filter_iter = new_filter;
×
1899
        } else {
1900
            this->tss_filters.add_filter(new_filter);
9✔
1901
        }
1902
    } else if (old_filter_iter != this->tss_filters.end()) {
836✔
1903
        this->tss_filters.delete_filter((*old_filter_iter)->get_id());
9✔
1904
    }
1905

1906
    return Ok();
836✔
1907
}
1908

1909
Result<void, lnav::console::user_message>
1910
logfile_sub_source::set_sql_marker(std::string stmt_str, sqlite3_stmt* stmt)
832✔
1911
{
1912
    static auto op = lnav_operation{"set_sql_marker"};
832✔
1913
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
832✔
1914
        auto top_cl = this->at(0_vl);
5✔
1915
        auto ld = this->find_data(top_cl);
5✔
1916
        auto eval_res
1917
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
5✔
1918

1919
        if (eval_res.isErr()) {
5✔
1920
            sqlite3_finalize(stmt);
×
1921
            return Err(eval_res.unwrapErr());
×
1922
        }
1923
    }
5✔
1924

1925
    auto op_guard = lnav_opid_guard::internal(op);
832✔
1926
    log_info("setting SQL marker: %s", stmt_str.c_str());
832✔
1927
    this->lss_marker_stmt_text = std::move(stmt_str);
832✔
1928
    this->lss_marker_stmt = stmt;
832✔
1929

1930
    if (this->tss_view == nullptr || this->lss_force_rebuild) {
832✔
1931
        log_info("skipping SQL marker update");
130✔
1932
        return Ok();
130✔
1933
    }
1934

1935
    auto& vis_bm = this->tss_view->get_bookmarks();
702✔
1936
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
702✔
1937
    auto& cl_marks_bv = this->lss_user_marks[&textview_curses::BM_USER_EXPR];
702✔
1938

1939
    expr_marks_bv.clear();
702✔
1940
    if (this->lss_index_delegate) {
702✔
1941
        this->lss_index_delegate->index_start(*this);
702✔
1942
    }
1943
    for (auto row = 0_vl; row < vis_line_t(this->lss_filtered_index.size());
1,135✔
1944
         row += 1_vl)
433✔
1945
    {
1946
        auto cl = this->at(row);
433✔
1947
        uint64_t line_number;
1948
        auto ld = this->find_data(cl, line_number);
433✔
1949

1950
        if (!(*ld)->is_visible()) {
433✔
1951
            continue;
1✔
1952
        }
1953
        auto ll = (*ld)->get_file()->begin() + line_number;
433✔
1954
        if (ll->is_continued() || ll->is_ignored()) {
433✔
1955
            continue;
1✔
1956
        }
1957
        auto eval_res
1958
            = this->eval_sql_filter(this->lss_marker_stmt.in(), ld, ll);
432✔
1959

1960
        if (eval_res.isErr()) {
432✔
1961
            ll->set_expr_mark(false);
×
1962
        } else {
1963
            auto matched = eval_res.unwrap();
432✔
1964

1965
            ll->set_expr_mark(matched);
432✔
1966
            if (matched) {
432✔
1967
                expr_marks_bv.insert_once(row);
22✔
1968
                cl_marks_bv.insert_once(cl);
22✔
1969
            }
1970
        }
1971
        if (this->lss_index_delegate) {
432✔
1972
            this->lss_index_delegate->index_line(
432✔
1973
                *this, (*ld)->get_file_ptr(), ll);
432✔
1974
        }
1975
    }
432✔
1976
    if (this->lss_index_delegate) {
702✔
1977
        this->lss_index_delegate->index_complete(*this);
702✔
1978
    }
1979
    log_info("SQL marker update complete");
702✔
1980

1981
    return Ok();
702✔
1982
}
832✔
1983

1984
Result<void, lnav::console::user_message>
1985
logfile_sub_source::set_preview_sql_filter(sqlite3_stmt* stmt)
829✔
1986
{
1987
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
829✔
1988
        auto top_cl = this->at(0_vl);
×
1989
        auto ld = this->find_data(top_cl);
×
1990
        auto eval_res
1991
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
×
1992

1993
        if (eval_res.isErr()) {
×
1994
            sqlite3_finalize(stmt);
×
1995
            return Err(eval_res.unwrapErr());
×
1996
        }
1997
    }
1998

1999
    this->lss_preview_filter_stmt = stmt;
829✔
2000

2001
    return Ok();
829✔
2002
}
2003

2004
Result<bool, lnav::console::user_message>
2005
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
16,161✔
2006
                                    iterator ld,
2007
                                    logfile::const_iterator ll)
2008
{
2009
    if (stmt == nullptr) {
16,161✔
2010
        return Ok(false);
31,070✔
2011
    }
2012

2013
    auto* lf = (*ld)->get_file_ptr();
626✔
2014
    char timestamp_buffer[64];
2015
    shared_buffer_ref raw_sbr;
626✔
2016
    logline_value_vector values;
626✔
2017
    auto& sbr = values.lvv_sbr;
626✔
2018
    lf->read_full_message(ll, sbr);
626✔
2019
    sbr.erase_ansi();
626✔
2020
    auto format = lf->get_format();
626✔
2021
    string_attrs_t sa;
626✔
2022
    auto line_number = std::distance(lf->cbegin(), ll);
626✔
2023
    format->annotate(lf, line_number, sa, values);
626✔
2024
    auto lffs = lf->get_format_file_state();
626✔
2025

2026
    sqlite3_reset(stmt);
626✔
2027
    sqlite3_clear_bindings(stmt);
626✔
2028

2029
    auto count = sqlite3_bind_parameter_count(stmt);
626✔
2030
    for (int lpc = 0; lpc < count; lpc++) {
1,264✔
2031
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
638✔
2032

2033
        if (name[0] == '$') {
638✔
2034
            const char* env_value;
2035

2036
            if ((env_value = getenv(&name[1])) != nullptr) {
4✔
2037
                sqlite3_bind_text(stmt, lpc + 1, env_value, -1, SQLITE_STATIC);
1✔
2038
            }
2039
            continue;
4✔
2040
        }
4✔
2041
        if (strcmp(name, ":log_level") == 0) {
634✔
2042
            auto lvl = ll->get_level_name();
6✔
2043
            sqlite3_bind_text(
6✔
2044
                stmt, lpc + 1, lvl.data(), lvl.length(), SQLITE_STATIC);
2045
            continue;
6✔
2046
        }
6✔
2047
        if (strcmp(name, ":log_time") == 0) {
628✔
2048
            auto len = sql_strftime(timestamp_buffer,
×
2049
                                    sizeof(timestamp_buffer),
2050
                                    ll->get_timeval(),
×
2051
                                    'T');
2052
            sqlite3_bind_text(
×
2053
                stmt, lpc + 1, timestamp_buffer, len, SQLITE_STATIC);
2054
            continue;
×
2055
        }
2056
        if (strcmp(name, ":log_time_msecs") == 0) {
628✔
2057
            sqlite3_bind_int64(
1✔
2058
                stmt,
2059
                lpc + 1,
2060
                ll->get_time<std::chrono::milliseconds>().count());
1✔
2061
            continue;
1✔
2062
        }
2063
        if (strcmp(name, ":log_mark") == 0) {
627✔
2064
            sqlite3_bind_int(stmt, lpc + 1, ll->is_marked());
×
2065
            continue;
×
2066
        }
2067
        if (strcmp(name, ":log_comment") == 0) {
627✔
2068
            const auto& bm = lf->get_bookmark_metadata();
×
2069
            auto line_number
2070
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
2071
            auto bm_iter = bm.find(line_number);
×
2072
            if (bm_iter != bm.end() && !bm_iter->second.bm_comment.empty()) {
×
2073
                const auto& meta = bm_iter->second;
×
2074
                sqlite3_bind_text(stmt,
×
2075
                                  lpc + 1,
2076
                                  meta.bm_comment.c_str(),
2077
                                  meta.bm_comment.length(),
×
2078
                                  SQLITE_STATIC);
2079
            }
2080
            continue;
×
2081
        }
2082
        if (strcmp(name, ":log_annotations") == 0) {
627✔
2083
            const auto& bm = lf->get_bookmark_metadata();
×
2084
            auto line_number
2085
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
2086
            auto bm_iter = bm.find(line_number);
×
2087
            if (bm_iter != bm.end()
×
2088
                && !bm_iter->second.bm_annotations.la_pairs.empty())
×
2089
            {
2090
                const auto& meta = bm_iter->second;
×
2091
                auto anno_str = logmsg_annotations_handlers.to_string(
2092
                    meta.bm_annotations);
×
2093

2094
                sqlite3_bind_text(stmt,
×
2095
                                  lpc + 1,
2096
                                  anno_str.c_str(),
2097
                                  anno_str.length(),
×
2098
                                  SQLITE_TRANSIENT);
2099
            }
2100
            continue;
×
2101
        }
2102
        if (strcmp(name, ":log_tags") == 0) {
627✔
2103
            const auto& bm = lf->get_bookmark_metadata();
20✔
2104
            auto line_number
2105
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
20✔
2106
            auto bm_iter = bm.find(line_number);
20✔
2107
            if (bm_iter != bm.end() && !bm_iter->second.bm_tags.empty()) {
20✔
2108
                const auto& meta = bm_iter->second;
4✔
2109
                yajlpp_gen gen;
4✔
2110

2111
                yajl_gen_config(gen, yajl_gen_beautify, false);
4✔
2112

2113
                {
2114
                    yajlpp_array arr(gen);
4✔
2115

2116
                    for (const auto& entry : meta.bm_tags) {
8✔
2117
                        arr.gen(entry.te_tag);
4✔
2118
                    }
2119
                }
4✔
2120

2121
                string_fragment sf = gen.to_string_fragment();
4✔
2122

2123
                sqlite3_bind_text(
4✔
2124
                    stmt, lpc + 1, sf.data(), sf.length(), SQLITE_TRANSIENT);
2125
            }
4✔
2126
            continue;
20✔
2127
        }
20✔
2128
        if (strcmp(name, ":log_format") == 0) {
607✔
2129
            const auto format_name = format->get_name();
6✔
2130
            sqlite3_bind_text(stmt,
6✔
2131
                              lpc + 1,
2132
                              format_name.get(),
2133
                              format_name.size(),
6✔
2134
                              SQLITE_STATIC);
2135
            continue;
6✔
2136
        }
6✔
2137
        if (strcmp(name, ":log_format_regex") == 0) {
601✔
2138
            const auto pat_name = format->get_pattern_name(
×
2139
                lffs.lffs_pattern_locks, line_number);
2140
            sqlite3_bind_text(
×
2141
                stmt, lpc + 1, pat_name.get(), pat_name.size(), SQLITE_STATIC);
×
2142
            continue;
×
2143
        }
2144
        if (strcmp(name, ":log_path") == 0) {
601✔
2145
            const auto& filename = lf->get_filename();
×
2146
            sqlite3_bind_text(stmt,
×
2147
                              lpc + 1,
2148
                              filename.c_str(),
2149
                              filename.native().length(),
×
2150
                              SQLITE_STATIC);
2151
            continue;
×
2152
        }
2153
        if (strcmp(name, ":log_unique_path") == 0) {
601✔
2154
            const auto& filename = lf->get_unique_path();
×
2155
            sqlite3_bind_text(stmt,
×
2156
                              lpc + 1,
2157
                              filename.c_str(),
2158
                              filename.native().length(),
×
2159
                              SQLITE_STATIC);
2160
            continue;
×
2161
        }
2162
        if (strcmp(name, ":log_text") == 0) {
601✔
2163
            sqlite3_bind_text(
4✔
2164
                stmt, lpc + 1, sbr.get_data(), sbr.length(), SQLITE_STATIC);
4✔
2165
            continue;
4✔
2166
        }
2167
        if (strcmp(name, ":log_body") == 0) {
597✔
2168
            auto body_attr_opt = get_string_attr(sa, SA_BODY);
16✔
2169
            if (body_attr_opt) {
16✔
2170
                const auto& sar
2171
                    = body_attr_opt.value().saw_string_attr->sa_range;
16✔
2172

2173
                sqlite3_bind_text(stmt,
32✔
2174
                                  lpc + 1,
2175
                                  sbr.get_data_at(sar.lr_start),
16✔
2176
                                  sar.length(),
2177
                                  SQLITE_STATIC);
2178
            } else {
2179
                sqlite3_bind_null(stmt, lpc + 1);
×
2180
            }
2181
            continue;
16✔
2182
        }
16✔
2183
        if (strcmp(name, ":log_raw_text") == 0) {
581✔
2184
            auto res = lf->read_raw_message(ll);
×
2185

2186
            if (res.isOk()) {
×
2187
                raw_sbr = res.unwrap();
×
2188
                sqlite3_bind_text(stmt,
×
2189
                                  lpc + 1,
2190
                                  raw_sbr.get_data(),
2191
                                  raw_sbr.length(),
×
2192
                                  SQLITE_STATIC);
2193
            }
2194
            continue;
×
2195
        }
2196
        if (strcmp(name, ":log_opid") == 0) {
581✔
2197
            bind_to_sqlite(stmt, lpc + 1, values.lvv_opid_value);
×
2198
            continue;
×
2199
        }
2200
        if (strcmp(name, ":log_opid_definition") == 0) {
581✔
2201
            if (values.lvv_opid_value) {
×
2202
                auto opids = lf->get_opids().readAccess();
×
2203

2204
                auto iter = opids->los_opid_ranges.find(
×
2205
                    values.lvv_opid_value.value());
×
2206
                if (iter != opids->los_opid_ranges.end()
×
2207
                    && iter->second.otr_description.lod_index)
×
2208
                {
2209
                    const auto& opid_def
2210
                        = (*format->lf_opid_description_def_vec)
×
2211
                            [iter->second.otr_description.lod_index.value()];
×
2212
                    bind_to_sqlite(stmt, lpc + 1, opid_def->od_name);
×
2213
                } else {
2214
                    sqlite3_bind_null(stmt, lpc + 1);
×
2215
                }
2216
            } else {
×
2217
                sqlite3_bind_null(stmt, lpc + 1);
×
2218
            }
2219
            continue;
×
2220
        }
2221
        if (strcmp(name, ":log_src_file") == 0) {
581✔
2222
            bind_to_sqlite(stmt, lpc + 1, values.lvv_src_file_value);
×
2223
            continue;
×
2224
        }
2225
        if (strcmp(name, ":log_src_line") == 0) {
581✔
2226
            bind_to_sqlite(stmt, lpc + 1, values.lvv_src_line_value);
×
2227
            continue;
×
2228
        }
2229
        if (strcmp(name, ":log_thread_id") == 0) {
581✔
2230
            bind_to_sqlite(stmt, lpc + 1, values.lvv_thread_id_value);
×
2231
            continue;
×
2232
        }
2233
        for (const auto& lv : values.lvv_values) {
6,782✔
2234
            if (lv.lv_meta.lvm_name != &name[1]) {
6,777✔
2235
                continue;
6,201✔
2236
            }
2237

2238
            switch (lv.lv_meta.lvm_kind) {
576✔
2239
                case value_kind_t::VALUE_BOOLEAN:
×
2240
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
2241
                    break;
×
2242
                case value_kind_t::VALUE_FLOAT:
×
2243
                    sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
2244
                    break;
×
2245
                case value_kind_t::VALUE_INTEGER:
436✔
2246
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
436✔
2247
                    break;
436✔
2248
                case value_kind_t::VALUE_NULL:
×
2249
                    sqlite3_bind_null(stmt, lpc + 1);
×
2250
                    break;
×
2251
                default:
140✔
2252
                    sqlite3_bind_text(stmt,
140✔
2253
                                      lpc + 1,
2254
                                      lv.text_value(),
2255
                                      lv.text_length(),
140✔
2256
                                      SQLITE_TRANSIENT);
2257
                    break;
140✔
2258
            }
2259
            break;
576✔
2260
        }
2261
    }
2262

2263
    auto step_res = sqlite3_step(stmt);
626✔
2264

2265
    sqlite3_reset(stmt);
626✔
2266
    sqlite3_clear_bindings(stmt);
626✔
2267
    switch (step_res) {
626✔
2268
        case SQLITE_OK:
477✔
2269
        case SQLITE_DONE:
2270
            return Ok(false);
954✔
2271
        case SQLITE_ROW:
148✔
2272
            return Ok(true);
296✔
2273
        default:
1✔
2274
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2275
    }
2276
}
626✔
2277

2278
bool
2279
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
15,574✔
2280
{
2281
    auto retval = true;
15,574✔
2282

2283
    if (this->lss_marked_only) {
15,574✔
2284
        auto found_mark = ll->is_marked() || ll->is_expr_marked();
6✔
2285
        auto to_start_ll = ll;
6✔
2286
        while (!found_mark && to_start_ll->is_continued()) {
6✔
2287
            if (to_start_ll->is_marked() || to_start_ll->is_expr_marked()) {
×
2288
                found_mark = true;
×
2289
            }
2290
            --to_start_ll;
×
2291
        }
2292
        auto to_end_ll = std::next(ll);
6✔
2293
        while (!found_mark && to_end_ll != (*ld)->get_file_ptr()->end()
10✔
2294
               && to_end_ll->is_continued())
11✔
2295
        {
2296
            if (to_end_ll->is_marked() || to_end_ll->is_expr_marked()) {
1✔
2297
                found_mark = true;
1✔
2298
            }
2299
            ++to_end_ll;
1✔
2300
        }
2301
        if (!found_mark) {
6✔
2302
            retval = false;
3✔
2303
        }
2304
    }
2305

2306
    if (ll->get_msg_level() < this->tss_min_log_level) {
15,574✔
2307
        this->tss_level_filtered_count += 1;
2✔
2308
        retval = false;
2✔
2309
    }
2310

2311
    if (*ll < this->ttt_min_row_time) {
15,574✔
2312
        retval = false;
36✔
2313
    }
2314

2315
    if (!(*ll <= this->ttt_max_row_time)) {
15,574✔
2316
        retval = false;
4✔
2317
    }
2318

2319
    return retval;
15,574✔
2320
}
2321

2322
void
2323
logfile_sub_source::invalidate_sql_filter()
47✔
2324
{
2325
    for (auto& ld : *this) {
94✔
2326
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
47✔
2327
    }
2328
}
47✔
2329

2330
void
2331
logfile_sub_source::text_mark(const bookmark_type_t* bm,
83✔
2332
                              vis_line_t line,
2333
                              bool added)
2334
{
2335
    if (line >= (int) this->lss_index.size()) {
83✔
2336
        return;
×
2337
    }
2338

2339
    auto cl = this->at(line);
83✔
2340

2341
    if (bm == &textview_curses::BM_USER) {
83✔
2342
        auto* ll = this->find_line(cl);
33✔
2343

2344
        ll->set_mark(added);
33✔
2345
    }
2346
    if (added) {
83✔
2347
        this->lss_user_marks[bm].insert_once(cl);
75✔
2348
    } else {
2349
        this->lss_user_marks[bm].erase(cl);
8✔
2350
    }
2351
    if (bm == &textview_curses::BM_META
83✔
2352
        && this->lss_meta_grepper.gps_proc != nullptr)
16✔
2353
    {
2354
        this->tss_view->search_range(
1✔
2355
            line, grep_proc<vis_line_t>::until_line(line + 1_vl));
1✔
2356
        this->tss_view->search_new_data();
1✔
2357
    }
2358
}
2359

2360
void
2361
logfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
40✔
2362
{
2363
    if (bm == &textview_curses::BM_USER) {
40✔
2364
        for (auto iter = this->lss_user_marks[bm].bv_tree.begin();
6✔
2365
             iter != this->lss_user_marks[bm].bv_tree.end();
6✔
2366
             ++iter)
×
2367
        {
2368
            this->find_line(*iter)->set_mark(false);
×
2369
        }
2370
    }
2371
    this->lss_user_marks[bm].clear();
40✔
2372
}
40✔
2373

2374
void
2375
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
574✔
2376
{
2377
    auto iter = std::find_if(
574✔
2378
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
1,148✔
2379
    if (iter != this->lss_files.end()) {
574✔
2380
        int file_index = iter - this->lss_files.begin();
574✔
2381

2382
        (*iter)->clear();
574✔
2383
        for (auto& bv : this->lss_user_marks) {
5,740✔
2384
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
5,166✔
2385
            auto mark_end
2386
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
5,166✔
2387
            auto file_range = bv.equal_range(mark_curr, mark_end);
5,166✔
2388

2389
            if (file_range.first != file_range.second) {
5,166✔
2390
                auto to_del = std::vector<content_line_t>{};
78✔
2391
                for (auto file_iter = file_range.first;
78✔
2392
                     file_iter != file_range.second;
200✔
2393
                     ++file_iter)
122✔
2394
                {
2395
                    to_del.emplace_back(*file_iter);
122✔
2396
                }
2397

2398
                for (auto cl : to_del) {
200✔
2399
                    bv.erase(cl);
122✔
2400
                }
2401
            }
78✔
2402
        }
2403

2404
        this->lss_force_rebuild = true;
574✔
2405
    }
2406
    while (!this->lss_files.empty()) {
1,148✔
2407
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
626✔
2408
            this->lss_files.pop_back();
574✔
2409
        } else {
2410
            break;
52✔
2411
        }
2412
    }
2413
    this->lss_token_file = nullptr;
574✔
2414
}
574✔
2415

2416
std::optional<vis_line_t>
2417
logfile_sub_source::find_from_content(content_line_t cl)
5✔
2418
{
2419
    content_line_t line = cl;
5✔
2420
    std::shared_ptr<logfile> lf = this->find(line);
5✔
2421

2422
    if (lf != nullptr) {
5✔
2423
        auto ll_iter = lf->begin() + line;
5✔
2424
        auto& ll = *ll_iter;
5✔
2425
        auto vis_start_opt = this->find_from_time(ll.get_timeval());
5✔
2426

2427
        if (!vis_start_opt) {
5✔
2428
            return std::nullopt;
×
2429
        }
2430

2431
        auto vis_start = *vis_start_opt;
5✔
2432

2433
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2434
            content_line_t guess_cl = this->at(vis_start);
5✔
2435

2436
            if (cl == guess_cl) {
5✔
2437
                return vis_start;
5✔
2438
            }
2439

2440
            auto guess_line = this->find_line(guess_cl);
×
2441

2442
            if (!guess_line || ll < *guess_line) {
×
2443
                return std::nullopt;
×
2444
            }
2445

2446
            ++vis_start;
×
2447
        }
2448
    }
2449

2450
    return std::nullopt;
×
2451
}
5✔
2452

2453
void
2454
logfile_sub_source::reload_index_delegate()
704✔
2455
{
2456
    if (this->lss_index_delegate == nullptr) {
704✔
2457
        return;
×
2458
    }
2459

2460
    this->lss_index_delegate->index_start(*this);
704✔
2461
    for (const auto index : this->lss_filtered_index) {
732✔
2462
        auto cl = this->lss_index[index].value();
28✔
2463
        uint64_t line_number;
2464
        auto ld = this->find_data(cl, line_number);
28✔
2465
        std::shared_ptr<logfile> lf = (*ld)->get_file();
28✔
2466

2467
        this->lss_index_delegate->index_line(
28✔
2468
            *this, lf.get(), lf->begin() + line_number);
28✔
2469
    }
28✔
2470
    this->lss_index_delegate->index_complete(*this);
704✔
2471
}
2472

2473
std::optional<std::shared_ptr<text_filter>>
2474
logfile_sub_source::get_sql_filter()
2,634✔
2475
{
2476
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
2,634✔
2477
               return filt->get_index() == 0
268✔
2478
                   && dynamic_cast<sql_filter*>(filt.get()) != nullptr;
268✔
2479
           })
2480
        | lnav::itertools::deref();
5,268✔
2481
}
2482

2483
void
2484
log_location_history::loc_history_append(vis_line_t top)
116✔
2485
{
2486
    if (top < 0_vl || top >= vis_line_t(this->llh_log_source.text_line_count()))
116✔
2487
    {
2488
        return;
1✔
2489
    }
2490

2491
    auto cl = this->llh_log_source.at(top);
115✔
2492

2493
    auto iter = this->llh_history.begin();
115✔
2494
    iter += this->llh_history.size() - this->lh_history_position;
115✔
2495
    this->llh_history.erase_from(iter);
115✔
2496
    this->lh_history_position = 0;
115✔
2497
    this->llh_history.push_back(cl);
115✔
2498
}
2499

2500
std::optional<vis_line_t>
2501
log_location_history::loc_history_back(vis_line_t current_top)
2✔
2502
{
2503
    while (this->lh_history_position < this->llh_history.size()) {
2✔
2504
        auto iter = this->llh_history.rbegin();
2✔
2505

2506
        auto vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2507

2508
        if (this->lh_history_position == 0 && vis_for_pos != current_top) {
2✔
2509
            return vis_for_pos;
2✔
2510
        }
2511

2512
        if ((this->lh_history_position + 1) >= this->llh_history.size()) {
2✔
2513
            break;
×
2514
        }
2515

2516
        this->lh_history_position += 1;
2✔
2517

2518
        iter += this->lh_history_position;
2✔
2519

2520
        vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2521

2522
        if (vis_for_pos) {
2✔
2523
            return vis_for_pos;
2✔
2524
        }
2525
    }
2526

2527
    return std::nullopt;
×
2528
}
2529

2530
std::optional<vis_line_t>
2531
log_location_history::loc_history_forward(vis_line_t current_top)
1✔
2532
{
2533
    while (this->lh_history_position > 0) {
1✔
2534
        this->lh_history_position -= 1;
1✔
2535

2536
        auto iter = this->llh_history.rbegin();
1✔
2537

2538
        iter += this->lh_history_position;
1✔
2539

2540
        auto vis_for_pos = this->llh_log_source.find_from_content(*iter);
1✔
2541

2542
        if (vis_for_pos) {
1✔
2543
            return vis_for_pos;
1✔
2544
        }
2545
    }
2546

2547
    return std::nullopt;
×
2548
}
2549

2550
bool
2551
sql_filter::matches(std::optional<line_source> ls_opt,
175✔
2552
                    const shared_buffer_ref& line)
2553
{
2554
    if (!ls_opt) {
175✔
2555
        return false;
×
2556
    }
2557

2558
    auto ls = ls_opt;
175✔
2559

2560
    if (!ls->ls_line->is_message()) {
175✔
2561
        return false;
45✔
2562
    }
2563
    if (this->sf_filter_stmt == nullptr) {
130✔
2564
        return false;
×
2565
    }
2566

2567
    auto lfp = ls->ls_file.shared_from_this();
130✔
2568
    auto ld = this->sf_log_source.find_data_i(lfp);
130✔
2569
    if (ld == this->sf_log_source.end()) {
130✔
2570
        return false;
×
2571
    }
2572

2573
    auto eval_res = this->sf_log_source.eval_sql_filter(
130✔
2574
        this->sf_filter_stmt, ld, ls->ls_line);
130✔
2575
    if (eval_res.unwrapOr(true)) {
130✔
2576
        return false;
81✔
2577
    }
2578

2579
    return true;
49✔
2580
}
130✔
2581

2582
std::string
2583
sql_filter::to_command() const
1✔
2584
{
2585
    return fmt::format(FMT_STRING("filter-expr {}"), this->lf_id);
4✔
2586
}
2587

2588
std::optional<line_info>
2589
logfile_sub_source::meta_grepper::grep_value_for_line(vis_line_t line,
×
2590
                                                      std::string& value_out)
2591
{
2592
    auto line_meta_opt = this->lmg_source.find_bookmark_metadata(line);
×
2593
    if (!line_meta_opt) {
×
2594
        value_out.clear();
×
2595
    } else {
2596
        auto& bm = *(line_meta_opt.value());
×
2597

2598
        {
2599
            md2attr_line mdal;
×
2600

2601
            auto parse_res = md4cpp::parse(bm.bm_comment, mdal);
×
2602
            if (parse_res.isOk()) {
×
2603
                value_out.append(parse_res.unwrap().get_string());
×
2604
            } else {
2605
                value_out.append(bm.bm_comment);
×
2606
            }
2607
        }
2608

2609
        value_out.append("\x1c");
×
NEW
2610
        for (const auto& entry : bm.bm_tags) {
×
NEW
2611
            value_out.append(entry.te_tag);
×
UNCOV
2612
            value_out.append("\x1c");
×
2613
        }
2614
        value_out.append("\x1c");
×
2615
        for (const auto& pair : bm.bm_annotations.la_pairs) {
×
2616
            value_out.append(pair.first);
×
2617
            value_out.append("\x1c");
×
2618

2619
            md2attr_line mdal;
×
2620

2621
            auto parse_res = md4cpp::parse(pair.second, mdal);
×
2622
            if (parse_res.isOk()) {
×
2623
                value_out.append(parse_res.unwrap().get_string());
×
2624
            } else {
2625
                value_out.append(pair.second);
×
2626
            }
2627
            value_out.append("\x1c");
×
2628
        }
2629
        value_out.append("\x1c");
×
2630
        value_out.append(bm.bm_opid);
×
2631
    }
2632

2633
    if (!this->lmg_done) {
×
2634
        return line_info{};
×
2635
    }
2636

2637
    return std::nullopt;
×
2638
}
2639

2640
vis_line_t
2641
logfile_sub_source::meta_grepper::grep_initial_line(vis_line_t start,
×
2642
                                                    vis_line_t highest)
2643
{
2644
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2645
    auto& bv = bm[&textview_curses::BM_META];
×
2646

2647
    if (bv.empty()) {
×
2648
        return -1_vl;
×
2649
    }
2650
    return *bv.bv_tree.begin();
×
2651
}
2652

2653
void
2654
logfile_sub_source::meta_grepper::grep_next_line(vis_line_t& line)
×
2655
{
2656
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2657
    auto& bv = bm[&textview_curses::BM_META];
×
2658

2659
    auto line_opt = bv.next(vis_line_t(line));
×
2660
    if (!line_opt) {
×
2661
        this->lmg_done = true;
×
2662
    }
2663
    line = line_opt.value_or(-1_vl);
×
2664
}
2665

2666
void
2667
logfile_sub_source::meta_grepper::grep_begin(grep_proc<vis_line_t>& gp,
23✔
2668
                                             vis_line_t start,
2669
                                             vis_line_t stop)
2670
{
2671
    this->lmg_source.quiesce();
23✔
2672

2673
    this->lmg_source.tss_view->grep_begin(gp, start, stop);
23✔
2674
}
23✔
2675

2676
void
2677
logfile_sub_source::meta_grepper::grep_end(grep_proc<vis_line_t>& gp)
23✔
2678
{
2679
    this->lmg_source.tss_view->grep_end(gp);
23✔
2680
}
23✔
2681

2682
void
2683
logfile_sub_source::meta_grepper::grep_match(grep_proc<vis_line_t>& gp,
3✔
2684
                                             vis_line_t line)
2685
{
2686
    this->lmg_source.tss_view->grep_match(gp, line);
3✔
2687
}
3✔
2688

2689
static std::vector<breadcrumb::possibility>
2690
timestamp_poss()
11✔
2691
{
2692
    const static std::vector<breadcrumb::possibility> retval = {
2693
        breadcrumb::possibility{"-1 day"},
2694
        breadcrumb::possibility{"-1h"},
2695
        breadcrumb::possibility{"-30m"},
2696
        breadcrumb::possibility{"-15m"},
2697
        breadcrumb::possibility{"-5m"},
2698
        breadcrumb::possibility{"-1m"},
2699
        breadcrumb::possibility{"+1m"},
2700
        breadcrumb::possibility{"+5m"},
2701
        breadcrumb::possibility{"+15m"},
2702
        breadcrumb::possibility{"+30m"},
2703
        breadcrumb::possibility{"+1h"},
2704
        breadcrumb::possibility{"+1 day"},
2705
    };
95✔
2706

2707
    return retval;
11✔
2708
}
24✔
2709

2710
static attr_line_t
2711
to_display(const std::shared_ptr<logfile>& lf)
30✔
2712
{
2713
    const auto& loo = lf->get_open_options();
30✔
2714
    attr_line_t retval;
30✔
2715

2716
    if (loo.loo_piper) {
30✔
2717
        if (!lf->get_open_options().loo_piper->is_finished()) {
×
2718
            retval.append("\u21bb "_list_glyph);
×
2719
        }
2720
    } else if (loo.loo_child_poller && loo.loo_child_poller->is_alive()) {
30✔
2721
        retval.append("\u21bb "_list_glyph);
×
2722
    }
2723
    retval.append(lf->get_unique_path());
30✔
2724

2725
    return retval;
30✔
2726
}
×
2727

2728
void
2729
logfile_sub_source::text_crumbs_for_line(int line,
28✔
2730
                                         std::vector<breadcrumb::crumb>& crumbs)
2731
{
2732
    text_sub_source::text_crumbs_for_line(line, crumbs);
28✔
2733

2734
    if (this->lss_filtered_index.empty()) {
28✔
2735
        return;
9✔
2736
    }
2737

2738
    auto vl = vis_line_t(line);
19✔
2739
    auto bmc = this->get_bookmark_metadata_context(
19✔
2740
        vl, bookmark_metadata::categories::partition);
2741
    if (bmc.bmc_current_metadata) {
19✔
2742
        const auto& name = bmc.bmc_current_metadata.value()->bm_name;
1✔
2743
        auto key = text_anchors::to_anchor_string(name);
1✔
2744
        auto display = attr_line_t()
1✔
2745
                           .append("\u2291 "_symbol)
1✔
2746
                           .append(lnav::roles::variable(name))
2✔
2747
                           .move();
1✔
2748
        crumbs.emplace_back(
1✔
2749
            key,
2750
            display,
2751
            [this]() -> std::vector<breadcrumb::possibility> {
×
2752
                auto& vb = this->tss_view->get_bookmarks();
1✔
2753
                const auto& bv = vb[&textview_curses::BM_PARTITION];
1✔
2754
                std::vector<breadcrumb::possibility> retval;
1✔
2755

2756
                for (const auto& vl : bv.bv_tree) {
2✔
2757
                    auto meta_opt = this->find_bookmark_metadata(vl);
1✔
2758
                    if (!meta_opt || meta_opt.value()->bm_name.empty()) {
1✔
2759
                        continue;
×
2760
                    }
2761

2762
                    const auto& name = meta_opt.value()->bm_name;
1✔
2763
                    retval.emplace_back(text_anchors::to_anchor_string(name),
1✔
2764
                                        name);
2765
                }
2766

2767
                return retval;
1✔
2768
            },
×
2769
            [ec = this->lss_exec_context](const auto& part) {
1✔
2770
                auto cmd = fmt::format(FMT_STRING(":goto {}"),
×
2771
                                       part.template get<std::string>());
2772
                ec->execute(INTERNAL_SRC_LOC, cmd);
×
2773
            });
×
2774
    }
1✔
2775

2776
    auto line_pair_opt = this->find_line_with_file(vl);
19✔
2777
    if (!line_pair_opt) {
19✔
2778
        return;
×
2779
    }
2780
    auto line_pair = line_pair_opt.value();
19✔
2781
    auto& lf = line_pair.first;
19✔
2782
    auto format = lf->get_format();
19✔
2783
    char ts[64];
2784

2785
    sql_strftime(ts, sizeof(ts), line_pair.second->get_timeval(), 'T');
19✔
2786

2787
    crumbs.emplace_back(std::string(ts),
19✔
2788
                        timestamp_poss,
2789
                        [ec = this->lss_exec_context](const auto& ts) {
19✔
2790
                            auto cmd
×
2791
                                = fmt::format(FMT_STRING(":goto {}"),
×
2792
                                              ts.template get<std::string>());
2793
                            ec->execute(INTERNAL_SRC_LOC, cmd);
×
2794
                        });
×
2795
    crumbs.back().c_expected_input
19✔
2796
        = breadcrumb::crumb::expected_input_t::anything;
19✔
2797
    crumbs.back().c_search_placeholder = "(Enter an absolute or relative time)";
19✔
2798

2799
    auto format_name = format->get_name().to_string();
19✔
2800

2801
    crumbs.emplace_back(
19✔
2802
        format_name,
2803
        attr_line_t().append(format_name),
19✔
2804
        [this]() -> std::vector<breadcrumb::possibility> {
×
2805
            return this->lss_files
11✔
2806
                | lnav::itertools::filter_in([](const auto& file_data) {
22✔
2807
                       return file_data->is_visible();
11✔
2808
                   })
2809
                | lnav::itertools::map(&logfile_data::get_file_ptr)
33✔
2810
                | lnav::itertools::map(&logfile::get_format_name)
33✔
2811
                | lnav::itertools::unique()
33✔
2812
                | lnav::itertools::map([](const auto& elem) {
44✔
2813
                       return breadcrumb::possibility{
2814
                           elem.to_string(),
2815
                       };
11✔
2816
                   })
2817
                | lnav::itertools::to_vector();
33✔
2818
        },
2819
        [ec = this->lss_exec_context](const auto& format_name) {
19✔
2820
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2821
     SET selection = ifnull(
2822
         (SELECT log_line FROM all_logs WHERE log_format = $format_name LIMIT 1),
2823
         (SELECT raise_error(
2824
            'Could not find format: ' || $format_name,
2825
            'The corresponding log messages might have been filtered out'))
2826
       )
2827
     WHERE name = 'log'
2828
)";
2829

2830
            ec->execute_with(
×
2831
                INTERNAL_SRC_LOC,
×
2832
                MOVE_STMT,
2833
                std::make_pair("format_name",
2834
                               format_name.template get<std::string>()));
2835
        });
×
2836

2837
    auto msg_start_iter = lf->message_start(line_pair.second);
19✔
2838
    auto file_line_number = std::distance(lf->begin(), msg_start_iter);
19✔
2839
    crumbs.emplace_back(
38✔
2840
        lf->get_unique_path(),
19✔
2841
        to_display(lf).appendf(FMT_STRING("[{:L}]"), file_line_number),
76✔
2842
        [this]() -> std::vector<breadcrumb::possibility> {
×
2843
            return this->lss_files
11✔
2844
                | lnav::itertools::filter_in([](const auto& file_data) {
22✔
2845
                       return file_data->is_visible();
11✔
2846
                   })
2847
                | lnav::itertools::map([](const auto& file_data) {
22✔
2848
                       return breadcrumb::possibility{
2849
                           file_data->get_file_ptr()->get_unique_path(),
11✔
2850
                           to_display(file_data->get_file()),
2851
                       };
11✔
2852
                   });
22✔
2853
        },
2854
        [ec = this->lss_exec_context](const auto& uniq_path) {
19✔
2855
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2856
     SET selection = ifnull(
2857
          (SELECT log_line FROM all_logs WHERE log_unique_path = $uniq_path LIMIT 1),
2858
          (SELECT raise_error(
2859
            'Could not find file: ' || $uniq_path,
2860
            'The corresponding log messages might have been filtered out'))
2861
         )
2862
     WHERE name = 'log'
2863
)";
2864

2865
            ec->execute_with(
×
2866
                INTERNAL_SRC_LOC,
×
2867
                MOVE_STMT,
2868
                std::make_pair("uniq_path",
2869
                               uniq_path.template get<std::string>()));
2870
        });
×
2871

2872
    shared_buffer sb;
19✔
2873
    logline_value_vector values;
19✔
2874
    auto& sbr = values.lvv_sbr;
19✔
2875

2876
    lf->read_full_message(msg_start_iter, sbr);
19✔
2877
    attr_line_t al(to_string(sbr));
19✔
2878
    if (!sbr.get_metadata().m_valid_utf) {
19✔
2879
        scrub_to_utf8(al.al_string.data(), al.al_string.length());
×
2880
    }
2881
    if (sbr.get_metadata().m_has_ansi) {
19✔
2882
        // bleh
2883
        scrub_ansi_string(al.get_string(), &al.al_attrs);
×
2884
        sbr.share(sb, al.al_string.data(), al.al_string.size());
×
2885
    }
2886
    format->annotate(lf.get(), file_line_number, al.get_attrs(), values);
19✔
2887

2888
    {
2889
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
31✔
2890
          SET selection = ifnull(
2891
            (SELECT log_line FROM all_logs WHERE log_thread_id = $tid LIMIT 1),
2892
            (SELECT raise_error('Could not find thread ID: ' || $tid,
2893
                                'The corresponding log messages might have been filtered out')))
2894
          WHERE name = 'log'
2895
        )";
2896
        static const auto ELLIPSIS = "\u22ef"_frag;
2897

2898
        auto tid_display = values.lvv_thread_id_value.has_value()
19✔
2899
            ? lnav::roles::identifier(values.lvv_thread_id_value.value())
19✔
2900
            : lnav::roles::hidden(ELLIPSIS);
19✔
2901
        crumbs.emplace_back(
19✔
2902
            (values.lvv_thread_id_value.has_value()
19✔
2903
                 ? values.lvv_thread_id_value.value()
19✔
2904
                 : ""_frag)
18✔
2905
                .to_string(),
38✔
2906
            attr_line_t()
19✔
2907
                .append(ui_icon_t::thread)
19✔
2908
                .append(" ")
19✔
2909
                .append(tid_display),
2910
            [this]() -> std::vector<breadcrumb::possibility> {
×
2911
                std::set<std::string> poss_strs;
11✔
2912

2913
                for (const auto& file_data : this->lss_files) {
22✔
2914
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2915
                        continue;
×
2916
                    }
2917
                    safe::ReadAccess<logfile::safe_thread_id_state> r_tid_map(
2918
                        file_data->get_file_ptr()->get_thread_ids());
11✔
2919

2920
                    for (const auto& pair : r_tid_map->ltis_tid_ranges) {
24✔
2921
                        poss_strs.emplace(pair.first.to_string());
13✔
2922
                    }
2923
                }
11✔
2924

2925
                std::vector<breadcrumb::possibility> retval;
11✔
2926

2927
                std::transform(poss_strs.begin(),
11✔
2928
                               poss_strs.end(),
2929
                               std::back_inserter(retval),
2930
                               [](const auto& tid_str) {
13✔
2931
                                   return breadcrumb::possibility(tid_str);
13✔
2932
                               });
2933

2934
                return retval;
22✔
2935
            },
11✔
2936
            [ec = this->lss_exec_context](const auto& tid) {
19✔
2937
                ec->execute_with(
×
2938
                    INTERNAL_SRC_LOC,
×
2939
                    MOVE_STMT,
2940
                    std::make_pair("tid", tid.template get<std::string>()));
2941
            });
×
2942
    }
19✔
2943

2944
    {
2945
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
31✔
2946
          SET selection = ifnull(
2947
            (SELECT log_line FROM all_logs WHERE log_opid = $opid LIMIT 1),
2948
            (SELECT raise_error('Could not find opid: ' || $opid,
2949
                                'The corresponding log messages might have been filtered out')))
2950
          WHERE name = 'log'
2951
        )";
2952
        static const std::string ELLIPSIS = "\u22ef";
31✔
2953

2954
        auto opid_display = values.lvv_opid_value.has_value()
19✔
2955
            ? lnav::roles::identifier(values.lvv_opid_value.value())
9✔
2956
            : lnav::roles::hidden(ELLIPSIS);
28✔
2957
        crumbs.emplace_back(
38✔
2958
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
48✔
2959
                                              : "",
2960
            attr_line_t().append(opid_display),
19✔
2961
            [this]() -> std::vector<breadcrumb::possibility> {
×
2962
                std::unordered_set<std::string> poss_strs;
11✔
2963

2964
                for (const auto& file_data : this->lss_files) {
22✔
2965
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2966
                        continue;
×
2967
                    }
2968
                    safe::ReadAccess<logfile::safe_opid_state> r_opid_map(
2969
                        file_data->get_file_ptr()->get_opids());
11✔
2970

2971
                    poss_strs.reserve(poss_strs.size()
22✔
2972
                                      + r_opid_map->los_opid_ranges.size());
11✔
2973
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
783✔
2974
                        poss_strs.insert(pair.first.to_string());
772✔
2975
                    }
2976
                }
11✔
2977

2978
                std::vector<breadcrumb::possibility> retval;
11✔
2979
                retval.reserve(poss_strs.size());
11✔
2980

2981
                std::transform(poss_strs.begin(),
11✔
2982
                               poss_strs.end(),
2983
                               std::back_inserter(retval),
2984
                               [](const auto& opid_str) {
772✔
2985
                                   return breadcrumb::possibility(opid_str);
772✔
2986
                               });
2987

2988
                return retval;
22✔
2989
            },
11✔
2990
            [ec = this->lss_exec_context](const auto& opid) {
19✔
2991
                ec->execute_with(
×
2992
                    INTERNAL_SRC_LOC,
×
2993
                    MOVE_STMT,
2994
                    std::make_pair("opid", opid.template get<std::string>()));
2995
            });
×
2996
    }
19✔
2997

2998
    auto sf = string_fragment::from_str(al.get_string());
19✔
2999
    auto body_opt = get_string_attr(al.get_attrs(), SA_BODY);
19✔
3000
    auto nl_pos_opt = sf.find('\n');
19✔
3001
    auto msg_line_number = std::distance(msg_start_iter, line_pair.second);
19✔
3002
    auto line_from_top = line - msg_line_number;
19✔
3003
    if (body_opt && nl_pos_opt) {
19✔
3004
        if (this->lss_token_meta_line != file_line_number
20✔
3005
            || this->lss_token_meta_size != sf.length())
10✔
3006
        {
3007
            if (body_opt->saw_string_attr->sa_range.length() < 128 * 1024) {
3✔
3008
                this->lss_token_meta
3009
                    = lnav::document::discover(al)
3✔
3010
                          .over_range(
3✔
3011
                              body_opt.value().saw_string_attr->sa_range)
3✔
3012
                          .perform();
3✔
3013
                // XXX discover_structure() changes `al`, have to recompute
3014
                // stuff
3015
                sf = al.to_string_fragment();
3✔
3016
                body_opt = get_string_attr(al.get_attrs(), SA_BODY);
3✔
3017
            } else {
3018
                this->lss_token_meta = lnav::document::metadata{};
×
3019
            }
3020
            this->lss_token_meta_line = file_line_number;
3✔
3021
            this->lss_token_meta_size = sf.length();
3✔
3022
        }
3023

3024
        const auto initial_size = crumbs.size();
10✔
3025
        auto sf_body
3026
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
10✔
3027
                           body_opt->saw_string_attr->sa_range.lr_end);
10✔
3028
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
10✔
3029
        file_off_t line_end_offset = sf.length();
10✔
3030
        ssize_t line_number = 0;
10✔
3031

3032
        for (const auto& sf_line : sf_body.split_lines()) {
20✔
3033
            if (line_number >= msg_line_number) {
13✔
3034
                line_end_offset = line_offset + sf_line.length();
3✔
3035
                break;
3✔
3036
            }
3037
            line_number += 1;
10✔
3038
            line_offset += sf_line.length();
10✔
3039
        }
10✔
3040

3041
        this->lss_token_meta.m_sections_tree.visit_overlapping(
10✔
3042
            line_offset,
3043
            line_end_offset,
3044
            [this,
2✔
3045
             initial_size,
3046
             meta = &this->lss_token_meta,
10✔
3047
             &crumbs,
3048
             line_from_top](const auto& iv) {
3049
                auto path = crumbs | lnav::itertools::skip(initial_size)
6✔
3050
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
4✔
3051
                    | lnav::itertools::append(iv.value);
2✔
3052
                auto curr_node = lnav::document::hier_node::lookup_path(
2✔
3053
                    meta->m_sections_root.get(), path);
2✔
3054

3055
                crumbs.emplace_back(
4✔
3056
                    iv.value,
2✔
3057
                    [meta, path]() { return meta->possibility_provider(path); },
4✔
3058
                    [this, curr_node, path, line_from_top](const auto& key) {
4✔
3059
                        if (!curr_node) {
×
3060
                            return;
×
3061
                        }
3062
                        auto* parent_node = curr_node.value()->hn_parent;
×
3063
                        if (parent_node == nullptr) {
×
3064
                            return;
×
3065
                        }
3066
                        key.match(
3067
                            [parent_node](const std::string& str) {
×
3068
                                return parent_node->find_line_number(str);
×
3069
                            },
3070
                            [parent_node](size_t index) {
×
3071
                                return parent_node->find_line_number(index);
×
3072
                            })
3073
                            | [this, line_from_top](auto line_number) {
×
3074
                                  this->tss_view->set_selection(
×
3075
                                      vis_line_t(line_from_top + line_number));
×
3076
                              };
3077
                    });
3078
                if (curr_node
2✔
3079
                    && !curr_node.value()->hn_parent->is_named_only()) {
2✔
3080
                    auto node = lnav::document::hier_node::lookup_path(
×
3081
                        meta->m_sections_root.get(), path);
×
3082

3083
                    crumbs.back().c_expected_input
×
3084
                        = curr_node.value()
×
3085
                              ->hn_parent->hn_named_children.empty()
×
3086
                        ? breadcrumb::crumb::expected_input_t::index
×
3087
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
3088
                    crumbs.back().with_possible_range(
×
3089
                        node | lnav::itertools::map([](const auto hn) {
×
3090
                            return hn->hn_parent->hn_children.size();
×
3091
                        })
3092
                        | lnav::itertools::unwrap_or(size_t{0}));
×
3093
                }
3094
            });
2✔
3095

3096
        auto path = crumbs | lnav::itertools::skip(initial_size)
20✔
3097
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
20✔
3098
        auto node = lnav::document::hier_node::lookup_path(
10✔
3099
            this->lss_token_meta.m_sections_root.get(), path);
10✔
3100

3101
        if (node && !node.value()->hn_children.empty()) {
10✔
3102
            auto poss_provider = [curr_node = node.value()]() {
1✔
3103
                std::vector<breadcrumb::possibility> retval;
1✔
3104
                for (const auto& child : curr_node->hn_named_children) {
1✔
3105
                    retval.emplace_back(child.first);
×
3106
                }
3107
                return retval;
1✔
3108
            };
3109
            auto path_performer
3110
                = [this, curr_node = node.value(), line_from_top](
2✔
3111
                      const breadcrumb::crumb::key_t& value) {
3112
                      value.match(
×
3113
                          [curr_node](const std::string& str) {
×
3114
                              return curr_node->find_line_number(str);
×
3115
                          },
3116
                          [curr_node](size_t index) {
×
3117
                              return curr_node->find_line_number(index);
×
3118
                          })
3119
                          | [this, line_from_top](size_t line_number) {
×
3120
                                this->tss_view->set_selection(
×
3121
                                    vis_line_t(line_from_top + line_number));
×
3122
                            };
3123
                  };
1✔
3124
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
1✔
3125
            crumbs.back().c_expected_input
1✔
3126
                = node.value()->hn_named_children.empty()
2✔
3127
                ? breadcrumb::crumb::expected_input_t::index
1✔
3128
                : breadcrumb::crumb::expected_input_t::index_or_exact;
3129
        }
3130
    }
10✔
3131
}
19✔
3132

3133
void
3134
logfile_sub_source::quiesce()
38✔
3135
{
3136
    for (auto& ld : this->lss_files) {
76✔
3137
        auto* lf = ld->get_file_ptr();
38✔
3138

3139
        if (lf == nullptr) {
38✔
3140
            continue;
×
3141
        }
3142

3143
        lf->quiesce();
38✔
3144
    }
3145
}
38✔
3146

3147
bookmark_metadata&
3148
logfile_sub_source::get_bookmark_metadata(content_line_t cl)
30✔
3149
{
3150
    auto line_pair = this->find_line_with_file(cl).value();
30✔
3151
    auto line_number = static_cast<uint32_t>(
3152
        std::distance(line_pair.first->begin(), line_pair.second));
30✔
3153

3154
    return line_pair.first->get_bookmark_metadata()[line_number];
60✔
3155
}
30✔
3156

3157
logfile_sub_source::bookmark_metadata_context
3158
logfile_sub_source::get_bookmark_metadata_context(
4,638✔
3159
    vis_line_t vl, bookmark_metadata::categories desired) const
3160
{
3161
    const auto& vb = this->tss_view->get_bookmarks();
4,638✔
3162
    const auto& bv = vb[desired == bookmark_metadata::categories::partition
3163
                            ? &textview_curses::BM_PARTITION
3164
                            : &textview_curses::BM_META];
4,638✔
3165
    auto vl_iter = bv.bv_tree.lower_bound(vl + 1_vl);
4,638✔
3166

3167
    std::optional<vis_line_t> next_line;
4,638✔
3168
    for (auto next_vl_iter = vl_iter; next_vl_iter != bv.bv_tree.end();
4,638✔
3169
         ++next_vl_iter)
×
3170
    {
3171
        auto bm_opt = this->find_bookmark_metadata(*next_vl_iter);
3✔
3172
        if (!bm_opt) {
3✔
3173
            continue;
×
3174
        }
3175

3176
        if (bm_opt.value()->has(desired)) {
3✔
3177
            next_line = *next_vl_iter;
3✔
3178
            break;
3✔
3179
        }
3180
    }
3181
    if (vl_iter == bv.bv_tree.begin()) {
4,638✔
3182
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
4,621✔
3183
    }
3184

3185
    --vl_iter;
17✔
3186
    while (true) {
3187
        auto bm_opt = this->find_bookmark_metadata(*vl_iter);
17✔
3188
        if (bm_opt) {
17✔
3189
            if (bm_opt.value()->has(desired)) {
14✔
3190
                return bookmark_metadata_context{
3191
                    *vl_iter, bm_opt.value(), next_line};
17✔
3192
            }
3193
        }
3194

3195
        if (vl_iter == bv.bv_tree.begin()) {
3✔
3196
            return bookmark_metadata_context{
3197
                std::nullopt, std::nullopt, next_line};
3✔
3198
        }
3199
        --vl_iter;
×
3200
    }
3201
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3202
}
3203

3204
std::optional<bookmark_metadata*>
3205
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
25,739✔
3206
{
3207
    auto line_pair = this->find_line_with_file(cl).value();
25,739✔
3208
    auto line_number = static_cast<uint32_t>(
3209
        std::distance(line_pair.first->begin(), line_pair.second));
25,739✔
3210

3211
    auto& bm = line_pair.first->get_bookmark_metadata();
25,739✔
3212
    auto bm_iter = bm.find(line_number);
25,739✔
3213
    if (bm_iter == bm.end()) {
25,739✔
3214
        return std::nullopt;
25,326✔
3215
    }
3216

3217
    return &bm_iter->second;
413✔
3218
}
25,739✔
3219

3220
void
3221
logfile_sub_source::erase_bookmark_metadata(content_line_t cl)
26✔
3222
{
3223
    auto line_pair = this->find_line_with_file(cl).value();
26✔
3224
    auto line_number = static_cast<uint32_t>(
3225
        std::distance(line_pair.first->begin(), line_pair.second));
26✔
3226

3227
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
3228
    auto bm_iter = bm.find(line_number);
26✔
3229
    if (bm_iter != bm.end()) {
26✔
3230
        bm.erase(bm_iter);
6✔
3231
    }
3232
}
26✔
3233

3234
void
3235
logfile_sub_source::clear_bookmark_metadata()
6✔
3236
{
3237
    for (auto& ld : *this) {
14✔
3238
        if (ld->get_file_ptr() == nullptr) {
8✔
3239
            continue;
×
3240
        }
3241

3242
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3243
    }
3244
}
6✔
3245

3246
void
3247
logfile_sub_source::increase_line_context()
×
3248
{
3249
    auto old_context = this->lss_line_context;
×
3250

3251
    switch (this->lss_line_context) {
×
3252
        case line_context_t::filename:
×
3253
            // nothing to do
3254
            break;
×
3255
        case line_context_t::basename:
×
3256
            this->lss_line_context = line_context_t::filename;
×
3257
            break;
×
3258
        case line_context_t::none:
×
3259
            this->lss_line_context = line_context_t::basename;
×
3260
            break;
×
3261
        case line_context_t::time_column:
×
3262
            this->lss_line_context = line_context_t::none;
×
3263
            break;
×
3264
    }
3265
    if (old_context != this->lss_line_context) {
×
3266
        this->clear_line_size_cache();
×
3267
    }
3268
}
3269

3270
bool
3271
logfile_sub_source::decrease_line_context()
×
3272
{
3273
    static const auto& cfg
3274
        = injector::get<const logfile_sub_source_ns::config&>();
×
3275
    auto old_context = this->lss_line_context;
×
3276

3277
    switch (this->lss_line_context) {
×
3278
        case line_context_t::filename:
×
3279
            this->lss_line_context = line_context_t::basename;
×
3280
            break;
×
3281
        case line_context_t::basename:
×
3282
            this->lss_line_context = line_context_t::none;
×
3283
            break;
×
3284
        case line_context_t::none:
×
3285
            if (cfg.c_time_column
×
3286
                != logfile_sub_source_ns::time_column_feature_t::Disabled)
3287
            {
3288
                this->lss_line_context = line_context_t::time_column;
×
3289
            }
3290
            break;
×
3291
        case line_context_t::time_column:
×
3292
            break;
×
3293
    }
3294
    if (old_context != this->lss_line_context) {
×
3295
        this->clear_line_size_cache();
×
3296

3297
        return true;
×
3298
    }
3299

3300
    return false;
×
3301
}
3302

3303
size_t
3304
logfile_sub_source::get_filename_offset() const
297✔
3305
{
3306
    switch (this->lss_line_context) {
297✔
3307
        case line_context_t::filename:
×
3308
            return this->lss_filename_width;
×
3309
        case line_context_t::basename:
×
3310
            return this->lss_basename_width;
×
3311
        default:
297✔
3312
            return 0;
297✔
3313
    }
3314
}
3315

3316
size_t
3317
logfile_sub_source::file_count() const
4,621✔
3318
{
3319
    size_t retval = 0;
4,621✔
3320
    const_iterator iter;
4,621✔
3321

3322
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
8,778✔
3323
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
4,157✔
3324
            retval += 1;
4,151✔
3325
        }
3326
    }
3327

3328
    return retval;
4,621✔
3329
}
3330

3331
size_t
3332
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3333
                                       int row,
3334
                                       text_sub_source::line_flags_t flags)
3335
{
3336
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3337

3338
    if (this->lss_line_size_cache[index].first != row) {
×
3339
        std::string value;
×
3340

3341
        this->text_value_for_line(tc, row, value, flags);
×
3342
        scrub_ansi_string(value, nullptr);
×
3343
        auto line_width = string_fragment::from_str(value).column_width();
×
3344
        if (this->lss_line_context == line_context_t::time_column) {
×
3345
            auto time_attr
3346
                = find_string_attr(this->lss_token_al.al_attrs, &L_TIMESTAMP);
×
3347
            if (time_attr != this->lss_token_al.al_attrs.end()) {
×
3348
                line_width -= time_attr->sa_range.length();
×
3349
                auto format = this->lss_token_file->get_format();
×
3350
                if (format->lf_level_hideable) {
×
3351
                    auto level_attr = find_string_attr(
×
3352
                        this->lss_token_al.al_attrs, &L_LEVEL);
×
3353
                    if (level_attr != this->lss_token_al.al_attrs.end()) {
×
3354
                        line_width -= level_attr->sa_range.length();
×
3355
                    }
3356
                }
3357
            }
3358
        }
3359
        this->lss_line_size_cache[index].second = line_width;
×
3360
        this->lss_line_size_cache[index].first = row;
×
3361
    }
3362
    return this->lss_line_size_cache[index].second;
×
3363
}
3364

3365
int
3366
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
2✔
3367
{
3368
    int retval = 0;
2✔
3369

3370
    for (const auto& ld : this->lss_files) {
4✔
3371
        retval += ld->ld_filter_state.lfo_filter_state
2✔
3372
                      .tfs_filter_hits[filter_index];
2✔
3373
    }
3374

3375
    return retval;
2✔
3376
}
3377

3378
std::optional<vis_line_t>
3379
logfile_sub_source::row_for(const row_info& ri)
324✔
3380
{
3381
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
648✔
3382
                               this->lss_filtered_index.end(),
3383
                               ri.ri_time,
324✔
3384
                               filtered_logline_cmp(*this));
3385
    if (lb != this->lss_filtered_index.end()) {
324✔
3386
        auto first_lb = lb;
319✔
3387
        while (true) {
3388
            auto cl = this->lss_index[*lb].value();
337✔
3389
            if (content_line_t(ri.ri_id) == cl) {
337✔
3390
                first_lb = lb;
285✔
3391
                break;
285✔
3392
            }
3393
            auto ll = this->find_line(cl);
52✔
3394
            if (ll->get_timeval() != ri.ri_time) {
52✔
3395
                break;
34✔
3396
            }
3397
            auto next_lb = std::next(lb);
18✔
3398
            if (next_lb == this->lss_filtered_index.end()) {
18✔
3399
                break;
×
3400
            }
3401
            lb = next_lb;
18✔
3402
        }
18✔
3403

3404
        const auto dst
3405
            = std::distance(this->lss_filtered_index.begin(), first_lb);
319✔
3406
        return vis_line_t(dst);
319✔
3407
    }
3408

3409
    return std::nullopt;
5✔
3410
}
3411

3412
std::unique_ptr<logline_window>
3413
logfile_sub_source::window_at(vis_line_t start_vl, vis_line_t end_vl)
58✔
3414
{
3415
    return std::make_unique<logline_window>(*this, start_vl, end_vl);
58✔
3416
}
3417

3418
std::unique_ptr<logline_window>
3419
logfile_sub_source::window_at(vis_line_t start_vl)
260✔
3420
{
3421
    return std::make_unique<logline_window>(*this, start_vl, start_vl + 1_vl);
260✔
3422
}
3423

3424
std::unique_ptr<logline_window>
3425
logfile_sub_source::window_to_end(vis_line_t start_vl)
1✔
3426
{
3427
    return std::make_unique<logline_window>(
3428
        *this, start_vl, vis_line_t(this->text_line_count()));
1✔
3429
}
3430

3431
std::optional<vis_line_t>
3432
logfile_sub_source::row_for_anchor(const std::string& id)
3✔
3433
{
3434
    if (startswith(id, "#msg")) {
3✔
3435
        static const auto ANCHOR_RE
3436
            = lnav::pcre2pp::code::from_const(R"(#msg([0-9a-fA-F]+)-(.+))");
3✔
3437
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
3✔
3438

3439
        if (ANCHOR_RE.capture_from(id).into(md).found_p()) {
3✔
3440
            auto scan_res = scn::scan<int64_t>(md[1]->to_string_view(), "{:x}");
3✔
3441
            if (scan_res) {
3✔
3442
                auto ts_low = std::chrono::microseconds{scan_res->value()};
3✔
3443
                auto ts_high = ts_low + 1us;
3✔
3444

3445
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3446
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3447
                if (low_vl) {
3✔
3448
                    auto lw = this->window_at(
3449
                        low_vl.value(),
3✔
3450
                        high_vl.value_or(low_vl.value() + 1_vl));
6✔
3451

3452
                    for (const auto& li : *lw) {
3✔
3453
                        auto hash_res = li.get_line_hash();
3✔
3454
                        if (hash_res.isErr()) {
3✔
3455
                            auto errmsg = hash_res.unwrapErr();
×
3456

3457
                            log_error("unable to get line hash: %s",
×
3458
                                      errmsg.c_str());
3459
                            continue;
×
3460
                        }
3461

3462
                        auto hash = hash_res.unwrap();
3✔
3463
                        if (hash == md[2]) {
3✔
3464
                            return li.get_vis_line();
3✔
3465
                        }
3466
                    }
12✔
3467
                }
3✔
3468
            }
3469
        }
3470

3471
        return std::nullopt;
×
3472
    }
3473

3474
    auto& vb = this->tss_view->get_bookmarks();
×
3475
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3476

3477
    for (const auto& vl : bv.bv_tree) {
×
3478
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3479
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3480
            continue;
×
3481
        }
3482

3483
        const auto& name = meta_opt.value()->bm_name;
×
3484
        if (id == text_anchors::to_anchor_string(name)) {
×
3485
            return vl;
×
3486
        }
3487
    }
3488

3489
    return std::nullopt;
×
3490
}
3491

3492
std::optional<vis_line_t>
3493
logfile_sub_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
2✔
3494
{
3495
    if (vl < this->lss_filtered_index.size()) {
2✔
3496
        auto file_and_line_pair = this->find_line_with_file(vl);
2✔
3497
        if (file_and_line_pair) {
2✔
3498
            const auto& [lf, line] = file_and_line_pair.value();
2✔
3499
            if (line->is_continued()) {
2✔
3500
                auto retval = vl;
×
3501
                switch (dir) {
×
3502
                    case direction::prev: {
×
3503
                        auto first_line = line;
×
3504
                        while (first_line->is_continued()) {
×
3505
                            --first_line;
×
3506
                            retval -= 1_vl;
×
3507
                        }
3508
                        return retval;
×
3509
                    }
3510
                    case direction::next: {
×
3511
                        auto first_line = line;
×
3512
                        while (first_line->is_continued()) {
×
3513
                            ++first_line;
×
3514
                            retval += 1_vl;
×
3515
                        }
3516
                        return retval;
×
3517
                    }
3518
                }
3519
            }
3520
        }
3521
    }
2✔
3522

3523
    auto bmc = this->get_bookmark_metadata_context(
2✔
3524
        vl, bookmark_metadata::categories::partition);
3525
    switch (dir) {
2✔
3526
        case direction::prev: {
×
3527
            if (bmc.bmc_current && bmc.bmc_current.value() != vl) {
×
3528
                return bmc.bmc_current;
×
3529
            }
3530
            if (!bmc.bmc_current || bmc.bmc_current.value() == 0_vl) {
×
3531
                return 0_vl;
×
3532
            }
3533
            auto prev_bmc = this->get_bookmark_metadata_context(
×
3534
                bmc.bmc_current.value() - 1_vl,
×
3535
                bookmark_metadata::categories::partition);
3536
            if (!prev_bmc.bmc_current) {
×
3537
                return 0_vl;
×
3538
            }
3539
            return prev_bmc.bmc_current;
×
3540
        }
3541
        case direction::next:
2✔
3542
            return bmc.bmc_next_line;
2✔
3543
    }
3544
    return std::nullopt;
×
3545
}
3546

3547
std::optional<std::string>
3548
logfile_sub_source::anchor_for_row(vis_line_t vl)
88✔
3549
{
3550
    auto line_meta = this->get_bookmark_metadata_context(
88✔
3551
        vl, bookmark_metadata::categories::partition);
3552
    if (!line_meta.bmc_current_metadata) {
88✔
3553
        auto lw = window_at(vl);
85✔
3554

3555
        for (const auto& li : *lw) {
85✔
3556
            auto hash_res = li.get_line_hash();
85✔
3557
            if (hash_res.isErr()) {
85✔
3558
                auto errmsg = hash_res.unwrapErr();
×
3559
                log_error("unable to compute line hash: %s", errmsg.c_str());
×
3560
                break;
×
3561
            }
3562
            auto hash = hash_res.unwrap();
85✔
3563
            auto retval = fmt::format(
3564
                FMT_STRING("#msg{:016x}-{}"),
170✔
3565
                li.get_logline().get_time<std::chrono::microseconds>().count(),
85✔
3566
                hash);
×
3567

3568
            return retval;
85✔
3569
        }
340✔
3570

3571
        return std::nullopt;
×
3572
    }
85✔
3573

3574
    return text_anchors::to_anchor_string(
3✔
3575
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3576
}
3577

3578
std::unordered_set<std::string>
3579
logfile_sub_source::get_anchors()
×
3580
{
3581
    auto& vb = this->tss_view->get_bookmarks();
×
3582
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3583
    std::unordered_set<std::string> retval;
×
3584

3585
    for (const auto& vl : bv.bv_tree) {
×
3586
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3587
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3588
            continue;
×
3589
        }
3590

3591
        const auto& name = meta_opt.value()->bm_name;
×
3592
        retval.emplace(text_anchors::to_anchor_string(name));
×
3593
    }
3594

3595
    return retval;
×
3596
}
×
3597

3598
bool
3599
logfile_sub_source::text_handle_mouse(
×
3600
    textview_curses& tc,
3601
    const listview_curses::display_line_content_t& mouse_line,
3602
    mouse_event& me)
3603
{
3604
    if (mouse_line.is<listview_curses::static_overlay_content>()
×
3605
        && this->text_line_count() > 0)
×
3606
    {
3607
        auto top = tc.get_top();
×
3608
        if (top > 0) {
×
3609
            auto win = this->window_at(top - 1_vl);
×
3610
            for (const auto& li : *win) {
×
3611
                tc.set_top(li.get_vis_line());
×
3612
                tc.set_selection(li.get_vis_line());
×
3613
                return true;
×
3614
            }
3615
        }
3616
    }
3617

3618
    if (tc.get_overlay_selection()) {
×
3619
        auto nci = ncinput{};
×
3620
        if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 2, 4)) {
×
3621
            nci.id = ' ';
×
3622
            nci.eff_text[0] = ' ';
×
3623
            this->list_input_handle_key(tc, nci);
×
3624
        } else if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 5, 6)) {
×
3625
            nci.id = '#';
×
3626
            nci.eff_text[0] = '#';
×
3627
            this->list_input_handle_key(tc, nci);
×
3628
        }
3629
    }
3630
    return true;
×
3631
}
3632

3633
void
3634
logfile_sub_source::reload_config(error_reporter& reporter)
725✔
3635
{
3636
    static const auto& cfg
3637
        = injector::get<const logfile_sub_source_ns::config&>();
725✔
3638

3639
    switch (cfg.c_time_column) {
725✔
3640
        case logfile_sub_source_ns::time_column_feature_t::Default:
×
3641
            if (this->lss_line_context == line_context_t::none) {
×
3642
                this->lss_line_context = line_context_t::time_column;
×
3643
            }
3644
            break;
×
3645
        case logfile_sub_source_ns::time_column_feature_t::Disabled:
725✔
3646
            if (this->lss_line_context == line_context_t::time_column) {
725✔
3647
                this->lss_line_context = line_context_t::none;
×
3648
            }
3649
            break;
725✔
3650
        case logfile_sub_source_ns::time_column_feature_t::Enabled:
×
3651
            break;
×
3652
    }
3653
}
725✔
3654

3655
void
3656
logfile_sub_source::clear_preview()
1✔
3657
{
3658
    text_sub_source::clear_preview();
1✔
3659

3660
    this->set_preview_sql_filter(nullptr);
1✔
3661
    auto last = std::remove_if(this->lss_highlighters.begin(),
1✔
3662
                               this->lss_highlighters.end(),
3663
                               [](const auto& hl) { return hl.h_preview; });
×
3664
    this->lss_highlighters.erase(last, this->lss_highlighters.end());
1✔
3665
}
1✔
3666

3667
void
3668
logfile_sub_source::add_commands_for_session(
57✔
3669
    const std::function<void(const std::string&)>& receiver)
3670
{
3671
    text_sub_source::add_commands_for_session(receiver);
57✔
3672

3673
    auto mark_expr = this->get_sql_marker_text();
57✔
3674
    if (!mark_expr.empty()) {
57✔
3675
        receiver(fmt::format(FMT_STRING("mark-expr {}"), mark_expr));
4✔
3676
    }
3677

3678
    for (const auto& hl : this->lss_highlighters) {
57✔
3679
        auto cmd = "highlight-field "s;
×
3680
        if (hl.h_attrs.has_style(text_attrs::style::bold)) {
×
3681
            cmd += "--bold ";
×
3682
        }
3683
        if (hl.h_attrs.has_style(text_attrs::style::underline)) {
×
3684
            cmd += "--underline ";
×
3685
        }
3686
        if (hl.h_attrs.has_style(text_attrs::style::blink)) {
×
3687
            cmd += "--blink ";
×
3688
        }
3689
        if (hl.h_attrs.has_style(text_attrs::style::struck)) {
×
3690
            cmd += "--strike ";
×
3691
        }
3692
        if (hl.h_attrs.has_style(text_attrs::style::italic)) {
×
3693
            cmd += "--italic ";
×
3694
        }
3695
        cmd += hl.h_field.to_string() + " " + hl.h_regex->get_pattern();
×
3696
        receiver(cmd);
×
3697
    }
3698

3699
    for (const auto& format : log_format::get_root_formats()) {
4,494✔
3700
        auto field_states = format->get_field_states();
4,437✔
3701

3702
        for (const auto& fs_pair : field_states) {
60,975✔
3703
            if (!fs_pair.second.lvm_user_hidden) {
56,538✔
3704
                continue;
56,536✔
3705
            }
3706

3707
            if (fs_pair.second.lvm_user_hidden.value()) {
2✔
3708
                receiver(fmt::format(FMT_STRING("hide-fields {}.{}"),
8✔
3709
                                     format->get_name().to_string(),
4✔
3710
                                     fs_pair.first.to_string()));
4✔
3711
            } else if (fs_pair.second.lvm_hidden) {
×
3712
                receiver(fmt::format(FMT_STRING("show-fields {}.{}"),
×
3713
                                     format->get_name().to_string(),
×
3714
                                     fs_pair.first.to_string()));
×
3715
            }
3716
        }
3717
    }
4,437✔
3718

3719
    for (const auto& [schema_id, bp] : this->lss_breakpoints) {
58✔
3720
        if (bp.bp_source != breakpoint_info::source_type::src_location) {
1✔
3721
            continue;
×
3722
        }
3723
        receiver(fmt::format(FMT_STRING("breakpoint {}"), bp.bp_description));
4✔
3724
    }
3725
}
57✔
3726

3727
void
3728
logfile_sub_source::update_filter_hash_state(hasher& h) const
9✔
3729
{
3730
    text_sub_source::update_filter_hash_state(h);
9✔
3731

3732
    for (const auto& ld : this->lss_files) {
12✔
3733
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
3✔
3734
            h.update(0);
×
3735
        } else {
3736
            h.update(1);
3✔
3737
        }
3738
    }
3739
    h.update(this->tss_min_log_level);
9✔
3740
    h.update(this->lss_marked_only);
9✔
3741
}
9✔
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