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

tstack / lnav / 20245728190-2749

15 Dec 2025 07:59PM UTC coverage: 68.864% (-0.07%) from 68.929%
20245728190-2749

push

github

tstack
[text_format] add plaintext type

Related to #1296

85 of 132 new or added lines in 24 files covered. (64.39%)

73 existing lines in 10 files now uncovered.

51605 of 74938 relevant lines covered (68.86%)

434003.35 hits per line

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

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

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

39
#include "logfile_sub_source.hh"
40

41
#include <sqlite3.h>
42

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

70
using namespace std::chrono_literals;
71
using namespace lnav::roles::literals;
72

73
const DIST_SLICE(bm_types) bookmark_type_t logfile_sub_source::BM_FILES("file");
74

75
static int
76
pretty_sql_callback(exec_context& ec, sqlite3_stmt* stmt)
73✔
77
{
78
    if (!sqlite3_stmt_busy(stmt)) {
73✔
79
        return 0;
32✔
80
    }
81

82
    const auto ncols = sqlite3_column_count(stmt);
41✔
83

84
    for (int lpc = 0; lpc < ncols; lpc++) {
82✔
85
        if (!ec.ec_accumulator->empty()) {
41✔
86
            ec.ec_accumulator->append(", ");
10✔
87
        }
88

89
        const char* res = (const char*) sqlite3_column_text(stmt, lpc);
41✔
90
        if (res == nullptr) {
41✔
91
            continue;
×
92
        }
93

94
        ec.ec_accumulator->append(res);
41✔
95
    }
96

97
    for (int lpc = 0; lpc < ncols; lpc++) {
82✔
98
        const auto* colname = sqlite3_column_name(stmt, lpc);
41✔
99
        auto* raw_value = sqlite3_column_value(stmt, lpc);
41✔
100
        auto value_type = sqlite3_value_type(raw_value);
41✔
101
        scoped_value_t value;
41✔
102

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

125
            if (vars.find(colname) != vars.end()) {
42✔
126
                continue;
10✔
127
            }
128

129
            if (value.is<string_fragment>()) {
4✔
130
                value = value.get<string_fragment>().to_string();
4✔
131
            }
132
            vars[colname] = value;
8✔
133
        }
134
    }
41✔
135
    return 0;
41✔
136
}
137

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

146
        while ((rc = read(fd, buffer, sizeof(buffer))) > 0) {
×
147
            ss.write(buffer, rc);
×
148
        }
149

150
        auto retval = ss.str();
×
151

152
        if (endswith(retval, "\n")) {
×
153
            retval.resize(retval.length() - 1);
×
154
        }
155

156
        return retval;
×
157
    });
×
158

159
    return retval;
×
160
}
161

162
logfile_sub_source::logfile_sub_source()
1,168✔
163
    : text_sub_source(1), lnav_config_listener(__FILE__),
164
      lss_meta_grepper(*this), lss_location_history(*this)
1,168✔
165
{
166
    this->tss_supports_filtering = true;
1,168✔
167
    this->clear_line_size_cache();
1,168✔
168
    this->clear_min_max_row_times();
1,168✔
169
}
1,168✔
170

171
std::shared_ptr<logfile>
172
logfile_sub_source::find(const char* fn, content_line_t& line_base)
25✔
173
{
174
    std::shared_ptr<logfile> retval = nullptr;
25✔
175

176
    line_base = content_line_t(0);
25✔
177
    for (auto iter = this->lss_files.begin();
25✔
178
         iter != this->lss_files.end() && retval == nullptr;
51✔
179
         ++iter)
26✔
180
    {
181
        auto& ld = *(*iter);
26✔
182
        auto* lf = ld.get_file_ptr();
26✔
183

184
        if (lf == nullptr) {
26✔
185
            continue;
×
186
        }
187
        if (strcmp(lf->get_filename_as_string().c_str(), fn) == 0) {
26✔
188
            retval = ld.get_file();
25✔
189
        } else {
190
            line_base += content_line_t(MAX_LINES_PER_FILE);
1✔
191
        }
192
    }
193

194
    return retval;
25✔
195
}
×
196

197
struct filtered_logline_cmp {
198
    filtered_logline_cmp(const logfile_sub_source& lc) : llss_controller(lc) {}
349✔
199

200
    bool operator()(const uint32_t& lhs, const uint32_t& rhs) const
201
    {
202
        auto cl_lhs = llss_controller.lss_index[lhs].value();
203
        auto cl_rhs = llss_controller.lss_index[rhs].value();
204
        auto ll_lhs = this->llss_controller.find_line(cl_lhs);
205
        auto ll_rhs = this->llss_controller.find_line(cl_rhs);
206

207
        if (ll_lhs == nullptr) {
208
            return true;
209
        }
210
        if (ll_rhs == nullptr) {
211
            return false;
212
        }
213
        return (*ll_lhs) < (*ll_rhs);
214
    }
215

216
    bool operator()(const uint32_t& lhs, const timeval& rhs) const
1,152✔
217
    {
218
        const auto cl_lhs = llss_controller.lss_index[lhs].value();
1,152✔
219
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
1,152✔
220

221
        if (ll_lhs == nullptr) {
1,152✔
222
            return true;
×
223
        }
224
        return (*ll_lhs) < rhs;
1,152✔
225
    }
226

227
    const logfile_sub_source& llss_controller;
228
};
229

230
std::optional<vis_line_t>
231
logfile_sub_source::find_from_time(const timeval& start) const
85✔
232
{
233
    const auto lb = std::lower_bound(this->lss_filtered_index.begin(),
85✔
234
                                     this->lss_filtered_index.end(),
235
                                     start,
236
                                     filtered_logline_cmp(*this));
237
    if (lb != this->lss_filtered_index.end()) {
85✔
238
        auto retval = std::distance(this->lss_filtered_index.begin(), lb);
65✔
239
        return vis_line_t(retval);
65✔
240
    }
241

242
    return std::nullopt;
20✔
243
}
244

245
line_info
246
logfile_sub_source::text_value_for_line(textview_curses& tc,
4,421✔
247
                                        int row,
248
                                        std::string& value_out,
249
                                        line_flags_t flags)
