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

tstack / lnav / 19092286039-2639

05 Nov 2025 05:26AM UTC coverage: 68.913% (-0.007%) from 68.92%
19092286039-2639

push

github

tstack
[session] skip restoring commands

Related to #1450

94 of 116 new or added lines in 8 files covered. (81.03%)

40 existing lines in 8 files now uncovered.

50578 of 73394 relevant lines covered (68.91%)

429603.27 hits per line

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

63.99
/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 <future>
33

34
#include "logfile_sub_source.hh"
35

36
#include <sqlite3.h>
37

38
#include "base/ansi_scrubber.hh"
39
#include "base/ansi_vars.hh"
40
#include "base/fs_util.hh"
41
#include "base/injector.hh"
42
#include "base/itertools.enumerate.hh"
43
#include "base/itertools.hh"
44
#include "base/string_util.hh"
45
#include "bookmarks.json.hh"
46
#include "command_executor.hh"
47
#include "config.h"
48
#include "field_overlay_source.hh"
49
#include "hasher.hh"
50
#include "k_merge_tree.h"
51
#include "lnav_util.hh"
52
#include "log_accel.hh"
53
#include "logfile_sub_source.cfg.hh"
54
#include "logline_window.hh"
55
#include "md2attr_line.hh"
56
#include "ptimec.hh"
57
#include "scn/scan.h"
58
#include "shlex.hh"
59
#include "sql_util.hh"
60
#include "vtab_module.hh"
61
#include "yajlpp/yajlpp.hh"
62
#include "yajlpp/yajlpp_def.hh"
63

64
using namespace std::chrono_literals;
65
using namespace lnav::roles::literals;
66

67
const DIST_SLICE(bm_types) bookmark_type_t logfile_sub_source::BM_FILES("file");
68

69
static int
70
pretty_sql_callback(exec_context& ec, sqlite3_stmt* stmt)
73✔
71
{
72
    if (!sqlite3_stmt_busy(stmt)) {
73✔
73
        return 0;
32✔
74
    }
75

76
    const auto ncols = sqlite3_column_count(stmt);
41✔
77

78
    for (int lpc = 0; lpc < ncols; lpc++) {
82✔
79
        if (!ec.ec_accumulator->empty()) {
41✔
80
            ec.ec_accumulator->append(", ");
10✔
81
        }
82

83
        const char* res = (const char*) sqlite3_column_text(stmt, lpc);
41✔
84
        if (res == nullptr) {
41✔
85
            continue;
×
86
        }
87

88
        ec.ec_accumulator->append(res);
41✔
89
    }
90

91
    for (int lpc = 0; lpc < ncols; lpc++) {
82✔
92
        const auto* colname = sqlite3_column_name(stmt, lpc);
41✔
93
        auto* raw_value = sqlite3_column_value(stmt, lpc);
41✔
94
        auto value_type = sqlite3_value_type(raw_value);
41✔
95
        scoped_value_t value;
41✔
96

97
        switch (value_type) {
41✔
98
            case SQLITE_INTEGER:
×
99
                value = (int64_t) sqlite3_value_int64(raw_value);
×
100
                break;
×
101
            case SQLITE_FLOAT:
×
102
                value = sqlite3_value_double(raw_value);
×
103
                break;
×
104
            case SQLITE_NULL:
×
105
                value = null_value_t{};
×
106
                break;
×
107
            default:
41✔
108
                value = string_fragment::from_bytes(
41✔
109
                    sqlite3_value_text(raw_value),
110
                    sqlite3_value_bytes(raw_value));
82✔
111
                break;
41✔
112
        }
113
        if (!ec.ec_local_vars.empty() && !ec.ec_dry_run) {
41✔
114
            if (sql_ident_needs_quote(colname)) {
41✔
115
                continue;
27✔
116
            }
117
            auto& vars = ec.ec_local_vars.top();
14✔
118

119
            if (vars.find(colname) != vars.end()) {
42✔
120
                continue;
10✔
121
            }
122

123
            if (value.is<string_fragment>()) {
4✔
124
                value = value.get<string_fragment>().to_string();
4✔
125
            }
126
            vars[colname] = value;
8✔
127
        }
128
    }
41✔
129
    return 0;
41✔
130
}
131

132
static std::future<std::string>
133
pretty_pipe_callback(exec_context& ec, const std::string& cmdline, auto_fd& fd)
×
134
{
135
    auto retval = std::async(std::launch::async, [&]() {
×
136
        char buffer[1024];
137
        std::ostringstream ss;
×
138
        ssize_t rc;
139

140
        while ((rc = read(fd, buffer, sizeof(buffer))) > 0) {
×
141
            ss.write(buffer, rc);
×
142
        }
143

144
        auto retval = ss.str();
×
145

146
        if (endswith(retval, "\n")) {
×
147
            retval.resize(retval.length() - 1);
×
148
        }
149

150
        return retval;
×
151
    });
×
152

153
    return retval;
×
154
}
155

156
logfile_sub_source::logfile_sub_source()
1,150✔
157
    : text_sub_source(1), lnav_config_listener(__FILE__),
158
      lss_meta_grepper(*this), lss_location_history(*this)
1,150✔
159
{
160
    this->tss_supports_filtering = true;
1,150✔
161
    this->clear_line_size_cache();
1,150✔
162
    this->clear_min_max_row_times();
1,150✔
163
}
1,150✔
164

165
std::shared_ptr<logfile>
166
logfile_sub_source::find(const char* fn, content_line_t& line_base)
24✔
167
{
168
    std::shared_ptr<logfile> retval = nullptr;
24✔
169

170
    line_base = content_line_t(0);
24✔
171
    for (auto iter = this->lss_files.begin();
24✔
172
         iter != this->lss_files.end() && retval == nullptr;
49✔
173
         ++iter)
25✔
174
    {
175
        auto& ld = *(*iter);
25✔
176
        auto* lf = ld.get_file_ptr();
25✔
177

178
        if (lf == nullptr) {
25✔
179
            continue;
×
180
        }
181
        if (strcmp(lf->get_filename_as_string().c_str(), fn) == 0) {
25✔
182
            retval = ld.get_file();
24✔
183
        } else {
184
            line_base += content_line_t(MAX_LINES_PER_FILE);
1✔
185
        }
186
    }
187

188
    return retval;
24✔
189
}
×
190

191
struct filtered_logline_cmp {
192
    filtered_logline_cmp(const logfile_sub_source& lc) : llss_controller(lc) {}
335✔
193

194
    bool operator()(const uint32_t& lhs, const uint32_t& rhs) const
195
    {
196
        auto cl_lhs = llss_controller.lss_index[lhs].value();
197
        auto cl_rhs = llss_controller.lss_index[rhs].value();
198
        auto ll_lhs = this->llss_controller.find_line(cl_lhs);
199
        auto ll_rhs = this->llss_controller.find_line(cl_rhs);
200

201
        if (ll_lhs == nullptr) {
202
            return true;
203
        }
204
        if (ll_rhs == nullptr) {
205
            return false;
206
        }
207
        return (*ll_lhs) < (*ll_rhs);
208
    }
209

210
    bool operator()(const uint32_t& lhs, const timeval& rhs) const
1,106✔
211
    {
212
        const auto cl_lhs = llss_controller.lss_index[lhs].value();
1,106✔
213
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
1,106✔
214

215
        if (ll_lhs == nullptr) {
1,106✔
216
            return true;
×
217
        }
218
        return (*ll_lhs) < rhs;
1,106✔
219
    }
220

221
    const logfile_sub_source& llss_controller;
222
};
223

224
std::optional<vis_line_t>
225
logfile_sub_source::find_from_time(const timeval& start) const
83✔
226
{
227
    const auto lb = std::lower_bound(this->lss_filtered_index.begin(),
83✔
228
                                     this->lss_filtered_index.end(),
229
                                     start,
230
                                     filtered_logline_cmp(*this));
231
    if (lb != this->lss_filtered_index.end()) {
83✔
232
        auto retval = std::distance(this->lss_filtered_index.begin(), lb);
66✔
233
        return vis_line_t(retval);
66✔
234
    }
235

236
    return std::nullopt;
17✔
237
}
238

239
line_info
240
logfile_sub_source::text_value_for_line(textview_curses& tc,
3,395✔
241
                                        int row,
242
                                        std::string& value_out,
243
                                        line_flags_t flags)
244
{
245
    if (this->lss_indexing_in_progress) {
3,395✔
246
        value_out = "";
×
247
        this->lss_token_attrs.clear();
×
248
        return {};
×
249
    }
250

251
    line_info retval;
3,395✔
252
    content_line_t line(0);
3,395✔
253

254
    require_ge(row, 0);
3,395✔
255
    require_lt((size_t) row, this->lss_filtered_index.size());
3,395✔
256

257
    line = this->at(vis_line_t(row));
3,395✔
258

259
    if (flags & RF_RAW) {
3,395✔
260
        auto lf = this->find(line);
32✔
261
        auto ll = lf->begin() + line;
32✔
262
        retval.li_file_range = lf->get_file_range(ll, false);
32✔
263
        retval.li_level = ll->get_msg_level();
32✔
264
        // retval.li_timestamp = ll->get_timeval();
265
        retval.li_partial = false;
32✔
266
        retval.li_utf8_scan_result.usr_has_ansi = ll->has_ansi();
32✔
267
        retval.li_utf8_scan_result.usr_message = ll->is_valid_utf() ? nullptr
32✔
268
                                                                    : "bad";
269
        // timeval start_time, end_time;
270
        // gettimeofday(&start_time, NULL);
271
        value_out = lf->read_line(lf->begin() + line)
64✔
272
                        .map([](auto sbr) { return to_string(sbr); })
64✔
273
                        .unwrapOr({});
32✔
274
        // gettimeofday(&end_time, NULL);
275
        // timeval diff = end_time - start_time;
276
        // log_debug("read time %d.%06d %s:%d", diff.tv_sec, diff.tv_usec,
277
        // lf->get_filename().c_str(), line);
278
        return retval;
32✔
279
    }
32✔
280

281
    require_false(this->lss_in_value_for_line);
3,363✔
282

283
    this->lss_in_value_for_line = true;
3,363✔
284
    this->lss_token_flags = flags;
3,363✔
285
    this->lss_token_file_data = this->find_data(line);
3,363✔
286
    this->lss_token_file = (*this->lss_token_file_data)->get_file();
3,363✔
287
    this->lss_token_line = this->lss_token_file->begin() + line;
3,363✔
288

289
    this->lss_token_attrs.clear();
3,363✔
290
    this->lss_token_values.clear();
3,363✔
291
    this->lss_share_manager.invalidate_refs();
3,363✔
292
    if (flags & text_sub_source::RF_FULL) {
3,363✔
293
        shared_buffer_ref sbr;
48✔
294

295
        this->lss_token_file->read_full_message(this->lss_token_line, sbr);
48✔
296
        this->lss_token_value = to_string(sbr);
48✔
297
        if (sbr.get_metadata().m_has_ansi) {
48✔
298
            scrub_ansi_string(this->lss_token_value, &this->lss_token_attrs);
17✔
299
            sbr.get_metadata().m_has_ansi = false;
17✔
300
        }
301
    } else {
48✔
302
        this->lss_token_value
303
            = this->lss_token_file->read_line(this->lss_token_line)
6,630✔
304
                  .map([](auto sbr) { return to_string(sbr); })
6,630✔
305
                  .unwrapOr({});
3,315✔
306
        if (this->lss_token_line->has_ansi()) {
3,315✔
307
            scrub_ansi_string(this->lss_token_value, &this->lss_token_attrs);
24✔
308
        }
309
    }
310
    this->lss_token_shift_start = 0;
3,363✔
311
    this->lss_token_shift_size = 0;
3,363✔
312

313
    auto format = this->lss_token_file->get_format();
3,363✔
314

315
    value_out = this->lss_token_value;
3,363✔
316

317
    auto& sbr = this->lss_token_values.lvv_sbr;
3,363✔
318

319
    sbr.share(this->lss_share_manager,
3,363✔
320
              (char*) this->lss_token_value.c_str(),
321
              this->lss_token_value.size());
322
    format->annotate(this->lss_token_file.get(),
6,726✔
323
                     line,
324
                     this->lss_token_attrs,
3,363✔
325
                     this->lss_token_values);
3,363✔
326
    if (flags & RF_REWRITE) {
3,363✔
327
        exec_context ec(
328
            &this->lss_token_values, pretty_sql_callback, pretty_pipe_callback);
48✔
329
        std::string rewritten_line;
48✔
330
        db_label_source rewrite_label_source;
48✔
331

332
        ec.with_perms(exec_context::perm_t::READ_ONLY);
48✔
333
        ec.ec_local_vars.push(std::map<std::string, scoped_value_t>());
48✔
334
        ec.ec_top_line = vis_line_t(row);
48✔
335
        ec.ec_label_source_stack.push_back(&rewrite_label_source);
48✔
336
        add_ansi_vars(ec.ec_global_vars);
48✔
337
        add_global_vars(ec);
48✔
338
        format->rewrite(ec, sbr, this->lss_token_attrs, rewritten_line);
48✔
339
        this->lss_token_value.assign(rewritten_line);
48✔
340
        value_out = this->lss_token_value;
48✔
341
    }
48✔
342

343
    {
344
        auto lr = line_range{0, (int) this->lss_token_value.length()};
3,363✔
345
        this->lss_token_attrs.emplace_back(lr, SA_ORIGINAL_LINE.value());
3,363✔
346
    }
347

348
    std::optional<exttm> adjusted_tm;
3,363✔
349
    auto time_attr = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
3,363✔
350
    if (!this->lss_token_line->is_continued() && !format->lf_formatted_lines
4,892✔
351
        && (this->lss_token_file->is_time_adjusted()
1,293✔
352
            || ((format->lf_timestamp_flags & ETF_ZONE_SET
1,278✔
353
                 || format->lf_date_time.dts_default_zone != nullptr)
967✔
354
                && format->lf_date_time.dts_zoned_to_local)
395✔
355
            || format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
883✔
356
            || !(format->lf_timestamp_flags & ETF_DAY_SET)
883✔
357
            || !(format->lf_timestamp_flags & ETF_MONTH_SET))
862✔
358
        && format->lf_date_time.dts_fmt_lock != -1)
4,892✔
359
    {
360
        if (time_attr != this->lss_token_attrs.end()) {
375✔
361
            const auto time_range = time_attr->sa_range;
375✔
362
            const auto time_sf = string_fragment::from_str_range(
375✔
363
                this->lss_token_value, time_range.lr_start, time_range.lr_end);
375✔
364
            adjusted_tm = format->tm_for_display(this->lss_token_line, time_sf);
375✔
365

366
            char buffer[128];
367
            const char* fmt;
368
            ssize_t len;
369

370
            if (format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
375✔
371
                || !(format->lf_timestamp_flags & ETF_DAY_SET)
315✔
372
                || !(format->lf_timestamp_flags & ETF_MONTH_SET))
690✔
373
            {
374
                if (format->lf_timestamp_flags & ETF_NANOS_SET) {
66✔
375
                    fmt = "%Y-%m-%d %H:%M:%S.%N";
×
376
                } else if (format->lf_timestamp_flags & ETF_MICROS_SET) {
66✔
377
                    fmt = "%Y-%m-%d %H:%M:%S.%f";
61✔
378
                } else if (format->lf_timestamp_flags & ETF_MILLIS_SET) {
5✔
379
                    fmt = "%Y-%m-%d %H:%M:%S.%L";
2✔
380
                } else {
381
                    fmt = "%Y-%m-%d %H:%M:%S";
3✔
382
                }
383
                len = ftime_fmt(
66✔
384
                    buffer, sizeof(buffer), fmt, adjusted_tm.value());
66✔
385
            } else {
386
                len = format->lf_date_time.ftime(
618✔
387
                    buffer,
388
                    sizeof(buffer),
389
                    format->get_timestamp_formats(),
390
                    adjusted_tm.value());
309✔
391
            }
392

393
            value_out.replace(
750✔
394
                time_range.lr_start, time_range.length(), buffer, len);
375✔
395
            this->lss_token_shift_start = time_range.lr_start;
375✔
396
            this->lss_token_shift_size = len - time_range.length();
375✔
397
        }
398
    }
399

400
    // Insert space for the file/search-hit markers.
401
    value_out.insert(0, 1, ' ');
3,363✔
402
    this->lss_time_column_size = 0;
3,363✔
403
    if (this->lss_line_context == line_context_t::time_column) {
3,363✔
404
        if (time_attr != this->lss_token_attrs.end()) {
×
405
            const char* fmt;
406
            if (this->lss_all_timestamp_flags
×
407
                & (ETF_MICROS_SET | ETF_NANOS_SET))
×
408
            {
409
                fmt = "%H:%M:%S.%f";
×
410
            } else if (this->lss_all_timestamp_flags & ETF_MILLIS_SET) {
×
411
                fmt = "%H:%M:%S.%L";
×
412
            } else {
413
                fmt = "%H:%M:%S";
×
414
            }
415
            if (!adjusted_tm) {
×
416
                const auto time_range = time_attr->sa_range;
×
417
                const auto time_sf
418
                    = string_fragment::from_str_range(this->lss_token_value,
×
419
                                                      time_range.lr_start,
×
420
                                                      time_range.lr_end);
×
421
                adjusted_tm
422
                    = format->tm_for_display(this->lss_token_line, time_sf);
×
423
            }
424
            char buffer[128];
425
            this->lss_time_column_size
426
                = ftime_fmt(buffer, sizeof(buffer), fmt, adjusted_tm.value());
×
427
            if (this->tss_view->is_selectable()
×
428
                && this->tss_view->get_selection() == row)
×
429
            {
430
                buffer[this->lss_time_column_size] = ' ';
×
431
                buffer[this->lss_time_column_size + 1] = ' ';
×
432
                this->lss_time_column_size += 2;
×
433
            } else {
434
                constexpr char block[] = "\u258c ";
×
435

436
                strcpy(&buffer[this->lss_time_column_size], block);
×
437
                this->lss_time_column_size += sizeof(block) - 1;
×
438
            }
439
            if (time_attr->sa_range.lr_start != 0) {
×
440
                buffer[this->lss_time_column_size] = ' ';
×
441
                this->lss_time_column_size += 1;
×
442
                this->lss_time_column_padding = 1;
×
443
            } else {
444
                this->lss_time_column_padding = 0;
×
445
            }
446
            value_out.insert(1, buffer, this->lss_time_column_size);
×
447
            this->lss_token_attrs.emplace_back(time_attr->sa_range,
×
448
                                               SA_REPLACED.value());
×
449
        }
450
        if (format->lf_level_hideable) {
×
451
            auto level_attr = find_string_attr(this->lss_token_attrs, &L_LEVEL);
×
452
            if (level_attr != this->lss_token_attrs.end()) {
×
453
                this->lss_token_attrs.emplace_back(level_attr->sa_range,
×
454
                                                   SA_REPLACED.value());
×
455
            }
456
        }
457
    } else if (this->lss_line_context < line_context_t::none) {
3,363✔
458
        size_t file_offset_end;
459
        std::string name;
×
460
        if (this->lss_line_context == line_context_t::filename) {
×
461
            file_offset_end = this->lss_filename_width;
×
462
            name = fmt::to_string(this->lss_token_file->get_filename());
×
463
            if (file_offset_end < name.size()) {
×
464
                file_offset_end = name.size();
×
465
                this->lss_filename_width = name.size();
×
466
            }
467
        } else {
468
            file_offset_end = this->lss_basename_width;
×
469
            name = fmt::to_string(this->lss_token_file->get_unique_path());
×
470
            if (file_offset_end < name.size()) {
×
471
                file_offset_end = name.size();
×
472
                this->lss_basename_width = name.size();
×
473
            }
474
        }
475
        value_out.insert(0, file_offset_end - name.size(), ' ');
×
476
        value_out.insert(0, name);
×
477
    }
478

479
    if (this->tas_display_time_offset) {
3,363✔
480
        auto row_vl = vis_line_t(row);
210✔
481
        auto relstr = this->get_time_offset_for_line(tc, row_vl);
210✔
482
        value_out = fmt::format(FMT_STRING("{: >12}|{}"), relstr, value_out);
840✔
483
    }
210✔
484

485
    this->lss_in_value_for_line = false;
3,363✔
486

487
    return retval;
3,363✔
488
}
3,363✔
489

