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

tstack / lnav / 19313192519-2666

12 Nov 2025 10:00PM UTC coverage: 68.649% (-0.07%) from 68.714%
19313192519-2666

push

github

tstack
some more fixes to find_content_map_entry

1 of 27 new or added lines in 1 file covered. (3.7%)

43 existing lines in 4 files now uncovered.

50696 of 73848 relevant lines covered (68.65%)

430723.05 hits per line

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

63.92
/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,161✔
157
    : text_sub_source(1), lnav_config_listener(__FILE__),
158
      lss_meta_grepper(*this), lss_location_history(*this)
1,161✔
159
{
160
    this->tss_supports_filtering = true;
1,161✔
161
    this->clear_line_size_cache();
1,161✔
162
    this->clear_min_max_row_times();
1,161✔
163
}
1,161✔
164

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

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

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

188
    return retval;
25✔
189
}
×
190

191
struct filtered_logline_cmp {
192
    filtered_logline_cmp(const logfile_sub_source& lc) : llss_controller(lc) {}
346✔
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,146✔
211
    {
212
        const auto cl_lhs = llss_controller.lss_index[lhs].value();
1,146✔
213
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
1,146✔
214

215
        if (ll_lhs == nullptr) {
1,146✔
216
            return true;
×
217
        }
218
        return (*ll_lhs) < rhs;
1,146✔
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,823✔
241
                                        int row,
242
                                        std::string& value_out,
243
                                        line_flags_t flags)
244
{
245
    if (this->lss_indexing_in_progress) {
3,823✔
246
        value_out = "";
×
247
        this->lss_token_attrs.clear();
×
248
        return {};
×
249
    }
250

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

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

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

259
    if (flags & RF_RAW) {
3,823✔
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,791✔
282

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

289
    this->lss_token_attrs.clear();
3,791✔
290
    this->lss_token_values.clear();
3,791✔
291
    this->lss_share_manager.invalidate_refs();
3,791✔
292
    if (flags & text_sub_source::RF_FULL) {
3,791✔
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)
7,486✔
304
                  .map([](auto sbr) { return to_string(sbr); })
7,486✔
305
                  .unwrapOr({});
3,743✔
306
        if (this->lss_token_line->has_ansi()) {
3,743✔
307
            scrub_ansi_string(this->lss_token_value, &this->lss_token_attrs);
24✔
308
        }
309
    }
310
    this->lss_token_shift_start = 0;
3,791✔
311
    this->lss_token_shift_size = 0;
3,791✔
312

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

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

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

319
    sbr.share(this->lss_share_manager,
3,791✔
320
              (char*) this->lss_token_value.c_str(),
321
              this->lss_token_value.size());
322
    format->annotate(this->lss_token_file.get(),
7,582✔
323
                     line,
324
                     this->lss_token_attrs,
3,791✔
325
                     this->lss_token_values);
3,791✔
326
    if (flags & RF_REWRITE) {
3,791✔
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,791✔
345
        this->lss_token_attrs.emplace_back(lr, SA_ORIGINAL_LINE.value());
3,791✔
346
    }
347

348
    std::optional<exttm> adjusted_tm;
3,791✔
349
    auto time_attr = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
3,791✔
350
    if (!this->lss_token_line->is_continued() && !format->lf_formatted_lines
5,748✔
351
        && (this->lss_token_file->is_time_adjusted()
1,721✔
352
            || ((format->lf_timestamp_flags & ETF_ZONE_SET
1,706✔
353
                 || format->lf_date_time.dts_default_zone != nullptr)
1,387✔
354
                && format->lf_date_time.dts_zoned_to_local)
403✔
355
            || format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
1,303✔
356
            || !(format->lf_timestamp_flags & ETF_DAY_SET)
1,303✔
357
            || !(format->lf_timestamp_flags & ETF_MONTH_SET))
1,282✔
358
        && format->lf_date_time.dts_fmt_lock != -1)
5,748✔
359
    {
360
        if (time_attr != this->lss_token_attrs.end()) {
383✔
361
            const auto time_range = time_attr->sa_range;
383✔
362
            const auto time_sf = string_fragment::from_str_range(
383✔
363
                this->lss_token_value, time_range.lr_start, time_range.lr_end);
383✔
364
            adjusted_tm = format->tm_for_display(this->lss_token_line, time_sf);
383✔
365

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

370
            if (format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
383✔
371
                || !(format->lf_timestamp_flags & ETF_DAY_SET)
323✔
372
                || !(format->lf_timestamp_flags & ETF_MONTH_SET))
706✔
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(
634✔
387
                    buffer,
388
                    sizeof(buffer),
389
                    format->get_timestamp_formats(),
390
                    adjusted_tm.value());
317✔
391
            }
392

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

400
    // Insert space for the file/search-hit markers.
401
    value_out.insert(0, 1, ' ');
3,791✔
402
    this->lss_time_column_size = 0;
3,791✔
403
    if (this->lss_line_context == line_context_t::time_column) {
3,791✔
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,791✔
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,791✔
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,791✔
486

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

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

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

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

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

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

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

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

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

529
    if (!attrs.empty()) {
3,791✔
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,791✔
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) {
28,420✔
545
        if ((!(this->lss_token_flags & RF_FULL)
53,695✔
546
             && line_value.lv_sub_offset
48,814✔
547
                 != this->lss_token_line->get_sub_offset())
24,407✔
548
            || !line_value.lv_origin.is_valid())
49,036✔
549
        {
550
            continue;
4,437✔
551
        }
552

553
        if (line_value.lv_meta.is_hidden()) {
20,192✔
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
52,733✔
559
            || !line_value.lv_origin.is_valid())
20,192✔
560
        {
561
            continue;
12,349✔
562
        }
563

564
        value_out.emplace_back(line_value.lv_origin,
7,843✔
565
                               VC_ROLE.value(role_t::VCR_IDENTIFIER));
15,686✔
566
    }
567

568
    if (this->lss_token_shift_size) {
3,791✔
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,791✔
575

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

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

598
            if (bv_search.bv_tree.exists(vis_line_t(row))) {
3,743✔
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,791✔
608
                           VC_STYLE.value(vc.attrs_for_ident(
7,582✔
609
                               this->lss_token_file->get_filename())));
3,791✔
610

611
    if (this->lss_line_context < line_context_t::none) {
3,791✔
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,791✔
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,791✔
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,791✔
714
    lr.lr_end = -1;
3,791✔
715
    value_out.emplace_back(lr, L_FILE.value(this->lss_token_file));
3,791✔
716
    value_out.emplace_back(
3,791✔
717
        lr, SA_FORMAT.value(this->lss_token_file->get_format()->get_name()));
7,582✔
718

719
    {
720
        auto line_meta_context = this->get_bookmark_metadata_context(
3,791✔
721
            vis_line_t(row + 1), bookmark_metadata::categories::partition);
722
        if (line_meta_context.bmc_current_metadata) {
3,791✔
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,791✔
732

733
        if (line_meta_opt) {
3,791✔
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,791✔
741
    if (src_file_attr) {
3,791✔
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,791✔
754
        if (this->lss_token_file->is_time_adjusted()) {
3,791✔
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,774✔
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,791✔
772
        && this->lss_token_line->get_timeval() < this->ttt_preview_min_time)
3,791✔
773
    {
774
        auto color = styling::color_unit::from_palette(
×
775
            lnav::enums::to_underlying(ansi_color::red));
×
776
        value_out.emplace_back(line_range{0, 1}, VC_BACKGROUND.value(color));
×
777
    }
778
    if (this->ttt_preview_max_time
3,791✔
779
        && this->ttt_preview_max_time < this->lss_token_line->get_timeval())
3,791✔
780
    {
781
        auto color = styling::color_unit::from_palette(
×
782
            lnav::enums::to_underlying(ansi_color::red));
×
783
        value_out.emplace_back(line_range{0, 1}, VC_BACKGROUND.value(color));
×
784
    }
785
    if (!this->lss_token_line->is_continued()) {
3,791✔
786
        if (this->lss_preview_filter_stmt != nullptr) {
1,957✔
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,957✔
818
        if (sql_filter_opt) {
1,957✔
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,957✔
836
}
837

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

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

847
        return (*ll_lhs) < (*ll_rhs);
103,462✔
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,412✔
891
{
892
    if (this->tss_view == nullptr) {
4,412✔
893
        return rebuild_result::rr_no_change;
124✔
894
    }
895

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

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

910
    if (force) {
4,288✔
911
        log_debug("forced to full rebuild");
477✔
912
        retval = rebuild_result::rr_full_rebuild;
477✔
913
        full_sort = true;
477✔
914
        this->lss_index.clear();
477✔
915
    }
916

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

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

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

936
                             return left_ld->get_file_ptr()->back()
243✔
937
                                 < right_ld->get_file_ptr()->back();
486✔
938
                         });
939
    }
940

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1165
        for (auto& ld : this->lss_files) {
1,677✔
1166
            auto* lf = ld->get_file_ptr();
547✔
1167

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

1180
        if (full_sort) {
1,130✔
1181
            log_trace("rebuild_index full sort");
1,130✔
1182
            for (auto& ld : this->lss_files) {
1,677✔
1183
                auto* lf = ld->get_file_ptr();
547✔
1184

1185
                if (lf == nullptr) {
547✔
1186
                    continue;
1✔
1187
                }
1188

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

1198
                    content_line_t con_line(
1199
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
13,577✔
1200

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

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

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

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

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

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

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

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

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

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

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

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

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

1326
            if (lf == nullptr) {
547✔
1327
                continue;
1✔
1328
            }
1329

1330
            (*iter)->ld_lines_indexed = lf->size();
546✔
1331
        }
1332

1333
        this->lss_filtered_index.reserve(this->lss_index.size());
1,130✔
1334

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

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

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

1351
            if (!(*ld)->is_visible()) {
13,577✔
1352
                continue;
×
1353
            }
1354

1355
            auto* lf = (*ld)->get_file_ptr();
13,577✔
1356
            auto line_iter = lf->begin() + line_number;
13,577✔
1357

1358
            if (line_iter->is_ignored()) {
13,577✔
1359
                continue;
×
1360
            }
1361

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

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

1389
        this->lss_indexing_in_progress = false;
1,130✔
1390

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

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

1417
    return retval;
4,288✔
1418
}
4,288✔
1419

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

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

1430
    bm_warnings.clear();
2,193✔
1431
    bm_errors.clear();
2,193✔
1432
    bm_files.clear();
2,193✔
1433

1434
    std::vector<const bookmark_type_t*> used_marks;
2,193✔
1435
    for (const auto* bmt : {
10,965✔
1436
             &textview_curses::BM_USER,
1437
             &textview_curses::BM_USER_EXPR,
1438
             &textview_curses::BM_PARTITION,
1439
             &textview_curses::BM_META,
1440
         })
13,158✔
1441
    {
1442
        bm[bmt].clear();
8,772✔
1443
        if (!this->lss_user_marks[bmt].empty()) {
8,772✔
1444
            used_marks.emplace_back(bmt);
118✔
1445
        }
1446
    }
1447

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

1453
        for (const auto& bmt : used_marks) {
19,230✔
1454
            auto& user_mark = this->lss_user_marks[bmt];
2,138✔
1455
            if (user_mark.bv_tree.exists(orig_ic.value())) {
2,138✔
1456
                bm[bmt].insert_once(vl);
156✔
1457
            }
1458
        }
1459

1460
        if (lf != last_file) {
17,092✔
1461
            bm_files.insert_once(vl);
962✔
1462
        }
1463

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

1469
            case indexed_content::level_t::error:
3,418✔
1470
                bm_errors.insert_once(vl);
3,418✔
1471
                break;
3,418✔
1472

1473
            default:
13,580✔
1474
                break;
13,580✔
1475
        }
1476

1477
        last_file = lf;
17,092✔
1478
    }
1479
}
2,193✔
1480

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

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

1491
    for (auto& ld : *this) {
284✔
1492
        auto* lf = ld->get_file_ptr();
118✔
1493

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

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

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

1508
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
166✔
1509

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

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

1523
        if (!(*ld)->is_visible()) {
1,328✔
1524
            continue;
213✔
1525
        }
1526

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

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

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

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

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

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

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

1585
                {
1586
                    yajlpp_map root(gen);
×
1587

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

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

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

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

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

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

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

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

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

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

1725
    require_lt(lf->size(), MAX_LINES_PER_FILE);
519✔
1726

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

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

1743
    return true;
519✔
1744
}
1745

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

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

1761
    for (auto& ld : *this) {
782✔
1762
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
17✔
1763
    }
1764

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

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

1782
    return Ok();
765✔
1783
}
765✔
1784

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

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

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

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

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

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

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

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

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

1857
    return Ok();
633✔
1858
}
763✔
1859

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

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

1875
    this->lss_preview_filter_stmt = stmt;
758✔
1876

1877
    return Ok();
758✔
1878
}
1879

1880
Result<bool, lnav::console::user_message>
1881
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
15,102✔
1882
                                    iterator ld,
1883
                                    logfile::const_iterator ll)
1884
{
1885
    if (stmt == nullptr) {
15,102✔
1886
        return Ok(false);
28,974✔
1887
    }
1888

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

1902
    sqlite3_reset(stmt);
615✔
1903
    sqlite3_clear_bindings(stmt);
615✔
1904

1905
    auto count = sqlite3_bind_parameter_count(stmt);
615✔
1906
    for (int lpc = 0; lpc < count; lpc++) {
1,242✔
1907
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
627✔
1908

1909
        if (name[0] == '$') {
627✔
1910
            const char* env_value;
1911

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

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

1987
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
1988

1989
                {
1990
                    yajlpp_array arr(gen);
1✔
1991

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

1997
                string_fragment sf = gen.to_string_fragment();
1✔
1998

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

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

2074
            if (res.isOk()) {
×
2075
                raw_sbr = res.unwrap();
×
2076
                sqlite3_bind_text(stmt,
×
2077
                                  lpc + 1,
2078
                                  raw_sbr.get_data(),
2079
                                  raw_sbr.length(),
×
2080
                                  SQLITE_STATIC);
2081
            }
2082
            continue;
×
2083
        }
2084
        for (const auto& lv : values.lvv_values) {
6,782✔
2085
            if (lv.lv_meta.lvm_name != &name[1]) {
6,777✔
2086
                continue;
6,201✔
2087
            }
2088

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

2114
    auto step_res = sqlite3_step(stmt);
615✔
2115

2116
    sqlite3_reset(stmt);
615✔
2117
    sqlite3_clear_bindings(stmt);
615✔
2118
    switch (step_res) {
615✔
2119
        case SQLITE_OK:
474✔
2120
        case SQLITE_DONE:
2121
            return Ok(false);
948✔
2122
        case SQLITE_ROW:
140✔
2123
            return Ok(true);
280✔
2124
        default:
1✔
2125
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2126
    }
2127
}
615✔
2128

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

2155
    if (ll->get_msg_level() < this->lss_min_log_level) {
14,523✔
2156
        return false;
2✔
2157
    }
2158

2159
    if (*ll < this->ttt_min_row_time) {
14,521✔
2160
        return false;
36✔
2161
    }
2162

2163
    if (!(*ll <= this->ttt_max_row_time)) {
14,485✔
2164
        return false;
4✔
2165
    }
2166

2167
    return true;
14,481✔
2168
}
2169

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

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

2187
    auto cl = this->at(line);
148✔
2188

2189
    if (bm == &textview_curses::BM_USER) {
148✔
2190
        auto* ll = this->find_line(cl);
59✔
2191

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

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

2221
void
2222
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
519✔
2223
{
2224
    auto iter = std::find_if(
519✔
2225
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
1,038✔
2226
    if (iter != this->lss_files.end()) {
519✔
2227
        int file_index = iter - this->lss_files.begin();
519✔
2228

2229
        (*iter)->clear();
519✔
2230
        for (auto& bv : this->lss_user_marks) {
4,671✔
2231
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
4,152✔
2232
            auto mark_end
2233
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
4,152✔
2234
            auto file_range = bv.equal_range(mark_curr, mark_end);
4,152✔
2235

2236
            if (file_range.first != file_range.second) {
4,152✔
2237
                auto to_del = std::vector<content_line_t>{};
69✔
2238
                for (auto file_iter = file_range.first;
69✔
2239
                     file_iter != file_range.second;
178✔
2240
                     ++file_iter)
109✔
2241
                {
2242
                    to_del.emplace_back(*file_iter);
109✔
2243
                }
2244

2245
                for (auto cl : to_del) {
178✔
2246
                    bv.erase(cl);
109✔
2247
                }
2248
            }
69✔
2249
        }
2250

2251
        this->lss_force_rebuild = true;
519✔
2252
    }
2253
    while (!this->lss_files.empty()) {
1,038✔
2254
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
573✔
2255
            this->lss_files.pop_back();
519✔
2256
        } else {
2257
            break;
54✔
2258
        }
2259
    }
2260
    this->lss_token_file = nullptr;
519✔
2261
}
519✔
2262

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

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

2274
        if (!vis_start_opt) {
5✔
2275
            return std::nullopt;
×
2276
        }
2277

2278
        auto vis_start = *vis_start_opt;
5✔
2279

2280
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2281
            content_line_t guess_cl = this->at(vis_start);
5✔
2282

2283
            if (cl == guess_cl) {
5✔
2284
                return vis_start;
5✔
2285
            }
2286

2287
            auto guess_line = this->find_line(guess_cl);
×
2288

2289
            if (!guess_line || ll < *guess_line) {
×
2290
                return std::nullopt;
×
2291
            }
2292

2293
            ++vis_start;
×
2294
        }
2295
    }
2296

2297
    return std::nullopt;
×
2298
}
5✔
2299

2300
void
2301
logfile_sub_source::reload_index_delegate()
635✔
2302
{
2303
    if (this->lss_index_delegate == nullptr) {
635✔
2304
        return;
×
2305
    }
2306

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

2314
        this->lss_index_delegate->index_line(
28✔
2315
            *this, lf.get(), lf->begin() + line_number);
28✔
2316
    }
28✔
2317
    this->lss_index_delegate->index_complete(*this);
635✔
2318
}
2319

2320
std::optional<std::shared_ptr<text_filter>>
2321
logfile_sub_source::get_sql_filter()
2,775✔
2322
{
2323
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
2,775✔
2324
               return filt->get_index() == 0;
359✔
2325
           })
2326
        | lnav::itertools::deref();
5,550✔
2327
}
2328

2329
void
2330
log_location_history::loc_history_append(vis_line_t top)
95✔
2331
{
2332
    if (top < 0_vl || top >= vis_line_t(this->llh_log_source.text_line_count()))
95✔
2333
    {
2334
        return;
1✔
2335
    }
2336

2337
    auto cl = this->llh_log_source.at(top);
94✔
2338

2339
    auto iter = this->llh_history.begin();
94✔
2340
    iter += this->llh_history.size() - this->lh_history_position;
94✔
2341
    this->llh_history.erase_from(iter);
94✔
2342
    this->lh_history_position = 0;
94✔
2343
    this->llh_history.push_back(cl);
94✔
2344
}
2345

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

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

2354
        if (this->lh_history_position == 0 && vis_for_pos != current_top) {
2✔
2355
            return vis_for_pos;
2✔
2356
        }
2357

2358
        if ((this->lh_history_position + 1) >= this->llh_history.size()) {
2✔
2359
            break;
×
2360
        }
2361

2362
        this->lh_history_position += 1;
2✔
2363

2364
        iter += this->lh_history_position;
2✔
2365

2366
        vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2367

2368
        if (vis_for_pos) {
2✔
2369
            return vis_for_pos;
2✔
2370
        }
2371
    }
2372

2373
    return std::nullopt;
×
2374
}
2375

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

2382
        auto iter = this->llh_history.rbegin();
1✔
2383

2384
        iter += this->lh_history_position;
1✔
2385

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

2388
        if (vis_for_pos) {
1✔
2389
            return vis_for_pos;
1✔
2390
        }
2391
    }