250
{
251
    if (this->lss_indexing_in_progress) {
4,421✔
252
        value_out = "";
×
253
        this->lss_token_attrs.clear();
×
254
        return {};
×
255
    }
256

257
    line_info retval;
4,421✔
258
    content_line_t line(0);
4,421✔
259

260
    require_ge(row, 0);
4,421✔
261
    require_lt((size_t) row, this->lss_filtered_index.size());
4,421✔
262

263
    line = this->at(vis_line_t(row));
4,421✔
264

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

287
    require_false(this->lss_in_value_for_line);
4,389✔
288

289
    this->lss_in_value_for_line = true;
4,389✔
290
    this->lss_token_flags = flags;
4,389✔
291
    this->lss_token_file_data = this->find_data(line);
4,389✔
292
    this->lss_token_file = (*this->lss_token_file_data)->get_file();
4,389✔
293
    this->lss_token_line = this->lss_token_file->begin() + line;
4,389✔
294

295
    this->lss_token_attrs.clear();
4,389✔
296
    this->lss_token_values.clear();
4,389✔
297
    this->lss_share_manager.invalidate_refs();
4,389✔
298
    if (flags & text_sub_source::RF_FULL) {
4,389✔
299
        shared_buffer_ref sbr;
48✔
300

301
        this->lss_token_file->read_full_message(this->lss_token_line, sbr);
48✔
302
        this->lss_token_value = to_string(sbr);
48✔
303
        if (sbr.get_metadata().m_has_ansi) {
48✔
304
            scrub_ansi_string(this->lss_token_value, &this->lss_token_attrs);
17✔
305
            sbr.get_metadata().m_has_ansi = false;
17✔
306
        }
307
    } else {
48✔
308
        this->lss_token_value
309
            = this->lss_token_file->read_line(this->lss_token_line)
8,682✔
310
                  .map([](auto sbr) { return to_string(sbr); })
8,682✔
311
                  .unwrapOr({});
4,341✔
312
        if (this->lss_token_line->has_ansi()) {
4,341✔
313
            scrub_ansi_string(this->lss_token_value, &this->lss_token_attrs);
24✔
314
        }
315
    }
316
    this->lss_token_shift_start = 0;
4,389✔
317
    this->lss_token_shift_size = 0;
4,389✔
318

319
    auto format = this->lss_token_file->get_format();
4,389✔
320

321
    value_out = this->lss_token_value;
4,389✔
322

323
    auto& sbr = this->lss_token_values.lvv_sbr;
4,389✔
324

325
    sbr.share(this->lss_share_manager,
4,389✔
326
              (char*) this->lss_token_value.c_str(),
327
              this->lss_token_value.size());
328
    format->annotate(this->lss_token_file.get(),
8,778✔
329
                     line,
330
                     this->lss_token_attrs,
4,389✔
331
                     this->lss_token_values);
4,389✔
332
    if (flags & RF_REWRITE) {
4,389✔
333
        exec_context ec(
334
            &this->lss_token_values, pretty_sql_callback, pretty_pipe_callback);
48✔
335
        std::string rewritten_line;
48✔
336
        db_label_source rewrite_label_source;
48✔
337

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

349
    {
350
        auto lr = line_range{0, (int) this->lss_token_value.length()};
4,389✔
351
        this->lss_token_attrs.emplace_back(lr, SA_ORIGINAL_LINE.value());
4,389✔
352
    }
353

354
    std::optional<exttm> adjusted_tm;
4,389✔
355
    auto time_attr = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
4,389✔
356
    if (!this->lss_token_line->is_continued() && !format->lf_formatted_lines
6,926✔
357
        && (this->lss_token_file->is_time_adjusted()
2,297✔
358
            || ((format->lf_timestamp_flags & ETF_ZONE_SET
2,282✔
359
                 || format->lf_date_time.dts_default_zone != nullptr)
1,389✔
360
                && format->lf_date_time.dts_zoned_to_local)
977✔
361
            || format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
1,305✔
362
            || !(format->lf_timestamp_flags & ETF_DAY_SET)
1,305✔
363
            || !(format->lf_timestamp_flags & ETF_MONTH_SET))
1,284✔
364
        && format->lf_date_time.dts_fmt_lock != -1)
6,926✔
365
    {
366
        if (time_attr != this->lss_token_attrs.end()) {
957✔
367
            const auto time_range = time_attr->sa_range;
957✔
368
            const auto time_sf = string_fragment::from_str_range(
957✔
369
                this->lss_token_value, time_range.lr_start, time_range.lr_end);
957✔
370
            adjusted_tm = format->tm_for_display(this->lss_token_line, time_sf);
957✔
371

372
            char buffer[128];
373
            const char* fmt;
374
            ssize_t len;
375

376
            if (format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
957✔
377
                || !(format->lf_timestamp_flags & ETF_DAY_SET)
323✔
378
                || !(format->lf_timestamp_flags & ETF_MONTH_SET))
1,280✔
379
            {
380
                if (format->lf_timestamp_flags & ETF_NANOS_SET) {
640✔
381
                    fmt = "%Y-%m-%d %H:%M:%S.%N";
×
382
                } else if (format->lf_timestamp_flags & ETF_MICROS_SET) {
640✔
383
                    fmt = "%Y-%m-%d %H:%M:%S.%f";
635✔
384
                } else if (format->lf_timestamp_flags & ETF_MILLIS_SET) {
5✔
385
                    fmt = "%Y-%m-%d %H:%M:%S.%L";
2✔
386
                } else {
387
                    fmt = "%Y-%m-%d %H:%M:%S";
3✔
388
                }
389
                len = ftime_fmt(
640✔
390
                    buffer, sizeof(buffer), fmt, adjusted_tm.value());
640✔
391
            } else {
392
                len = format->lf_date_time.ftime(
634✔
393
                    buffer,
394
                    sizeof(buffer),
395
                    format->get_timestamp_formats(),
396
                    adjusted_tm.value());
317✔
397
            }
398

399
            value_out.replace(
1,914✔
400
                time_range.lr_start, time_range.length(), buffer, len);
957✔
401
            this->lss_token_shift_start = time_range.lr_start;
957✔
402
            this->lss_token_shift_size = len - time_range.length();
957✔
403
        }
404
    }
405

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

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

487
    if (this->tas_display_time_offset) {
4,389✔
488
        auto row_vl = vis_line_t(row);
210✔
489
        auto relstr = this->get_time_offset_for_line(tc, row_vl);
210✔
490
        value_out = fmt::format(FMT_STRING("{: >12}|{}"), relstr, value_out);
840✔
491
    }
210✔
492

493
    this->lss_in_value_for_line = false;
4,389✔
494

495
    return retval;
4,389✔
496
}
4,389✔
497

498
void
499
logfile_sub_source::text_attrs_for_line(textview_curses& lv,
4,389✔
500
                                        int row,
501
                                        string_attrs_t& value_out)
502
{
503
    if (this->lss_indexing_in_progress) {
4,389✔
504
        return;
×
505
    }
506

507
    auto& vc = view_colors::singleton();
4,389✔
508
    logline* next_line = nullptr;
4,389✔
509
    line_range lr;
4,389✔
510
    int time_offset_end = 0;
4,389✔
511
    text_attrs attrs;
4,389✔
512

513
    value_out = this->lss_token_attrs;
4,389✔
514

515
    if ((row + 1) < (int) this->lss_filtered_index.size()) {
4,389✔
516
        next_line = this->find_line(this->at(vis_line_t(row + 1)));
4,198✔
517
    }
518

519
    if (next_line != nullptr
4,389✔
520
        && (day_num(next_line->get_time<std::chrono::seconds>().count())
8,587✔
521
            > day_num(this->lss_token_line->get_time<std::chrono::seconds>()
8,587✔
522
                          .count())))
523
    {
524
        attrs |= text_attrs::style::underline;
15✔
525
    }
526

527
    const auto& line_values = this->lss_token_values;
4,389✔
528

529
    lr.lr_start = 0;
4,389✔
530
    lr.lr_end = -1;
4,389✔
531
    value_out.emplace_back(
4,389✔
532
        lr, SA_LEVEL.value(this->lss_token_line->get_msg_level()));
8,778✔
533

534
    lr.lr_start = time_offset_end;
4,389✔
535
    lr.lr_end = -1;
4,389✔
536

537
    if (!attrs.empty()) {
4,389✔
538
        value_out.emplace_back(lr, VC_STYLE.value(attrs));
15✔
539
    }
540

541
    if (this->lss_token_line->get_msg_level() == log_level_t::LEVEL_INVALID) {
4,389✔
542
        for (auto& token_attr : this->lss_token_attrs) {
37✔
543
            if (token_attr.sa_type != &SA_INVALID) {
20✔
544
                continue;
17✔
545
            }
546

547
            value_out.emplace_back(token_attr.sa_range,
3✔
548
                                   VC_ROLE.value(role_t::VCR_INVALID_MSG));
6✔
549
        }
550
    }
551

552
    for (const auto& line_value : line_values.lvv_values) {
45,675✔
553
        if ((!(this->lss_token_flags & RF_FULL)
92,401✔
554
             && line_value.lv_sub_offset
82,128✔
555
                 != this->lss_token_line->get_sub_offset())
41,064✔
556
            || !line_value.lv_origin.is_valid())
82,350✔
557
        {
558
            continue;
9,829✔
559
        }
560

561
        if (line_value.lv_meta.is_hidden()) {
31,457✔
562
            value_out.emplace_back(line_value.lv_origin,
146✔
563
                                   SA_HIDDEN.value(ui_icon_t::hidden));
292✔
564
        }
565

566
        if (!line_value.lv_meta.lvm_identifier
80,899✔
567
            || !line_value.lv_origin.is_valid())
31,457✔
568
        {
569
            continue;
17,985✔
570
        }
571

572
        value_out.emplace_back(line_value.lv_origin,
13,472✔
573
                               VC_ROLE.value(role_t::VCR_IDENTIFIER));
26,944✔
574
    }
575

576
    if (this->lss_token_shift_size) {
4,389✔
577
        shift_string_attrs(value_out,
668✔
578
                           this->lss_token_shift_start + 1,
668✔
579
                           this->lss_token_shift_size);
580
    }
581

582
    shift_string_attrs(value_out, 0, 1);
4,389✔
583

584
    lr.lr_start = 0;
4,389✔
585
    lr.lr_end = 1;
4,389✔
586
    {
587
        auto& bm = lv.get_bookmarks();
4,389✔
588
        const auto& bv = bm[&BM_FILES];
4,389✔
589
        bool is_first_for_file = bv.bv_tree.exists(vis_line_t(row));
4,389✔
590
        bool is_last_for_file = bv.bv_tree.exists(vis_line_t(row + 1));
4,389✔
591
        auto graph = NCACS_VLINE;
4,389✔
592
        if (is_first_for_file) {
4,389✔
593
            if (is_last_for_file) {
215✔
594
                graph = NCACS_HLINE;
8✔
595
            } else {
596
                graph = NCACS_ULCORNER;
207✔
597
            }
598
        } else if (is_last_for_file) {
4,174✔
599
            graph = NCACS_LLCORNER;
21✔
600
        }
601
        value_out.emplace_back(lr, VC_GRAPHIC.value(graph));
4,389✔
602

603
        if (!(this->lss_token_flags & RF_FULL)) {
4,389✔
604
            const auto& bv_search = bm[&textview_curses::BM_SEARCH];
4,341✔
605

606
            if (bv_search.bv_tree.exists(vis_line_t(row))) {
4,341✔
607
                lr.lr_start = 0;
9✔
608
                lr.lr_end = 1;
9✔
609
                value_out.emplace_back(
9✔
610
                    lr, VC_STYLE.value(text_attrs::with_reverse()));
18✔
611
            }
612
        }
613
    }
614

615
    value_out.emplace_back(lr,
4,389✔
616
                           VC_STYLE.value(vc.attrs_for_ident(
8,778✔
617
                               this->lss_token_file->get_filename())));
4,389✔
618

619
    if (this->lss_line_context < line_context_t::none) {
4,389✔
620
        size_t file_offset_end
×
621
            = (this->lss_line_context == line_context_t::filename)
×
622
            ? this->lss_filename_width
×
623
            : this->lss_basename_width;
624

625
        shift_string_attrs(value_out, 0, file_offset_end);
×
626

627
        lr.lr_start = 0;
×
628
        lr.lr_end = file_offset_end + 1;
×
629
        value_out.emplace_back(lr,
×
630
                               VC_STYLE.value(vc.attrs_for_ident(
×
631
                                   this->lss_token_file->get_filename())));
×
632
    } else if (this->lss_time_column_size > 0) {
4,389✔
633
        shift_string_attrs(value_out, 1, this->lss_time_column_size);
×
634

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

693
    if (this->tas_display_time_offset) {
4,389✔
694
        time_offset_end = 13;
210✔
695
        lr.lr_start = 0;
210✔
696
        lr.lr_end = time_offset_end;
210✔
697

698
        shift_string_attrs(value_out, 0, time_offset_end);
210✔
699

700
        value_out.emplace_back(lr, VC_ROLE.value(role_t::VCR_OFFSET_TIME));
210✔
701
        value_out.emplace_back(line_range(12, 13),
210✔
702
                               VC_GRAPHIC.value(NCACS_VLINE));
420✔
703

704
        auto bar_role = role_t::VCR_NONE;
210✔
705

706
        switch (this->get_line_accel_direction(vis_line_t(row))) {
210✔
707
            case log_accel::direction_t::A_STEADY:
126✔
708
                break;
126✔
709
            case log_accel::direction_t::A_DECEL:
42✔
710
                bar_role = role_t::VCR_DIFF_DELETE;
42✔
711
                break;
42✔
712
            case log_accel::direction_t::A_ACCEL:
42✔
713
                bar_role = role_t::VCR_DIFF_ADD;
42✔
714
                break;
42✔
715
        }
716
        if (bar_role != role_t::VCR_NONE) {
210✔
717
            value_out.emplace_back(line_range(12, 13), VC_ROLE.value(bar_role));
84✔
718
        }
719
    }
720

721
    lr.lr_start = 0;
4,389✔
722
    lr.lr_end = -1;
4,389✔
723
    value_out.emplace_back(lr, L_FILE.value(this->lss_token_file));
4,389✔
724
    value_out.emplace_back(
4,389✔
725
        lr, SA_FORMAT.value(this->lss_token_file->get_format()->get_name()));
8,778✔
726

727
    {
728
        auto line_meta_context = this->get_bookmark_metadata_context(
4,389✔
729
            vis_line_t(row + 1), bookmark_metadata::categories::partition);
730
        if (line_meta_context.bmc_current_metadata) {
4,389✔
731
            lr.lr_start = 0;
8✔
732
            lr.lr_end = -1;
8✔
733
            value_out.emplace_back(
8✔
734
                lr,
735
                L_PARTITION.value(
16✔
736
                    line_meta_context.bmc_current_metadata.value()));
737
        }
738

739
        auto line_meta_opt = this->find_bookmark_metadata(vis_line_t(row));
4,389✔
740

741
        if (line_meta_opt) {
4,389✔
742
            lr.lr_start = 0;
25✔
743
            lr.lr_end = -1;
25✔
744
            value_out.emplace_back(lr, L_META.value(line_meta_opt.value()));
25✔
745
        }
746
    }
747

748
    auto src_file_attr = get_string_attr(value_out, SA_SRC_FILE);
4,389✔
749
    if (src_file_attr) {
4,389✔
750
        auto lr = src_file_attr->saw_string_attr->sa_range;
23✔
751
        lr.lr_end = lr.lr_start + 1;
23✔
752
        value_out.emplace_back(lr,
23✔
753
                               VC_STYLE.value(text_attrs::with_underline()));
46✔
754
        value_out.emplace_back(lr,
23✔
755
                               VC_COMMAND.value(ui_command{
92✔
756
                                   source_location{},
757
                                   ":toggle-breakpoint",
758
                               }));
759
    }
760

761
    if (this->lss_time_column_size == 0) {
4,389✔
762
        if (this->lss_token_file->is_time_adjusted()) {
4,389✔
763
            auto time_range = find_string_attr_range(value_out, &L_TIMESTAMP);
17✔
764

765
            if (time_range.lr_end != -1) {
17✔
766
                value_out.emplace_back(
15✔
767
                    time_range, VC_ROLE.value(role_t::VCR_ADJUSTED_TIME));
30✔
768
            }
769
        } else if (this->lss_token_line->is_time_skewed()) {
4,372✔
770
            auto time_range = find_string_attr_range(value_out, &L_TIMESTAMP);
8✔
771

772
            if (time_range.lr_end != -1) {
8✔
773
                value_out.emplace_back(time_range,
8✔
774
                                       VC_ROLE.value(role_t::VCR_SKEWED_TIME));
16✔
775
            }
776
        }
777
    }
778

779
    if (this->tss_preview_min_log_level
4,389✔
780
        && this->lss_token_line->get_msg_level()
4,389✔
781
            < this->tss_preview_min_log_level)
4,389✔
782
    {
783
        auto color = styling::color_unit::from_palette(
×
784
            lnav::enums::to_underlying(ansi_color::red));
×
785
        value_out.emplace_back(line_range{0, 1}, VC_BACKGROUND.value(color));
×
786
    }
787
    if (this->ttt_preview_min_time
4,389✔
788
        && this->lss_token_line->get_timeval() < this->ttt_preview_min_time)
4,389✔
789
    {
790
        auto color = styling::color_unit::from_palette(
×
791
            lnav::enums::to_underlying(ansi_color::red));
×
792
        value_out.emplace_back(line_range{0, 1}, VC_BACKGROUND.value(color));
×
793
    }
794
    if (this->ttt_preview_max_time
4,389✔
795
        && this->ttt_preview_max_time < this->lss_token_line->get_timeval())
4,389✔
796
    {
797
        auto color = styling::color_unit::from_palette(
×
798
            lnav::enums::to_underlying(ansi_color::red));
×
799
        value_out.emplace_back(line_range{0, 1}, VC_BACKGROUND.value(color));
×
800
    }
801
    if (!this->lss_token_line->is_continued()) {
4,389✔
802
        if (this->lss_preview_filter_stmt != nullptr) {
2,537✔
803
            auto color = styling::color_unit::EMPTY;
×
804
            auto eval_res
805
                = this->eval_sql_filter(this->lss_preview_filter_stmt.in(),
806
                                        this->lss_token_file_data,
807
                                        this->lss_token_line);
×
808
            if (eval_res.isErr()) {
×
809
                color = palette_color{
×
810
                    lnav::enums::to_underlying(ansi_color::yellow)};
×
811
                value_out.emplace_back(
×
812
                    line_range{0, -1},
×
813
                    SA_ERROR.value(
×
814
                        eval_res.unwrapErr().to_attr_line().get_string()));
×
815
            } else {
816
                auto matched = eval_res.unwrap();
×
817

818
                if (matched) {
×
819
                    color = palette_color{
×
820
                        lnav::enums::to_underlying(ansi_color::green)};
×
821
                } else {
822
                    color = palette_color{
×
823
                        lnav::enums::to_underlying(ansi_color::red)};
×
824
                    value_out.emplace_back(
×
825
                        line_range{0, 1},
×
826
                        VC_STYLE.value(text_attrs::with_blink()));
×
827
                }
828
            }
829
            value_out.emplace_back(line_range{0, 1},
×
830
                                   VC_BACKGROUND.value(color));
×
831
        }
832

833
        auto sql_filter_opt = this->get_sql_filter();
2,537✔
834
        if (sql_filter_opt) {
2,537✔
835
            auto* sf = (sql_filter*) sql_filter_opt.value().get();
36✔
836
            auto eval_res = this->eval_sql_filter(sf->sf_filter_stmt.in(),
837
                                                  this->lss_token_file_data,
838
                                                  this->lss_token_line);
36✔
839
            if (eval_res.isErr()) {
36✔
840
                auto msg = fmt::format(
841
                    FMT_STRING(
×
842
                        "filter expression evaluation failed with -- {}"),
843
                    eval_res.unwrapErr().to_attr_line().get_string());
×
844
                auto cu = styling::color_unit::from_palette(palette_color{
×
845
                    lnav::enums::to_underlying(ansi_color::yellow)});
846
                value_out.emplace_back(line_range{0, -1}, SA_ERROR.value(msg));
×
847
                value_out.emplace_back(line_range{0, 1},
×
848
                                       VC_BACKGROUND.value(cu));
×
849
            }
850
        }
36✔
851
    }
2,537✔
852
}
853

854
struct logline_cmp {
855
    logline_cmp(logfile_sub_source& lc) : llss_controller(lc) {}
1,141✔
856

857
    bool operator()(const logfile_sub_source::indexed_content& lhs,
105,153✔
858
                    const logfile_sub_source::indexed_content& rhs) const
859
    {
860
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
105,153✔
861
        const auto* ll_rhs = this->llss_controller.find_line(rhs.value());
105,153✔
862

863
        return (*ll_lhs) < (*ll_rhs);
105,153✔
864
    }
865

866
    bool operator()(const uint32_t& lhs, const uint32_t& rhs) const
867
    {
868
        auto cl_lhs = llss_controller.lss_index[lhs].value();
869
        auto cl_rhs = llss_controller.lss_index[rhs].value();
870
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
871
        const auto* ll_rhs = this->llss_controller.find_line(cl_rhs);
872

873
        return (*ll_lhs) < (*ll_rhs);
874
    }
875
#if 0
876
        bool operator()(const indexed_content &lhs, const indexed_content &rhs)
877
        {
878
            logline *ll_lhs = this->llss_controller.find_line(lhs.ic_value);
879
            logline *ll_rhs = this->llss_controller.find_line(rhs.ic_value);
880

881
            return (*ll_lhs) < (*ll_rhs);
882
        }
883
#endif
884

885
#if 0
886
    bool operator()(const content_line_t& lhs, const time_t& rhs) const
887
    {
888
        logline* ll_lhs = this->llss_controller.find_line(lhs);
889

890
        return *ll_lhs < rhs;
891
    }
892
#endif
893

894
    bool operator()(const logfile_sub_source::indexed_content& lhs,
×
895
                    const struct timeval& rhs) const
896
    {
897
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
×
898

899
        return *ll_lhs < rhs;
×
900
    }
901

902
    logfile_sub_source& llss_controller;
903
};
904

905
logfile_sub_source::rebuild_result
906
logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
4,439✔
907
{
908
    if (this->tss_view == nullptr) {
4,439✔
909
        return rebuild_result::rr_no_change;
124✔
910
    }
911

912
    this->lss_indexing_in_progress = true;
4,315✔
913
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
4,315✔
914

915
    iterator iter;
4,315✔
916
    size_t total_lines = 0;
4,315✔
917
    size_t est_remaining_lines = 0;
4,315✔
918
    auto all_time_ordered_formats = true;
4,315✔
919
    auto full_sort = this->lss_index.empty();
4,315✔
920
    int file_count = 0;
4,315✔
921
    auto force = std::exchange(this->lss_force_rebuild, false);
4,315✔
922
    auto retval = rebuild_result::rr_no_change;
4,315✔
923
    std::optional<timeval> lowest_tv = std::nullopt;
4,315✔
924
    auto search_start = 0_vl;
4,315✔
925

926
    if (force) {
4,315✔
927
        log_debug("forced to full rebuild");
480✔
928
        retval = rebuild_result::rr_full_rebuild;
480✔
929
        full_sort = true;
480✔
930
        this->tss_level_filtered_count = 0;
480✔
931
        this->lss_index.clear();
480✔
932
    }
933

934
    std::vector<size_t> file_order(this->lss_files.size());
4,315✔
935

936
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
7,309✔
937
        file_order[lpc] = lpc;
2,994✔
938
    }
939
    if (!this->lss_index.empty()) {
4,315✔
940
        std::stable_sort(file_order.begin(),
2,239✔
941
                         file_order.end(),
942
                         [this](const auto& left, const auto& right) {
246✔
943
                             const auto& left_ld = this->lss_files[left];
246✔
944
                             const auto& right_ld = this->lss_files[right];
246✔
945

946
                             if (left_ld->get_file_ptr() == nullptr) {
246✔
947
                                 return true;
×
948
                             }
949
                             if (right_ld->get_file_ptr() == nullptr) {
246✔
950
                                 return false;
3✔
951
                             }
952

953
                             return left_ld->get_file_ptr()->back()
243✔
954
                                 < right_ld->get_file_ptr()->back();
486✔
955
                         });
956
    }
957

958
    bool time_left = true;
4,315✔
959
    this->lss_all_timestamp_flags = 0;
4,315✔
960
    for (const auto file_index : file_order) {
7,309✔
961
        auto& ld = *(this->lss_files[file_index]);
2,994✔
962
        auto* lf = ld.get_file_ptr();
2,994✔
963

964
        if (lf == nullptr) {
2,994✔
965
            if (ld.ld_lines_indexed > 0) {
4✔
966
                log_debug("%zu: file closed, doing full rebuild",
1✔
967
                          ld.ld_file_index);
968
                force = true;
1✔
969
                retval = rebuild_result::rr_full_rebuild;
1✔
970
                full_sort = true;
1✔
971
            }
972
        } else {
973
            if (!lf->get_format_ptr()->lf_time_ordered) {
2,990✔
974
                all_time_ordered_formats = false;
191✔
975
            }
976
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
2,990✔
977
                log_debug("no time left, skipping %s",
2✔
978
                          lf->get_filename_as_string().c_str());
979
                time_left = false;
2✔
980
            }
981
            this->lss_all_timestamp_flags
2,990✔
982
                |= lf->get_format_ptr()->lf_timestamp_flags;
2,990✔
983

984
            if (!this->tss_view->is_paused() && time_left) {
2,990✔
985
                auto log_rebuild_res = lf->rebuild_index(deadline);
2,988✔
986

987
                if (ld.ld_lines_indexed < lf->size()
2,988✔
988
                    && log_rebuild_res
2,988✔
989
                        == logfile::rebuild_result_t::NO_NEW_LINES)
990
                {
991
                    // This is a bit awkward... if the logfile indexing was
992
                    // complete before being added to us, we need to adjust
993
                    // the rebuild result to make it look like new lines
994
                    // were added.
995
                    log_rebuild_res = logfile::rebuild_result_t::NEW_LINES;
50✔
996
                }
997
                switch (log_rebuild_res) {
2,988✔
998
                    case logfile::rebuild_result_t::NO_NEW_LINES:
2,445✔
999
                        break;
2,445✔
1000
                    case logfile::rebuild_result_t::NEW_LINES:
483✔
1001
                        if (retval == rebuild_result::rr_no_change) {
483✔
1002
                            retval = rebuild_result::rr_appended_lines;
433✔
1003
                        }
1004
                        log_debug("new lines for %s:%zu",
483✔
1005
                                  lf->get_filename_as_string().c_str(),
1006
                                  lf->size());
1007
                        if (!this->lss_index.empty()
483✔
1008
                            && lf->size() > ld.ld_lines_indexed)
483✔
1009
                        {
UNCOV
1010
                            auto& new_file_line = (*lf)[ld.ld_lines_indexed];
×
UNCOV
1011
                            auto cl = this->lss_index.back().value();
×
UNCOV
1012
                            auto* last_indexed_line = this->find_line(cl);
×
1013

1014
                            // If there are new lines that are older than what
1015
                            // we have in the index, we need to resort.
UNCOV
1016
                            if (last_indexed_line == nullptr
×
UNCOV
1017
                                || new_file_line
×
UNCOV
1018
                                    < last_indexed_line->get_timeval())
×
1019
                            {
1020
                                log_debug(
×
1021
                                    "%s:%ld: found older lines, full "
1022
                                    "rebuild: %p  %lld < %lld",
1023
                                    lf->get_filename().c_str(),
1024
                                    ld.ld_lines_indexed,
1025
                                    last_indexed_line,
1026
                                    new_file_line
1027
                                        .get_time<std::chrono::microseconds>()
1028
                                        .count(),
1029
                                    last_indexed_line == nullptr
1030
                                        ? (uint64_t) -1
1031
                                        : last_indexed_line
1032
                                              ->get_time<
1033
                                                  std::chrono::microseconds>()
1034
                                              .count());
1035
                                if (retval <= rebuild_result::rr_partial_rebuild
×
1036
                                    && all_time_ordered_formats)
×
1037
                                {
1038
                                    retval = rebuild_result::rr_partial_rebuild;
×
1039
                                    if (!lowest_tv
×
1040
                                        || new_file_line.get_timeval()
×
1041
                                            < lowest_tv.value())
×
1042
                                    {
1043
                                        lowest_tv = new_file_line.get_timeval();
×
1044
                                    }
1045
                                } else {
1046
                                    log_debug(
×
1047
                                        "already doing full rebuild, doing "
1048
                                        "full_sort as well");
1049
                                    force = true;
×
1050
                                    full_sort = true;
×
1051
                                }
1052
                            }
1053
                        }
1054
                        break;
483✔
1055
                    case logfile::rebuild_result_t::INVALID:
60✔
1056
                    case logfile::rebuild_result_t::NEW_ORDER:
1057
                        log_debug("%s: log file has a new order, full rebuild",
60✔
1058
                                  lf->get_filename().c_str());
1059
                        retval = rebuild_result::rr_full_rebuild;
60✔
1060
                        force = true;
60✔
1061
                        full_sort = true;
60✔
1062
                        break;
60✔
1063
                }
1064
            }
1065
            file_count += 1;
2,990✔
1066
            total_lines += lf->size();
2,990✔
1067

1068
            est_remaining_lines += lf->estimated_remaining_lines();
2,990✔
1069
        }
1070
    }
1071

1072
    if (!all_time_ordered_formats
4,315✔
1073
        && retval == rebuild_result::rr_partial_rebuild)
185✔
1074
    {
1075
        force = true;
×
1076
        full_sort = true;
×
1077
        retval = rebuild_result::rr_full_rebuild;
×
1078
    }
1079

1080
    if (this->lss_index.reserve(total_lines + est_remaining_lines)) {
4,315✔
1081
        // The index array was reallocated, just do a full sort/rebuild since
1082
        // it's been cleared out.
1083
        log_debug("expanding index capacity %zu", this->lss_index.ba_capacity);
634✔
1084
        force = true;
634✔
1085
        retval = rebuild_result::rr_full_rebuild;
634✔
1086
        full_sort = true;
634✔
1087
        this->tss_level_filtered_count = 0;
634✔
1088
    }
1089

1090
    auto& vis_bm = this->tss_view->get_bookmarks();
4,315✔
1091

1092
    if (force) {
4,315✔
1093
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,665✔
1094
             iter++)
536✔
1095
        {
1096
            (*iter)->ld_lines_indexed = 0;
536✔
1097
        }
1098

1099
        this->lss_index.clear();
1,129✔
1100
        this->lss_filtered_index.clear();
1,129✔
1101
        this->tss_level_filtered_count = 0;
1,129✔
1102
        this->lss_longest_line = 0;
1,129✔
1103
        this->lss_basename_width = 0;
1,129✔
1104
        this->lss_filename_width = 0;
1,129✔
1105
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,129✔
1106
        if (this->lss_index_delegate) {
1,129✔
1107
            this->lss_index_delegate->index_start(*this);
1,129✔
1108
        }
1109
    } else if (retval == rebuild_result::rr_partial_rebuild) {
3,186✔
1110
        size_t remaining = 0;
×
1111

1112
        log_debug("partial rebuild with lowest time: %ld",
×
1113
                  lowest_tv.value().tv_sec);
1114
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1115
             iter++)