490
void
491
logfile_sub_source::text_attrs_for_line(textview_curses& lv,
3,363✔
492
                                        int row,
493
                                        string_attrs_t& value_out)
494
{
495
    if (this->lss_indexing_in_progress) {
3,363✔
496
        return;
×
497
    }
498

499
    auto& vc = view_colors::singleton();
3,363✔
500
    logline* next_line = nullptr;
3,363✔
501
    line_range lr;
3,363✔
502
    int time_offset_end = 0;
3,363✔
503
    text_attrs attrs;
3,363✔
504

505
    value_out = this->lss_token_attrs;
3,363✔
506

507
    if ((row + 1) < (int) this->lss_filtered_index.size()) {
3,363✔
508
        next_line = this->find_line(this->at(vis_line_t(row + 1)));
3,180✔
509
    }
510

511
    if (next_line != nullptr
3,363✔
512
        && (day_num(next_line->get_time<std::chrono::seconds>().count())
6,543✔
513
            > day_num(this->lss_token_line->get_time<std::chrono::seconds>()
6,543✔
514
                          .count())))
515
    {
516
        attrs |= text_attrs::style::underline;
15✔
517
    }
518

519
    const auto& line_values = this->lss_token_values;
3,363✔
520

521
    lr.lr_start = 0;
3,363✔
522
    lr.lr_end = -1;
3,363✔
523
    value_out.emplace_back(
3,363✔
524
        lr, SA_LEVEL.value(this->lss_token_line->get_msg_level()));
6,726✔
525

526
    lr.lr_start = time_offset_end;
3,363✔
527
    lr.lr_end = -1;
3,363✔
528

529
    if (!attrs.empty()) {
3,363✔
530
        value_out.emplace_back(lr, VC_STYLE.value(attrs));
15✔
531
    }
532

533
    if (this->lss_token_line->get_msg_level() == log_level_t::LEVEL_INVALID) {
3,363✔
534
        for (auto& token_attr : this->lss_token_attrs) {
37✔
535
            if (token_attr.sa_type != &SA_INVALID) {
20✔
536
                continue;
17✔
537
            }
538

539
            value_out.emplace_back(token_attr.sa_range,
3✔
540
                                   VC_ROLE.value(role_t::VCR_INVALID_MSG));
6✔
541
        }
542
    }
543

544
    for (const auto& line_value : line_values.lvv_values) {
20,798✔
545
        if ((!(this->lss_token_flags & RF_FULL)
38,044✔
546
             && line_value.lv_sub_offset
34,426✔
547
                 != this->lss_token_line->get_sub_offset())
17,213✔
548
            || !line_value.lv_origin.is_valid())
34,648✔
549
        {
550
            continue;
3,174✔
551
        }
552

553
        if (line_value.lv_meta.is_hidden()) {
14,261✔
554
            value_out.emplace_back(line_value.lv_origin,
146✔
555
                                   SA_HIDDEN.value(ui_icon_t::hidden));
292✔
556
        }
557

558
        if (!line_value.lv_meta.lvm_identifier
37,066✔
559
            || !line_value.lv_origin.is_valid())
14,261✔
560
        {
561
            continue;
8,544✔
562
        }
563

564
        value_out.emplace_back(line_value.lv_origin,
5,717✔
565
                               VC_ROLE.value(role_t::VCR_IDENTIFIER));
11,434✔
566
    }
567

568
    if (this->lss_token_shift_size) {
3,363✔
569
        shift_string_attrs(value_out,
94✔
570
                           this->lss_token_shift_start + 1,
94✔
571
                           this->lss_token_shift_size);
572
    }
573

574
    shift_string_attrs(value_out, 0, 1);
3,363✔
575

576
    lr.lr_start = 0;
3,363✔
577
    lr.lr_end = 1;
3,363✔
578
    {
579
        auto& bm = lv.get_bookmarks();
3,363✔
580
        const auto& bv = bm[&BM_FILES];
3,363✔
581
        bool is_first_for_file = bv.bv_tree.exists(vis_line_t(row));
3,363✔
582
        bool is_last_for_file = bv.bv_tree.exists(vis_line_t(row + 1));
3,363✔
583
        auto graph = NCACS_VLINE;
3,363✔
584
        if (is_first_for_file) {
3,363✔
585
            if (is_last_for_file) {
202✔
586
                graph = NCACS_HLINE;
8✔
587
            } else {
588
                graph = NCACS_ULCORNER;
194✔
589
            }
590
        } else if (is_last_for_file) {
3,161✔
591
            graph = NCACS_LLCORNER;
21✔
592
        }
593
        value_out.emplace_back(lr, VC_GRAPHIC.value(graph));
3,363✔
594

595
        if (!(this->lss_token_flags & RF_FULL)) {
3,363✔
596
            const auto& bv_search = bm[&textview_curses::BM_SEARCH];
3,315✔
597

598
            if (bv_search.bv_tree.exists(vis_line_t(row))) {
3,315✔
599
                lr.lr_start = 0;
9✔
600
                lr.lr_end = 1;
9✔
601
                value_out.emplace_back(
9✔
602
                    lr, VC_STYLE.value(text_attrs::with_reverse()));
18✔
603
            }
604
        }
605
    }
606

607
    value_out.emplace_back(lr,
3,363✔
608
                           VC_STYLE.value(vc.attrs_for_ident(
6,726✔
609
                               this->lss_token_file->get_filename())));
3,363✔
610

611
    if (this->lss_line_context < line_context_t::none) {
3,363✔
612
        size_t file_offset_end
×
613
            = (this->lss_line_context == line_context_t::filename)
×
614
            ? this->lss_filename_width
×
615
            : this->lss_basename_width;
616

617
        shift_string_attrs(value_out, 0, file_offset_end);
×
618

619
        lr.lr_start = 0;
×
620
        lr.lr_end = file_offset_end + 1;
×
621
        value_out.emplace_back(lr,
×
622
                               VC_STYLE.value(vc.attrs_for_ident(
×
623
                                   this->lss_token_file->get_filename())));
×
624
    } else if (this->lss_time_column_size > 0) {
3,363✔
625
        shift_string_attrs(value_out, 1, this->lss_time_column_size);
×
626

627
        ui_icon_t icon;
628
        switch (this->lss_token_line->get_msg_level()) {
×
629
            case LEVEL_TRACE:
×
630
                icon = ui_icon_t::log_level_trace;
×
631
                break;
×
632
            case LEVEL_DEBUG:
×
633
            case LEVEL_DEBUG2:
634
            case LEVEL_DEBUG3:
635
            case LEVEL_DEBUG4:
636
            case LEVEL_DEBUG5:
637
                icon = ui_icon_t::log_level_debug;
×
638
                break;
×
639
            case LEVEL_INFO:
×
640
                icon = ui_icon_t::log_level_info;
×
641
                break;
×
642
            case LEVEL_STATS:
×
643
                icon = ui_icon_t::log_level_stats;
×
644
                break;
×
645
            case LEVEL_NOTICE:
×
646
                icon = ui_icon_t::log_level_notice;
×
647
                break;
×
648
            case LEVEL_WARNING:
×
649
                icon = ui_icon_t::log_level_warning;
×
650
                break;
×
651
            case LEVEL_ERROR:
×
652
                icon = ui_icon_t::log_level_error;
×
653
                break;
×
654
            case LEVEL_CRITICAL:
×
655
                icon = ui_icon_t::log_level_critical;
×
656
                break;
×
657
            case LEVEL_FATAL:
×
658
                icon = ui_icon_t::log_level_fatal;
×
659
                break;
×
660
            default:
×
661
                icon = ui_icon_t::hidden;
×
662
                break;
×
663
        }
664
        auto extra_space_size = this->lss_time_column_padding;
×
665
        lr.lr_start = 1 + this->lss_time_column_size - 1 - extra_space_size;
×
666
        lr.lr_end = 1 + this->lss_time_column_size - extra_space_size;
×
667
        value_out.emplace_back(lr, VC_ICON.value(icon));
×
668
        if (this->tss_view->is_selectable()
×
669
            && this->tss_view->get_selection() != row)
×
670
        {
671
            lr.lr_start = 1;
×
672
            lr.lr_end = 1 + this->lss_time_column_size - 2 - extra_space_size;
×
673
            value_out.emplace_back(lr, VC_ROLE.value(role_t::VCR_TIME_COLUMN));
×
674
            if (this->lss_token_line->is_time_skewed()) {
×
675
                value_out.emplace_back(lr,
×
676
                                       VC_ROLE.value(role_t::VCR_SKEWED_TIME));
×
677
            }
678
            lr.lr_start = 1 + this->lss_time_column_size - 2 - extra_space_size;
×
679
            lr.lr_end = 1 + this->lss_time_column_size - 1 - extra_space_size;
×
680
            value_out.emplace_back(
×
681
                lr, VC_ROLE.value(role_t::VCR_TIME_COLUMN_TO_TEXT));
×
682
        }
683
    }
684

685
    if (this->tas_display_time_offset) {
3,363✔
686
        time_offset_end = 13;
210✔
687
        lr.lr_start = 0;
210✔
688
        lr.lr_end = time_offset_end;
210✔
689

690
        shift_string_attrs(value_out, 0, time_offset_end);
210✔
691

692
        value_out.emplace_back(lr, VC_ROLE.value(role_t::VCR_OFFSET_TIME));
210✔
693
        value_out.emplace_back(line_range(12, 13),
210✔
694
                               VC_GRAPHIC.value(NCACS_VLINE));
420✔
695

696
        auto bar_role = role_t::VCR_NONE;
210✔
697

698
        switch (this->get_line_accel_direction(vis_line_t(row))) {
210✔
699
            case log_accel::direction_t::A_STEADY:
126✔
700
                break;
126✔
701
            case log_accel::direction_t::A_DECEL:
42✔
702
                bar_role = role_t::VCR_DIFF_DELETE;
42✔
703
                break;
42✔
704
            case log_accel::direction_t::A_ACCEL:
42✔
705
                bar_role = role_t::VCR_DIFF_ADD;
42✔
706
                break;
42✔
707
        }
708
        if (bar_role != role_t::VCR_NONE) {
210✔
709
            value_out.emplace_back(line_range(12, 13), VC_ROLE.value(bar_role));
84✔
710
        }
711
    }
712

713
    lr.lr_start = 0;
3,363✔
714
    lr.lr_end = -1;
3,363✔
715
    value_out.emplace_back(lr, L_FILE.value(this->lss_token_file));
3,363✔
716
    value_out.emplace_back(
3,363✔
717
        lr, SA_FORMAT.value(this->lss_token_file->get_format()->get_name()));
6,726✔
718

719
    {
720
        auto line_meta_context = this->get_bookmark_metadata_context(
3,363✔
721
            vis_line_t(row + 1), bookmark_metadata::categories::partition);
722
        if (line_meta_context.bmc_current_metadata) {
3,363✔
723
            lr.lr_start = 0;
8✔
724
            lr.lr_end = -1;
8✔
725
            value_out.emplace_back(
8✔
726
                lr,
727
                L_PARTITION.value(
16✔
728
                    line_meta_context.bmc_current_metadata.value()));
729
        }
730

731
        auto line_meta_opt = this->find_bookmark_metadata(vis_line_t(row));
3,363✔
732

733
        if (line_meta_opt) {
3,363✔
734
            lr.lr_start = 0;
31✔
735
            lr.lr_end = -1;
31✔
736
            value_out.emplace_back(lr, L_META.value(line_meta_opt.value()));
31✔
737
        }
738
    }
739

740
    auto src_file_attr = get_string_attr(value_out, SA_SRC_FILE);
3,363✔
741
    if (src_file_attr) {
3,363✔
742
        auto lr = src_file_attr->saw_string_attr->sa_range;
27✔
743
        lr.lr_end = lr.lr_start + 1;
27✔
744
        value_out.emplace_back(lr,
27✔
745
                               VC_STYLE.value(text_attrs::with_underline()));
54✔
746
        value_out.emplace_back(lr,
27✔
747
                               VC_COMMAND.value(ui_command{
108✔
748
                                   source_location{},
749
                                   ":toggle-breakpoint",
750
                               }));
751
    }
752

753
    if (this->lss_time_column_size == 0) {
3,363✔
754
        if (this->lss_token_file->is_time_adjusted()) {
3,363✔
755
            auto time_range = find_string_attr_range(value_out, &L_TIMESTAMP);
17✔
756

757
            if (time_range.lr_end != -1) {
17✔
758
                value_out.emplace_back(
15✔
759
                    time_range, VC_ROLE.value(role_t::VCR_ADJUSTED_TIME));
30✔
760
            }
761
        } else if (this->lss_token_line->is_time_skewed()) {
3,346✔
762
            auto time_range = find_string_attr_range(value_out, &L_TIMESTAMP);
8✔
763

764
            if (time_range.lr_end != -1) {
8✔
765
                value_out.emplace_back(time_range,
8✔
766
                                       VC_ROLE.value(role_t::VCR_SKEWED_TIME));
16✔
767
            }
768
        }
769
    }
770

771
    if (this->ttt_preview_min_time
3,363✔
772
        && this->lss_token_line->get_timeval() < this->ttt_preview_min_time)
3,363✔
773
    {
774
        auto color = styling::color_unit::from_palette(
×
775
            lnav::enums::to_underlying(ansi_color::red));
×
NEW
776
        value_out.emplace_back(line_range{0, 1}, VC_BACKGROUND.value(color));
×
777
    }
778
    if (this->ttt_preview_max_time
3,363✔
779
        && this->ttt_preview_max_time < this->lss_token_line->get_timeval())
3,363✔
780
    {
781
        auto color = styling::color_unit::from_palette(
×
782
            lnav::enums::to_underlying(ansi_color::red));
×
NEW
783
        value_out.emplace_back(line_range{0, 1}, VC_BACKGROUND.value(color));
×
784
    }
785
    if (!this->lss_token_line->is_continued()) {
3,363✔
786
        if (this->lss_preview_filter_stmt != nullptr) {
1,529✔
787
            auto color = styling::color_unit::EMPTY;
×
788
            auto eval_res
789
                = this->eval_sql_filter(this->lss_preview_filter_stmt.in(),
790
                                        this->lss_token_file_data,
791
                                        this->lss_token_line);
×
792
            if (eval_res.isErr()) {
×
793
                color = palette_color{
×
794
                    lnav::enums::to_underlying(ansi_color::yellow)};
×
795
                value_out.emplace_back(
×
796
                    line_range{0, -1},
×
797
                    SA_ERROR.value(
×
798
                        eval_res.unwrapErr().to_attr_line().get_string()));
×
799
            } else {
800
                auto matched = eval_res.unwrap();
×
801

802
                if (matched) {
×
803
                    color = palette_color{
×
804
                        lnav::enums::to_underlying(ansi_color::green)};
×
805
                } else {
806
                    color = palette_color{
×
807
                        lnav::enums::to_underlying(ansi_color::red)};
×
808
                    value_out.emplace_back(
×
809
                        line_range{0, 1},
×
810
                        VC_STYLE.value(text_attrs::with_blink()));
×
811
                }
812
            }
813
            value_out.emplace_back(line_range{0, 1},
×
814
                                   VC_BACKGROUND.value(color));
×
815
        }
816

817
        auto sql_filter_opt = this->get_sql_filter();
1,529✔
818
        if (sql_filter_opt) {
1,529✔
819
            auto* sf = (sql_filter*) sql_filter_opt.value().get();
36✔
820
            auto eval_res = this->eval_sql_filter(sf->sf_filter_stmt.in(),
821
                                                  this->lss_token_file_data,
822
                                                  this->lss_token_line);
36✔
823
            if (eval_res.isErr()) {
36✔
824
                auto msg = fmt::format(
825
                    FMT_STRING(
×
826
                        "filter expression evaluation failed with -- {}"),
827
                    eval_res.unwrapErr().to_attr_line().get_string());
×
828
                auto cu = styling::color_unit::from_palette(palette_color{
×
829
                    lnav::enums::to_underlying(ansi_color::yellow)});
830
                value_out.emplace_back(line_range{0, -1}, SA_ERROR.value(msg));
×
831
                value_out.emplace_back(line_range{0, 1},
×
832
                                       VC_BACKGROUND.value(cu));
×
833
            }
834
        }
36✔
835
    }
1,529✔
836
}
837