2392

2393
    return std::nullopt;
×
2394
}
2395

2396
bool
2397
sql_filter::matches(std::optional<line_source> ls_opt,
124✔
2398
                    const shared_buffer_ref& line)
2399
{
2400
    if (!ls_opt) {
124✔
2401
        return false;
×
2402
    }
2403

2404
    auto ls = ls_opt;
124✔
2405

2406
    if (!ls->ls_line->is_message()) {
124✔
2407
        return false;
3✔
2408
    }
2409
    if (this->sf_filter_stmt == nullptr) {
121✔
2410
        return false;
×
2411
    }
2412

2413
    auto lfp = ls->ls_file.shared_from_this();
121✔
2414
    auto ld = this->sf_log_source.find_data_i(lfp);
121✔
2415
    if (ld == this->sf_log_source.end()) {
121✔
2416
        return false;
×
2417
    }
2418

2419
    auto eval_res = this->sf_log_source.eval_sql_filter(
121✔
2420
        this->sf_filter_stmt, ld, ls->ls_line);
121✔
2421
    if (eval_res.unwrapOr(true)) {
121✔
2422
        return false;
74✔
2423
    }
2424

2425
    return true;
47✔
2426
}
121✔
2427

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

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

2444
        {
2445
            md2attr_line mdal;
×
2446

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

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

2465
            md2attr_line mdal;
×
2466

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

2479
    if (!this->lmg_done) {
×
2480
        return line_info{};
×
2481
    }
2482

2483
    return std::nullopt;
×
2484
}
2485

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