×
1116
        {
1117
            logfile_data& ld = *(*iter);
×
1118
            auto* lf = ld.get_file_ptr();
×
1119

1120
            if (lf == nullptr) {
×
1121
                continue;
×
1122
            }
1123

1124
            require(lf->get_format_ptr()->lf_time_ordered);
×
1125

1126
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1127

1128
            if (line_iter) {
×
1129
                log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
×
1130
                          line_iter.value()->get_timeval().tv_sec,
1131
                          std::distance(lf->cbegin(), line_iter.value()),
1132
                          lf->size(),
1133
                          lf->get_filename_as_string().c_str());
1134
            }
1135
            ld.ld_lines_indexed
1136
                = std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
×
1137
            remaining += lf->size() - ld.ld_lines_indexed;
×
1138
        }
1139

1140
        auto* row_iter = std::lower_bound(this->lss_index.begin(),
×
1141
                                          this->lss_index.end(),
1142
                                          lowest_tv.value(),
×
1143
                                          logline_cmp(*this));
1144
        this->lss_index.shrink_to(
×
1145
            std::distance(this->lss_index.begin(), row_iter));
×
1146
        log_debug("new index size %ld/%ld; remain %ld",
×
1147
                  this->lss_index.ba_size,
1148
                  this->lss_index.ba_capacity,
1149
                  remaining);
1150
        auto filt_row_iter = std::lower_bound(this->lss_filtered_index.begin(),
×
1151
                                              this->lss_filtered_index.end(),
1152
                                              lowest_tv.value(),
×
1153
                                              filtered_logline_cmp(*this));
1154
        this->lss_filtered_index.resize(
×
1155
            std::distance(this->lss_filtered_index.begin(), filt_row_iter));
×
1156
        search_start = vis_line_t(this->lss_filtered_index.size());
×
1157

1158
        if (this->lss_index_delegate) {
×
1159
            this->lss_index_delegate->index_start(*this);
×
1160
            for (const auto row_in_full_index : this->lss_filtered_index) {
×
1161
                auto cl = this->lss_index[row_in_full_index].value();
×
1162
                uint64_t line_number;
1163
                auto ld_iter = this->find_data(cl, line_number);
×
1164
                auto& ld = *ld_iter;
×
1165
                auto line_iter = ld->get_file_ptr()->begin() + line_number;
×
1166

1167
                this->lss_index_delegate->index_line(
×
1168
                    *this, ld->get_file_ptr(), line_iter);
1169
            }
1170
        }
1171
    }
1172

1173
    if (this->lss_index.empty() && !time_left) {
4,315✔
1174
        log_info("ran out of time, skipping rebuild");
×
1175
        // need to make sure we rebuild in case no new data comes in
1176
        this->lss_force_rebuild = true;
×
1177
        return rebuild_result::rr_appended_lines;
×
1178
    }
1179

1180
    if (retval != rebuild_result::rr_no_change || force) {
4,315✔
1181
        size_t index_size = 0, start_size = this->lss_index.size();
1,141✔
1182
        logline_cmp line_cmper(*this);
1,141✔
1183

1184
        for (auto& ld : this->lss_files) {
1,694✔
1185
            auto* lf = ld->get_file_ptr();
553✔
1186

1187
            if (lf == nullptr) {
553✔
1188
                continue;
1✔
1189
            }
1190
            this->lss_longest_line = std::max(
1,104✔
1191
                this->lss_longest_line, lf->get_longest_line_length() + 1);
552✔
1192
            this->lss_basename_width
1193
                = std::max(this->lss_basename_width,
1,104✔
1194
                           lf->get_unique_path().native().size());
552✔
1195
            this->lss_filename_width = std::max(
1,104✔
1196
                this->lss_filename_width, lf->get_filename().native().size());
552✔
1197
        }
1198

1199
        if (full_sort) {
1,141✔
1200
            log_trace("rebuild_index full sort");
1,141✔
1201
            for (auto& ld : this->lss_files) {
1,694✔
1202
                auto* lf = ld->get_file_ptr();
553✔
1203

1204
                if (lf == nullptr) {
553✔
1205
                    continue;
1✔
1206
                }
1207

1208
                for (size_t line_index = 0; line_index < lf->size();
14,812✔
1209
                     line_index++)
1210
                {
1211
                    const auto lf_iter
1212
                        = ld->get_file_ptr()->begin() + line_index;
14,260✔
1213
                    if (lf_iter->is_ignored()) {
14,260✔
1214
                        continue;
417✔
1215
                    }
1216

1217
                    content_line_t con_line(
1218
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
13,843✔
1219

1220
                    if (lf_iter->is_meta_marked()) {
13,843✔
1221
                        auto start_iter = lf_iter;
12✔
1222
                        while (start_iter->is_continued()) {
12✔
1223
                            --start_iter;
×
1224
                        }
1225
                        int start_index
1226
                            = start_iter - ld->get_file_ptr()->begin();
12✔
1227
                        content_line_t start_con_line(ld->ld_file_index
12✔
1228
                                                          * MAX_LINES_PER_FILE
12✔
1229
                                                      + start_index);
12✔
1230

1231
                        auto& line_meta
1232
                            = ld->get_file_ptr()
1233
                                  ->get_bookmark_metadata()[start_index];
12✔
1234
                        if (line_meta.has(bookmark_metadata::categories::notes))
12✔
1235
                        {
1236
                            this->lss_user_marks[&textview_curses::BM_META]
4✔
1237
                                .insert_once(start_con_line);
4✔
1238
                        }
1239
                        if (line_meta.has(
12✔
1240
                                bookmark_metadata::categories::partition))
1241
                        {
1242
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
8✔
1243
                                .insert_once(start_con_line);
8✔
1244
                        }
1245
                    }
1246
                    this->lss_index.push_back(
13,843✔
1247
                        indexed_content{con_line, lf_iter});
27,686✔
1248
                }
1249
            }
1250

1251
            // XXX get rid of this full sort on the initial run, it's not
1252
            // needed unless the file is not in time-order
1253
            if (this->lss_sorting_observer) {
1,141✔
1254
                this->lss_sorting_observer(*this, 0, this->lss_index.size());
3✔
1255
            }
1256
            std::sort(
1,141✔
1257
                this->lss_index.begin(), this->lss_index.end(), line_cmper);
1258
            if (this->lss_sorting_observer) {
1,141✔
1259
                this->lss_sorting_observer(
6✔
1260
                    *this, this->lss_index.size(), this->lss_index.size());
3✔
1261
            }
1262
        } else {
1263
            kmerge_tree_c<logline, logfile_data, logfile::iterator> merge(
UNCOV
1264
                file_count);
×
1265

UNCOV
1266
            for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
UNCOV
1267
                 iter++)
×
1268
            {
UNCOV
1269
                auto* ld = iter->get();
×
UNCOV
1270
                auto* lf = ld->get_file_ptr();
×
UNCOV
1271
                if (lf == nullptr) {
×
1272
                    continue;
×
1273
                }
1274

UNCOV
1275
                merge.add(ld, lf->begin() + ld->ld_lines_indexed, lf->end());
×
UNCOV
1276
                index_size += lf->size();
×
1277
            }
1278

UNCOV
1279
            file_off_t index_off = 0;
×
UNCOV
1280
            merge.execute();
×
UNCOV
1281
            if (this->lss_sorting_observer) {
×
1282
                this->lss_sorting_observer(*this, index_off, index_size);
×
1283
            }
UNCOV
1284
            log_trace("k-way merge");
×
1285
            for (;;) {
UNCOV
1286
                logfile::iterator lf_iter;
×
1287
                logfile_data* ld;
1288

UNCOV
1289
                if (!merge.get_top(ld, lf_iter)) {
×
UNCOV
1290
                    break;
×
1291
                }
1292

UNCOV
1293
                if (!lf_iter->is_ignored()) {
×
UNCOV
1294
                    int file_index = ld->ld_file_index;
×
UNCOV
1295
                    int line_index = lf_iter - ld->get_file_ptr()->begin();
×
1296

UNCOV
1297
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
UNCOV
1298
                                            + line_index);
×
1299

UNCOV
1300
                    if (lf_iter->is_meta_marked()) {
×
1301
                        auto start_iter = lf_iter;
×
1302
                        while (start_iter->is_continued()) {
×
1303
                            --start_iter;
×
1304
                        }
1305
                        int start_index
1306
                            = start_iter - ld->get_file_ptr()->begin();
×
1307
                        content_line_t start_con_line(
1308
                            file_index * MAX_LINES_PER_FILE + start_index);
×
1309

1310
                        auto& line_meta
1311
                            = ld->get_file_ptr()
1312
                                  ->get_bookmark_metadata()[start_index];
×
1313
                        if (line_meta.has(bookmark_metadata::categories::notes))
×
1314
                        {
1315
                            this->lss_user_marks[&textview_curses::BM_META]
×
1316
                                .insert_once(start_con_line);
×
1317
                        }
1318
                        if (line_meta.has(
×
1319
                                bookmark_metadata::categories::partition))
1320
                        {
1321
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
×
1322
                                .insert_once(start_con_line);
×
1323
                        }
1324
                    }
UNCOV
1325
                    this->lss_index.push_back(
×
UNCOV
1326
                        indexed_content{con_line, lf_iter});
×
1327
                }
1328

UNCOV
1329
                merge.next();
×
UNCOV
1330
                index_off += 1;
×
UNCOV
1331
                if (index_off % 100000 == 0 && this->lss_sorting_observer) {
×
1332
                    this->lss_sorting_observer(*this, index_off, index_size);
×
1333
                }
1334
            }
UNCOV
1335
            if (this->lss_sorting_observer) {
×
1336
                this->lss_sorting_observer(*this, index_size, index_size);
×
1337
            }
1338
        }
1339

1340
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,694✔
1341
             ++iter)
553✔
1342
        {
1343
            auto* lf = (*iter)->get_file_ptr();
553✔
1344

1345
            if (lf == nullptr) {
553✔
1346
                continue;
1✔
1347
            }
1348

1349
            (*iter)->ld_lines_indexed = lf->size();
552✔
1350
        }
1351

1352
        this->lss_filtered_index.reserve(this->lss_index.size());
1,141✔
1353

1354
        uint32_t filter_in_mask, filter_out_mask;
1355
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,141✔
1356

1357
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,141✔
1358
            this->lss_index_delegate->index_start(*this);
1,141✔
1359
        }
1360

1361
        log_trace("filtered index");
1,141✔
1362
        for (size_t index_index = start_size;
14,984✔
1363
             index_index < this->lss_index.size();
14,984✔
1364
             index_index++)
1365
        {
1366
            const auto cl = this->lss_index[index_index].value();
13,843✔
1367
            uint64_t line_number;
1368
            auto ld = this->find_data(cl, line_number);
13,843✔
1369

1370
            if (!(*ld)->is_visible()) {
13,843✔
1371
                continue;
×
1372
            }
1373

1374
            auto* lf = (*ld)->get_file_ptr();
13,843✔
1375
            auto line_iter = lf->begin() + line_number;
13,843✔
1376

1377
            if (line_iter->is_ignored()) {
13,843✔
1378
                continue;
×
1379
            }
1380

1381
            if (!this->tss_apply_filters
27,686✔
1382
                || (!(*ld)->ld_filter_state.excluded(
27,658✔
1383
                        filter_in_mask, filter_out_mask, line_number)
1384
                    && this->check_extra_filters(ld, line_iter)))
13,815✔
1385
            {
1386
                auto eval_res = this->eval_sql_filter(
1387
                    this->lss_marker_stmt.in(), ld, line_iter);
13,815✔
1388
                if (eval_res.isErr()) {
13,815✔
1389
                    line_iter->set_expr_mark(false);
×
1390
                } else {
1391
                    auto matched = eval_res.unwrap();
13,815✔
1392

1393
                    line_iter->set_expr_mark(matched);
13,815✔
1394
                    if (matched) {
13,815✔
1395
                        vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1396
                            vis_line_t(this->lss_filtered_index.size()));
×
1397
                        this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1398
                            .insert_once(cl);
×
1399
                    }
1400
                }
1401
                this->lss_filtered_index.push_back(index_index);
13,815✔
1402
                if (this->lss_index_delegate != nullptr) {
13,815✔
1403
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
13,815✔
1404
                }
1405
            }
13,815✔
1406
        }
1407

1408
        this->lss_indexing_in_progress = false;
1,141✔
1409

1410
        if (this->lss_index_delegate != nullptr) {
1,141✔
1411
            this->lss_index_delegate->index_complete(*this);
1,141✔
1412
        }
1413
    }
1414

1415
    switch (retval) {
4,315✔
1416
        case rebuild_result::rr_no_change:
3,174✔
1417
            break;
3,174✔
1418
        case rebuild_result::rr_full_rebuild:
1,129✔
1419
            log_debug("redoing search");
1,129✔
1420
            this->lss_index_generation += 1;
1,129✔
1421
            this->tss_view->reload_data();
1,129✔
1422
            this->tss_view->redo_search();
1,129✔
1423
            break;
1,129✔
1424
        case rebuild_result::rr_partial_rebuild:
×
1425
            log_debug("redoing search from: %d", (int) search_start);
×
1426
            this->lss_index_generation += 1;
×
1427
            this->tss_view->reload_data();
×
1428
            this->tss_view->search_new_data(search_start);
×
1429
            break;
×
1430
        case rebuild_result::rr_appended_lines:
12✔
1431
            this->tss_view->reload_data();
12✔
1432
            this->tss_view->search_new_data();
12✔
1433
            break;
12✔
1434
    }