838
struct logline_cmp {
839
    logline_cmp(logfile_sub_source& lc) : llss_controller(lc) {}
1,113✔
840

841
    bool operator()(const logfile_sub_source::indexed_content& lhs,
98,745✔
842
                    const logfile_sub_source::indexed_content& rhs) const
843
    {
844
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
98,745✔
845
        const auto* ll_rhs = this->llss_controller.find_line(rhs.value());
98,745✔
846

847
        return (*ll_lhs) < (*ll_rhs);
98,745✔
848
    }
849

850
    bool operator()(const uint32_t& lhs, const uint32_t& rhs) const
851
    {
852
        auto cl_lhs = llss_controller.lss_index[lhs].value();
853
        auto cl_rhs = llss_controller.lss_index[rhs].value();
854
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
855
        const auto* ll_rhs = this->llss_controller.find_line(cl_rhs);
856

857
        return (*ll_lhs) < (*ll_rhs);
858
    }
859
#if 0
860
        bool operator()(const indexed_content &lhs, const indexed_content &rhs)
861
        {
862
            logline *ll_lhs = this->llss_controller.find_line(lhs.ic_value);
863
            logline *ll_rhs = this->llss_controller.find_line(rhs.ic_value);
864

865
            return (*ll_lhs) < (*ll_rhs);
866
        }
867
#endif
868

869
#if 0
870
    bool operator()(const content_line_t& lhs, const time_t& rhs) const
871
    {
872
        logline* ll_lhs = this->llss_controller.find_line(lhs);
873

874
        return *ll_lhs < rhs;
875
    }
876
#endif
877

878
    bool operator()(const logfile_sub_source::indexed_content& lhs,
×
879
                    const struct timeval& rhs) const
880
    {
881
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
×
882

883
        return *ll_lhs < rhs;
×
884
    }
885

886
    logfile_sub_source& llss_controller;
887
};
888

889
logfile_sub_source::rebuild_result
890
logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
4,331✔
891
{
892
    if (this->tss_view == nullptr) {
4,331✔
893
        return rebuild_result::rr_no_change;
124✔
894
    }
895

896
    this->lss_indexing_in_progress = true;
4,207✔
897
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
4,207✔
898

899
    iterator iter;
4,207✔
900
    size_t total_lines = 0;
4,207✔
901
    size_t est_remaining_lines = 0;
4,207✔
902
    auto all_time_ordered_formats = true;
4,207✔
903
    auto full_sort = this->lss_index.empty();
4,207✔
904
    int file_count = 0;
4,207✔
905
    auto force = std::exchange(this->lss_force_rebuild, false);
4,207✔
906
    auto retval = rebuild_result::rr_no_change;
4,207✔
907
    std::optional<timeval> lowest_tv = std::nullopt;
4,207✔
908
    auto search_start = 0_vl;
4,207✔
909

910
    if (force) {
4,207✔
911
        log_debug("forced to full rebuild");
470✔
912
        retval = rebuild_result::rr_full_rebuild;
470✔
913
        full_sort = true;
470✔
914
    }
915

916
    std::vector<size_t> file_order(this->lss_files.size());
4,207✔
917

918
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
7,135✔
919
        file_order[lpc] = lpc;
2,928✔
920
    }
921
    if (!this->lss_index.empty()) {
4,207✔
922
        std::stable_sort(file_order.begin(),
2,661✔
923
                         file_order.end(),
924
                         [this](const auto& left, const auto& right) {
245✔
925
                             const auto& left_ld = this->lss_files[left];
245✔
926
                             const auto& right_ld = this->lss_files[right];
245✔
927

928
                             if (left_ld->get_file_ptr() == nullptr) {
245✔
929
                                 return true;
×
930
                             }
931
                             if (right_ld->get_file_ptr() == nullptr) {
245✔
932
                                 return false;
4✔
933
                             }
934

935
                             return left_ld->get_file_ptr()->back()
241✔
936
                                 < right_ld->get_file_ptr()->back();
482✔
937
                         });
938
    }
939

940
    bool time_left = true;
4,207✔
941
    this->lss_all_timestamp_flags = 0;
4,207✔
942
    for (const auto file_index : file_order) {
7,135✔
943
        auto& ld = *(this->lss_files[file_index]);
2,928✔
944
        auto* lf = ld.get_file_ptr();
2,928✔
945

946
        if (lf == nullptr) {
2,928✔
947
            if (ld.ld_lines_indexed > 0) {
4✔
948
                log_debug("%d: file closed, doing full rebuild",
1✔
949
                          ld.ld_file_index);
950
                force = true;
1✔
951
                retval = rebuild_result::rr_full_rebuild;
1✔
952
                full_sort = true;
1✔
953
            }
954
        } else {
955
            if (!lf->get_format_ptr()->lf_time_ordered) {
2,924✔
956
                all_time_ordered_formats = false;
172✔
957
            }
958
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
2,924✔
959
                log_debug("no time left, skipping %s",
2✔
960
                          lf->get_filename_as_string().c_str());
961
                time_left = false;
2✔
962
            }
963
            this->lss_all_timestamp_flags
2,924✔
964
                |= lf->get_format_ptr()->lf_timestamp_flags;
2,924✔
965

966
            if (!this->tss_view->is_paused() && time_left) {
2,924✔
967
                auto log_rebuild_res = lf->rebuild_index(deadline);
2,922✔
968

969
                if (ld.ld_lines_indexed < lf->size()
2,922✔
970
                    && log_rebuild_res
2,922✔
971
                        == logfile::rebuild_result_t::NO_NEW_LINES)
972
                {
973
                    // This is a bit awkward... if the logfile indexing was
974
                    // complete before being added to us, we need to adjust
975
                    // the rebuild result to make it look like new lines
976
                    // were added.
977
                    log_rebuild_res = logfile::rebuild_result_t::NEW_LINES;
50✔
978
                }
979
                switch (log_rebuild_res) {
2,922✔
980
                    case logfile::rebuild_result_t::NO_NEW_LINES:
2,392✔
981
                        break;
2,392✔
982
                    case logfile::rebuild_result_t::NEW_LINES:
476✔
983
                        if (retval == rebuild_result::rr_no_change) {
476✔
984
                            retval = rebuild_result::rr_appended_lines;
427✔
985
                        }
986
                        log_debug("new lines for %s:%d",
476✔
987
                                  lf->get_filename_as_string().c_str(),
988
                                  lf->size());
989
                        if (!this->lss_index.empty()
476✔
990
                            && lf->size() > ld.ld_lines_indexed)
476✔
991
                        {
992
                            auto& new_file_line = (*lf)[ld.ld_lines_indexed];
×
993
                            auto cl = this->lss_index.back().value();
×
994
                            auto* last_indexed_line = this->find_line(cl);
×
995

996
                            // If there are new lines that are older than what
997
                            // we have in the index, we need to resort.
998
                            if (last_indexed_line == nullptr
×
999
                                || new_file_line
×
1000
                                    < last_indexed_line->get_timeval())
×
1001
                            {
1002
                                log_debug(
×
1003
                                    "%s:%ld: found older lines, full "
1004
                                    "rebuild: %p  %lld < %lld",
1005
                                    lf->get_filename().c_str(),
1006
                                    ld.ld_lines_indexed,
1007
                                    last_indexed_line,
1008
                                    new_file_line
1009
                                        .get_time<std::chrono::microseconds>()
1010
                                        .count(),
1011
                                    last_indexed_line == nullptr
1012
                                        ? (uint64_t) -1
1013
                                        : last_indexed_line
1014
                                              ->get_time<
1015
                                                  std::chrono::microseconds>()
1016
                                              .count());
1017
                                if (retval <= rebuild_result::rr_partial_rebuild
×
1018
                                    && all_time_ordered_formats)
×
1019
                                {
1020
                                    retval = rebuild_result::rr_partial_rebuild;
×
1021
                                    if (!lowest_tv
×
1022
                                        || new_file_line.get_timeval()
×
1023
                                            < lowest_tv.value())
×
1024
                                    {
1025
                                        lowest_tv = new_file_line.get_timeval();
×
1026
                                    }
1027
                                } else {
1028
                                    log_debug(
×
1029
                                        "already doing full rebuild, doing "
1030
                                        "full_sort as well");
1031
                                    force = true;
×
1032
                                    full_sort = true;
×
1033
                                }
1034
                            }
1035
                        }
1036
                        break;
476✔
1037
                    case logfile::rebuild_result_t::INVALID:
54✔
1038
                    case logfile::rebuild_result_t::NEW_ORDER:
1039
                        log_debug("%s: log file has a new order, full rebuild",
54✔
1040
                                  lf->get_filename().c_str());
1041
                        retval = rebuild_result::rr_full_rebuild;
54✔
1042
                        force = true;
54✔
1043
                        full_sort = true;
54✔
1044
                        break;
54✔
1045
                }
1046
            }
1047
            file_count += 1;
2,924✔
1048
            total_lines += lf->size();
2,924✔
1049

1050
            est_remaining_lines += lf->estimated_remaining_lines();
2,924✔
1051
        }
1052
    }
1053

1054
    if (!all_time_ordered_formats
4,207✔
1055
        && retval == rebuild_result::rr_partial_rebuild)
166✔
1056
    {
1057
        force = true;
×
1058
        full_sort = true;
×
1059
        retval = rebuild_result::rr_full_rebuild;
×
1060
    }
1061

1062
    if (this->lss_index.reserve(total_lines + est_remaining_lines)) {
4,207✔
1063
        // The index array was reallocated, just do a full sort/rebuild since
1064
        // it's been cleared out.
1065
        log_debug("expanding index capacity %zu", this->lss_index.ba_capacity);
616✔
1066
        force = true;
616✔
1067
        retval = rebuild_result::rr_full_rebuild;
616✔
1068
        full_sort = true;
616✔
1069
    }
1070

1071
    auto& vis_bm = this->tss_view->get_bookmarks();
4,207✔
1072

1073
    if (force) {
4,207✔
1074
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,620✔
1075
             iter++)
520✔
1076
        {
1077
            (*iter)->ld_lines_indexed = 0;
520✔
1078
        }
1079

1080
        this->lss_index.clear();
1,100✔
1081
        this->lss_filtered_index.clear();
1,100✔
1082
        this->lss_longest_line = 0;
1,100✔
1083
        this->lss_basename_width = 0;
1,100✔
1084
        this->lss_filename_width = 0;
1,100✔
1085
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,100✔
1086
        if (this->lss_index_delegate) {
1,100✔
1087
            this->lss_index_delegate->index_start(*this);
1,100✔
1088
        }
1089
    } else if (retval == rebuild_result::rr_partial_rebuild) {
3,107✔
1090
        size_t remaining = 0;
×
1091

1092
        log_debug("partial rebuild with lowest time: %ld",
×
1093
                  lowest_tv.value().tv_sec);
1094
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1095
             iter++)
×
1096
        {
1097
            logfile_data& ld = *(*iter);
×
1098
            auto* lf = ld.get_file_ptr();
×
1099

1100
            if (lf == nullptr) {
×
1101
                continue;
×
1102
            }
1103

1104
            require(lf->get_format_ptr()->lf_time_ordered);
×
1105

1106
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1107

1108
            if (line_iter) {
×
1109
                log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
×
1110
                          line_iter.value()->get_timeval().tv_sec,
1111
                          std::distance(lf->cbegin(), line_iter.value()),
1112
                          lf->size(),
1113
                          lf->get_filename_as_string().c_str());
1114
            }
1115
            ld.ld_lines_indexed
1116
                = std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
×
1117
            remaining += lf->size() - ld.ld_lines_indexed;
×
1118
        }
1119

1120
        auto* row_iter = std::lower_bound(this->lss_index.begin(),
×
1121
                                          this->lss_index.end(),
1122
                                          lowest_tv.value(),
×
1123
                                          logline_cmp(*this));
1124
        this->lss_index.shrink_to(
×
1125
            std::distance(this->lss_index.begin(), row_iter));
×
1126
        log_debug("new index size %ld/%ld; remain %ld",
×
1127
                  this->lss_index.ba_size,
1128
                  this->lss_index.ba_capacity,
1129
                  remaining);
1130
        auto filt_row_iter = std::lower_bound(this->lss_filtered_index.begin(),
×
1131
                                              this->lss_filtered_index.end(),
1132
                                              lowest_tv.value(),
×
1133
                                              filtered_logline_cmp(*this));
1134
        this->lss_filtered_index.resize(
×
1135
            std::distance(this->lss_filtered_index.begin(), filt_row_iter));
×
1136
        search_start = vis_line_t(this->lss_filtered_index.size());
×
1137

1138
        if (this->lss_index_delegate) {
×
1139
            this->lss_index_delegate->index_start(*this);
×
1140
            for (const auto row_in_full_index : this->lss_filtered_index) {
×
1141
                auto cl = this->lss_index[row_in_full_index].value();
×
1142
                uint64_t line_number;
1143
                auto ld_iter = this->find_data(cl, line_number);
×
1144
                auto& ld = *ld_iter;
×
1145
                auto line_iter = ld->get_file_ptr()->begin() + line_number;
×
1146

1147
                this->lss_index_delegate->index_line(
×
1148
                    *this, ld->get_file_ptr(), line_iter);
1149
            }
1150
        }
1151
    }
1152

1153
    if (this->lss_index.empty() && !time_left) {
4,207✔
1154
        log_info("ran out of time, skipping rebuild");
×
1155
        // need to make sure we rebuild in case no new data comes in
1156
        this->lss_force_rebuild = true;
×
1157
        return rebuild_result::rr_appended_lines;
×
1158
    }
1159

1160
    if (retval != rebuild_result::rr_no_change || force) {
4,207✔
1161
        size_t index_size = 0, start_size = this->lss_index.size();
1,113✔
1162
        logline_cmp line_cmper(*this);
1,113✔
1163

1164
        for (auto& ld : this->lss_files) {
1,653✔
1165
            auto* lf = ld->get_file_ptr();
540✔
1166

1167
            if (lf == nullptr) {
540✔
1168
                continue;
1✔
1169
            }
1170
            this->lss_longest_line = std::max(
1,078✔
1171
                this->lss_longest_line, lf->get_longest_line_length() + 1);
539✔
1172
            this->lss_basename_width
1173
                = std::max(this->lss_basename_width,
1,078✔
1174
                           lf->get_unique_path().native().size());
539✔
1175
            this->lss_filename_width = std::max(
1,078✔
1176
                this->lss_filename_width, lf->get_filename().native().size());
539✔
1177
        }
1178

1179
        if (full_sort) {
1,113✔
1180
            log_trace("rebuild_index full sort");
1,113✔
1181
            for (auto& ld : this->lss_files) {
1,653✔
1182
                auto* lf = ld->get_file_ptr();
540✔
1183

1184
                if (lf == nullptr) {
540✔
1185
                    continue;
1✔
1186
                }
1187

1188
                for (size_t line_index = 0; line_index < lf->size();
13,810✔
1189
                     line_index++)
1190
                {
1191
                    const auto lf_iter
1192
                        = ld->get_file_ptr()->begin() + line_index;
13,271✔
1193
                    if (lf_iter->is_ignored()) {
13,271✔
1194
                        continue;
321✔
1195
                    }
1196

1197
                    content_line_t con_line(
1198
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
12,950✔
1199

1200
                    if (lf_iter->is_meta_marked()) {
12,950✔
1201
                        auto start_iter = lf_iter;
12✔
1202
                        while (start_iter->is_continued()) {
12✔
1203
                            --start_iter;
×
1204
                        }
1205
                        int start_index
1206
                            = start_iter - ld->get_file_ptr()->begin();
12✔
1207
                        content_line_t start_con_line(ld->ld_file_index
12✔
1208
                                                          * MAX_LINES_PER_FILE
12✔
1209
                                                      + start_index);
12✔
1210

1211
                        auto& line_meta
1212
                            = ld->get_file_ptr()
1213
                                  ->get_bookmark_metadata()[start_index];
12✔
1214
                        if (line_meta.has(bookmark_metadata::categories::notes))
12✔
1215
                        {
1216
                            this->lss_user_marks[&textview_curses::BM_META]
4✔
1217
                                .insert_once(start_con_line);
4✔
1218
                        }
1219
                        if (line_meta.has(
12✔
1220
                                bookmark_metadata::categories::partition))
1221
                        {
1222
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
8✔
1223
                                .insert_once(start_con_line);
8✔
1224
                        }
1225
                    }
1226
                    this->lss_index.push_back(
12,950✔
1227
                        indexed_content{con_line, lf_iter});
25,900✔
1228
                }
1229
            }
1230

1231
            // XXX get rid of this full sort on the initial run, it's not
1232
            // needed unless the file is not in time-order
1233
            if (this->lss_sorting_observer) {
1,113✔
1234
                this->lss_sorting_observer(*this, 0, this->lss_index.size());
3✔
1235
            }
1236
            std::sort(
1,113✔
1237
                this->lss_index.begin(), this->lss_index.end(), line_cmper);
1238
            if (this->lss_sorting_observer) {
1,113✔
1239
                this->lss_sorting_observer(
6✔
1240
                    *this, this->lss_index.size(), this->lss_index.size());
3✔
1241
            }
1242
        } else {
1243
            kmerge_tree_c<logline, logfile_data, logfile::iterator> merge(
1244
                file_count);
×
1245

1246
            for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1247
                 iter++)
×
1248
            {
1249
                auto* ld = iter->get();
×
1250
                auto* lf = ld->get_file_ptr();
×
1251
                if (lf == nullptr) {
×
1252
                    continue;
×
1253
                }
1254

1255
                merge.add(ld, lf->begin() + ld->ld_lines_indexed, lf->end());
×
1256
                index_size += lf->size();
×
1257
            }
1258

1259
            file_off_t index_off = 0;
×
1260
            merge.execute();
×
1261
            if (this->lss_sorting_observer) {
×
1262
                this->lss_sorting_observer(*this, index_off, index_size);
×
1263
            }
1264
            log_trace("k-way merge");
×
1265
            for (;;) {
1266
                logfile::iterator lf_iter;
×
1267
                logfile_data* ld;
1268

1269
                if (!merge.get_top(ld, lf_iter)) {
×
1270
                    break;
×
1271
                }
1272

1273
                if (!lf_iter->is_ignored()) {
×
1274
                    int file_index = ld->ld_file_index;
×
1275
                    int line_index = lf_iter - ld->get_file_ptr()->begin();
×
1276

1277
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
1278
                                            + line_index);
×
1279

1280
                    if (lf_iter->is_meta_marked()) {
×
1281
                        auto start_iter = lf_iter;
×
1282
                        while (start_iter->is_continued()) {
×
1283
                            --start_iter;
×
1284
                        }
1285
                        int start_index
1286
                            = start_iter - ld->get_file_ptr()->begin();
×
1287
                        content_line_t start_con_line(
1288
                            file_index * MAX_LINES_PER_FILE + start_index);
×
1289

1290
                        auto& line_meta
1291
                            = ld->get_file_ptr()
1292
                                  ->get_bookmark_metadata()[start_index];
×
1293
                        if (line_meta.has(bookmark_metadata::categories::notes))
×
1294
                        {
1295
                            this->lss_user_marks[&textview_curses::BM_META]
×
1296
                                .insert_once(start_con_line);
×
1297
                        }
1298
                        if (line_meta.has(
×
1299
                                bookmark_metadata::categories::partition))
1300
                        {
1301
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
×
1302
                                .insert_once(start_con_line);
×
1303
                        }
1304
                    }
1305
                    this->lss_index.push_back(
×
1306
                        indexed_content{con_line, lf_iter});
×
1307
                }
1308

1309
                merge.next();
×
1310
                index_off += 1;
×
1311
                if (index_off % 100000 == 0 && this->lss_sorting_observer) {
×
1312
                    this->lss_sorting_observer(*this, index_off, index_size);
×
1313
                }
1314
            }
1315
            if (this->lss_sorting_observer) {
×
1316
                this->lss_sorting_observer(*this, index_size, index_size);
×
1317
            }
1318
        }