2493
    if (bv.empty()) {
×
2494
        return -1_vl;
×
2495
    }
2496
    return *bv.bv_tree.begin();
×
2497
}
2498

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

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

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

2519
    this->lmg_source.tss_view->grep_begin(gp, start, stop);
23✔
2520
}
23✔
2521

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

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

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

2553
    return retval;
11✔
2554
}
24✔
2555

2556
static attr_line_t
2557
to_display(const std::shared_ptr<logfile>& lf)
29✔
2558
{
2559
    attr_line_t retval;
29✔
2560

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

2568
    return retval;
29✔
2569
}
×
2570

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

2577
    if (this->lss_filtered_index.empty()) {
27✔
2578
        return;
9✔
2579
    }
2580

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

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

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

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

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

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

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

2642
    auto format_name = format->get_name().to_string();
18✔
2643

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

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

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

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

2715
    shared_buffer sb;
18✔
2716
    logline_value_vector values;
18✔
2717
    auto& sbr = values.lvv_sbr;
18✔
2718

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

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

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

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

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

2763
                std::vector<breadcrumb::possibility> retval;
11✔
2764

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

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

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

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

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

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

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

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

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

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

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

2923
        if (lf == nullptr) {
42✔
2924
            continue;
×
2925
        }
2926

2927
        lf->quiesce();