1435

1436
    return retval;
4,315✔
1437
}
4,315✔
1438

1439
void
1440
logfile_sub_source::text_update_marks(vis_bookmarks& bm)
2,211✔
1441
{
1442
    logfile* last_file = nullptr;
2,211✔
1443
    vis_line_t vl;
2,211✔
1444

1445
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,211✔
1446
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,211✔
1447
    auto& bm_files = bm[&BM_FILES];
2,211✔
1448

1449
    bm_warnings.clear();
2,211✔
1450
    bm_errors.clear();
2,211✔
1451
    bm_files.clear();
2,211✔
1452

1453
    std::vector<const bookmark_type_t*> used_marks;
2,211✔
1454
    for (const auto* bmt :
11,055✔
1455
         {
1456
             &textview_curses::BM_USER,
1457
             &textview_curses::BM_USER_EXPR,
1458
             &textview_curses::BM_PARTITION,
1459
             &textview_curses::BM_META,
1460
         })
13,266✔
1461
    {
1462
        bm[bmt].clear();
8,844✔
1463
        if (!this->lss_user_marks[bmt].empty()) {
8,844✔
1464
            used_marks.emplace_back(bmt);
118✔
1465
        }
1466
    }
1467

1468
    for (; vl < (int) this->lss_filtered_index.size(); ++vl) {
19,572✔
1469
        const auto& orig_ic = this->lss_index[this->lss_filtered_index[vl]];
17,361✔
1470
        auto cl = orig_ic.value();
17,361✔
1471
        auto* lf = this->find_file_ptr(cl);
17,361✔
1472

1473
        for (const auto& bmt : used_marks) {
19,499✔
1474
            auto& user_mark = this->lss_user_marks[bmt];
2,138✔
1475
            if (user_mark.bv_tree.exists(orig_ic.value())) {
2,138✔
1476
                bm[bmt].insert_once(vl);
156✔
1477
            }
1478
        }
1479

1480
        if (lf != last_file) {
17,361✔
1481
            bm_files.insert_once(vl);
967✔
1482
        }
1483

1484
        switch (orig_ic.level()) {
17,361✔
1485
            case indexed_content::level_t::warning:
94✔
1486
                bm_warnings.insert_once(vl);
94✔
1487
                break;
94✔
1488

1489
            case indexed_content::level_t::error:
3,421✔
1490
                bm_errors.insert_once(vl);
3,421✔
1491
                break;
3,421✔
1492

1493
            default:
13,846✔
1494
                break;
13,846✔
1495
        }
1496

1497
        last_file = lf;
17,361✔
1498
    }
1499
}
2,211✔
1500

1501
void
1502
logfile_sub_source::text_filters_changed()
166✔
1503
{
1504
    this->lss_index_generation += 1;
166✔
1505
    this->tss_level_filtered_count = 0;
166✔
1506

1507
    if (this->lss_line_meta_changed) {
166✔
1508
        this->invalidate_sql_filter();
24✔
1509
        this->lss_line_meta_changed = false;
24✔
1510
    }
1511

1512
    for (auto& ld : *this) {
285✔
1513
        auto* lf = ld->get_file_ptr();
119✔
1514

1515
        if (lf != nullptr) {
119✔
1516
            ld->ld_filter_state.clear_deleted_filter_state();
119✔
1517
            lf->reobserve_from(lf->begin()
119✔
1518
                               + ld->ld_filter_state.get_min_count(lf->size()));
119✔
1519
        }
1520
    }
1521

1522
    if (this->lss_force_rebuild) {
166✔
1523
        return;
×
1524
    }
1525

1526
    auto& vis_bm = this->tss_view->get_bookmarks();
166✔
1527
    uint32_t filtered_in_mask, filtered_out_mask;
1528

1529
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
166✔
1530

1531
    if (this->lss_index_delegate != nullptr) {
166✔
1532
        this->lss_index_delegate->index_start(*this);
166✔
1533
    }
1534
    vis_bm[&textview_curses::BM_USER_EXPR].clear();
166✔
1535

1536
    this->lss_filtered_index.clear();
166✔
1537
    for (size_t index_index = 0; index_index < this->lss_index.size();
1,497✔
1538
         index_index++)
1539
    {
1540
        auto cl = this->lss_index[index_index].value();
1,331✔
1541
        uint64_t line_number;
1542
        auto ld = this->find_data(cl, line_number);
1,331✔
1543

1544
        if (!(*ld)->is_visible()) {
1,331✔
1545
            continue;
213✔
1546
        }
1547

1548
        auto lf = (*ld)->get_file_ptr();
1,118✔
1549
        auto line_iter = lf->begin() + line_number;
1,118✔
1550

1551
        if (!this->tss_apply_filters
2,236✔
1552
            || (!(*ld)->ld_filter_state.excluded(
2,098✔
1553
                    filtered_in_mask, filtered_out_mask, line_number)
1554
                && this->check_extra_filters(ld, line_iter)))
980✔
1555
        {
1556
            auto eval_res = this->eval_sql_filter(
1557
                this->lss_marker_stmt.in(), ld, line_iter);
938✔
1558
            if (eval_res.isErr()) {
938✔
1559
                line_iter->set_expr_mark(false);
×
1560
            } else {
1561
                auto matched = eval_res.unwrap();
938✔
1562

1563
                line_iter->set_expr_mark(matched);
938✔
1564
                if (matched) {
938✔
1565
                    vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1566
                        vis_line_t(this->lss_filtered_index.size()));
×
1567
                    this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1568
                        .insert_once(cl);
×
1569
                }
1570
            }
1571
            this->lss_filtered_index.push_back(index_index);
938✔
1572
            if (this->lss_index_delegate != nullptr) {
938✔
1573
                this->lss_index_delegate->index_line(*this, lf, line_iter);
938✔
1574
            }
1575
        }
938✔
1576
    }
1577

1578
    if (this->lss_index_delegate != nullptr) {
166✔
1579
        this->lss_index_delegate->index_complete(*this);
166✔
1580
    }
1581

1582
    if (this->tss_view != nullptr) {
166✔
1583
        this->tss_view->reload_data();
166✔
1584
        this->tss_view->redo_search();
166✔
1585
    }
1586
}
1587

1588
std::optional<json_string>
1589
logfile_sub_source::text_row_details(const textview_curses& tc)
29✔
1590
{
1591
    if (this->lss_index.empty()) {
29✔
1592
        log_trace("logfile_sub_source::text_row_details empty");
1✔
1593
        return std::nullopt;
1✔
1594
    }
1595

1596
    auto ov_sel = tc.get_overlay_selection();
28✔
1597
    if (ov_sel.has_value()) {
28✔
1598
        auto* fos
1599
            = dynamic_cast<field_overlay_source*>(tc.get_overlay_source());
×
1600
        auto iter = fos->fos_row_to_field_meta.find(ov_sel.value());
×
1601
        if (iter != fos->fos_row_to_field_meta.end()) {
×
1602
            auto find_res = this->find_line_with_file(tc.get_top());
×
1603
            if (find_res) {
×
1604
                yajlpp_gen gen;
×
1605

1606
                {
1607
                    yajlpp_map root(gen);
×
1608

1609
                    root.gen("value");
×
1610
                    root.gen(iter->second.ri_value);
×
1611
                }
1612

1613
                return json_string(gen);
×
1614
            }
1615
        }
1616
    }
1617

1618
    return std::nullopt;
28✔
1619
}
1620

1621
bool
1622
logfile_sub_source::list_input_handle_key(listview_curses& lv,
×
1623
                                          const ncinput& ch)
1624
{
1625
    switch (ch.eff_text[0]) {
×
1626
        case ' ': {
×
1627
            auto ov_vl = lv.get_overlay_selection();
×
1628
            if (ov_vl) {
×
1629
                auto* fos = dynamic_cast<field_overlay_source*>(
×
1630
                    lv.get_overlay_source());
×
1631
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
1632
                if (iter != fos->fos_row_to_field_meta.end()
×
1633
                    && iter->second.ri_meta)
×
1634
                {
1635
                    auto find_res = this->find_line_with_file(lv.get_top());
×
1636
                    if (find_res) {
×
1637
                        auto file_and_line = find_res.value();
×
1638
                        auto* format = file_and_line.first->get_format_ptr();
×
1639
                        auto fstates = format->get_field_states();
×
1640
                        auto state_iter
1641
                            = fstates.find(iter->second.ri_meta->lvm_name);
×
1642
                        if (state_iter != fstates.end()) {
×
1643
                            format->hide_field(iter->second.ri_meta->lvm_name,
×
1644
                                               !state_iter->second.is_hidden());
×
1645
                            lv.set_needs_update();
×
1646
                        }
1647
                    }
1648
                }
1649
                return true;
×
1650
            }
1651
            return false;
×
1652
        }
1653
        case '#': {
×
1654
            auto ov_vl = lv.get_overlay_selection();
×
1655
            if (ov_vl) {
×
1656
                auto* fos = dynamic_cast<field_overlay_source*>(
×
1657
                    lv.get_overlay_source());
×
1658
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
1659
                if (iter != fos->fos_row_to_field_meta.end()
×
1660
                    && iter->second.ri_meta)
×
1661
                {
1662
                    const auto& meta = iter->second.ri_meta.value();
×
1663
                    std::string cmd;
×
1664

1665
                    switch (meta.to_chart_type()) {
×
1666
                        case chart_type_t::none:
×
1667
                            break;
×
1668
                        case chart_type_t::hist: {
×
1669
                            auto prql = fmt::format(
1670
                                FMT_STRING(
×
1671
                                    "from {} | stats.hist {} slice:'1h'"),
1672
                                meta.lvm_format.value()->get_name(),
×
1673
                                meta.lvm_name);
×
1674
                            cmd = fmt::format(FMT_STRING(":prompt sql ; '{}'"),
×
1675
                                              shlex::escape(prql));
×
1676
                            break;
×
1677
                        }
1678
                        case chart_type_t::spectro:
×
1679
                            cmd = fmt::format(FMT_STRING(":spectrogram {}"),
×
1680
                                              meta.lvm_name);
×
1681
                            break;
×
1682
                    }
1683
                    if (!cmd.empty()) {
×
1684
                        this->lss_exec_context
×
1685
                            ->with_provenance(exec_context::mouse_input{})
×
1686
                            ->execute(INTERNAL_SRC_LOC, cmd);
×
1687
                    }
1688
                }
1689
                return true;
×
1690
            }
1691
            return false;
×
1692
        }
1693
        case 'h':
×
1694
        case 'H':
1695
        case NCKEY_LEFT:
1696
            if (lv.get_left() == 0) {
×
1697
                this->increase_line_context();
×
1698
                lv.set_needs_update();
×
1699
                return true;
×
1700
            }
1701
            break;
×
1702
        case 'l':
×
1703
        case 'L':
1704
        case NCKEY_RIGHT:
1705
            if (this->decrease_line_context()) {
×
1706
                lv.set_needs_update();
×
1707
                return true;
×
1708
            }
1709
            break;
×
1710
    }
1711
    return false;
×
1712
}
1713

1714
std::optional<
1715
    std::pair<grep_proc_source<vis_line_t>*, grep_proc_sink<vis_line_t>*>>
1716
logfile_sub_source::get_grepper()
9✔
1717
{
1718
    return std::make_pair(
18✔
1719
        (grep_proc_source<vis_line_t>*) &this->lss_meta_grepper,
×
1720
        (grep_proc_sink<vis_line_t>*) &this->lss_meta_grepper);
9✔
1721
}
1722

1723
/**
1724
 * Functor for comparing the ld_file field of the logfile_data struct.
1725
 */
1726
struct logfile_data_eq {
1727
    explicit logfile_data_eq(std::shared_ptr<logfile> lf)
1,038✔
1728
        : lde_file(std::move(lf))
1,038✔
1729
    {
1730
    }
1,038✔
1731

1732
    bool operator()(
635✔
1733
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1734
    {
1735
        return this->lde_file == ld->get_file();
635✔
1736
    }
1737

1738
    std::shared_ptr<logfile> lde_file;
1739
};
1740

1741
bool
1742
logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
519✔
1743
{
1744
    iterator existing;
519✔
1745

1746
    require_lt(lf->size(), MAX_LINES_PER_FILE);
519✔
1747

1748
    existing = std::find_if(this->lss_files.begin(),
519✔
1749
                            this->lss_files.end(),
1750
                            logfile_data_eq(nullptr));
1,038✔
1751
    if (existing == this->lss_files.end()) {
519✔
1752
        if (this->lss_files.size() >= MAX_FILES) {
519✔
1753
            return false;
×
1754
        }
1755

1756
        auto ld = std::make_unique<logfile_data>(
1757
            this->lss_files.size(), this->get_filters(), lf);
519✔
1758
        ld->set_visibility(lf->get_open_options().loo_is_visible);
519✔
1759
        this->lss_files.push_back(std::move(ld));
519✔
1760
    } else {
519✔
UNCOV
1761
        (*existing)->set_file(lf);
×
1762
    }
1763

1764
    return true;
519✔
1765
}
1766

1767
Result<void, lnav::console::user_message>
1768
logfile_sub_source::set_sql_filter(std::string stmt_str, sqlite3_stmt* stmt)
773✔
1769
{
1770
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
773✔
1771
        auto top_cl = this->at(0_vl);
8✔
1772
        auto ld = this->find_data(top_cl);
8✔
1773
        auto eval_res
1774
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
8✔
1775

1776
        if (eval_res.isErr()) {
8✔
1777
            sqlite3_finalize(stmt);
1✔
1778
            return Err(eval_res.unwrapErr());
1✔
1779
        }
1780
    }
8✔
1781

1782
    for (auto& ld : *this) {
789✔
1783
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
17✔
1784
    }
1785

1786
    auto old_filter_iter = this->tss_filters.find(0);
772✔
1787
    if (stmt != nullptr) {
772✔
1788
        auto new_filter
1789
            = std::make_shared<sql_filter>(*this, std::move(stmt_str), stmt);
7✔
1790

1791
        if (old_filter_iter != this->tss_filters.end()) {
7✔
1792
            *old_filter_iter = new_filter;
×
1793
        } else {
1794
            this->tss_filters.add_filter(new_filter);
7✔
1795
        }
1796
    } else if (old_filter_iter != this->tss_filters.end()) {
772✔
1797
        this->tss_filters.delete_filter((*old_filter_iter)->get_id());
7✔
1798
    }
1799

1800
    return Ok();
772✔
1801
}
1802

1803
Result<void, lnav::console::user_message>
1804
logfile_sub_source::set_sql_marker(std::string stmt_str, sqlite3_stmt* stmt)
770✔
1805
{
1806
    static auto op = lnav_operation{"set_sql_marker"};
770✔
1807
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
770✔
1808
        auto top_cl = this->at(0_vl);
5✔
1809
        auto ld = this->find_data(top_cl);
5✔
1810
        auto eval_res
1811
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
5✔
1812

1813
        if (eval_res.isErr()) {
5✔
1814
            sqlite3_finalize(stmt);
×
1815
            return Err(eval_res.unwrapErr());
×
1816
        }
1817
    }
5✔
1818

1819
    auto op_guard = lnav_opid_guard::internal(op);
770✔
1820
    log_info("setting SQL marker: %s", stmt_str.c_str());
770✔
1821
    this->lss_marker_stmt_text = std::move(stmt_str);
770✔
1822
    this->lss_marker_stmt = stmt;
770✔
1823

1824
    if (this->tss_view == nullptr || this->lss_force_rebuild) {
770✔
1825
        log_info("skipping SQL marker update");
130✔
1826
        return Ok();
130✔
1827
    }
1828

1829
    auto& vis_bm = this->tss_view->get_bookmarks();
640✔
1830
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
640✔
1831
    auto& cl_marks_bv = this->lss_user_marks[&textview_curses::BM_USER_EXPR];
640✔
1832

1833
    expr_marks_bv.clear();
640✔
1834
    if (this->lss_index_delegate) {
640✔
1835
        this->lss_index_delegate->index_start(*this);
640✔
1836
    }
1837
    for (auto row = 0_vl; row < vis_line_t(this->lss_filtered_index.size());
1,073✔
1838
         row += 1_vl)
433✔
1839
    {
1840
        auto cl = this->at(row);
433✔
1841
        uint64_t line_number;
1842
        auto ld = this->find_data(cl, line_number);
433✔
1843

1844
        if (!(*ld)->is_visible()) {
433✔
1845
            continue;
1✔
1846
        }
1847
        auto ll = (*ld)->get_file()->begin() + line_number;
433✔
1848
        if (ll->is_continued() || ll->is_ignored()) {
433✔
1849
            continue;
1✔
1850
        }
1851
        auto eval_res
1852
            = this->eval_sql_filter(this->lss_marker_stmt.in(), ld, ll);
432✔
1853

1854
        if (eval_res.isErr()) {
432✔
1855
            ll->set_expr_mark(false);
×
1856
        } else {
1857
            auto matched = eval_res.unwrap();
432✔
1858

1859
            ll->set_expr_mark(matched);
432✔
1860
            if (matched) {
432✔
1861
                expr_marks_bv.insert_once(row);
22✔
1862
                cl_marks_bv.insert_once(cl);
22✔
1863
            }
1864
        }
1865
        if (this->lss_index_delegate) {
432✔
1866
            this->lss_index_delegate->index_line(
432✔
1867
                *this, (*ld)->get_file_ptr(), ll);
432✔
1868
        }
1869
    }