1319

1320
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,653✔
1321
             ++iter)
540✔
1322
        {
1323
            auto* lf = (*iter)->get_file_ptr();
540✔
1324

1325
            if (lf == nullptr) {
540✔
1326
                continue;
1✔
1327
            }
1328

1329
            (*iter)->ld_lines_indexed = lf->size();
539✔
1330
        }
1331

1332
        this->lss_filtered_index.reserve(this->lss_index.size());
1,113✔
1333

1334
        uint32_t filter_in_mask, filter_out_mask;
1335
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,113✔
1336

1337
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,113✔
1338
            this->lss_index_delegate->index_start(*this);
1,113✔
1339
        }
1340

1341
        log_trace("filtered index");
1,113✔
1342
        for (size_t index_index = start_size;
14,063✔
1343
             index_index < this->lss_index.size();
14,063✔
1344
             index_index++)
1345
        {
1346
            const auto cl = this->lss_index[index_index].value();
12,950✔
1347
            uint64_t line_number;
1348
            auto ld = this->find_data(cl, line_number);
12,950✔
1349

1350
            if (!(*ld)->is_visible()) {
12,950✔
1351
                continue;
×
1352
            }
1353

1354
            auto* lf = (*ld)->get_file_ptr();
12,950✔
1355
            auto line_iter = lf->begin() + line_number;
12,950✔
1356

1357
            if (line_iter->is_ignored()) {
12,950✔
1358
                continue;
×
1359
            }
1360

1361
            if (!this->tss_apply_filters
25,900✔
1362
                || (!(*ld)->ld_filter_state.excluded(
25,870✔
1363
                        filter_in_mask, filter_out_mask, line_number)
1364
                    && this->check_extra_filters(ld, line_iter)))
12,920✔
1365
            {
1366
                auto eval_res = this->eval_sql_filter(
1367
                    this->lss_marker_stmt.in(), ld, line_iter);
12,920✔
1368
                if (eval_res.isErr()) {
12,920✔
1369
                    line_iter->set_expr_mark(false);
×
1370
                } else {
1371
                    auto matched = eval_res.unwrap();
12,920✔
1372

1373
                    line_iter->set_expr_mark(matched);
12,920✔
1374
                    if (matched) {
12,920✔
1375
                        vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1376
                            vis_line_t(this->lss_filtered_index.size()));
×
NEW
1377
                        this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
NEW
1378
                            .insert_once(cl);
×
1379
                    }
1380
                }
1381
                this->lss_filtered_index.push_back(index_index);
12,920✔
1382
                if (this->lss_index_delegate != nullptr) {
12,920✔
1383
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
12,920✔
1384
                }
1385
            }
12,920✔
1386
        }
1387

1388
        this->lss_indexing_in_progress = false;
1,113✔
1389

1390
        if (this->lss_index_delegate != nullptr) {
1,113✔
1391
            this->lss_index_delegate->index_complete(*this);
1,113✔
1392
        }
1393
    }
1394

1395
    switch (retval) {
4,207✔
1396
        case rebuild_result::rr_no_change:
3,094✔
1397
            break;
3,094✔
1398
        case rebuild_result::rr_full_rebuild:
1,100✔
1399
            log_debug("redoing search");
1,100✔
1400
            this->lss_index_generation += 1;
1,100✔
1401
            this->tss_view->reload_data();
1,100✔
1402
            this->tss_view->redo_search();
1,100✔
1403
            break;
1,100✔
1404
        case rebuild_result::rr_partial_rebuild:
×
1405
            log_debug("redoing search from: %d", (int) search_start);
×
1406
            this->lss_index_generation += 1;
×
1407
            this->tss_view->reload_data();
×
1408
            this->tss_view->search_new_data(search_start);
×
1409
            break;
×
1410
        case rebuild_result::rr_appended_lines:
13✔
1411
            this->tss_view->reload_data();
13✔
1412
            this->tss_view->search_new_data();
13✔
1413
            break;
13✔
1414
    }
1415

1416
    return retval;
4,207✔
1417
}
4,207✔
1418

1419
void
1420
logfile_sub_source::text_update_marks(vis_bookmarks& bm)
2,148✔
1421
{
1422
    logfile* last_file = nullptr;
2,148✔
1423
    vis_line_t vl;
2,148✔
1424

1425
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,148✔
1426
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,148✔
1427
    auto& bm_files = bm[&BM_FILES];
2,148✔
1428

1429
    bm_warnings.clear();
2,148✔
1430
    bm_errors.clear();
2,148✔
1431
    bm_files.clear();
2,148✔
1432

1433
    std::vector<const bookmark_type_t*> used_marks;
2,148✔
1434
    for (const auto* bmt : {
10,740✔
1435
             &textview_curses::BM_USER,
1436
             &textview_curses::BM_USER_EXPR,
1437
             &textview_curses::BM_PARTITION,
1438
             &textview_curses::BM_META,
1439
         })
12,888✔
1440
    {
1441
        bm[bmt].clear();
8,592✔
1442
        if (!this->lss_user_marks[bmt].empty()) {
8,592✔
1443
            used_marks.emplace_back(bmt);
114✔
1444
        }
1445
    }
1446

1447
    for (; vl < (int) this->lss_filtered_index.size(); ++vl) {
17,963✔
1448
        const auto& orig_ic = this->lss_index[this->lss_filtered_index[vl]];
15,815✔
1449
        auto cl = orig_ic.value();
15,815✔
1450
        auto* lf = this->find_file_ptr(cl);
15,815✔
1451

1452
        for (const auto& bmt : used_marks) {
17,320✔
1453
            auto& user_mark = this->lss_user_marks[bmt];
1,505✔
1454
            if (user_mark.bv_tree.exists(orig_ic.value())) {
1,505✔
1455
                bm[bmt].insert_once(vl);
130✔
1456
            }
1457
        }
1458

1459
        if (lf != last_file) {
15,815✔
1460
            bm_files.insert_once(vl);
940✔
1461
        }
1462

1463
        switch (orig_ic.level()) {
15,815✔
1464
            case indexed_content::level_t::warning:
94✔
1465
                bm_warnings.insert_once(vl);
94✔
1466
                break;
94✔
1467

1468
            case indexed_content::level_t::error:
2,396✔
1469
                bm_errors.insert_once(vl);
2,396✔
1470
                break;
2,396✔
1471

1472
            default:
13,325✔
1473
                break;
13,325✔
1474
        }
1475

1476
        last_file = lf;
15,815✔
1477
    }
1478
}
2,148✔
1479

1480
void
1481
logfile_sub_source::text_filters_changed()
160✔
1482
{
1483
    this->lss_index_generation += 1;
160✔
1484

1485
    if (this->lss_line_meta_changed) {
160✔
1486
        this->invalidate_sql_filter();
24✔
1487
        this->lss_line_meta_changed = false;
24✔
1488
    }
1489

1490
    for (auto& ld : *this) {
271✔
1491
        auto* lf = ld->get_file_ptr();
111✔
1492

1493
        if (lf != nullptr) {
111✔
1494
            ld->ld_filter_state.clear_deleted_filter_state();
111✔
1495
            lf->reobserve_from(lf->begin()
111✔
1496
                               + ld->ld_filter_state.get_min_count(lf->size()));
111✔
1497
        }
1498
    }
1499

1500
    if (this->lss_force_rebuild) {
160✔
1501
        return;
×
1502
    }
1503

1504
    auto& vis_bm = this->tss_view->get_bookmarks();
160✔
1505
    uint32_t filtered_in_mask, filtered_out_mask;
1506

1507
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
160✔
1508

1509
    if (this->lss_index_delegate != nullptr) {
160✔
1510
        this->lss_index_delegate->index_start(*this);
160✔
1511
    }
1512
    vis_bm[&textview_curses::BM_USER_EXPR].clear();
160✔
1513

1514
    this->lss_filtered_index.clear();
160✔
1515
    for (size_t index_index = 0; index_index < this->lss_index.size();
1,467✔
1516
         index_index++)
1517
    {
1518
        auto cl = this->lss_index[index_index].value();
1,307✔
1519
        uint64_t line_number;
1520
        auto ld = this->find_data(cl, line_number);
1,307✔
1521

1522
        if (!(*ld)->is_visible()) {
1,307✔
1523
            continue;
210✔
1524
        }
1525

1526
        auto lf = (*ld)->get_file_ptr();
1,097✔
1527
        auto line_iter = lf->begin() + line_number;
1,097✔
1528

1529
        if (!this->tss_apply_filters
2,194✔
1530
            || (!(*ld)->ld_filter_state.excluded(
2,061✔
1531
                    filtered_in_mask, filtered_out_mask, line_number)
1532
                && this->check_extra_filters(ld, line_iter)))
964✔
1533
        {
1534
            auto eval_res = this->eval_sql_filter(
1535
                this->lss_marker_stmt.in(), ld, line_iter);
922✔
1536
            if (eval_res.isErr()) {
922✔
1537
                line_iter->set_expr_mark(false);
×
1538
            } else {
1539
                auto matched = eval_res.unwrap();
922✔
1540

1541
                line_iter->set_expr_mark(matched);
922✔
1542
                if (matched) {
922✔
1543
                    vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1544
                        vis_line_t(this->lss_filtered_index.size()));
×
NEW
1545
                    this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
NEW
1546
                        .insert_once(cl);
×
1547
                }
1548
            }
1549
            this->lss_filtered_index.push_back(index_index);
922✔
1550
            if (this->lss_index_delegate != nullptr) {
922✔
1551
                this->lss_index_delegate->index_line(*this, lf, line_iter);
922✔
1552
            }
1553
        }
922✔
1554
    }
1555

1556
    if (this->lss_index_delegate != nullptr) {
160✔
1557
        this->lss_index_delegate->index_complete(*this);
160✔
1558
    }
1559

1560
    if (this->tss_view != nullptr) {
160✔
1561
        this->tss_view->reload_data();
160✔
1562
        this->tss_view->redo_search();
160✔
1563
    }
1564
}
1565

1566
std::optional<json_string>
1567
logfile_sub_source::text_row_details(const textview_curses& tc)
28✔
1568
{
1569
    if (this->lss_index.empty()) {
28✔
1570
        log_trace("logfile_sub_source::text_row_details empty");
1✔
1571
        return std::nullopt;
1✔
1572
    }
1573

1574
    auto ov_sel = tc.get_overlay_selection();
27✔
1575
    if (ov_sel.has_value()) {
27✔
1576
        auto* fos
1577
            = dynamic_cast<field_overlay_source*>(tc.get_overlay_source());
×
1578
        auto iter = fos->fos_row_to_field_meta.find(ov_sel.value());
×
1579
        if (iter != fos->fos_row_to_field_meta.end()) {
×
1580
            auto find_res = this->find_line_with_file(tc.get_top());
×
1581
            if (find_res) {
×
1582
                yajlpp_gen gen;
×
1583

1584
                {
1585
                    yajlpp_map root(gen);
×
1586

1587
                    root.gen("value");
×
1588
                    root.gen(iter->second.ri_value);
×
1589
                }
1590

1591
                return json_string(gen);
×
1592
            }
1593
        }
1594
    }
1595

1596
    return std::nullopt;
27✔
1597
}
1598

1599
bool
1600
logfile_sub_source::list_input_handle_key(listview_curses& lv,
1✔
1601
                                          const ncinput& ch)
1602
{
1603
    switch (ch.eff_text[0]) {
1✔
1604
        case ' ': {
×
1605
            auto ov_vl = lv.get_overlay_selection();
×
1606
            if (ov_vl) {
×
1607
                auto* fos = dynamic_cast<field_overlay_source*>(
×
1608
                    lv.get_overlay_source());
×
1609
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
1610
                if (iter != fos->fos_row_to_field_meta.end()
×
1611
                    && iter->second.ri_meta)
×
1612
                {
1613
                    auto find_res = this->find_line_with_file(lv.get_top());
×
1614
                    if (find_res) {
×
1615
                        auto file_and_line = find_res.value();
×
1616
                        auto* format = file_and_line.first->get_format_ptr();
×
1617
                        auto fstates = format->get_field_states();
×
1618
                        auto state_iter
1619
                            = fstates.find(iter->second.ri_meta->lvm_name);
×
1620
                        if (state_iter != fstates.end()) {
×
1621
                            format->hide_field(iter->second.ri_meta->lvm_name,
×
1622
                                               !state_iter->second.is_hidden());
×
1623
                            lv.set_needs_update();
×
1624
                        }
1625
                    }
1626
                }
1627
                return true;
×
1628
            }
1629
            return false;
×
1630
        }
1631
        case '#': {
×
1632
            auto ov_vl = lv.get_overlay_selection();
×
1633
            if (ov_vl) {
×
1634
                auto* fos = dynamic_cast<field_overlay_source*>(
×
1635
                    lv.get_overlay_source());
×
1636
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
1637
                if (iter != fos->fos_row_to_field_meta.end()
×
1638
                    && iter->second.ri_meta)
×
1639
                {
1640
                    const auto& meta = iter->second.ri_meta.value();
×
1641
                    std::string cmd;
×
1642

1643
                    switch (meta.to_chart_type()) {
×
1644
                        case chart_type_t::none:
×
1645
                            break;
×
1646
                        case chart_type_t::hist: {
×
1647
                            auto prql = fmt::format(
1648
                                FMT_STRING(
×
1649
                                    "from {} | stats.hist {} slice:'1h'"),
1650
                                meta.lvm_format.value()->get_name(),
×
1651
                                meta.lvm_name);
×
1652
                            cmd = fmt::format(FMT_STRING(":prompt sql ; '{}'"),
×
1653
                                              shlex::escape(prql));
×
1654
                            break;
×
1655
                        }
1656
                        case chart_type_t::spectro:
×
1657
                            cmd = fmt::format(FMT_STRING(":spectrogram {}"),
×
1658
                                              meta.lvm_name);
×
1659
                            break;
×
1660
                    }
1661
                    if (!cmd.empty()) {
×
1662
                        this->lss_exec_context
×
1663
                            ->with_provenance(exec_context::mouse_input{})
×
1664
                            ->execute(INTERNAL_SRC_LOC, cmd);
×
1665
                    }
1666
                }
1667
                return true;
×
1668
            }
1669
            return false;
×
1670
        }
1671
        case 'h':
×
1672
        case 'H':
1673
        case NCKEY_LEFT:
1674
            if (lv.get_left() == 0) {
×
1675
                this->increase_line_context();
×
1676
                lv.set_needs_update();
×
1677
                return true;
×
1678
            }
1679
            break;
×
1680
        case 'l':
×
1681
        case 'L':
1682
        case NCKEY_RIGHT:
1683
            if (this->decrease_line_context()) {
×
1684
                lv.set_needs_update();
×
1685
                return true;
×
1686
            }
1687
            break;
×
1688
    }
1689
    return false;
1✔
1690
}
1691