42✔
2928
    }
2929
}
42✔
2930

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

2938
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
2939
}
25✔
2940

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

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

2960
        if (bm_opt.value()->has(desired)) {
2✔
2961
            next_line = *next_vl_iter;
2✔
2962
            break;
2✔
2963
        }
2964
    }
2965
    if (vl_iter == bv.bv_tree.begin()) {
3,886✔
2966
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3,870✔
2967
    }
2968

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

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

2988
std::optional<bookmark_metadata*>
2989
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
22,543✔
2990
{
2991
    auto line_pair = this->find_line_with_file(cl).value();
22,543✔
2992
    auto line_number = static_cast<uint32_t>(
2993
        std::distance(line_pair.first->begin(), line_pair.second));
22,543✔
2994

2995
    auto& bm = line_pair.first->get_bookmark_metadata();
22,543✔
2996
    auto bm_iter = bm.find(line_number);
22,543✔
2997
    if (bm_iter == bm.end()) {
22,543✔
2998
        return std::nullopt;
22,165✔
2999
    }
3000

3001
    return &bm_iter->second;
378✔
3002
}
22,543✔
3003

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

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

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

3026
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3027
    }
3028
}
6✔
3029

3030
void
3031
logfile_sub_source::increase_line_context()
×
3032
{
3033
    auto old_context = this->lss_line_context;
×
3034

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

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

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

3081
        return true;
×
3082
    }