432✔
1870
    if (this->lss_index_delegate) {
640✔
1871
        this->lss_index_delegate->index_complete(*this);
640✔
1872
    }
1873
    log_info("SQL marker update complete");
640✔
1874

1875
    return Ok();
640✔
1876
}
770✔
1877

1878
Result<void, lnav::console::user_message>
1879
logfile_sub_source::set_preview_sql_filter(sqlite3_stmt* stmt)
765✔
1880
{
1881
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
765✔
1882
        auto top_cl = this->at(0_vl);
×
1883
        auto ld = this->find_data(top_cl);
×
1884
        auto eval_res
1885
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
×
1886

1887
        if (eval_res.isErr()) {
×
1888
            sqlite3_finalize(stmt);
×
1889
            return Err(eval_res.unwrapErr());
×
1890
        }
1891
    }
1892

1893
    this->lss_preview_filter_stmt = stmt;
765✔
1894

1895
    return Ok();
765✔
1896
}
1897

1898
Result<bool, lnav::console::user_message>
1899
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
15,371✔
1900
                                    iterator ld,
1901
                                    logfile::const_iterator ll)
1902
{
1903
    if (stmt == nullptr) {
15,371✔
1904
        return Ok(false);
29,512✔
1905
    }
1906

1907
    auto* lf = (*ld)->get_file_ptr();
615✔
1908
    char timestamp_buffer[64];
1909
    shared_buffer_ref raw_sbr;
615✔
1910
    logline_value_vector values;
615✔
1911
    auto& sbr = values.lvv_sbr;
615✔
1912
    lf->read_full_message(ll, sbr);
615✔
1913
    sbr.erase_ansi();
615✔
1914
    auto format = lf->get_format();
615✔
1915
    string_attrs_t sa;
615✔
1916
    auto line_number = std::distance(lf->cbegin(), ll);
615✔
1917
    format->annotate(lf, line_number, sa, values);
615✔
1918
    auto lffs = lf->get_format_file_state();
615✔
1919

1920
    sqlite3_reset(stmt);
615✔
1921
    sqlite3_clear_bindings(stmt);
615✔
1922

1923
    auto count = sqlite3_bind_parameter_count(stmt);
615✔
1924
    for (int lpc = 0; lpc < count; lpc++) {
1,242✔
1925
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
627✔
1926

1927
        if (name[0] == '$') {
627✔
1928
            const char* env_value;
1929

1930
            if ((env_value = getenv(&name[1])) != nullptr) {
4✔
1931
                sqlite3_bind_text(stmt, lpc + 1, env_value, -1, SQLITE_STATIC);
1✔
1932
            }
1933
            continue;
4✔
1934
        }
4✔
1935
        if (strcmp(name, ":log_level") == 0) {
623✔
1936
            auto lvl = ll->get_level_name();
6✔
1937
            sqlite3_bind_text(
6✔
1938
                stmt, lpc + 1, lvl.data(), lvl.length(), SQLITE_STATIC);
1939
            continue;
6✔
1940
        }
6✔
1941
        if (strcmp(name, ":log_time") == 0) {
617✔
1942
            auto len = sql_strftime(timestamp_buffer,
×
1943
                                    sizeof(timestamp_buffer),
1944
                                    ll->get_timeval(),
×
1945
                                    'T');
1946
            sqlite3_bind_text(
×
1947
                stmt, lpc + 1, timestamp_buffer, len, SQLITE_STATIC);
1948
            continue;
×
1949
        }
1950
        if (strcmp(name, ":log_time_msecs") == 0) {
617✔
1951
            sqlite3_bind_int64(
1✔
1952
                stmt,
1953
                lpc + 1,
1954
                ll->get_time<std::chrono::milliseconds>().count());
1✔
1955
            continue;
1✔
1956
        }
1957
        if (strcmp(name, ":log_mark") == 0) {
616✔
1958
            sqlite3_bind_int(stmt, lpc + 1, ll->is_marked());
×
1959
            continue;
×
1960
        }
1961
        if (strcmp(name, ":log_comment") == 0) {
616✔
1962
            const auto& bm = lf->get_bookmark_metadata();
×
1963
            auto line_number
1964
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
1965
            auto bm_iter = bm.find(line_number);
×
1966
            if (bm_iter != bm.end() && !bm_iter->second.bm_comment.empty()) {
×
1967
                const auto& meta = bm_iter->second;
×
1968
                sqlite3_bind_text(stmt,
×
1969
                                  lpc + 1,
1970
                                  meta.bm_comment.c_str(),
1971
                                  meta.bm_comment.length(),
×
1972
                                  SQLITE_STATIC);
1973
            }
1974
            continue;
×
1975
        }
1976
        if (strcmp(name, ":log_annotations") == 0) {
616✔
1977
            const auto& bm = lf->get_bookmark_metadata();
×
1978
            auto line_number
1979
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
1980
            auto bm_iter = bm.find(line_number);
×
1981
            if (bm_iter != bm.end()
×
1982
                && !bm_iter->second.bm_annotations.la_pairs.empty())
×
1983
            {
1984
                const auto& meta = bm_iter->second;
×
1985
                auto anno_str = logmsg_annotations_handlers.to_string(
1986
                    meta.bm_annotations);
×
1987

1988
                sqlite3_bind_text(stmt,
×
1989
                                  lpc + 1,
1990
                                  anno_str.c_str(),
1991
                                  anno_str.length(),
×
1992
                                  SQLITE_TRANSIENT);
1993
            }
1994
            continue;
×
1995
        }
1996
        if (strcmp(name, ":log_tags") == 0) {
616✔
1997
            const auto& bm = lf->get_bookmark_metadata();
9✔
1998
            auto line_number
1999
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
9✔
2000
            auto bm_iter = bm.find(line_number);
9✔
2001
            if (bm_iter != bm.end() && !bm_iter->second.bm_tags.empty()) {
9✔
2002
                const auto& meta = bm_iter->second;
1✔
2003
                yajlpp_gen gen;
1✔
2004

2005
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
2006

2007
                {
2008
                    yajlpp_array arr(gen);
1✔
2009

2010
                    for (const auto& str : meta.bm_tags) {
2✔
2011
                        arr.gen(str);
1✔
2012
                    }
2013
                }
1✔
2014

2015
                string_fragment sf = gen.to_string_fragment();
1✔
2016

2017
                sqlite3_bind_text(
1✔
2018
                    stmt, lpc + 1, sf.data(), sf.length(), SQLITE_TRANSIENT);
2019
            }
1✔
2020
            continue;
9✔
2021
        }
9✔
2022
        if (strcmp(name, ":log_format") == 0) {
607✔
2023
            const auto format_name = format->get_name();
6✔
2024
            sqlite3_bind_text(stmt,
6✔
2025
                              lpc + 1,
2026
                              format_name.get(),
2027
                              format_name.size(),
6✔
2028
                              SQLITE_STATIC);
2029
            continue;
6✔
2030
        }
6✔
2031
        if (strcmp(name, ":log_format_regex") == 0) {
601✔
2032
            const auto pat_name = format->get_pattern_name(
×
2033
                lffs.lffs_pattern_locks, line_number);
2034
            sqlite3_bind_text(
×
2035
                stmt, lpc + 1, pat_name.get(), pat_name.size(), SQLITE_STATIC);
×
2036
            continue;
×
2037
        }
2038
        if (strcmp(name, ":log_path") == 0) {
601✔
2039
            const auto& filename = lf->get_filename();
×
2040
            sqlite3_bind_text(stmt,
×
2041
                              lpc + 1,
2042
                              filename.c_str(),
2043
                              filename.native().length(),
×
2044
                              SQLITE_STATIC);
2045
            continue;
×
2046
        }
2047
        if (strcmp(name, ":log_unique_path") == 0) {
601✔
2048
            const auto& filename = lf->get_unique_path();
×
2049
            sqlite3_bind_text(stmt,
×
2050
                              lpc + 1,
2051
                              filename.c_str(),
2052
                              filename.native().length(),
×
2053
                              SQLITE_STATIC);
2054
            continue;
×
2055
        }
2056
        if (strcmp(name, ":log_text") == 0) {
601✔
2057
            sqlite3_bind_text(
4✔
2058
                stmt, lpc + 1, sbr.get_data(), sbr.length(), SQLITE_STATIC);
4✔
2059
            continue;
4✔
2060
        }
2061
        if (strcmp(name, ":log_body") == 0) {
597✔
2062
            auto body_attr_opt = get_string_attr(sa, SA_BODY);
16✔
2063
            if (body_attr_opt) {
16✔
2064
                const auto& sar
2065
                    = body_attr_opt.value().saw_string_attr->sa_range;
16✔
2066

2067
                sqlite3_bind_text(stmt,
32✔
2068
                                  lpc + 1,
2069
                                  sbr.get_data_at(sar.lr_start),
16✔
2070
                                  sar.length(),
2071
                                  SQLITE_STATIC);
2072
            } else {
2073
                sqlite3_bind_null(stmt, lpc + 1);
×
2074
            }
2075
            continue;
16✔
2076
        }
16✔
2077
        if (strcmp(name, ":log_opid") == 0) {
581✔
2078
            if (values.lvv_opid_value) {
×
2079
                sqlite3_bind_text(stmt,
×
2080
                                  lpc + 1,
2081
                                  values.lvv_opid_value->c_str(),
2082
                                  values.lvv_opid_value->length(),
×
2083
                                  SQLITE_STATIC);
2084
            } else {
2085
                sqlite3_bind_null(stmt, lpc + 1);
×
2086
            }
2087
            continue;
×
2088
        }
2089
        if (strcmp(name, ":log_raw_text") == 0) {
581✔
2090
            auto res = lf->read_raw_message(ll);
×
2091

2092
            if (res.isOk()) {
×
2093
                raw_sbr = res.unwrap();
×
2094
                sqlite3_bind_text(stmt,
×
2095
                                  lpc + 1,
2096
                                  raw_sbr.get_data(),
2097
                                  raw_sbr.length(),
×
2098
                                  SQLITE_STATIC);
2099
            }
2100
            continue;
×
2101
        }
2102
        for (const auto& lv : values.lvv_values) {
6,782✔
2103
            if (lv.lv_meta.lvm_name != &name[1]) {
6,777✔
2104
                continue;
6,201✔
2105
            }
2106

2107
            switch (lv.lv_meta.lvm_kind) {
576✔
2108
                case value_kind_t::VALUE_BOOLEAN:
×
2109
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
2110
                    break;
×
2111
                case value_kind_t::VALUE_FLOAT:
×
2112
                    sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
2113
                    break;
×
2114
                case value_kind_t::VALUE_INTEGER:
436✔
2115
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
436✔
2116
                    break;
436✔
2117
                case value_kind_t::VALUE_NULL:
×
2118
                    sqlite3_bind_null(stmt, lpc + 1);
×
2119
                    break;
×
2120
                default:
140✔
2121
                    sqlite3_bind_text(stmt,
140✔
2122
                                      lpc + 1,
2123
                                      lv.text_value(),
2124
                                      lv.text_length(),
140✔
2125
                                      SQLITE_TRANSIENT);
2126
                    break;
140✔
2127
            }
2128
            break;
576✔
2129
        }
2130
    }
2131

2132
    auto step_res = sqlite3_step(stmt);
615✔
2133

2134
    sqlite3_reset(stmt);
615✔
2135
    sqlite3_clear_bindings(stmt);
615✔
2136
    switch (step_res) {
615✔
2137
        case SQLITE_OK:
474✔
2138
        case SQLITE_DONE:
2139
            return Ok(false);
948✔
2140
        case SQLITE_ROW:
140✔
2141
            return Ok(true);
280✔
2142
        default:
1✔
2143
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2144
    }
2145
}
615✔
2146

2147
bool
2148
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
14,795✔
2149
{
2150
    auto retval = true;
14,795✔
2151

2152
    if (this->lss_marked_only) {
14,795✔
2153
        auto found_mark = ll->is_marked() || ll->is_expr_marked();
6✔
2154
        auto to_start_ll = ll;
6✔
2155
        while (!found_mark && to_start_ll->is_continued()) {
6✔
2156
            if (to_start_ll->is_marked() || to_start_ll->is_expr_marked()) {
×
2157
                found_mark = true;
×
2158
            }
2159
            --to_start_ll;
×
2160
        }
2161
        auto to_end_ll = std::next(ll);
6✔
2162
        while (!found_mark && to_end_ll != (*ld)->get_file_ptr()->end()
10✔
2163
               && to_end_ll->is_continued())
11✔
2164
        {
2165
            if (to_end_ll->is_marked() || to_end_ll->is_expr_marked()) {
1✔
2166
                found_mark = true;
1✔
2167
            }
2168
            ++to_end_ll;
1✔
2169
        }
2170
        if (!found_mark) {
6✔
2171
            retval = false;
3✔
2172
        }
2173
    }
2174

2175
    if (ll->get_msg_level() < this->tss_min_log_level) {
14,795✔
2176
        this->tss_level_filtered_count += 1;
2✔
2177
        retval = false;
2✔
2178
    }
2179

2180
    if (*ll < this->ttt_min_row_time) {
14,795✔
2181
        retval = false;
36✔
2182
    }
2183

2184
    if (!(*ll <= this->ttt_max_row_time)) {
14,795✔
2185
        retval = false;
4✔
2186
    }
2187

2188
    return retval;
14,795✔
2189
}
2190

2191
void
2192
logfile_sub_source::invalidate_sql_filter()
24✔
2193
{
2194
    for (auto& ld : *this) {
48✔
2195
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
24✔
2196
    }
2197
}
24✔
2198

2199
void
2200
logfile_sub_source::text_mark(const bookmark_type_t* bm,
148✔
2201
                              vis_line_t line,
2202
                              bool added)
2203
{
2204
    if (line >= (int) this->lss_index.size()) {
148✔
2205
        return;
×
2206
    }
2207

2208
    auto cl = this->at(line);
148✔
2209

2210
    if (bm == &textview_curses::BM_USER) {
148✔
2211
        auto* ll = this->find_line(cl);
59✔
2212

2213
        ll->set_mark(added);
59✔
2214
    }
2215
    if (added) {
148✔
2216
        this->lss_user_marks[bm].insert_once(cl);
67✔
2217
    } else {
2218
        this->lss_user_marks[bm].erase(cl);
81✔
2219
    }
2220
    if (bm == &textview_curses::BM_META
148✔
2221
        && this->lss_meta_grepper.gps_proc != nullptr)
16✔
2222
    {
2223
        this->tss_view->search_range(line, line + 1_vl);
1✔
2224
        this->tss_view->search_new_data();
1✔
2225
    }
2226
}
2227

2228
void
2229
logfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
40✔
2230
{
2231
    if (bm == &textview_curses::BM_USER) {
40✔
2232
        for (auto iter = this->lss_user_marks[bm].bv_tree.begin();
6✔
2233
             iter != this->lss_user_marks[bm].bv_tree.end();
6✔
2234
             ++iter)
×
2235
        {
2236
            this->find_line(*iter)->set_mark(false);
×
2237
        }
2238
    }
2239
    this->lss_user_marks[bm].clear();
40✔
2240
}
40✔
2241

2242
void
2243
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
519✔
2244
{
2245
    auto iter = std::find_if(
519✔
2246
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
1,038✔
2247
    if (iter != this->lss_files.end()) {
519✔
2248
        int file_index = iter - this->lss_files.begin();
519✔
2249

2250
        (*iter)->clear();
519✔
2251
        for (auto& bv : this->lss_user_marks) {
4,671✔
2252
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
4,152✔
2253
            auto mark_end
2254
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
4,152✔
2255
            auto file_range = bv.equal_range(mark_curr, mark_end);
4,152✔
2256

2257
            if (file_range.first != file_range.second) {
4,152✔
2258
                auto to_del = std::vector<content_line_t>{};
69✔
2259
                for (auto file_iter = file_range.first;
69✔
2260
                     file_iter != file_range.second;
178✔
2261
                     ++file_iter)
109✔
2262
                {
2263
                    to_del.emplace_back(*file_iter);
109✔
2264
                }
2265

2266
                for (auto cl : to_del) {
178✔
2267
                    bv.erase(cl);
109✔
2268
                }
2269
            }
69✔
2270
        }
2271

2272
        this->lss_force_rebuild = true;
519✔
2273
    }
2274
    while (!this->lss_files.empty()) {
1,038✔
2275
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
570✔
2276
            this->lss_files.pop_back();
519✔
2277
        } else {
2278
            break;
51✔
2279
        }
2280
    }
2281
    this->lss_token_file = nullptr;
519✔
2282
}
519✔
2283