1692
std::optional<
1693
    std::pair<grep_proc_source<vis_line_t>*, grep_proc_sink<vis_line_t>*>>
1694
logfile_sub_source::get_grepper()
9✔
1695
{
1696
    return std::make_pair(
18✔
1697
        (grep_proc_source<vis_line_t>*) &this->lss_meta_grepper,
×
1698
        (grep_proc_sink<vis_line_t>*) &this->lss_meta_grepper);
9✔
1699
}
1700

1701
/**
1702
 * Functor for comparing the ld_file field of the logfile_data struct.
1703
 */
1704
struct logfile_data_eq {
1705
    explicit logfile_data_eq(std::shared_ptr<logfile> lf)
1,016✔
1706
        : lde_file(std::move(lf))
1,016✔
1707
    {
1708
    }
1,016✔
1709

1710
    bool operator()(
622✔
1711
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1712
    {
1713
        return this->lde_file == ld->get_file();
622✔
1714
    }
1715

1716
    std::shared_ptr<logfile> lde_file;
1717
};
1718

1719
bool
1720
logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
508✔
1721
{
1722
    iterator existing;
508✔
1723

1724
    require_lt(lf->size(), MAX_LINES_PER_FILE);
508✔
1725

1726
    existing = std::find_if(this->lss_files.begin(),
508✔
1727
                            this->lss_files.end(),
1728
                            logfile_data_eq(nullptr));
1,016✔
1729
    if (existing == this->lss_files.end()) {
508✔
1730
        if (this->lss_files.size() >= MAX_FILES) {
508✔
1731
            return false;
×
1732
        }
1733

1734
        auto ld = std::make_unique<logfile_data>(
1735
            this->lss_files.size(), this->get_filters(), lf);
508✔
1736
        ld->set_visibility(lf->get_open_options().loo_is_visible);
508✔
1737
        this->lss_files.push_back(std::move(ld));
508✔
1738
    } else {
508✔
1739
        (*existing)->set_file(lf);
×
1740
    }
1741

1742
    return true;
508✔
1743
}
1744

1745
Result<void, lnav::console::user_message>
1746
logfile_sub_source::set_sql_filter(std::string stmt_str, sqlite3_stmt* stmt)
753✔
1747
{
1748
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
753✔
1749
        auto top_cl = this->at(0_vl);
7✔
1750
        auto ld = this->find_data(top_cl);
7✔
1751
        auto eval_res
1752
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
7✔
1753

1754
        if (eval_res.isErr()) {
7✔
1755
            sqlite3_finalize(stmt);
1✔
1756
            return Err(eval_res.unwrapErr());
1✔
1757
        }
1758
    }
7✔
1759

1760
    for (auto& ld : *this) {
767✔
1761
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
15✔
1762
    }
1763

1764
    auto old_filter = this->get_sql_filter();
752✔
1765
    if (stmt != nullptr) {
752✔
1766
        auto new_filter
1767
            = std::make_shared<sql_filter>(*this, std::move(stmt_str), stmt);
6✔
1768

1769
        if (old_filter) {
6✔
1770
            auto existing_iter = std::find(this->tss_filters.begin(),
×
1771
                                           this->tss_filters.end(),
1772
                                           old_filter.value());
×
1773
            *existing_iter = new_filter;
×
1774
        } else {
1775
            this->tss_filters.add_filter(new_filter);
6✔
1776
        }
1777
    } else if (old_filter) {
752✔
1778
        this->tss_filters.delete_filter(old_filter.value()->get_id());
6✔
1779
    }
1780

1781
    return Ok();
752✔
1782
}
752✔
1783

1784
Result<void, lnav::console::user_message>
1785
logfile_sub_source::set_sql_marker(std::string stmt_str, sqlite3_stmt* stmt)
748✔
1786
{
1787
    static auto op = lnav_operation{"set_sql_marker"};
748✔
1788
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
748✔
1789
        auto top_cl = this->at(0_vl);
2✔
1790
        auto ld = this->find_data(top_cl);
2✔
1791
        auto eval_res
1792
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
2✔
1793

1794
        if (eval_res.isErr()) {
2✔
1795
            sqlite3_finalize(stmt);
×
1796
            return Err(eval_res.unwrapErr());
×
1797
        }
1798
    }
2✔
1799

1800
    auto op_guard = lnav_opid_guard::internal(op);
748✔
1801
    log_info("setting SQL marker: %s", stmt_str.c_str());
748✔
1802
    this->lss_marker_stmt_text = std::move(stmt_str);
748✔
1803
    this->lss_marker_stmt = stmt;
748✔
1804

1805
    if (this->tss_view == nullptr || this->lss_force_rebuild) {
748✔
1806
        log_info("skipping SQL marker update");
130✔
1807
        return Ok();
130✔
1808
    }
1809

1810
    auto& vis_bm = this->tss_view->get_bookmarks();
618✔
1811
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
618✔
1812
    auto& cl_marks_bv = this->lss_user_marks[&textview_curses::BM_USER_EXPR];
618✔
1813

1814
    expr_marks_bv.clear();
618✔
1815
    if (this->lss_index_delegate) {
618✔
1816
        this->lss_index_delegate->index_start(*this);
618✔
1817
    }
1818
    for (auto row = 0_vl; row < vis_line_t(this->lss_filtered_index.size());
625✔
1819
         row += 1_vl)
7✔
1820
    {
1821
        auto cl = this->at(row);
7✔
1822
        uint64_t line_number;
1823
        auto ld = this->find_data(cl, line_number);
7✔
1824

1825
        if (!(*ld)->is_visible()) {
7✔
1826
            continue;
1✔
1827
        }
1828
        auto ll = (*ld)->get_file()->begin() + line_number;
7✔
1829
        if (ll->is_continued() || ll->is_ignored()) {
7✔
1830
            continue;
1✔
1831
        }
1832
        auto eval_res
1833
            = this->eval_sql_filter(this->lss_marker_stmt.in(), ld, ll);
6✔
1834

1835
        if (eval_res.isErr()) {
6✔
1836
            ll->set_expr_mark(false);
×
1837
        } else {
1838
            auto matched = eval_res.unwrap();
6✔
1839

1840
            ll->set_expr_mark(matched);
6✔
1841
            if (matched) {
6✔
1842
                expr_marks_bv.insert_once(row);
4✔
1843
                cl_marks_bv.insert_once(cl);
4✔
1844
            }
1845
        }
1846
        if (this->lss_index_delegate) {
6✔
1847
            this->lss_index_delegate->index_line(
6✔
1848
                *this, (*ld)->get_file_ptr(), ll);
6✔
1849
        }
1850
    }
6✔
1851
    if (this->lss_index_delegate) {
618✔
1852
        this->lss_index_delegate->index_complete(*this);
618✔
1853
    }
1854
    log_info("SQL marker update complete");
618✔
1855

1856
    return Ok();
618✔
1857
}
748✔
1858

1859
Result<void, lnav::console::user_message>
1860
logfile_sub_source::set_preview_sql_filter(sqlite3_stmt* stmt)
746✔
1861
{
1862
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
746✔
1863
        auto top_cl = this->at(0_vl);
×
1864
        auto ld = this->find_data(top_cl);
×
1865
        auto eval_res
1866
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
×
1867

1868
        if (eval_res.isErr()) {
×
1869
            sqlite3_finalize(stmt);
×
1870
            return Err(eval_res.unwrapErr());
×
1871
        }
1872
    }
1873

1874
    this->lss_preview_filter_stmt = stmt;
746✔
1875

1876
    return Ok();
746✔
1877
}
1878

1879
Result<bool, lnav::console::user_message>
1880
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
14,027✔
1881
                                    iterator ld,
1882
                                    logfile::const_iterator ll)
1883
{
1884
    if (stmt == nullptr) {
14,027✔
1885
        return Ok(false);
27,684✔
1886
    }
1887

1888
    auto* lf = (*ld)->get_file_ptr();
185✔
1889
    char timestamp_buffer[64];
1890
    shared_buffer_ref raw_sbr;
185✔
1891
    logline_value_vector values;
185✔
1892
    auto& sbr = values.lvv_sbr;
185✔
1893
    lf->read_full_message(ll, sbr);
185✔
1894
    sbr.erase_ansi();
185✔
1895
    auto format = lf->get_format();
185✔
1896
    string_attrs_t sa;
185✔
1897
    auto line_number = std::distance(lf->cbegin(), ll);
185✔
1898
    format->annotate(lf, line_number, sa, values);
185✔
1899

1900
    sqlite3_reset(stmt);
185✔
1901
    sqlite3_clear_bindings(stmt);
185✔
1902

1903
    auto count = sqlite3_bind_parameter_count(stmt);
185✔
1904
    for (int lpc = 0; lpc < count; lpc++) {
382✔
1905
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
197✔
1906

1907
        if (name[0] == '$') {
197✔
1908
            const char* env_value;
1909

1910
            if ((env_value = getenv(&name[1])) != nullptr) {
4✔
1911
                sqlite3_bind_text(stmt, lpc + 1, env_value, -1, SQLITE_STATIC);
1✔
1912
            }
1913
            continue;
4✔
1914
        }
4✔
1915
        if (strcmp(name, ":log_level") == 0) {
193✔
1916
            auto lvl = ll->get_level_name();
6✔
1917
            sqlite3_bind_text(
6✔
1918
                stmt, lpc + 1, lvl.data(), lvl.length(), SQLITE_STATIC);
1919
            continue;
6✔
1920
        }
6✔
1921
        if (strcmp(name, ":log_time") == 0) {
187✔
1922
            auto len = sql_strftime(timestamp_buffer,
×
1923
                                    sizeof(timestamp_buffer),
1924
                                    ll->get_timeval(),
×
1925
                                    'T');
1926
            sqlite3_bind_text(
×
1927
                stmt, lpc + 1, timestamp_buffer, len, SQLITE_STATIC);
1928
            continue;
×
1929
        }
1930
        if (strcmp(name, ":log_time_msecs") == 0) {
187✔
1931
            sqlite3_bind_int64(
1✔
1932
                stmt,
1933
                lpc + 1,
1934
                ll->get_time<std::chrono::milliseconds>().count());
1✔
1935
            continue;
1✔
1936
        }
1937
        if (strcmp(name, ":log_mark") == 0) {
186✔
1938
            sqlite3_bind_int(stmt, lpc + 1, ll->is_marked());
×
1939
            continue;
×
1940
        }
1941
        if (strcmp(name, ":log_comment") == 0) {
186✔
1942
            const auto& bm = lf->get_bookmark_metadata();
×
1943
            auto line_number
1944
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
1945
            auto bm_iter = bm.find(line_number);
×
1946
            if (bm_iter != bm.end() && !bm_iter->second.bm_comment.empty()) {
×
1947
                const auto& meta = bm_iter->second;
×
1948
                sqlite3_bind_text(stmt,
×
1949
                                  lpc + 1,
1950
                                  meta.bm_comment.c_str(),
1951
                                  meta.bm_comment.length(),
×
1952
                                  SQLITE_STATIC);
1953
            }
1954
            continue;
×
1955
        }
1956
        if (strcmp(name, ":log_annotations") == 0) {
186✔
1957
            const auto& bm = lf->get_bookmark_metadata();
×
1958
            auto line_number
1959
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
1960
            auto bm_iter = bm.find(line_number);
×
1961
            if (bm_iter != bm.end()
×
1962
                && !bm_iter->second.bm_annotations.la_pairs.empty())
×
1963
            {
1964
                const auto& meta = bm_iter->second;
×
1965
                auto anno_str = logmsg_annotations_handlers.to_string(
1966
                    meta.bm_annotations);
×
1967

1968
                sqlite3_bind_text(stmt,
×
1969
                                  lpc + 1,
1970
                                  anno_str.c_str(),
1971
                                  anno_str.length(),
×
1972
                                  SQLITE_TRANSIENT);
1973
            }
1974
            continue;
×
1975
        }
1976
        if (strcmp(name, ":log_tags") == 0) {
186✔
1977
            const auto& bm = lf->get_bookmark_metadata();
9✔
1978
            auto line_number
1979
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
9✔
1980
            auto bm_iter = bm.find(line_number);
9✔
1981
            if (bm_iter != bm.end() && !bm_iter->second.bm_tags.empty()) {
9✔
1982
                const auto& meta = bm_iter->second;
1✔
1983
                yajlpp_gen gen;
1✔
1984

1985
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
1986

1987
                {
1988
                    yajlpp_array arr(gen);
1✔
1989

1990
                    for (const auto& str : meta.bm_tags) {
2✔
1991
                        arr.gen(str);
1✔
1992
                    }
1993
                }
1✔
1994

1995
                string_fragment sf = gen.to_string_fragment();
1✔
1996

1997
                sqlite3_bind_text(
1✔
1998
                    stmt, lpc + 1, sf.data(), sf.length(), SQLITE_TRANSIENT);
1999
            }
1✔
2000
            continue;
9✔
2001
        }
9✔
2002
        if (strcmp(name, ":log_format") == 0) {
177✔
2003
            const auto format_name = format->get_name();
6✔
2004
            sqlite3_bind_text(stmt,
6✔
2005
                              lpc + 1,
2006
                              format_name.get(),
2007
                              format_name.size(),
6✔
2008
                              SQLITE_STATIC);
2009
            continue;
6✔
2010
        }
6✔
2011
        if (strcmp(name, ":log_format_regex") == 0) {
171✔
2012
            const auto pat_name = format->get_pattern_name(line_number);
×
2013
            sqlite3_bind_text(
×
2014
                stmt, lpc + 1, pat_name.get(), pat_name.size(), SQLITE_STATIC);
×
2015
            continue;
×
2016
        }
2017
        if (strcmp(name, ":log_path") == 0) {
171✔
2018
            const auto& filename = lf->get_filename();
×
2019
            sqlite3_bind_text(stmt,
×
2020
                              lpc + 1,
2021
                              filename.c_str(),
2022
                              filename.native().length(),
×
2023
                              SQLITE_STATIC);
2024
            continue;
×
2025
        }
2026
        if (strcmp(name, ":log_unique_path") == 0) {
171✔
2027
            const auto& filename = lf->get_unique_path();
×
2028
            sqlite3_bind_text(stmt,
×
2029
                              lpc + 1,
2030
                              filename.c_str(),
2031
                              filename.native().length(),
×
2032
                              SQLITE_STATIC);
2033
            continue;
×
2034
        }
2035
        if (strcmp(name, ":log_text") == 0) {
171✔
2036
            sqlite3_bind_text(
4✔
2037
                stmt, lpc + 1, sbr.get_data(), sbr.length(), SQLITE_STATIC);
4✔
2038
            continue;
4✔
2039
        }
2040
        if (strcmp(name, ":log_body") == 0) {
167✔
2041
            auto body_attr_opt = get_string_attr(sa, SA_BODY);
16✔
2042
            if (body_attr_opt) {
16✔
2043
                const auto& sar
2044
                    = body_attr_opt.value().saw_string_attr->sa_range;
16✔
2045

2046
                sqlite3_bind_text(stmt,
32✔
2047
                                  lpc + 1,
2048
                                  sbr.get_data_at(sar.lr_start),
16✔
2049
                                  sar.length(),
2050
                                  SQLITE_STATIC);
2051
            } else {
2052
                sqlite3_bind_null(stmt, lpc + 1);
×
2053
            }
2054
            continue;
16✔
2055
        }
16✔
2056
        if (strcmp(name, ":log_opid") == 0) {
151✔
2057
            if (values.lvv_opid_value) {
×
2058
                sqlite3_bind_text(stmt,
×
2059
                                  lpc + 1,
2060
                                  values.lvv_opid_value->c_str(),
2061
                                  values.lvv_opid_value->length(),
×
2062
                                  SQLITE_STATIC);
2063
            } else {
2064
                sqlite3_bind_null(stmt, lpc + 1);
×
2065
            }
2066
            continue;
×
2067
        }
2068
        if (strcmp(name, ":log_raw_text") == 0) {
151✔
2069
            auto res = lf->read_raw_message(ll);
×
2070

2071
            if (res.isOk()) {
×
2072
                raw_sbr = res.unwrap();
×
2073
                sqlite3_bind_text(stmt,
×
2074
                                  lpc + 1,
2075
                                  raw_sbr.get_data(),
2076
                                  raw_sbr.length(),
×
2077
                                  SQLITE_STATIC);
2078
            }
2079
            continue;
×
2080
        }
2081
        for (const auto& lv : values.lvv_values) {
412✔
2082
            if (lv.lv_meta.lvm_name != &name[1]) {
409✔
2083
                continue;
261✔
2084
            }
2085

2086
            switch (lv.lv_meta.lvm_kind) {
148✔
2087
                case value_kind_t::VALUE_BOOLEAN:
×
2088
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
2089
                    break;
×
2090
                case value_kind_t::VALUE_FLOAT:
×
2091
                    sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
2092
                    break;
×
2093
                case value_kind_t::VALUE_INTEGER:
12✔
2094
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
12✔
2095
                    break;
12✔
2096
                case value_kind_t::VALUE_NULL:
×
2097
                    sqlite3_bind_null(stmt, lpc + 1);
×
2098
                    break;
×
2099
                default:
136✔
2100
                    sqlite3_bind_text(stmt,
136✔
2101
                                      lpc + 1,
2102
                                      lv.text_value(),
2103
                                      lv.text_length(),
136✔
2104
                                      SQLITE_TRANSIENT);
2105
                    break;
136✔
2106
            }
2107
            break;
148✔
2108
        }
2109
    }
2110

2111
    auto step_res = sqlite3_step(stmt);
185✔
2112

2113
    sqlite3_reset(stmt);
185✔
2114
    sqlite3_clear_bindings(stmt);
185✔
2115
    switch (step_res) {
185✔
2116
        case SQLITE_OK:
64✔
2117
        case SQLITE_DONE:
2118
            return Ok(false);
128✔
2119
        case SQLITE_ROW:
120✔
2120
            return Ok(true);
240✔
2121
        default:
1✔
2122
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2123
    }
2124
}
185✔
2125