3083

3084
    return false;
×
3085
}
3086

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

3100
size_t
3101
logfile_sub_source::file_count() const
3,992✔
3102
{
3103
    size_t retval = 0;
3,992✔
3104
    const_iterator iter;
3,992✔
3105

3106
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,582✔
3107
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,590✔
3108
            retval += 1;
3,584✔
3109
        }
3110
    }
3111

3112
    return retval;
3,992✔
3113
}
3114

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

3122
    if (this->lss_line_size_cache[index].first != row) {
×
3123
        std::string value;
×
3124

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

3149
int
3150
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
1✔
3151
{
3152
    int retval = 0;
1✔
3153

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

3159
    return retval;
1✔
3160
}
3161

3162
std::optional<vis_line_t>
3163
logfile_sub_source::row_for(const row_info& ri)
263✔
3164
{
3165
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
526✔
3166
                               this->lss_filtered_index.end(),
3167
                               ri.ri_time,
263✔
3168
                               filtered_logline_cmp(*this));
3169
    if (lb != this->lss_filtered_index.end()) {
263✔
3170
        auto first_lb = lb;
258✔
3171
        while (true) {
3172
            auto cl = this->lss_index[*lb].value();
272✔
3173
            if (content_line_t(ri.ri_id) == cl) {
272✔
3174
                first_lb = lb;
226✔
3175
                break;
226✔
3176
            }
3177
            auto ll = this->find_line(cl);
46✔
3178
            if (ll->get_timeval() != ri.ri_time) {
46✔
3179
                break;
32✔
3180
            }
3181
            auto next_lb = std::next(lb);
14✔
3182
            if (next_lb == this->lss_filtered_index.end()) {
14✔
3183
                break;
×
3184
            }
3185
            lb = next_lb;
14✔
3186
        }
14✔
3187

3188
        const auto dst
3189
            = std::distance(this->lss_filtered_index.begin(), first_lb);
258✔
3190
        return vis_line_t(dst);
258✔
3191
    }
3192

3193
    return std::nullopt;
5✔
3194
}
3195

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

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

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

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

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

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

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