2284
std::optional<vis_line_t>
2285
logfile_sub_source::find_from_content(content_line_t cl)
5✔
2286
{
2287
    content_line_t line = cl;
5✔
2288
    std::shared_ptr<logfile> lf = this->find(line);
5✔
2289

2290
    if (lf != nullptr) {
5✔
2291
        auto ll_iter = lf->begin() + line;
5✔
2292
        auto& ll = *ll_iter;
5✔
2293
        auto vis_start_opt = this->find_from_time(ll.get_timeval());
5✔
2294

2295
        if (!vis_start_opt) {
5✔
2296
            return std::nullopt;
×
2297
        }
2298

2299
        auto vis_start = *vis_start_opt;
5✔
2300

2301
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2302
            content_line_t guess_cl = this->at(vis_start);
5✔
2303

2304
            if (cl == guess_cl) {
5✔
2305
                return vis_start;
5✔
2306
            }
2307

2308
            auto guess_line = this->find_line(guess_cl);
×
2309

2310
            if (!guess_line || ll < *guess_line) {
×
2311
                return std::nullopt;
×
2312
            }
2313

2314
            ++vis_start;
×
2315
        }
2316
    }
2317

2318
    return std::nullopt;
×
2319
}
5✔
2320

2321
void
2322
logfile_sub_source::reload_index_delegate()
642✔
2323
{
2324
    if (this->lss_index_delegate == nullptr) {
642✔
2325
        return;
×
2326
    }
2327

2328
    this->lss_index_delegate->index_start(*this);
642✔
2329
    for (const auto index : this->lss_filtered_index) {
670✔
2330
        auto cl = this->lss_index[index].value();
28✔
2331
        uint64_t line_number;
2332
        auto ld = this->find_data(cl, line_number);
28✔
2333
        std::shared_ptr<logfile> lf = (*ld)->get_file();
28✔
2334

2335
        this->lss_index_delegate->index_line(
28✔
2336
            *this, lf.get(), lf->begin() + line_number);
28✔
2337
    }
28✔
2338
    this->lss_index_delegate->index_complete(*this);
642✔
2339
}
2340

2341
std::optional<std::shared_ptr<text_filter>>
2342
logfile_sub_source::get_sql_filter()
2,590✔
2343
{
2344
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
2,590✔
2345
               return filt->get_index() == 0
268✔
2346
                   && dynamic_cast<sql_filter*>(filt.get()) != nullptr;
268✔
2347
           })
2348
        | lnav::itertools::deref();
5,180✔
2349
}
2350

2351
void
2352
log_location_history::loc_history_append(vis_line_t top)
96✔
2353
{
2354
    if (top < 0_vl || top >= vis_line_t(this->llh_log_source.text_line_count()))
96✔
2355
    {
2356
        return;
1✔
2357
    }
2358

2359
    auto cl = this->llh_log_source.at(top);
95✔
2360

2361
    auto iter = this->llh_history.begin();
95✔
2362
    iter += this->llh_history.size() - this->lh_history_position;
95✔
2363
    this->llh_history.erase_from(iter);
95✔
2364
    this->lh_history_position = 0;
95✔
2365
    this->llh_history.push_back(cl);
95✔
2366
}
2367

2368
std::optional<vis_line_t>
2369
log_location_history::loc_history_back(vis_line_t current_top)
2✔
2370
{
2371
    while (this->lh_history_position < this->llh_history.size()) {
2✔
2372
        auto iter = this->llh_history.rbegin();
2✔
2373

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

2376
        if (this->lh_history_position == 0 && vis_for_pos != current_top) {
2✔
2377
            return vis_for_pos;
2✔
2378
        }
2379

2380
        if ((this->lh_history_position + 1) >= this->llh_history.size()) {
2✔
2381
            break;
×
2382
        }
2383

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

2386
        iter += this->lh_history_position;
2✔
2387

2388
        vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2389

2390
        if (vis_for_pos) {
2✔
2391
            return vis_for_pos;
2✔
2392
        }
2393
    }
2394

2395
    return std::nullopt;
×
2396
}
2397

2398
std::optional<vis_line_t>
2399
log_location_history::loc_history_forward(vis_line_t current_top)
1✔
2400
{
2401
    while (this->lh_history_position > 0) {
1✔
2402
        this->lh_history_position -= 1;
1✔
2403

2404
        auto iter = this->llh_history.rbegin();
1✔
2405

2406
        iter += this->lh_history_position;
1✔
2407

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

2410
        if (vis_for_pos) {
1✔
2411
            return vis_for_pos;
1✔
2412
        }
2413
    }
2414

2415
    return std::nullopt;
×
2416
}
2417

2418
bool
2419
sql_filter::matches(std::optional<line_source> ls_opt,
124✔
2420
                    const shared_buffer_ref& line)
2421
{
2422
    if (!ls_opt) {
124✔
2423
        return false;
×
2424
    }
2425

2426
    auto ls = ls_opt;
124✔
2427

2428
    if (!ls->ls_line->is_message()) {
124✔
2429
        return false;
3✔
2430
    }
2431
    if (this->sf_filter_stmt == nullptr) {
121✔
2432
        return false;
×
2433
    }
2434

2435
    auto lfp = ls->ls_file.shared_from_this();
121✔
2436
    auto ld = this->sf_log_source.find_data_i(lfp);
121✔
2437
    if (ld == this->sf_log_source.end()) {
121✔
2438
        return false;
×
2439
    }
2440

2441
    auto eval_res = this->sf_log_source.eval_sql_filter(
121✔
2442
        this->sf_filter_stmt, ld, ls->ls_line);
121✔
2443
    if (eval_res.unwrapOr(true)) {
121✔
2444
        return false;
74✔
2445
    }
2446

2447
    return true;
47✔
2448
}
121✔
2449

2450
std::string
2451
sql_filter::to_command() const
×
2452
{
2453
    return fmt::format(FMT_STRING("filter-expr {}"), this->lf_id);
×
2454
}
2455

2456
std::optional<line_info>
2457
logfile_sub_source::meta_grepper::grep_value_for_line(vis_line_t line,
×
2458
                                                      std::string& value_out)
2459
{
2460
    auto line_meta_opt = this->lmg_source.find_bookmark_metadata(line);
×
2461
    if (!line_meta_opt) {
×
2462
        value_out.clear();
×
2463
    } else {
2464
        auto& bm = *(line_meta_opt.value());
×
2465

2466
        {
2467
            md2attr_line mdal;
×
2468

2469
            auto parse_res = md4cpp::parse(bm.bm_comment, mdal);
×
2470
            if (parse_res.isOk()) {
×
2471
                value_out.append(parse_res.unwrap().get_string());
×
2472
            } else {
2473
                value_out.append(bm.bm_comment);
×
2474
            }
2475
        }
2476

2477
        value_out.append("\x1c");
×
2478
        for (const auto& tag : bm.bm_tags) {
×
2479
            value_out.append(tag);
×
2480
            value_out.append("\x1c");
×
2481
        }
2482
        value_out.append("\x1c");
×
2483
        for (const auto& pair : bm.bm_annotations.la_pairs) {
×
2484
            value_out.append(pair.first);
×
2485
            value_out.append("\x1c");
×
2486

2487
            md2attr_line mdal;
×
2488

2489
            auto parse_res = md4cpp::parse(pair.second, mdal);
×
2490
            if (parse_res.isOk()) {
×
2491
                value_out.append(parse_res.unwrap().get_string());
×
2492
            } else {
2493
                value_out.append(pair.second);
×
2494
            }
2495
            value_out.append("\x1c");
×
2496
        }
2497
        value_out.append("\x1c");
×
2498
        value_out.append(bm.bm_opid);
×
2499
    }
2500

2501
    if (!this->lmg_done) {
×
2502
        return line_info{};
×
2503
    }
2504

2505
    return std::nullopt;
×
2506
}
2507

2508
vis_line_t
2509
logfile_sub_source::meta_grepper::grep_initial_line(vis_line_t start,
×
2510
                                                    vis_line_t highest)
2511
{
2512
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2513
    auto& bv = bm[&textview_curses::BM_META];
×
2514

2515
    if (bv.empty()) {
×
2516
        return -1_vl;
×
2517
    }
2518
    return *bv.bv_tree.begin();
×
2519
}
2520

2521
void
2522
logfile_sub_source::meta_grepper::grep_next_line(vis_line_t& line)
×
2523
{
2524
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2525
    auto& bv = bm[&textview_curses::BM_META];
×
2526

2527
    auto line_opt = bv.next(vis_line_t(line));
×
2528
    if (!line_opt) {
×
2529
        this->lmg_done = true;
×
2530
    }
2531
    line = line_opt.value_or(-1_vl);
×
2532
}
2533

2534
void
2535
logfile_sub_source::meta_grepper::grep_begin(grep_proc<vis_line_t>& gp,
23✔
2536
                                             vis_line_t start,
2537
                                             vis_line_t stop)
2538
{
2539
    this->lmg_source.quiesce();
23✔
2540

2541
    this->lmg_source.tss_view->grep_begin(gp, start, stop);
23✔
2542
}
23✔
2543

2544
void
2545
logfile_sub_source::meta_grepper::grep_end(grep_proc<vis_line_t>& gp)
23✔
2546
{
2547
    this->lmg_source.tss_view->grep_end(gp);
23✔
2548
}
23✔
2549

2550
void
2551
logfile_sub_source::meta_grepper::grep_match(grep_proc<vis_line_t>& gp,
3✔
2552
                                             vis_line_t line)
2553
{
2554
    this->lmg_source.tss_view->grep_match(gp, line);
3✔
2555
}
3✔
2556

2557
static std::vector<breadcrumb::possibility>
2558
timestamp_poss()
11✔
2559
{
2560
    const static std::vector<breadcrumb::possibility> retval = {
2561
        breadcrumb::possibility{"-1 day"},
2562
        breadcrumb::possibility{"-1h"},
2563
        breadcrumb::possibility{"-30m"},
2564
        breadcrumb::possibility{"-15m"},
2565
        breadcrumb::possibility{"-5m"},
2566
        breadcrumb::possibility{"-1m"},
2567
        breadcrumb::possibility{"+1m"},
2568
        breadcrumb::possibility{"+5m"},
2569
        breadcrumb::possibility{"+15m"},
2570
        breadcrumb::possibility{"+30m"},
2571
        breadcrumb::possibility{"+1h"},
2572
        breadcrumb::possibility{"+1 day"},
2573
    };
95✔
2574

2575
    return retval;
11✔
2576
}
24✔
2577

2578
static attr_line_t
2579
to_display(const std::shared_ptr<logfile>& lf)
29✔
2580
{
2581
    attr_line_t retval;
29✔
2582

2583
    if (lf->get_open_options().loo_piper) {
29✔
2584
        if (!lf->get_open_options().loo_piper->is_finished()) {
×
2585
            retval.append("\u21bb "_list_glyph);
×
2586
        }
2587
    }
2588
    retval.append(lf->get_unique_path());
29✔
2589

2590
    return retval;
29✔
2591
}
×
2592

2593
void
2594
logfile_sub_source::text_crumbs_for_line(int line,
27✔
2595
                                         std::vector<breadcrumb::crumb>& crumbs)
2596
{
2597
    text_sub_source::text_crumbs_for_line(line, crumbs);
27✔
2598

2599
    if (this->lss_filtered_index.empty()) {
27✔
2600
        return;
9✔
2601
    }
2602

2603
    auto vl = vis_line_t(line);
18✔
2604
    auto bmc = this->get_bookmark_metadata_context(
18✔
2605
        vl, bookmark_metadata::categories::partition);
2606
    if (bmc.bmc_current_metadata) {
18✔
2607
        const auto& name = bmc.bmc_current_metadata.value()->bm_name;
1✔
2608
        auto key = text_anchors::to_anchor_string(name);
1✔
2609
        auto display = attr_line_t()
1✔
2610
                           .append("\u2291 "_symbol)
1✔
2611
                           .append(lnav::roles::variable(name))
2✔
2612
                           .move();
1✔
2613
        crumbs.emplace_back(
1✔
2614
            key,
2615
            display,
2616
            [this]() -> std::vector<breadcrumb::possibility> {
×
2617
                auto& vb = this->tss_view->get_bookmarks();
1✔
2618
                const auto& bv = vb[&textview_curses::BM_PARTITION];
1✔
2619
                std::vector<breadcrumb::possibility> retval;
1✔
2620

2621
                for (const auto& vl : bv.bv_tree) {
2✔
2622
                    auto meta_opt = this->find_bookmark_metadata(vl);
1✔
2623
                    if (!meta_opt || meta_opt.value()->bm_name.empty()) {
1✔
2624
                        continue;
×
2625
                    }
2626

2627
                    const auto& name = meta_opt.value()->bm_name;
1✔
2628
                    retval.emplace_back(text_anchors::to_anchor_string(name),
1✔
2629
                                        name);
2630
                }
2631

2632
                return retval;
1✔
2633
            },
×
2634
            [ec = this->lss_exec_context](const auto& part) {
1✔
2635
                auto cmd = fmt::format(FMT_STRING(":goto {}"),
×
2636
                                       part.template get<std::string>());
2637
                ec->execute(INTERNAL_SRC_LOC, cmd);
×
2638
            });
×
2639
    }
1✔
2640

2641
    auto line_pair_opt = this->find_line_with_file(vl);
18✔
2642
    if (!line_pair_opt) {
18✔
2643
        return;
×
2644
    }
2645
    auto line_pair = line_pair_opt.value();
18✔
2646
    auto& lf = line_pair.first;
18✔
2647
    auto format = lf->get_format();
18✔
2648
    char ts[64];
2649

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

2652
    crumbs.emplace_back(std::string(ts),
18✔
2653
                        timestamp_poss,
2654
                        [ec = this->lss_exec_context](const auto& ts) {
18✔
2655
                            auto cmd
×
2656
                                = fmt::format(FMT_STRING(":goto {}"),
×
2657
                                              ts.template get<std::string>());
2658
                            ec->execute(INTERNAL_SRC_LOC, cmd);
×
2659
                        });
×
2660
    crumbs.back().c_expected_input
18✔
2661
        = breadcrumb::crumb::expected_input_t::anything;
18✔
2662
    crumbs.back().c_search_placeholder = "(Enter an absolute or relative time)";
18✔
2663

2664
    auto format_name = format->get_name().to_string();
18✔
2665

2666
    crumbs.emplace_back(
18✔
2667
        format_name,
2668
        attr_line_t().append(format_name),
18✔
2669
        [this]() -> std::vector<breadcrumb::possibility> {
×
2670
            return this->lss_files
11✔
2671
                | lnav::itertools::filter_in([](const auto& file_data) {
22✔
2672
                       return file_data->is_visible();
11✔
2673
                   })
2674
                | lnav::itertools::map(&logfile_data::get_file_ptr)
33✔
2675
                | lnav::itertools::map(&logfile::get_format_name)
33✔
2676
                | lnav::itertools::unique()
33✔
2677
                | lnav::itertools::map([](const auto& elem) {
44✔
2678
                       return breadcrumb::possibility{
2679
                           elem.to_string(),
2680
                       };
11✔
2681
                   })
2682
                | lnav::itertools::to_vector();
33✔
2683
        },
2684
        [ec = this->lss_exec_context](const auto& format_name) {
18✔
2685
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2686
     SET selection = ifnull(
2687
         (SELECT log_line FROM all_logs WHERE log_format = $format_name LIMIT 1),
2688
         (SELECT raise_error(
2689
            'Could not find format: ' || $format_name,
2690
            'The corresponding log messages might have been filtered out'))
2691
       )
2692
     WHERE name = 'log'
2693
)";
2694

2695
            ec->execute_with(
×
2696
                INTERNAL_SRC_LOC,
×
2697
                MOVE_STMT,
2698
                std::make_pair("format_name",
2699
                               format_name.template get<std::string>()));
2700
        });
×
2701

2702
    auto msg_start_iter = lf->message_start(line_pair.second);
18✔
2703
    auto file_line_number = std::distance(lf->begin(), msg_start_iter);
18✔
2704
    crumbs.emplace_back(
36✔
2705
        lf->get_unique_path(),
18✔
2706
        to_display(lf).appendf(FMT_STRING("[{:L}]"), file_line_number),
72✔
2707
        [this]() -> std::vector<breadcrumb::possibility> {
×
2708
            return this->lss_files
11✔
2709
                | lnav::itertools::filter_in([](const auto& file_data) {
22✔
2710
                       return file_data->is_visible();
11✔
2711
                   })
2712
                | lnav::itertools::map([](const auto& file_data) {
22✔
2713
                       return breadcrumb::possibility{
2714
                           file_data->get_file_ptr()->get_unique_path(),
11✔
2715
                           to_display(file_data->get_file()),
2716
                       };
11✔
2717
                   });
22✔
2718
        },
2719
        [ec = this->lss_exec_context](const auto& uniq_path) {
18✔
2720
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2721
     SET selection = ifnull(
2722
          (SELECT log_line FROM all_logs WHERE log_unique_path = $uniq_path LIMIT 1),
2723
          (SELECT raise_error(
2724
            'Could not find file: ' || $uniq_path,
2725
            'The corresponding log messages might have been filtered out'))
2726
         )
2727
     WHERE name = 'log'
2728
)";
2729

2730
            ec->execute_with(
×
2731
                INTERNAL_SRC_LOC,
×
2732
                MOVE_STMT,
2733
                std::make_pair("uniq_path",
2734
                               uniq_path.template get<std::string>()));
2735
        });