2126
bool
2127
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
13,884✔
2128
{
2129
    if (this->lss_marked_only) {
13,884✔
2130
        auto found_mark = ll->is_marked() || ll->is_expr_marked();
6✔
2131
        auto to_start_ll = ll;
6✔
2132
        while (!found_mark && to_start_ll->is_continued()) {
6✔
2133
            if (to_start_ll->is_marked() || to_start_ll->is_expr_marked()) {
×
2134
                found_mark = true;
×
2135
            }
2136
            --to_start_ll;
×
2137
        }
2138
        auto to_end_ll = std::next(ll);
6✔
2139
        while (!found_mark && to_end_ll != (*ld)->get_file_ptr()->end()
10✔
2140
               && to_end_ll->is_continued())
11✔
2141
        {
2142
            if (to_end_ll->is_marked() || to_end_ll->is_expr_marked()) {
1✔
2143
                found_mark = true;
1✔
2144
            }
2145
            ++to_end_ll;
1✔
2146
        }
2147
        if (!found_mark) {
6✔
2148
            return false;
3✔
2149
        }
2150
    }
2151

2152
    if (ll->get_msg_level() < this->lss_min_log_level) {
13,881✔
2153
        return false;
2✔
2154
    }
2155

2156
    if (*ll < this->ttt_min_row_time) {
13,879✔
2157
        return false;
36✔
2158
    }
2159

2160
    if (!(*ll <= this->ttt_max_row_time)) {
13,843✔
2161
        return false;
4✔
2162
    }
2163

2164
    return true;
13,839✔
2165
}
2166

2167
void
2168
logfile_sub_source::invalidate_sql_filter()
24✔
2169
{
2170
    for (auto& ld : *this) {
48✔
2171
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
24✔
2172
    }
2173
}
24✔
2174

2175
void
2176
logfile_sub_source::text_mark(const bookmark_type_t* bm,
148✔
2177
                              vis_line_t line,
2178
                              bool added)
2179
{
2180
    if (line >= (int) this->lss_index.size()) {
148✔
2181
        return;
×
2182
    }
2183

2184
    auto cl = this->at(line);
148✔
2185

2186
    if (bm == &textview_curses::BM_USER) {
148✔
2187
        auto* ll = this->find_line(cl);
59✔
2188

2189
        ll->set_mark(added);
59✔
2190
    }
2191
    if (added) {
148✔
2192
        this->lss_user_marks[bm].insert_once(cl);
67✔
2193
    } else {
2194
        this->lss_user_marks[bm].erase(cl);
81✔
2195
    }
2196
    if (bm == &textview_curses::BM_META
148✔
2197
        && this->lss_meta_grepper.gps_proc != nullptr)
16✔
2198
    {
2199
        this->tss_view->search_range(line, line + 1_vl);
1✔
2200
        this->tss_view->search_new_data();
1✔
2201
    }
2202
}
2203

2204
void
2205
logfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
39✔
2206
{
2207
    if (bm == &textview_curses::BM_USER) {
39✔
2208
        for (auto iter = this->lss_user_marks[bm].bv_tree.begin();
6✔
2209
             iter != this->lss_user_marks[bm].bv_tree.end();
6✔
2210
             ++iter)
×
2211
        {
2212
            this->find_line(*iter)->set_mark(false);
×
2213
        }
2214
    }
2215
    this->lss_user_marks[bm].clear();
39✔
2216
}
39✔
2217

2218
void
2219
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
508✔
2220
{
2221
    auto iter = std::find_if(
508✔
2222
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
1,016✔
2223
    if (iter != this->lss_files.end()) {
508✔
2224
        int file_index = iter - this->lss_files.begin();
508✔
2225

2226
        (*iter)->clear();
508✔
2227
        for (auto& bv : this->lss_user_marks) {
4,572✔
2228
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
4,064✔
2229
            auto mark_end
2230
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
4,064✔
2231
            auto file_range = bv.equal_range(mark_curr, mark_end);
4,064✔
2232

2233
            if (file_range.first != file_range.second) {
4,064✔
2234
                auto to_del = std::vector<content_line_t>{};
66✔
2235
                for (auto file_iter = file_range.first;
66✔
2236
                     file_iter != file_range.second;
157✔
2237
                     ++file_iter)
91✔
2238
                {
2239
                    to_del.emplace_back(*file_iter);
91✔
2240
                }
2241

2242
                for (auto cl : to_del) {
157✔
2243
                    bv.erase(cl);
91✔
2244
                }
2245
            }
66✔
2246
        }
2247

2248
        this->lss_force_rebuild = true;
508✔
2249
    }
2250
    while (!this->lss_files.empty()) {
1,016✔
2251
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
558✔
2252
            this->lss_files.pop_back();
508✔
2253
        } else {
2254
            break;
50✔
2255
        }
2256
    }
2257
    this->lss_token_file = nullptr;
508✔
2258
}
508✔
2259

2260
std::optional<vis_line_t>
2261
logfile_sub_source::find_from_content(content_line_t cl)
5✔
2262
{
2263
    content_line_t line = cl;
5✔
2264
    std::shared_ptr<logfile> lf = this->find(line);
5✔
2265

2266
    if (lf != nullptr) {
5✔
2267
        auto ll_iter = lf->begin() + line;
5✔
2268
        auto& ll = *ll_iter;
5✔
2269
        auto vis_start_opt = this->find_from_time(ll.get_timeval());
5✔
2270

2271
        if (!vis_start_opt) {
5✔
2272
            return std::nullopt;
×
2273
        }
2274

2275
        auto vis_start = *vis_start_opt;
5✔
2276

2277
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2278
            content_line_t guess_cl = this->at(vis_start);
5✔
2279

2280
            if (cl == guess_cl) {
5✔
2281
                return vis_start;
5✔
2282
            }
2283

2284
            auto guess_line = this->find_line(guess_cl);
×
2285

2286
            if (!guess_line || ll < *guess_line) {
×
2287
                return std::nullopt;
×
2288
            }
2289

2290
            ++vis_start;
×
2291
        }
2292
    }
2293

2294
    return std::nullopt;
×
2295
}
5✔
2296

2297
void
2298
logfile_sub_source::reload_index_delegate()
624✔
2299
{
2300
    if (this->lss_index_delegate == nullptr) {
624✔
2301
        return;
×
2302
    }
2303

2304
    this->lss_index_delegate->index_start(*this);
624✔
2305
    for (const auto index : this->lss_filtered_index) {
652✔
2306
        auto cl = this->lss_index[index].value();
28✔
2307
        uint64_t line_number;
2308
        auto ld = this->find_data(cl, line_number);
28✔
2309
        std::shared_ptr<logfile> lf = (*ld)->get_file();
28✔
2310

2311
        this->lss_index_delegate->index_line(
28✔
2312
            *this, lf.get(), lf->begin() + line_number);
28✔
2313
    }
28✔
2314
    this->lss_index_delegate->index_complete(*this);
624✔
2315
}
2316

2317
std::optional<std::shared_ptr<text_filter>>
2318
logfile_sub_source::get_sql_filter()
2,332✔
2319
{
2320
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
2,332✔
2321
               return filt->get_index() == 0;
355✔
2322
           })
2323
        | lnav::itertools::deref();
4,664✔
2324
}
2325

2326
void
2327
log_location_history::loc_history_append(vis_line_t top)
93✔
2328
{
2329
    if (top < 0_vl || top >= vis_line_t(this->llh_log_source.text_line_count()))
93✔
2330
    {
2331
        return;
1✔
2332
    }
2333

2334
    auto cl = this->llh_log_source.at(top);
92✔
2335

2336
    auto iter = this->llh_history.begin();
92✔
2337
    iter += this->llh_history.size() - this->lh_history_position;
92✔
2338
    this->llh_history.erase_from(iter);
92✔
2339
    this->lh_history_position = 0;
92✔
2340
    this->llh_history.push_back(cl);
92✔
2341
}
2342

2343
std::optional<vis_line_t>
2344
log_location_history::loc_history_back(vis_line_t current_top)
2✔
2345
{
2346
    while (this->lh_history_position < this->llh_history.size()) {
2✔
2347
        auto iter = this->llh_history.rbegin();
2✔
2348

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

2351
        if (this->lh_history_position == 0 && vis_for_pos != current_top) {
2✔
2352
            return vis_for_pos;
2✔
2353
        }
2354

2355
        if ((this->lh_history_position + 1) >= this->llh_history.size()) {
2✔
2356
            break;
×
2357
        }
2358

2359
        this->lh_history_position += 1;
2✔
2360

2361
        iter += this->lh_history_position;
2✔
2362

2363
        vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2364

2365
        if (vis_for_pos) {
2✔
2366
            return vis_for_pos;
2✔
2367
        }
2368
    }
2369

2370
    return std::nullopt;
×
2371
}
2372

2373
std::optional<vis_line_t>
2374
log_location_history::loc_history_forward(vis_line_t current_top)
1✔
2375
{
2376
    while (this->lh_history_position > 0) {
1✔
2377
        this->lh_history_position -= 1;
1✔
2378

2379
        auto iter = this->llh_history.rbegin();
1✔
2380

2381
        iter += this->lh_history_position;
1✔
2382

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

2385
        if (vis_for_pos) {
1✔
2386
            return vis_for_pos;
1✔
2387
        }
2388
    }
2389

2390
    return std::nullopt;
×
2391
}
2392

2393
bool
2394
sql_filter::matches(std::optional<line_source> ls_opt,
121✔
2395
                    const shared_buffer_ref& line)
2396
{
2397
    if (!ls_opt) {
121✔
2398
        return false;
×
2399
    }
2400

2401
    auto ls = ls_opt;
121✔
2402

2403
    if (!ls->ls_line->is_message()) {
121✔
2404
        return false;
3✔
2405
    }
2406
    if (this->sf_filter_stmt == nullptr) {
118✔
2407
        return false;
×
2408
    }
2409

2410
    auto lfp = ls->ls_file.shared_from_this();
118✔
2411
    auto ld = this->sf_log_source.find_data_i(lfp);
118✔
2412
    if (ld == this->sf_log_source.end()) {
118✔
2413
        return false;
×
2414
    }
2415

2416
    auto eval_res = this->sf_log_source.eval_sql_filter(
118✔
2417
        this->sf_filter_stmt, ld, ls->ls_line);
118✔
2418
    if (eval_res.unwrapOr(true)) {
118✔
2419
        return false;
72✔
2420
    }
2421

2422
    return true;
46✔
2423
}
118✔
2424

2425
std::string
2426
sql_filter::to_command() const
×
2427
{
2428
    return fmt::format(FMT_STRING("filter-expr {}"), this->lf_id);
×
2429
}
2430

2431
std::optional<line_info>
2432
logfile_sub_source::meta_grepper::grep_value_for_line(vis_line_t line,
×
2433
                                                      std::string& value_out)
2434
{
2435
    auto line_meta_opt = this->lmg_source.find_bookmark_metadata(line);
×
2436
    if (!line_meta_opt) {
×
2437
        value_out.clear();
×
2438
    } else {
2439
        auto& bm = *(line_meta_opt.value());
×
2440

2441
        {
2442
            md2attr_line mdal;
×
2443

2444
            auto parse_res = md4cpp::parse(bm.bm_comment, mdal);
×
2445
            if (parse_res.isOk()) {
×
2446
                value_out.append(parse_res.unwrap().get_string());
×
2447
            } else {
2448
                value_out.append(bm.bm_comment);
×
2449
            }
2450
        }
2451

2452
        value_out.append("\x1c");
×
2453
        for (const auto& tag : bm.bm_tags) {
×
2454
            value_out.append(tag);
×
2455
            value_out.append("\x1c");
×
2456
        }
2457
        value_out.append("\x1c");
×
2458
        for (const auto& pair : bm.bm_annotations.la_pairs) {
×
2459
            value_out.append(pair.first);
×
2460
            value_out.append("\x1c");
×
2461

2462
            md2attr_line mdal;
×
2463

2464
            auto parse_res = md4cpp::parse(pair.second, mdal);
×
2465
            if (parse_res.isOk()) {
×
2466
                value_out.append(parse_res.unwrap().get_string());
×
2467
            } else {
2468
                value_out.append(pair.second);
×
2469
            }
2470
            value_out.append("\x1c");
×
2471
        }
2472
        value_out.append("\x1c");
×
2473
        value_out.append(bm.bm_opid);
×
2474
    }
2475

2476
    if (!this->lmg_done) {
×
2477
        return line_info{};
×
2478
    }
2479

2480
    return std::nullopt;
×
2481
}
2482

2483
vis_line_t
2484
logfile_sub_source::meta_grepper::grep_initial_line(vis_line_t start,
×
2485
                                                    vis_line_t highest)
2486
{
2487
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2488
    auto& bv = bm[&textview_curses::BM_META];
×
2489

2490
    if (bv.empty()) {
×
2491
        return -1_vl;
×
2492
    }
2493
    return *bv.bv_tree.begin();
×
2494
}
2495

2496
void
2497
logfile_sub_source::meta_grepper::grep_next_line(vis_line_t& line)
×
2498
{
2499
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2500
    auto& bv = bm[&textview_curses::BM_META];
×
2501

2502
    auto line_opt = bv.next(vis_line_t(line));
×
2503
    if (!line_opt) {
×
2504
        this->lmg_done = true;
×
2505
    }
2506
    line = line_opt.value_or(-1_vl);
×
2507
}
2508

2509
void
2510
logfile_sub_source::meta_grepper::grep_begin(grep_proc<vis_line_t>& gp,
23✔
2511
                                             vis_line_t start,
2512
                                             vis_line_t stop)
2513
{
2514
    this->lmg_source.quiesce();
23✔
2515

2516
    this->lmg_source.tss_view->grep_begin(gp, start, stop);
23✔
2517
}
23✔
2518

2519
void
2520
logfile_sub_source::meta_grepper::grep_end(grep_proc<vis_line_t>& gp)
23✔
2521
{
2522
    this->lmg_source.tss_view->grep_end(gp);
23✔
2523
}
23✔
2524

2525
void
2526
logfile_sub_source::meta_grepper::grep_match(grep_proc<vis_line_t>& gp,
3✔
2527
                                             vis_line_t line)
2528
{
2529
    this->lmg_source.tss_view->grep_match(gp, line);
3✔
2530
}
3✔
2531

2532
static std::vector<breadcrumb::possibility>
2533
timestamp_poss()
11✔
2534
{
2535
    const static std::vector<breadcrumb::possibility> retval = {
2536
        breadcrumb::possibility{"-1 day"},
2537
        breadcrumb::possibility{"-1h"},
2538
        breadcrumb::possibility{"-30m"},
2539
        breadcrumb::possibility{"-15m"},
2540
        breadcrumb::possibility{"-5m"},
2541
        breadcrumb::possibility{"-1m"},
2542
        breadcrumb::possibility{"+1m"},
2543
        breadcrumb::possibility{"+5m"},
2544
        breadcrumb::possibility{"+15m"},
2545
        breadcrumb::possibility{"+30m"},
2546
        breadcrumb::possibility{"+1h"},
2547
        breadcrumb::possibility{"+1 day"},
2548
    };
95✔
2549

2550
    return retval;
11✔
2551
}
24✔
2552

2553
static attr_line_t
2554
to_display(const std::shared_ptr<logfile>& lf)
29✔
2555
{
2556
    attr_line_t retval;
29✔
2557

2558
    if (lf->get_open_options().loo_piper) {
29✔
2559
        if (!lf->get_open_options().loo_piper->is_finished()) {
×
2560
            retval.append("\u21bb "_list_glyph);
×
2561
        }
2562
    }
2563
    retval.append(lf->get_unique_path());
29✔
2564

2565
    return retval;
29✔
2566
}
×
2567

2568
void
2569
logfile_sub_source::text_crumbs_for_line(int line,
27✔
2570
                                         std::vector<breadcrumb::crumb>& crumbs)