3241
                            log_error("unable to get line hash: %s",
×
3242
                                      errmsg.c_str());
3243
                            continue;
×
3244
                        }
3245

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

3255
        return std::nullopt;
×
3256
    }
3257

3258
    auto& vb = this->tss_view->get_bookmarks();
×
3259
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3260

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

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

3273
    return std::nullopt;
×
3274
}
3275

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

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

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

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

3352
            return retval;
72✔
3353
        }
288✔
3354

3355
        return std::nullopt;
×
3356
    }
72✔
3357

3358
    return text_anchors::to_anchor_string(
3✔
3359
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3360
}
3361

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

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

3375
        const auto& name = meta_opt.value()->bm_name;
×
3376
        retval.emplace(text_anchors::to_anchor_string(name));
×
3377
    }
3378

3379
    return retval;
×
3380
}
×
3381

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

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

3417
void
3418
logfile_sub_source::reload_config(error_reporter& reporter)
656✔
3419
{
3420
    static const auto& cfg
3421
        = injector::get<const logfile_sub_source_ns::config&>();
656✔
3422

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

3439
void
3440
logfile_sub_source::add_commands_for_session(
47✔
3441
    const std::function<void(const std::string&)>& receiver)
3442
{
3443
    text_sub_source::add_commands_for_session(receiver);
47✔
3444

3445
    auto mark_expr = this->get_sql_marker_text();
47✔
3446
    if (!mark_expr.empty()) {
47✔
3447
        receiver(fmt::format(FMT_STRING("mark-expr {}"), mark_expr));
4✔
3448
    }
3449
    auto filter_expr = this->get_sql_filter_text();
47✔
3450
    if (!filter_expr.empty()) {
47✔
3451
        receiver(fmt::format(FMT_STRING("filter-expr {}"), filter_expr));
×
3452
    }
3453

3454
    for (const auto& format : log_format::get_root_formats()) {
3,428✔
3455
        auto field_states = format->get_field_states();
3,381✔
3456

3457
        for (const auto& fs_pair : field_states) {
46,025✔
3458
            if (!fs_pair.second.lvm_user_hidden) {
42,644✔
3459
                continue;
42,642✔
3460
            }
3461

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

3475
void
3476
logfile_sub_source::update_filter_hash_state(hasher& h) const
5✔
3477
{
3478
    text_sub_source::update_filter_hash_state(h);
5✔
3479

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