×
2736

2737
    shared_buffer sb;
18✔
2738
    logline_value_vector values;
18✔
2739
    auto& sbr = values.lvv_sbr;
18✔
2740

2741
    lf->read_full_message(msg_start_iter, sbr);
18✔
2742
    attr_line_t al(to_string(sbr));
18✔
2743
    if (!sbr.get_metadata().m_valid_utf) {
18✔
NEW
2744
        scrub_to_utf8(al.al_string.data(), al.al_string.length());
×
2745
    }
2746
    if (sbr.get_metadata().m_has_ansi) {
18✔
2747
        // bleh
2748
        scrub_ansi_string(al.get_string(), &al.al_attrs);
×
2749
        sbr.share(sb, al.al_string.data(), al.al_string.size());
×
2750
    }
2751
    format->annotate(lf.get(), file_line_number, al.get_attrs(), values);
18✔
2752

2753
    {
2754
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
30✔
2755
          SET selection = ifnull(
2756
            (SELECT log_line FROM all_logs WHERE log_thread_id = $tid LIMIT 1),
2757
            (SELECT raise_error('Could not find thread ID: ' || $tid,
2758
                                'The corresponding log messages might have been filtered out')))
2759
          WHERE name = 'log'
2760
        )";
2761
        static const std::string ELLIPSIS = "\u22ef";
30✔
2762

2763
        auto tid_display = values.lvv_thread_id_value.has_value()
18✔
2764
            ? lnav::roles::identifier(values.lvv_thread_id_value.value())
1✔
2765
            : lnav::roles::hidden(ELLIPSIS);
19✔
2766
        crumbs.emplace_back(
18✔
2767
            values.lvv_thread_id_value.has_value()
18✔
2768
                ? values.lvv_thread_id_value.value()
53✔
2769
                : "",
2770
            attr_line_t()
18✔
2771
                .append(ui_icon_t::thread)
18✔
2772
                .append(" ")
18✔
2773
                .append(tid_display),
2774
            [this]() -> std::vector<breadcrumb::possibility> {
×
2775
                std::set<std::string> poss_strs;
11✔
2776

2777
                for (const auto& file_data : this->lss_files) {
22✔
2778
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2779
                        continue;
×
2780
                    }
2781
                    safe::ReadAccess<logfile::safe_thread_id_state> r_tid_map(
2782
                        file_data->get_file_ptr()->get_thread_ids());
11✔
2783

2784
                    for (const auto& pair : r_tid_map->ltis_tid_ranges) {
24✔
2785
                        poss_strs.emplace(pair.first.to_string());
13✔
2786
                    }
2787
                }
11✔
2788

2789
                std::vector<breadcrumb::possibility> retval;
11✔
2790

2791
                std::transform(poss_strs.begin(),
11✔
2792
                               poss_strs.end(),
2793
                               std::back_inserter(retval),
2794
                               [](const auto& tid_str) {
13✔
2795
                                   return breadcrumb::possibility(tid_str);
13✔
2796
                               });
2797

2798
                return retval;
22✔
2799
            },
11✔
2800
            [ec = this->lss_exec_context](const auto& tid) {
18✔
2801
                ec->execute_with(
×
2802
                    INTERNAL_SRC_LOC,
×
2803
                    MOVE_STMT,
2804
                    std::make_pair("tid", tid.template get<std::string>()));
2805
            });
×
2806
    }
18✔
2807

2808
    {
2809
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
30✔
2810
          SET selection = ifnull(
2811
            (SELECT log_line FROM all_logs WHERE log_opid = $opid LIMIT 1),
2812
            (SELECT raise_error('Could not find opid: ' || $opid,
2813
                                'The corresponding log messages might have been filtered out')))
2814
          WHERE name = 'log'
2815
        )";
2816
        static const std::string ELLIPSIS = "\u22ef";
30✔
2817

2818
        auto opid_display = values.lvv_opid_value.has_value()
18✔
2819
            ? lnav::roles::identifier(values.lvv_opid_value.value())
9✔
2820
            : lnav::roles::hidden(ELLIPSIS);
27✔
2821
        crumbs.emplace_back(
36✔
2822
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
45✔
2823
                                              : "",
2824
            attr_line_t().append(opid_display),
18✔
2825
            [this]() -> std::vector<breadcrumb::possibility> {
×
2826
                std::unordered_set<std::string> poss_strs;
11✔
2827

2828
                for (const auto& file_data : this->lss_files) {
22✔
2829
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2830
                        continue;
×
2831
                    }
2832
                    safe::ReadAccess<logfile::safe_opid_state> r_opid_map(
2833
                        file_data->get_file_ptr()->get_opids());
11✔
2834

2835
                    poss_strs.reserve(poss_strs.size()
22✔
2836
                                      + r_opid_map->los_opid_ranges.size());
11✔
2837
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
783✔
2838
                        poss_strs.insert(pair.first.to_string());
772✔
2839
                    }
2840
                }
11✔
2841

2842
                std::vector<breadcrumb::possibility> retval;
11✔
2843
                retval.reserve(poss_strs.size());
11✔
2844

2845
                std::transform(poss_strs.begin(),
11✔
2846
                               poss_strs.end(),
2847
                               std::back_inserter(retval),
2848
                               [](const auto& opid_str) {
772✔
2849
                                   return breadcrumb::possibility(opid_str);
772✔
2850
                               });
2851

2852
                return retval;
22✔
2853
            },
11✔
2854
            [ec = this->lss_exec_context](const auto& opid) {
18✔
2855
                ec->execute_with(
×
2856
                    INTERNAL_SRC_LOC,
×
2857
                    MOVE_STMT,
2858
                    std::make_pair("opid", opid.template get<std::string>()));
2859
            });
×
2860
    }
18✔
2861

2862
    auto sf = string_fragment::from_str(al.get_string());
18✔
2863
    auto body_opt = get_string_attr(al.get_attrs(), SA_BODY);
18✔
2864
    auto nl_pos_opt = sf.find('\n');
18✔
2865
    auto msg_line_number = std::distance(msg_start_iter, line_pair.second);
18✔
2866
    auto line_from_top = line - msg_line_number;
18✔
2867
    if (body_opt && nl_pos_opt) {
18✔
2868
        if (this->lss_token_meta_line != file_line_number
18✔
2869
            || this->lss_token_meta_size != sf.length())
9✔
2870
        {
2871
            if (body_opt->saw_string_attr->sa_range.length() < 128 * 1024) {
3✔
2872
                this->lss_token_meta
2873
                    = lnav::document::discover(al)
3✔
2874
                          .over_range(
3✔
2875
                              body_opt.value().saw_string_attr->sa_range)
3✔
2876
                          .perform();
3✔
2877
                // XXX discover_structure() changes `al`, have to recompute
2878
                // stuff
2879
                sf = al.to_string_fragment();
3✔
2880
                body_opt = get_string_attr(al.get_attrs(), SA_BODY);
3✔
2881
            } else {
2882
                this->lss_token_meta = lnav::document::metadata{};
×
2883
            }
2884
            this->lss_token_meta_line = file_line_number;
3✔
2885
            this->lss_token_meta_size = sf.length();
3✔
2886
        }
2887

2888
        const auto initial_size = crumbs.size();
9✔
2889
        auto sf_body
2890
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
9✔
2891
                           body_opt->saw_string_attr->sa_range.lr_end);
9✔
2892
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
9✔
2893
        file_off_t line_end_offset = sf.length();
9✔
2894
        ssize_t line_number = 0;
9✔
2895

2896
        for (const auto& sf_line : sf_body.split_lines()) {
18✔
2897
            if (line_number >= msg_line_number) {
12✔
2898
                line_end_offset = line_offset + sf_line.length();
3✔
2899
                break;
3✔
2900
            }
2901
            line_number += 1;
9✔
2902
            line_offset += sf_line.length();
9✔
2903
        }
9✔
2904

2905
        this->lss_token_meta.m_sections_tree.visit_overlapping(
9✔
2906
            line_offset,
2907
            line_end_offset,
2908
            [this,
2✔
2909
             initial_size,
2910
             meta = &this->lss_token_meta,
9✔
2911
             &crumbs,
2912
             line_from_top](const auto& iv) {
2913
                auto path = crumbs | lnav::itertools::skip(initial_size)
6✔
2914
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
4✔
2915
                    | lnav::itertools::append(iv.value);
2✔
2916
                auto curr_node = lnav::document::hier_node::lookup_path(
2✔
2917
                    meta->m_sections_root.get(), path);
2✔
2918

2919
                crumbs.emplace_back(
4✔
2920
                    iv.value,
2✔
2921
                    [meta, path]() { return meta->possibility_provider(path); },
4✔
2922
                    [this, curr_node, path, line_from_top](const auto& key) {
4✔
2923
                        if (!curr_node) {
×
2924
                            return;
×
2925
                        }
2926
                        auto* parent_node = curr_node.value()->hn_parent;
×
2927
                        if (parent_node == nullptr) {
×
2928
                            return;
×
2929
                        }
2930
                        key.match(
2931
                            [parent_node](const std::string& str) {
×
2932
                                return parent_node->find_line_number(str);
×
2933
                            },
2934
                            [parent_node](size_t index) {
×
2935
                                return parent_node->find_line_number(index);
×
2936
                            })
2937
                            | [this, line_from_top](auto line_number) {
×
2938
                                  this->tss_view->set_selection(
×
2939
                                      vis_line_t(line_from_top + line_number));
×
2940
                              };
2941
                    });
2942
                if (curr_node
2✔
2943
                    && !curr_node.value()->hn_parent->is_named_only()) {
2✔
2944
                    auto node = lnav::document::hier_node::lookup_path(
×
2945
                        meta->m_sections_root.get(), path);
×
2946

2947
                    crumbs.back().c_expected_input
×
2948
                        = curr_node.value()
×
2949
                              ->hn_parent->hn_named_children.empty()
×
2950
                        ? breadcrumb::crumb::expected_input_t::index
×
2951
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
2952
                    crumbs.back().with_possible_range(
×
2953
                        node | lnav::itertools::map([](const auto hn) {
×
2954
                            return hn->hn_parent->hn_children.size();
×
2955
                        })
2956
                        | lnav::itertools::unwrap_or(size_t{0}));
×
2957
                }
2958
            });
2✔
2959

2960
        auto path = crumbs | lnav::itertools::skip(initial_size)
18✔
2961
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
18✔
2962
        auto node = lnav::document::hier_node::lookup_path(
9✔
2963
            this->lss_token_meta.m_sections_root.get(), path);
9✔
2964

2965
        if (node && !node.value()->hn_children.empty()) {
9✔
2966
            auto poss_provider = [curr_node = node.value()]() {
1✔
2967
                std::vector<breadcrumb::possibility> retval;
1✔
2968
                for (const auto& child : curr_node->hn_named_children) {
1✔
2969
                    retval.emplace_back(child.first);
×
2970
                }
2971
                return retval;
1✔
2972
            };
2973
            auto path_performer
2974
                = [this, curr_node = node.value(), line_from_top](
2✔
2975
                      const breadcrumb::crumb::key_t& value) {
2976
                      value.match(
×
2977
                          [curr_node](const std::string& str) {
×
2978
                              return curr_node->find_line_number(str);
×
2979
                          },
2980
                          [curr_node](size_t index) {
×
2981
                              return curr_node->find_line_number(index);
×
2982
                          })
2983
                          | [this, line_from_top](size_t line_number) {
×
2984
                                this->tss_view->set_selection(
×
2985
                                    vis_line_t(line_from_top + line_number));
×
2986
                            };
2987
                  };
1✔
2988
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
1✔
2989
            crumbs.back().c_expected_input
1✔
2990
                = node.value()->hn_named_children.empty()
2✔
2991
                ? breadcrumb::crumb::expected_input_t::index
1✔
2992
                : breadcrumb::crumb::expected_input_t::index_or_exact;
2993
        }
2994
    }
9✔
2995
}
18✔
2996

2997
void
2998
logfile_sub_source::quiesce()
42✔
2999
{
3000
    for (auto& ld : this->lss_files) {
84✔
3001
        auto* lf = ld->get_file_ptr();
42✔
3002

3003
        if (lf == nullptr) {
42✔
3004
            continue;
×
3005
        }
3006

3007
        lf->quiesce();
42✔
3008
    }
3009
}
42✔
3010

3011
bookmark_metadata&
3012
logfile_sub_source::get_bookmark_metadata(content_line_t cl)
25✔
3013
{
3014
    auto line_pair = this->find_line_with_file(cl).value();
25✔
3015
    auto line_number = static_cast<uint32_t>(
3016
        std::distance(line_pair.first->begin(), line_pair.second));
25✔
3017

3018
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
3019
}
25✔
3020

3021
logfile_sub_source::bookmark_metadata_context
3022
logfile_sub_source::get_bookmark_metadata_context(
4,488✔
3023
    vis_line_t vl, bookmark_metadata::categories desired) const
3024
{
3025
    const auto& vb = this->tss_view->get_bookmarks();
4,488✔
3026
    const auto& bv = vb[desired == bookmark_metadata::categories::partition
3027
                            ? &textview_curses::BM_PARTITION
3028
                            : &textview_curses::BM_META];
4,488✔
3029
    auto vl_iter = bv.bv_tree.lower_bound(vl + 1_vl);
4,488✔
3030

3031
    std::optional<vis_line_t> next_line;
4,488✔
3032
    for (auto next_vl_iter = vl_iter; next_vl_iter != bv.bv_tree.end();
4,488✔
3033
         ++next_vl_iter)
×
3034
    {
3035
        auto bm_opt = this->find_bookmark_metadata(*next_vl_iter);
2✔
3036
        if (!bm_opt) {
2✔
3037
            continue;
×
3038
        }
3039

3040
        if (bm_opt.value()->has(desired)) {
2✔
3041
            next_line = *next_vl_iter;
2✔
3042
            break;
2✔
3043
        }
3044
    }
3045
    if (vl_iter == bv.bv_tree.begin()) {
4,488✔
3046
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
4,472✔
3047
    }
3048

3049
    --vl_iter;
16✔
3050
    while (true) {
3051
        auto bm_opt = this->find_bookmark_metadata(*vl_iter);
16✔
3052
        if (bm_opt) {
16✔
3053
            if (bm_opt.value()->has(desired)) {
13✔
3054
                return bookmark_metadata_context{
3055
                    *vl_iter, bm_opt.value(), next_line};
16✔
3056
            }
3057
        }
3058

3059
        if (vl_iter == bv.bv_tree.begin()) {
3✔
3060
            return bookmark_metadata_context{
3061
                std::nullopt, std::nullopt, next_line};
3✔
3062
        }
3063
        --vl_iter;
×
3064
    }
3065
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3066
}
3067

3068
std::optional<bookmark_metadata*>
3069
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
23,700✔
3070
{
3071
    auto line_pair = this->find_line_with_file(cl).value();
23,700✔
3072
    auto line_number = static_cast<uint32_t>(
3073
        std::distance(line_pair.first->begin(), line_pair.second));
23,700✔
3074

3075
    auto& bm = line_pair.first->get_bookmark_metadata();
23,700✔
3076
    auto bm_iter = bm.find(line_number);
23,700✔
3077
    if (bm_iter == bm.end()) {
23,700✔
3078
        return std::nullopt;
23,344✔
3079
    }
3080

3081
    return &bm_iter->second;
356✔
3082
}
23,700✔
3083

3084
void
3085
logfile_sub_source::erase_bookmark_metadata(content_line_t cl)
26✔
3086
{
3087
    auto line_pair = this->find_line_with_file(cl).value();
26✔
3088
    auto line_number = static_cast<uint32_t>(
3089
        std::distance(line_pair.first->begin(), line_pair.second));
26✔
3090

3091
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
3092
    auto bm_iter = bm.find(line_number);
26✔
3093
    if (bm_iter != bm.end()) {
26✔
3094
        bm.erase(bm_iter);
6✔
3095
    }
3096
}
26✔
3097

3098
void
3099
logfile_sub_source::clear_bookmark_metadata()
6✔
3100
{
3101
    for (auto& ld : *this) {
14✔
3102
        if (ld->get_file_ptr() == nullptr) {
8✔
3103
            continue;
×
3104
        }
3105

3106
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3107
    }
3108
}
6✔
3109