2571
{
2572
    text_sub_source::text_crumbs_for_line(line, crumbs);
27✔
2573

2574
    if (this->lss_filtered_index.empty()) {
27✔
2575
        return;
9✔
2576
    }
2577

2578
    auto vl = vis_line_t(line);
18✔
2579
    auto bmc = this->get_bookmark_metadata_context(
18✔
2580
        vl, bookmark_metadata::categories::partition);
2581
    if (bmc.bmc_current_metadata) {
18✔
2582
        const auto& name = bmc.bmc_current_metadata.value()->bm_name;
1✔
2583
        auto key = text_anchors::to_anchor_string(name);
1✔
2584
        auto display = attr_line_t()
1✔
2585
                           .append("\u2291 "_symbol)
1✔
2586
                           .append(lnav::roles::variable(name))
2✔
2587
                           .move();
1✔
2588
        crumbs.emplace_back(
1✔
2589
            key,
2590
            display,
2591
            [this]() -> std::vector<breadcrumb::possibility> {
×
2592
                auto& vb = this->tss_view->get_bookmarks();
1✔
2593
                const auto& bv = vb[&textview_curses::BM_PARTITION];
1✔
2594
                std::vector<breadcrumb::possibility> retval;
1✔
2595

2596
                for (const auto& vl : bv.bv_tree) {
2✔
2597
                    auto meta_opt = this->find_bookmark_metadata(vl);
1✔
2598
                    if (!meta_opt || meta_opt.value()->bm_name.empty()) {
1✔
2599
                        continue;
×
2600
                    }
2601

2602
                    const auto& name = meta_opt.value()->bm_name;
1✔
2603
                    retval.emplace_back(text_anchors::to_anchor_string(name),
1✔
2604
                                        name);
2605
                }
2606

2607
                return retval;
1✔
2608
            },
×
2609
            [ec = this->lss_exec_context](const auto& part) {
1✔
2610
                auto cmd = fmt::format(FMT_STRING(":goto {}"),
×
2611
                                       part.template get<std::string>());
2612
                ec->execute(INTERNAL_SRC_LOC, cmd);
×
2613
            });
×
2614
    }
1✔
2615

2616
    auto line_pair_opt = this->find_line_with_file(vl);
18✔
2617
    if (!line_pair_opt) {
18✔
2618
        return;
×
2619
    }
2620
    auto line_pair = line_pair_opt.value();
18✔
2621
    auto& lf = line_pair.first;
18✔
2622
    auto format = lf->get_format();
18✔
2623
    char ts[64];
2624

2625
    sql_strftime(ts, sizeof(ts), line_pair.second->get_timeval(), 'T');
18✔
2626

2627
    crumbs.emplace_back(std::string(ts),
18✔
2628
                        timestamp_poss,
2629
                        [ec = this->lss_exec_context](const auto& ts) {
18✔
2630
                            auto cmd
×
2631
                                = fmt::format(FMT_STRING(":goto {}"),
×
2632
                                              ts.template get<std::string>());
2633
                            ec->execute(INTERNAL_SRC_LOC, cmd);
×
2634
                        });
×
2635
    crumbs.back().c_expected_input
18✔
2636
        = breadcrumb::crumb::expected_input_t::anything;
18✔
2637
    crumbs.back().c_search_placeholder = "(Enter an absolute or relative time)";
18✔
2638

2639
    auto format_name = format->get_name().to_string();
18✔
2640

2641
    crumbs.emplace_back(
18✔
2642
        format_name,
2643
        attr_line_t().append(format_name),
18✔
2644
        [this]() -> std::vector<breadcrumb::possibility> {
×
2645
            return this->lss_files
11✔
2646
                | lnav::itertools::filter_in([](const auto& file_data) {
22✔
2647
                       return file_data->is_visible();
11✔
2648
                   })
2649
                | lnav::itertools::map(&logfile_data::get_file_ptr)
33✔
2650
                | lnav::itertools::map(&logfile::get_format_name)
33✔
2651
                | lnav::itertools::unique()
33✔
2652
                | lnav::itertools::map([](const auto& elem) {
44✔
2653
                       return breadcrumb::possibility{
2654
                           elem.to_string(),
2655
                       };
11✔
2656
                   })
2657
                | lnav::itertools::to_vector();
33✔
2658
        },
2659
        [ec = this->lss_exec_context](const auto& format_name) {
18✔
2660
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2661
     SET selection = ifnull(
2662
         (SELECT log_line FROM all_logs WHERE log_format = $format_name LIMIT 1),
2663
         (SELECT raise_error(
2664
            'Could not find format: ' || $format_name,
2665
            'The corresponding log messages might have been filtered out'))
2666
       )
2667
     WHERE name = 'log'
2668
)";
2669

2670
            ec->execute_with(
×
2671
                INTERNAL_SRC_LOC,
×
2672
                MOVE_STMT,
2673
                std::make_pair("format_name",
2674
                               format_name.template get<std::string>()));
2675
        });
×
2676

2677
    auto msg_start_iter = lf->message_start(line_pair.second);
18✔
2678
    auto file_line_number = std::distance(lf->begin(), msg_start_iter);
18✔
2679
    crumbs.emplace_back(
36✔
2680
        lf->get_unique_path(),
18✔
2681
        to_display(lf).appendf(FMT_STRING("[{:L}]"), file_line_number),
72✔
2682
        [this]() -> std::vector<breadcrumb::possibility> {
×
2683
            return this->lss_files
11✔
2684
                | lnav::itertools::filter_in([](const auto& file_data) {
22✔
2685
                       return file_data->is_visible();
11✔
2686
                   })
2687
                | lnav::itertools::map([](const auto& file_data) {
22✔
2688
                       return breadcrumb::possibility{
2689
                           file_data->get_file_ptr()->get_unique_path(),
11✔
2690
                           to_display(file_data->get_file()),
2691
                       };
11✔
2692
                   });
22✔
2693
        },
2694
        [ec = this->lss_exec_context](const auto& uniq_path) {
18✔
2695
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2696
     SET selection = ifnull(
2697
          (SELECT log_line FROM all_logs WHERE log_unique_path = $uniq_path LIMIT 1),
2698
          (SELECT raise_error(
2699
            'Could not find file: ' || $uniq_path,
2700
            'The corresponding log messages might have been filtered out'))
2701
         )
2702
     WHERE name = 'log'
2703
)";
2704

2705
            ec->execute_with(
×
2706
                INTERNAL_SRC_LOC,
×
2707
                MOVE_STMT,
2708
                std::make_pair("uniq_path",
2709
                               uniq_path.template get<std::string>()));
2710
        });
×
2711

2712
    shared_buffer sb;
18✔
2713
    logline_value_vector values;
18✔
2714
    auto& sbr = values.lvv_sbr;
18✔
2715

2716
    lf->read_full_message(msg_start_iter, sbr);
18✔
2717
    attr_line_t al(to_string(sbr));
18✔
2718
    if (!sbr.get_metadata().m_valid_utf) {
18✔
2719
        scrub_to_utf8(&al.al_string[0], al.al_string.length());
×
2720
    }
2721
    if (sbr.get_metadata().m_has_ansi) {
18✔
2722
        // bleh
2723
        scrub_ansi_string(al.get_string(), &al.al_attrs);
×
2724
        sbr.share(sb, al.al_string.data(), al.al_string.size());
×
2725
    }
2726
    format->annotate(lf.get(), file_line_number, al.get_attrs(), values);
18✔
2727

2728
    {
2729
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
30✔
2730
          SET selection = ifnull(
2731
            (SELECT log_line FROM all_logs WHERE log_opid = $opid LIMIT 1),
2732
            (SELECT raise_error('Could not find opid: ' || $opid,
2733
                                'The corresponding log messages might have been filtered out')))
2734
          WHERE name = 'log'
2735
        )";
2736
        static const std::string ELLIPSIS = "\u22ef";
30✔
2737

2738
        auto opid_display = values.lvv_opid_value.has_value()
18✔
2739
            ? lnav::roles::identifier(values.lvv_opid_value.value())
10✔
2740
            : lnav::roles::hidden(ELLIPSIS);
28✔
2741
        crumbs.emplace_back(
36✔
2742
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
44✔
2743
                                              : "",
2744
            attr_line_t().append(opid_display),
18✔
2745
            [this]() -> std::vector<breadcrumb::possibility> {
×
2746
                std::set<std::string> poss_strs;
11✔
2747

2748
                for (const auto& file_data : this->lss_files) {
22✔
2749
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2750
                        continue;
×
2751
                    }
2752
                    safe::ReadAccess<logfile::safe_opid_state> r_opid_map(
2753
                        file_data->get_file_ptr()->get_opids());
11✔
2754

2755
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
786✔
2756
                        poss_strs.emplace(pair.first.to_string());
775✔
2757
                    }
2758
                }
11✔
2759

2760
                std::vector<breadcrumb::possibility> retval;
11✔
2761

2762
                std::transform(poss_strs.begin(),
11✔
2763
                               poss_strs.end(),
2764
                               std::back_inserter(retval),
2765
                               [](const auto& opid_str) {
775✔
2766
                                   return breadcrumb::possibility(opid_str);
775✔
2767
                               });
2768

2769
                return retval;
22✔
2770
            },
11✔
2771
            [ec = this->lss_exec_context](const auto& opid) {
18✔
2772
                ec->execute_with(
×
2773
                    INTERNAL_SRC_LOC,
×
2774
                    MOVE_STMT,
2775
                    std::make_pair("opid", opid.template get<std::string>()));
2776
            });
×
2777
    }
18✔
2778

2779
    auto sf = string_fragment::from_str(al.get_string());
18✔
2780
    auto body_opt = get_string_attr(al.get_attrs(), SA_BODY);
18✔
2781
    auto nl_pos_opt = sf.find('\n');
18✔
2782
    auto msg_line_number = std::distance(msg_start_iter, line_pair.second);
18✔
2783
    auto line_from_top = line - msg_line_number;
18✔
2784
    if (body_opt && nl_pos_opt) {
18✔
2785
        if (this->lss_token_meta_line != file_line_number
18✔
2786
            || this->lss_token_meta_size != sf.length())
9✔
2787
        {
2788
            if (body_opt->saw_string_attr->sa_range.length() < 128 * 1024) {
3✔
2789
                this->lss_token_meta
2790
                    = lnav::document::discover(al)
3✔
2791
                          .over_range(
3✔
2792
                              body_opt.value().saw_string_attr->sa_range)
3✔
2793
                          .perform();
3✔
2794
                // XXX discover_structure() changes `al`, have to recompute
2795
                // stuff
2796
                sf = al.to_string_fragment();
3✔
2797
                body_opt = get_string_attr(al.get_attrs(), SA_BODY);
3✔
2798
            } else {
2799
                this->lss_token_meta = lnav::document::metadata{};
×
2800
            }
2801
            this->lss_token_meta_line = file_line_number;
3✔
2802
            this->lss_token_meta_size = sf.length();
3✔
2803
        }
2804

2805
        const auto initial_size = crumbs.size();
9✔
2806
        auto sf_body
2807
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
9✔
2808
                           body_opt->saw_string_attr->sa_range.lr_end);
9✔
2809
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
9✔
2810
        file_off_t line_end_offset = sf.length();
9✔
2811
        ssize_t line_number = 0;
9✔
2812

2813
        for (const auto& sf_line : sf_body.split_lines()) {
18✔
2814
            if (line_number >= msg_line_number) {
12✔
2815
                line_end_offset = line_offset + sf_line.length();
3✔
2816
                break;
3✔
2817
            }
2818
            line_number += 1;
9✔
2819
            line_offset += sf_line.length();
9✔
2820
        }
9✔
2821

2822
        this->lss_token_meta.m_sections_tree.visit_overlapping(
9✔
2823
            line_offset,
2824
            line_end_offset,
2825
            [this,
2✔
2826
             initial_size,
2827
             meta = &this->lss_token_meta,
9✔
2828
             &crumbs,
2829
             line_from_top](const auto& iv) {
2830
                auto path = crumbs | lnav::itertools::skip(initial_size)
6✔
2831
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
4✔
2832
                    | lnav::itertools::append(iv.value);
2✔
2833
                auto curr_node = lnav::document::hier_node::lookup_path(
2✔
2834
                    meta->m_sections_root.get(), path);
2✔
2835

2836
                crumbs.emplace_back(
4✔
2837
                    iv.value,
2✔
2838
                    [meta, path]() { return meta->possibility_provider(path); },
4✔
2839
                    [this, curr_node, path, line_from_top](const auto& key) {
4✔
2840
                        if (!curr_node) {
×
2841
                            return;
×
2842
                        }
2843
                        auto* parent_node = curr_node.value()->hn_parent;
×
2844
                        if (parent_node == nullptr) {
×
2845
                            return;
×
2846
                        }
2847
                        key.match(
2848
                            [parent_node](const std::string& str) {
×
2849
                                return parent_node->find_line_number(str);
×
2850
                            },
2851
                            [parent_node](size_t index) {
×
2852
                                return parent_node->find_line_number(index);
×
2853
                            })
2854
                            | [this, line_from_top](auto line_number) {
×
2855
                                  this->tss_view->set_selection(
×
2856
                                      vis_line_t(line_from_top + line_number));
×
2857
                              };
2858
                    });
2859
                if (curr_node && !curr_node.value()->hn_parent->is_named_only())
2✔
2860
                {
2861
                    auto node = lnav::document::hier_node::lookup_path(
×
2862
                        meta->m_sections_root.get(), path);
×
2863

2864
                    crumbs.back().c_expected_input
×
2865
                        = curr_node.value()
×
2866
                              ->hn_parent->hn_named_children.empty()
×
2867
                        ? breadcrumb::crumb::expected_input_t::index
×
2868
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
2869
                    crumbs.back().with_possible_range(
×
2870
                        node | lnav::itertools::map([](const auto hn) {
×
2871
                            return hn->hn_parent->hn_children.size();
×
2872
                        })
2873
                        | lnav::itertools::unwrap_or(size_t{0}));
×
2874
                }
2875
            });
2✔
2876

2877
        auto path = crumbs | lnav::itertools::skip(initial_size)
18✔
2878
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
18✔
2879
        auto node = lnav::document::hier_node::lookup_path(
9✔
2880
            this->lss_token_meta.m_sections_root.get(), path);
9✔
2881

2882
        if (node && !node.value()->hn_children.empty()) {
9✔
2883
            auto poss_provider = [curr_node = node.value()]() {
1✔
2884
                std::vector<breadcrumb::possibility> retval;
1✔
2885
                for (const auto& child : curr_node->hn_named_children) {
1✔
2886
                    retval.emplace_back(child.first);
×
2887
                }
2888
                return retval;
1✔
2889
            };
2890
            auto path_performer
2891
                = [this, curr_node = node.value(), line_from_top](
2✔
2892
                      const breadcrumb::crumb::key_t& value) {
2893
                      value.match(
×
2894
                          [curr_node](const std::string& str) {
×
2895
                              return curr_node->find_line_number(str);
×
2896
                          },
2897
                          [curr_node](size_t index) {
×
2898
                              return curr_node->find_line_number(index);
×
2899
                          })
2900
                          | [this, line_from_top](size_t line_number) {
×
2901
                                this->tss_view->set_selection(
×
2902
                                    vis_line_t(line_from_top + line_number));
×
2903
                            };
2904
                  };
1✔
2905
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
1✔
2906
            crumbs.back().c_expected_input
1✔
2907
                = node.value()->hn_named_children.empty()
2✔
2908
                ? breadcrumb::crumb::expected_input_t::index
1✔
2909
                : breadcrumb::crumb::expected_input_t::index_or_exact;
2910
        }
2911
    }
9✔
2912
}
18✔
2913

2914
void
2915
logfile_sub_source::quiesce()
42✔
2916
{
2917
    for (auto& ld : this->lss_files) {
84✔
2918
        auto* lf = ld->get_file_ptr();
42✔
2919

2920
        if (lf == nullptr) {
42✔
2921
            continue;
×
2922
        }
2923

2924
        lf->quiesce();
42✔
2925
    }
2926
}
42✔
2927

2928
bookmark_metadata&
2929
logfile_sub_source::get_bookmark_metadata(content_line_t cl)
25✔
2930
{
2931
    auto line_pair = this->find_line_with_file(cl).value();
25✔
2932
    auto line_number = static_cast<uint32_t>(
2933
        std::distance(line_pair.first->begin(), line_pair.second));
25✔
2934

2935
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
2936
}
25✔
2937

2938
logfile_sub_source::bookmark_metadata_context
2939
logfile_sub_source::get_bookmark_metadata_context(
3,458✔
2940
    vis_line_t vl, bookmark_metadata::categories desired) const
2941
{
2942
    const auto& vb = this->tss_view->get_bookmarks();
3,458✔
2943
    const auto& bv = vb[desired == bookmark_metadata::categories::partition
2944
                            ? &textview_curses::BM_PARTITION
2945
                            : &textview_curses::BM_META];
3,458✔
2946
    auto vl_iter = bv.bv_tree.lower_bound(vl + 1_vl);
3,458✔
2947

2948
    std::optional<vis_line_t> next_line;
3,458✔
2949
    for (auto next_vl_iter = vl_iter; next_vl_iter != bv.bv_tree.end();
3,458✔
2950
         ++next_vl_iter)
×
2951
    {
2952
        auto bm_opt = this->find_bookmark_metadata(*next_vl_iter);
2✔
2953
        if (!bm_opt) {
2✔
2954
            continue;
×
2955
        }
2956

2957
        if (bm_opt.value()->has(desired)) {
2✔
2958
            next_line = *next_vl_iter;
2✔
2959
            break;
2✔
2960
        }
2961
    }
2962
    if (vl_iter == bv.bv_tree.begin()) {
3,458✔
2963
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3,442✔
2964
    }
2965

2966
    --vl_iter;
16✔
2967
    while (true) {
2968
        auto bm_opt = this->find_bookmark_metadata(*vl_iter);
16✔
2969
        if (bm_opt) {
16✔
2970
            if (bm_opt.value()->has(desired)) {
13✔
2971
                return bookmark_metadata_context{
2972
                    *vl_iter, bm_opt.value(), next_line};
16✔
2973
            }
2974
        }
2975

2976
        if (vl_iter == bv.bv_tree.begin()) {
3✔
2977
            return bookmark_metadata_context{
2978
                std::nullopt, std::nullopt, next_line};
3✔
2979
        }
2980
        --vl_iter;
×
2981
    }
2982
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
2983
}
2984

2985
std::optional<bookmark_metadata*>
2986
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
21,424✔
2987
{
2988
    auto line_pair = this->find_line_with_file(cl).value();
21,424✔
2989
    auto line_number = static_cast<uint32_t>(
2990
        std::distance(line_pair.first->begin(), line_pair.second));
21,424✔
2991

2992
    auto& bm = line_pair.first->get_bookmark_metadata();
21,424✔
2993
    auto bm_iter = bm.find(line_number);
21,424✔
2994
    if (bm_iter == bm.end()) {
21,424✔
2995
        return std::nullopt;
21,046✔
2996
    }
2997

2998
    return &bm_iter->second;
378✔
2999
}
21,424✔
3000