3110
void
3111
logfile_sub_source::increase_line_context()
×
3112
{
3113
    auto old_context = this->lss_line_context;
×
3114

3115
    switch (this->lss_line_context) {
×
3116
        case line_context_t::filename:
×
3117
            // nothing to do
3118
            break;
×
3119
        case line_context_t::basename:
×
3120
            this->lss_line_context = line_context_t::filename;
×
3121
            break;
×
3122
        case line_context_t::none:
×
3123
            this->lss_line_context = line_context_t::basename;
×
3124
            break;
×
3125
        case line_context_t::time_column:
×
3126
            this->lss_line_context = line_context_t::none;
×
3127
            break;
×
3128
    }
3129
    if (old_context != this->lss_line_context) {
×
3130
        this->clear_line_size_cache();
×
3131
    }
3132
}
3133

3134
bool
3135
logfile_sub_source::decrease_line_context()
×
3136
{
3137
    static const auto& cfg
3138
        = injector::get<const logfile_sub_source_ns::config&>();
×
3139
    auto old_context = this->lss_line_context;
×
3140

3141
    switch (this->lss_line_context) {
×
3142
        case line_context_t::filename:
×
3143
            this->lss_line_context = line_context_t::basename;
×
3144
            break;
×
3145
        case line_context_t::basename:
×
3146
            this->lss_line_context = line_context_t::none;
×
3147
            break;
×
3148
        case line_context_t::none:
×
3149
            if (cfg.c_time_column
×
3150
                != logfile_sub_source_ns::time_column_feature_t::Disabled)
3151
            {
3152
                this->lss_line_context = line_context_t::time_column;
×
3153
            }
3154
            break;
×
3155
        case line_context_t::time_column:
×
3156
            break;
×
3157
    }
3158
    if (old_context != this->lss_line_context) {
×
3159
        this->clear_line_size_cache();
×
3160

3161
        return true;
×
3162
    }
3163

3164
    return false;
×
3165
}
3166

3167
size_t
3168
logfile_sub_source::get_filename_offset() const
222✔
3169
{
3170
    switch (this->lss_line_context) {
222✔
3171
        case line_context_t::filename:
×
3172
            return this->lss_filename_width;
×
3173
        case line_context_t::basename:
×
3174
            return this->lss_basename_width;
×
3175
        default:
222✔
3176
            return 0;
222✔
3177
    }
3178
}
3179

3180
size_t
3181
logfile_sub_source::file_count() const
4,021✔
3182
{
3183
    size_t retval = 0;
4,021✔
3184
    const_iterator iter;
4,021✔
3185

3186
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,644✔
3187
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,623✔
3188
            retval += 1;
3,617✔
3189
        }
3190
    }
3191

3192
    return retval;
4,021✔
3193
}
3194

3195
size_t
3196
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3197
                                       int row,
3198
                                       text_sub_source::line_flags_t flags)
3199
{
3200
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3201

3202
    if (this->lss_line_size_cache[index].first != row) {
×
3203
        std::string value;
×
3204

3205
        this->text_value_for_line(tc, row, value, flags);
×
3206
        scrub_ansi_string(value, nullptr);
×
3207
        auto line_width = string_fragment::from_str(value).column_width();
×
3208
        if (this->lss_line_context == line_context_t::time_column) {
×
3209
            auto time_attr
3210
                = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
×
3211
            if (time_attr != this->lss_token_attrs.end()) {
×
3212
                line_width -= time_attr->sa_range.length();
×
3213
                auto format = this->lss_token_file->get_format();
×
3214
                if (format->lf_level_hideable) {
×
3215
                    auto level_attr
3216
                        = find_string_attr(this->lss_token_attrs, &L_LEVEL);
×
3217
                    if (level_attr != this->lss_token_attrs.end()) {
×
3218
                        line_width -= level_attr->sa_range.length();
×
3219
                    }
3220
                }
3221
            }
3222
        }
3223
        this->lss_line_size_cache[index].second = line_width;
×
3224
        this->lss_line_size_cache[index].first = row;
×
3225
    }
3226
    return this->lss_line_size_cache[index].second;
×
3227
}
3228

3229
int
3230
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
1✔
3231
{
3232
    int retval = 0;
1✔
3233

3234
    for (const auto& ld : this->lss_files) {
2✔
3235
        retval += ld->ld_filter_state.lfo_filter_state
1✔
3236
                      .tfs_filter_hits[filter_index];
1✔
3237
    }
3238

3239
    return retval;
1✔
3240
}
3241

3242
std::optional<vis_line_t>
3243
logfile_sub_source::row_for(const row_info& ri)
264✔
3244
{
3245
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
528✔
3246
                               this->lss_filtered_index.end(),
3247
                               ri.ri_time,
264✔
3248
                               filtered_logline_cmp(*this));
3249
    if (lb != this->lss_filtered_index.end()) {
264✔
3250
        auto first_lb = lb;
259✔
3251
        while (true) {
3252
            auto cl = this->lss_index[*lb].value();
273✔
3253
            if (content_line_t(ri.ri_id) == cl) {
273✔
3254
                first_lb = lb;
227✔
3255
                break;
227✔
3256
            }
3257
            auto ll = this->find_line(cl);
46✔
3258
            if (ll->get_timeval() != ri.ri_time) {
46✔
3259
                break;
32✔
3260
            }
3261
            auto next_lb = std::next(lb);
14✔
3262
            if (next_lb == this->lss_filtered_index.end()) {
14✔
3263
                break;
×
3264
            }
3265
            lb = next_lb;
14✔
3266
        }
14✔
3267

3268
        const auto dst
3269
            = std::distance(this->lss_filtered_index.begin(), first_lb);
259✔
3270
        return vis_line_t(dst);
259✔
3271
    }
3272

3273
    return std::nullopt;
5✔
3274
}
3275

3276
std::unique_ptr<logline_window>
3277
logfile_sub_source::window_at(vis_line_t start_vl, vis_line_t end_vl)
36✔
3278
{
3279
    return std::make_unique<logline_window>(*this, start_vl, end_vl);
36✔
3280
}
3281

3282
std::unique_ptr<logline_window>
3283
logfile_sub_source::window_at(vis_line_t start_vl)
206✔
3284
{
3285
    return std::make_unique<logline_window>(*this, start_vl, start_vl + 1_vl);
206✔
3286
}
3287

3288
std::unique_ptr<logline_window>
3289
logfile_sub_source::window_to_end(vis_line_t start_vl)
×
3290
{
3291
    return std::make_unique<logline_window>(
3292
        *this, start_vl, vis_line_t(this->text_line_count()));
×
3293
}
3294

3295
std::optional<vis_line_t>
3296
logfile_sub_source::row_for_anchor(const std::string& id)
3✔
3297
{
3298
    if (startswith(id, "#msg")) {
3✔
3299
        static const auto ANCHOR_RE
3300
            = lnav::pcre2pp::code::from_const(R"(#msg([0-9a-fA-F]+)-(.+))");
3✔
3301
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
3✔
3302

3303
        if (ANCHOR_RE.capture_from(id).into(md).found_p()) {
3✔
3304
            auto scan_res = scn::scan<int64_t>(md[1]->to_string_view(), "{:x}");
3✔
3305
            if (scan_res) {
3✔
3306
                auto ts_low = std::chrono::microseconds{scan_res->value()};
3✔
3307
                auto ts_high = ts_low + 1us;
3✔
3308

3309
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3310
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3311
                if (low_vl) {
3✔
3312
                    auto lw = this->window_at(
3313
                        low_vl.value(),
3✔
3314
                        high_vl.value_or(low_vl.value() + 1_vl));
6✔
3315

3316
                    for (const auto& li : *lw) {
3✔
3317
                        auto hash_res = li.get_line_hash();
3✔
3318
                        if (hash_res.isErr()) {
3✔
3319
                            auto errmsg = hash_res.unwrapErr();
×
3320

3321
                            log_error("unable to get line hash: %s",
×
3322
                                      errmsg.c_str());
3323
                            continue;
×
3324
                        }
3325

3326
                        auto hash = hash_res.unwrap();
3✔
3327
                        if (hash == md[2]) {
3✔
3328
                            return li.get_vis_line();
3✔
3329
                        }
3330
                    }
12✔
3331
                }
3✔
3332
            }
3333
        }
3334

3335
        return std::nullopt;
×
3336
    }
3337

3338
    auto& vb = this->tss_view->get_bookmarks();
×
3339
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3340

3341
    for (const auto& vl : bv.bv_tree) {
×
3342
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3343
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3344
            continue;
×
3345
        }
3346

3347
        const auto& name = meta_opt.value()->bm_name;
×
3348
        if (id == text_anchors::to_anchor_string(name)) {
×
3349
            return vl;
×
3350
        }
3351
    }
3352

3353
    return std::nullopt;
×
3354
}
3355

3356
std::optional<vis_line_t>
3357
logfile_sub_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
2✔
3358
{
3359
    if (vl < this->lss_filtered_index.size()) {
2✔
3360
        auto file_and_line_pair = this->find_line_with_file(vl);
2✔
3361
        if (file_and_line_pair) {
2✔
3362
            const auto& [lf, line] = file_and_line_pair.value();
2✔
3363
            if (line->is_continued()) {
2✔
3364
                auto retval = vl;
×
3365
                switch (dir) {
×
3366
                    case direction::prev: {
×
3367
                        auto first_line = line;
×
3368
                        while (first_line->is_continued()) {
×
3369
                            --first_line;
×
3370
                            retval -= 1_vl;
×
3371
                        }
3372
                        return retval;
×
3373
                    }
3374
                    case direction::next: {
×
3375
                        auto first_line = line;
×
3376
                        while (first_line->is_continued()) {
×
3377
                            ++first_line;
×
3378
                            retval += 1_vl;
×
3379
                        }
3380
                        return retval;
×
3381
                    }
3382
                }
3383
            }
3384
        }
3385
    }
2✔
3386

3387
    auto bmc = this->get_bookmark_metadata_context(
2✔
3388
        vl, bookmark_metadata::categories::partition);
3389
    switch (dir) {
2✔
3390
        case direction::prev: {
×
3391
            if (bmc.bmc_current && bmc.bmc_current.value() != vl) {
×
3392
                return bmc.bmc_current;
×
3393
            }
3394
            if (!bmc.bmc_current || bmc.bmc_current.value() == 0_vl) {
×
3395
                return 0_vl;
×
3396
            }
3397
            auto prev_bmc = this->get_bookmark_metadata_context(
×
3398
                bmc.bmc_current.value() - 1_vl,
×
3399
                bookmark_metadata::categories::partition);
3400
            if (!prev_bmc.bmc_current) {
×
3401
                return 0_vl;
×
3402
            }
3403
            return prev_bmc.bmc_current;
×
3404
        }
3405
        case direction::next:
2✔
3406
            return bmc.bmc_next_line;
2✔
3407
    }
3408
    return std::nullopt;
×
3409
}
3410

3411
std::optional<std::string>
3412
logfile_sub_source::anchor_for_row(vis_line_t vl)
79✔
3413
{
3414
    auto line_meta = this->get_bookmark_metadata_context(
79✔
3415
        vl, bookmark_metadata::categories::partition);
3416
    if (!line_meta.bmc_current_metadata) {
79✔
3417
        auto lw = window_at(vl);
76✔
3418

3419
        for (const auto& li : *lw) {
76✔
3420
            auto hash_res = li.get_line_hash();
76✔
3421
            if (hash_res.isErr()) {
76✔
3422
                auto errmsg = hash_res.unwrapErr();
×
3423
                log_error("unable to compute line hash: %s", errmsg.c_str());
×
3424
                break;
×
3425
            }
3426
            auto hash = hash_res.unwrap();
76✔
3427
            auto retval = fmt::format(
3428
                FMT_STRING("#msg{:016x}-{}"),
152✔
3429
                li.get_logline().get_time<std::chrono::microseconds>().count(),
76✔
3430
                hash);
×
3431

3432
            return retval;
76✔
3433
        }
304✔
3434

3435
        return std::nullopt;
×
3436
    }
76✔
3437

3438
    return text_anchors::to_anchor_string(
3✔
3439
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3440
}
3441

3442
std::unordered_set<std::string>
3443
logfile_sub_source::get_anchors()
×
3444
{
3445
    auto& vb = this->tss_view->get_bookmarks();
×
3446
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3447
    std::unordered_set<std::string> retval;
×
3448

3449
    for (const auto& vl : bv.bv_tree) {
×
3450
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3451
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3452
            continue;
×
3453
        }
3454

3455
        const auto& name = meta_opt.value()->bm_name;
×
3456
        retval.emplace(text_anchors::to_anchor_string(name));
×
3457
    }
3458

3459
    return retval;
×
3460
}
×
3461

3462
bool
3463
logfile_sub_source::text_handle_mouse(
×
3464
    textview_curses& tc,
3465
    const listview_curses::display_line_content_t& mouse_line,
3466
    mouse_event& me)
3467
{
3468
    if (mouse_line.is<listview_curses::static_overlay_content>()
×
3469
        && this->text_line_count() > 0)
×
3470
    {
3471
        auto top = tc.get_top();
×
3472
        if (top > 0) {
×
3473
            auto win = this->window_at(top - 1_vl);
×
3474
            for (const auto& li : *win) {
×
3475
                tc.set_top(li.get_vis_line());
×
3476
                tc.set_selection(li.get_vis_line());
×
3477
                return true;
×
3478
            }
3479
        }
3480
    }
3481

3482
    if (tc.get_overlay_selection()) {
×
3483
        auto nci = ncinput{};
×
3484
        if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 2, 4)) {
×
3485
            nci.id = ' ';
×
3486
            nci.eff_text[0] = ' ';
×
3487
            this->list_input_handle_key(tc, nci);
×
3488
        } else if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 5, 6)) {
×
3489
            nci.id = '#';
×
3490
            nci.eff_text[0] = '#';
×
3491
            this->list_input_handle_key(tc, nci);
×
3492
        }
3493
    }
3494
    return true;
×
3495
}
3496

3497
void
3498
logfile_sub_source::reload_config(error_reporter& reporter)
663✔
3499
{
3500
    static const auto& cfg
3501
        = injector::get<const logfile_sub_source_ns::config&>();
663✔
3502

3503
    switch (cfg.c_time_column) {
663✔
3504
        case logfile_sub_source_ns::time_column_feature_t::Default:
×
3505
            if (this->lss_line_context == line_context_t::none) {
×
3506
                this->lss_line_context = line_context_t::time_column;
×
3507
            }
3508
            break;
×
3509
        case logfile_sub_source_ns::time_column_feature_t::Disabled:
663✔
3510
            if (this->lss_line_context == line_context_t::time_column) {
663✔
3511
                this->lss_line_context = line_context_t::none;
×
3512
            }
3513
            break;
663✔
3514
        case logfile_sub_source_ns::time_column_feature_t::Enabled:
×
3515
            break;
×
3516
    }
3517
}
663✔
3518

3519
void
3520
logfile_sub_source::add_commands_for_session(
47✔
3521
    const std::function<void(const std::string&)>& receiver)
3522
{
3523
    text_sub_source::add_commands_for_session(receiver);
47✔
3524

3525
    auto mark_expr = this->get_sql_marker_text();
47✔
3526
    if (!mark_expr.empty()) {
47✔
3527
        receiver(fmt::format(FMT_STRING("mark-expr {}"), mark_expr));
4✔
3528
    }
3529
    auto filter_expr = this->get_sql_filter_text();
47✔
3530
    if (!filter_expr.empty()) {
47✔
3531
        receiver(fmt::format(FMT_STRING("filter-expr {}"), filter_expr));
×
3532
    }
3533

3534
    for (const auto& format : log_format::get_root_formats()) {
3,522✔
3535
        auto field_states = format->get_field_states();
3,475✔
3536

3537
        for (const auto& fs_pair : field_states) {
47,294✔
3538
            if (!fs_pair.second.lvm_user_hidden) {
43,819✔
3539
                continue;
43,817✔
3540
            }
3541

3542
            if (fs_pair.second.lvm_user_hidden.value()) {
2✔
3543
                receiver(fmt::format(FMT_STRING("hide-fields {}.{}"),
8✔
3544
                                     format->get_name().to_string(),
4✔
3545
                                     fs_pair.first.to_string()));
4✔
3546
            } else if (fs_pair.second.lvm_hidden) {
×
3547
                receiver(fmt::format(FMT_STRING("show-fields {}.{}"),
×
3548
                                     format->get_name().to_string(),
×
3549
                                     fs_pair.first.to_string()));
×
3550
            }
3551
        }
3552
    }
3,475✔
3553
}
47✔
3554

3555
void
3556
logfile_sub_source::update_filter_hash_state(hasher& h) const
8✔
3557
{
3558
    text_sub_source::update_filter_hash_state(h);
8✔
3559

3560
    for (const auto& ld : this->lss_files) {
10✔
3561
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
2✔
3562
            h.update(0);
×
3563
        } else {
3564
            h.update(1);
2✔
3565
        }
3566
    }
3567
    h.update(this->tss_min_log_level);
8✔
3568
    h.update(this->lss_marked_only);
8✔
3569
}
8✔
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