3001
void
3002
logfile_sub_source::erase_bookmark_metadata(content_line_t cl)
26✔
3003
{
3004
    auto line_pair = this->find_line_with_file(cl).value();
26✔
3005
    auto line_number = static_cast<uint32_t>(
3006
        std::distance(line_pair.first->begin(), line_pair.second));
26✔
3007

3008
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
3009
    auto bm_iter = bm.find(line_number);
26✔
3010
    if (bm_iter != bm.end()) {
26✔
3011
        bm.erase(bm_iter);
6✔
3012
    }
3013
}
26✔
3014

3015
void
3016
logfile_sub_source::clear_bookmark_metadata()
6✔
3017
{
3018
    for (auto& ld : *this) {
14✔
3019
        if (ld->get_file_ptr() == nullptr) {
8✔
3020
            continue;
×
3021
        }
3022

3023
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3024
    }
3025
}
6✔
3026

3027
void
3028
logfile_sub_source::increase_line_context()
×
3029
{
3030
    auto old_context = this->lss_line_context;
×
3031

3032
    switch (this->lss_line_context) {
×
3033
        case line_context_t::filename:
×
3034
            // nothing to do
3035
            break;
×
3036
        case line_context_t::basename:
×
3037
            this->lss_line_context = line_context_t::filename;
×
3038
            break;
×
3039
        case line_context_t::none:
×
3040
            this->lss_line_context = line_context_t::basename;
×
3041
            break;
×
3042
        case line_context_t::time_column:
×
3043
            this->lss_line_context = line_context_t::none;
×
3044
            break;
×
3045
    }
3046
    if (old_context != this->lss_line_context) {
×
3047
        this->clear_line_size_cache();
×
3048
    }
3049
}
3050

3051
bool
3052
logfile_sub_source::decrease_line_context()
×
3053
{
3054
    static const auto& cfg
3055
        = injector::get<const logfile_sub_source_ns::config&>();
×
3056
    auto old_context = this->lss_line_context;
×
3057

3058
    switch (this->lss_line_context) {
×
3059
        case line_context_t::filename:
×
3060
            this->lss_line_context = line_context_t::basename;
×
3061
            break;
×
3062
        case line_context_t::basename:
×
3063
            this->lss_line_context = line_context_t::none;
×
3064
            break;
×
3065
        case line_context_t::none:
×
3066
            if (cfg.c_time_column
×
3067
                != logfile_sub_source_ns::time_column_feature_t::Disabled)
3068
            {
3069
                this->lss_line_context = line_context_t::time_column;
×
3070
            }
3071
            break;
×
3072
        case line_context_t::time_column:
×
3073
            break;
×
3074
    }
3075
    if (old_context != this->lss_line_context) {
×
3076
        this->clear_line_size_cache();
×
3077

3078
        return true;
×
3079
    }
3080

3081
    return false;
×
3082
}
3083

3084
size_t
3085
logfile_sub_source::get_filename_offset() const
238✔
3086
{
3087
    switch (this->lss_line_context) {
238✔
3088
        case line_context_t::filename:
×
3089
            return this->lss_filename_width;
×
3090
        case line_context_t::basename:
×
3091
            return this->lss_basename_width;
×
3092
        default:
238✔
3093
            return 0;
238✔
3094
    }
3095
}
3096

3097
size_t
3098
logfile_sub_source::file_count() const
3,921✔
3099
{
3100
    size_t retval = 0;
3,921✔
3101
    const_iterator iter;
3,921✔
3102

3103
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,450✔
3104
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,529✔
3105
            retval += 1;
3,523✔
3106
        }
3107
    }
3108

3109
    return retval;
3,921✔
3110
}
3111

3112
size_t
3113
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3114
                                       int row,
3115
                                       text_sub_source::line_flags_t flags)
3116
{
3117
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3118

3119
    if (this->lss_line_size_cache[index].first != row) {
×
3120
        std::string value;
×
3121

3122
        this->text_value_for_line(tc, row, value, flags);
×
3123
        scrub_ansi_string(value, nullptr);
×
3124
        auto line_width = string_fragment::from_str(value).column_width();
×
3125
        if (this->lss_line_context == line_context_t::time_column) {
×
3126
            auto time_attr
3127
                = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
×
3128
            if (time_attr != this->lss_token_attrs.end()) {
×
3129
                line_width -= time_attr->sa_range.length();
×
3130
                auto format = this->lss_token_file->get_format();
×
3131
                if (format->lf_level_hideable) {
×
3132
                    auto level_attr
3133
                        = find_string_attr(this->lss_token_attrs, &L_LEVEL);
×
3134
                    if (level_attr != this->lss_token_attrs.end()) {
×
3135
                        line_width -= level_attr->sa_range.length();
×
3136
                    }
3137
                }
3138
            }
3139
        }
3140
        this->lss_line_size_cache[index].second = line_width;
×
3141
        this->lss_line_size_cache[index].first = row;
×
3142
    }
3143
    return this->lss_line_size_cache[index].second;
×
3144
}
3145

3146
int
3147
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
1✔
3148
{
3149
    int retval = 0;
1✔
3150

3151
    for (const auto& ld : this->lss_files) {
2✔
3152
        retval += ld->ld_filter_state.lfo_filter_state
1✔
3153
                      .tfs_filter_hits[filter_index];
1✔
3154
    }
3155

3156
    return retval;
1✔
3157
}
3158

3159
std::optional<vis_line_t>
3160
logfile_sub_source::row_for(const row_info& ri)
252✔
3161
{
3162
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
504✔
3163
                               this->lss_filtered_index.end(),
3164
                               ri.ri_time,
252✔
3165
                               filtered_logline_cmp(*this));
3166
    if (lb != this->lss_filtered_index.end()) {
252✔
3167
        auto first_lb = lb;
247✔
3168
        while (true) {
3169
            auto cl = this->lss_index[*lb].value();
261✔
3170
            if (content_line_t(ri.ri_id) == cl) {
261✔
3171
                first_lb = lb;
217✔
3172
                break;
217✔
3173
            }
3174
            auto ll = this->find_line(cl);
44✔
3175
            if (ll->get_timeval() != ri.ri_time) {
44✔
3176
                break;
30✔
3177
            }
3178
            auto next_lb = std::next(lb);
14✔
3179
            if (next_lb == this->lss_filtered_index.end()) {
14✔
3180
                break;
×
3181
            }
3182
            lb = next_lb;
14✔
3183
        }
14✔
3184

3185
        const auto dst
3186
            = std::distance(this->lss_filtered_index.begin(), first_lb);
247✔
3187
        return vis_line_t(dst);
247✔
3188
    }
3189

3190
    return std::nullopt;
5✔
3191
}
3192

3193
std::unique_ptr<logline_window>
3194
logfile_sub_source::window_at(vis_line_t start_vl, vis_line_t end_vl)
35✔
3195
{
3196
    return std::make_unique<logline_window>(*this, start_vl, end_vl);
35✔
3197
}
3198

3199
std::unique_ptr<logline_window>
3200
logfile_sub_source::window_at(vis_line_t start_vl)
201✔
3201
{
3202
    return std::make_unique<logline_window>(*this, start_vl, start_vl + 1_vl);
201✔
3203
}
3204

3205
std::unique_ptr<logline_window>
3206
logfile_sub_source::window_to_end(vis_line_t start_vl)
×
3207
{
3208
    return std::make_unique<logline_window>(
3209
        *this, start_vl, vis_line_t(this->text_line_count()));
×
3210
}
3211

3212
std::optional<vis_line_t>
3213
logfile_sub_source::row_for_anchor(const std::string& id)
3✔
3214
{
3215
    if (startswith(id, "#msg")) {
3✔
3216
        static const auto ANCHOR_RE
3217
            = lnav::pcre2pp::code::from_const(R"(#msg([0-9a-fA-F]+)-(.+))");
3✔
3218
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
3✔
3219

3220
        if (ANCHOR_RE.capture_from(id).into(md).found_p()) {
3✔
3221
            auto scan_res = scn::scan<int64_t>(md[1]->to_string_view(), "{:x}");
3✔
3222
            if (scan_res) {
3✔
3223
                auto ts_low = std::chrono::microseconds{scan_res->value()};
3✔
3224
                auto ts_high = ts_low + 1us;
3✔
3225

3226
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3227
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3228
                if (low_vl) {
3✔
3229
                    auto lw = this->window_at(
3230
                        low_vl.value(),
3✔
3231
                        high_vl.value_or(low_vl.value() + 1_vl));
6✔
3232

3233
                    for (const auto& li : *lw) {
3✔
3234
                        auto hash_res = li.get_line_hash();
3✔
3235
                        if (hash_res.isErr()) {
3✔
3236
                            auto errmsg = hash_res.unwrapErr();
×
3237

3238
                            log_error("unable to get line hash: %s",
×
3239
                                      errmsg.c_str());
3240
                            continue;
×
3241
                        }
3242

3243
                        auto hash = hash_res.unwrap();
3✔
3244
                        if (hash == md[2]) {
3✔
3245
                            return li.get_vis_line();
3✔
3246
                        }
3247
                    }
12✔
3248
                }
3✔
3249
            }
3250
        }
3251

3252
        return std::nullopt;
×
3253
    }
3254

3255
    auto& vb = this->tss_view->get_bookmarks();
×
3256
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3257

3258
    for (const auto& vl : bv.bv_tree) {
×
3259
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3260
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3261
            continue;
×
3262
        }
3263

3264
        const auto& name = meta_opt.value()->bm_name;
×
3265
        if (id == text_anchors::to_anchor_string(name)) {
×
3266
            return vl;
×
3267
        }
3268
    }
3269

3270
    return std::nullopt;
×
3271
}
3272

3273
std::optional<vis_line_t>
3274
logfile_sub_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
2✔
3275
{
3276
    if (vl < this->lss_filtered_index.size()) {
2✔
3277
        auto file_and_line_pair = this->find_line_with_file(vl);
2✔
3278
        if (file_and_line_pair) {
2✔
3279
            const auto& [lf, line] = file_and_line_pair.value();
2✔
3280
            if (line->is_continued()) {
2✔
3281
                auto retval = vl;
×
3282
                switch (dir) {
×
3283
                    case direction::prev: {
×
3284
                        auto first_line = line;
×
3285
                        while (first_line->is_continued()) {
×
3286
                            --first_line;
×
3287
                            retval -= 1_vl;
×
3288
                        }
3289
                        return retval;
×
3290
                    }
3291
                    case direction::next: {
×
3292
                        auto first_line = line;
×
3293
                        while (first_line->is_continued()) {
×
3294
                            ++first_line;
×
3295
                            retval += 1_vl;
×
3296
                        }
3297
                        return retval;
×
3298
                    }
3299
                }
3300
            }
3301
        }
3302
    }
2✔
3303

3304
    auto bmc = this->get_bookmark_metadata_context(
2✔
3305
        vl, bookmark_metadata::categories::partition);
3306
    switch (dir) {
2✔
3307
        case direction::prev: {
×
3308
            if (bmc.bmc_current && bmc.bmc_current.value() != vl) {
×
3309
                return bmc.bmc_current;
×
3310
            }
3311
            if (!bmc.bmc_current || bmc.bmc_current.value() == 0_vl) {
×
3312
                return 0_vl;
×
3313
            }
3314
            auto prev_bmc = this->get_bookmark_metadata_context(
×
3315
                bmc.bmc_current.value() - 1_vl,
×
3316
                bookmark_metadata::categories::partition);
3317
            if (!prev_bmc.bmc_current) {
×
3318
                return 0_vl;
×
3319
            }
3320
            return prev_bmc.bmc_current;
×
3321
        }
3322
        case direction::next:
2✔
3323
            return bmc.bmc_next_line;
2✔
3324
    }
3325
    return std::nullopt;
×
3326
}
3327

3328
std::optional<std::string>
3329
logfile_sub_source::anchor_for_row(vis_line_t vl)
75✔
3330
{
3331
    auto line_meta = this->get_bookmark_metadata_context(
75✔
3332
        vl, bookmark_metadata::categories::partition);
3333
    if (!line_meta.bmc_current_metadata) {
75✔
3334
        auto lw = window_at(vl);
72✔
3335

3336
        for (const auto& li : *lw) {
72✔
3337
            auto hash_res = li.get_line_hash();
72✔
3338
            if (hash_res.isErr()) {
72✔
3339
                auto errmsg = hash_res.unwrapErr();
×
3340
                log_error("unable to compute line hash: %s", errmsg.c_str());
×
3341
                break;
×
3342
            }
3343
            auto hash = hash_res.unwrap();
72✔
3344
            auto retval = fmt::format(
3345
                FMT_STRING("#msg{:016x}-{}"),
144✔
3346
                li.get_logline().get_time<std::chrono::microseconds>().count(),
72✔
3347
                hash);
×
3348

3349
            return retval;
72✔
3350
        }
288✔
3351

3352
        return std::nullopt;
×
3353
    }
72✔
3354

3355
    return text_anchors::to_anchor_string(
3✔
3356
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3357
}
3358

3359
std::unordered_set<std::string>
3360
logfile_sub_source::get_anchors()
×
3361
{
3362
    auto& vb = this->tss_view->get_bookmarks();
×
3363
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3364
    std::unordered_set<std::string> retval;
×
3365

3366
    for (const auto& vl : bv.bv_tree) {
×
3367
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3368
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3369
            continue;
×
3370
        }
3371

3372
        const auto& name = meta_opt.value()->bm_name;
×
3373
        retval.emplace(text_anchors::to_anchor_string(name));
×
3374
    }
3375

3376
    return retval;
×
3377
}
×
3378

3379
bool
3380
logfile_sub_source::text_handle_mouse(
×
3381
    textview_curses& tc,
3382
    const listview_curses::display_line_content_t& mouse_line,
3383
    mouse_event& me)
3384
{
3385
    if (mouse_line.is<listview_curses::static_overlay_content>()
×
3386
        && this->text_line_count() > 0)
×
3387
    {
3388
        auto top = tc.get_top();
×
3389
        if (top > 0) {
×
3390
            auto win = this->window_at(top - 1_vl);
×
3391
            for (const auto& li : *win) {
×
3392
                tc.set_top(li.get_vis_line());
×
3393
                tc.set_selection(li.get_vis_line());
×
3394
                return true;
×
3395
            }
3396
        }
3397
    }
3398

3399
    if (tc.get_overlay_selection()) {
×
3400
        auto nci = ncinput{};
×
3401
        if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 2, 4)) {
×
3402
            nci.id = ' ';
×
3403
            nci.eff_text[0] = ' ';
×
3404
            this->list_input_handle_key(tc, nci);
×
3405
        } else if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 5, 6)) {
×
3406
            nci.id = '#';
×
3407
            nci.eff_text[0] = '#';
×
3408
            this->list_input_handle_key(tc, nci);
×
3409
        }
3410
    }
3411
    return true;
×
3412
}
3413

3414
void
3415
logfile_sub_source::reload_config(error_reporter& reporter)
645✔
3416
{
3417
    static const auto& cfg
3418
        = injector::get<const logfile_sub_source_ns::config&>();
645✔
3419

3420
    switch (cfg.c_time_column) {
645✔
3421
        case logfile_sub_source_ns::time_column_feature_t::Default:
×
3422
            if (this->lss_line_context == line_context_t::none) {
×
3423
                this->lss_line_context = line_context_t::time_column;
×
3424
            }
3425
            break;
×
3426
        case logfile_sub_source_ns::time_column_feature_t::Disabled:
645✔
3427
            if (this->lss_line_context == line_context_t::time_column) {
645✔
3428
                this->lss_line_context = line_context_t::none;
×
3429
            }
3430
            break;
645✔
3431
        case logfile_sub_source_ns::time_column_feature_t::Enabled:
×
3432
            break;
×
3433
    }
3434
}
645✔
3435

3436
void
3437
logfile_sub_source::add_commands_for_session(
45✔
3438
    const std::function<void(const std::string&)>& receiver)
3439
{
3440
    text_sub_source::add_commands_for_session(receiver);
45✔
3441

3442
    auto mark_expr = this->get_sql_marker_text();
45✔
3443
    if (!mark_expr.empty()) {
45✔
NEW
3444
        receiver(fmt::format(FMT_STRING("mark-expr {}"), mark_expr));
×
3445
    }
3446
    auto filter_expr = this->get_sql_filter_text();
45✔
3447
    if (!filter_expr.empty()) {
45✔
NEW
3448
        receiver(fmt::format(FMT_STRING("filter-expr {}"), filter_expr));
×
3449
    }
3450

3451
    for (const auto& format : log_format::get_root_formats()) {
3,329✔
3452
        auto field_states = format->get_field_states();
3,284✔
3453

3454
        for (const auto& fs_pair : field_states) {
44,603✔
3455
            if (!fs_pair.second.lvm_user_hidden) {
41,319✔
3456
                continue;
41,317✔
3457
            }
3458

3459
            if (fs_pair.second.lvm_user_hidden.value()) {
2✔
3460
                receiver(fmt::format(FMT_STRING("hide-fields {}.{}"),
8✔
3461
                                     format->get_name().to_string(),
4✔
3462
                                     fs_pair.first.to_string()));
4✔
NEW
3463
            } else if (fs_pair.second.lvm_hidden) {
×
NEW
3464
                receiver(fmt::format(FMT_STRING("show-fields {}.{}"),
×
NEW
3465
                                     format->get_name().to_string(),
×
NEW
3466
                                     fs_pair.first.to_string()));
×
3467
            }
3468
        }
3469
    }
3,284✔
3470
}
45✔
3471

3472
void
3473
logfile_sub_source::update_filter_hash_state(hasher& h) const
4✔
3474
{
3475
    text_sub_source::update_filter_hash_state(h);
4✔
3476

3477
    for (const auto& ld : this->lss_files) {
4✔
3478
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
×
3479
            h.update(0);
×
3480
        } else {
3481
            h.update(1);
×
3482
        }
3483
    }
3484
    h.update(this->lss_min_log_level);
4✔
3485
    h.update(this->lss_marked_only);
4✔
3486
}
4✔
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