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

tstack / lnav / 21219323786-2766

21 Jan 2026 05:28PM UTC coverage: 68.961% (-0.04%) from 68.999%
21219323786-2766

push

github

tstack
[log_format] display an error if there is a JSON timestamp property, but it does not parse correctly

41 of 50 new or added lines in 3 files covered. (82.0%)

40 existing lines in 6 files now uncovered.

51838 of 75170 relevant lines covered (68.96%)

437089.4 hits per line

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

64.5
/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)
69✔
77
{
78
    if (!sqlite3_stmt_busy(stmt)) {
69✔
79
        return 0;
30✔
80
    }
81

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

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

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

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

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

103
        switch (value_type) {
39✔
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:
39✔
114
                value = string_fragment::from_bytes(
39✔
115
                    sqlite3_value_text(raw_value),
116
                    sqlite3_value_bytes(raw_value));
78✔
117
                break;
39✔
118
        }
119
        if (!ec.ec_local_vars.empty() && !ec.ec_dry_run) {
39✔
120
            if (sql_ident_needs_quote(colname)) {
39✔
121
                continue;
25✔
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
    }
39✔
135
    return 0;
39✔
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,171✔
163
    : text_sub_source(1), lnav_config_listener(__FILE__),
164
      lss_meta_grepper(*this), lss_location_history(*this)
1,171✔
165
{
166
    this->tss_supports_filtering = true;
1,171✔
167
    this->clear_line_size_cache();
1,171✔
168
    this->clear_min_max_row_times();
1,171✔
169
}
1,171✔
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) {}
348✔
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
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
205
        const 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,151✔
217
    {
218
        const auto cl_lhs = llss_controller.lss_index[lhs].value();
1,151✔
219
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
1,151✔
220

221
        if (ll_lhs == nullptr) {
1,151✔
222
            return true;
×
223
        }
224
        return (*ll_lhs) < rhs;
1,151✔
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,428✔
247
                                        int row,
248
                                        std::string& value_out,
249
                                        line_flags_t flags)
250
{
251
    if (this->lss_indexing_in_progress) {
4,428✔
252
        value_out = "";
×
253
        this->lss_token_attrs.clear();
×
254
        return {};
×
255
    }
256

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

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

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

265
    if (flags & RF_RAW) {
4,428✔
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,396✔
288

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

295
    this->lss_token_attrs.clear();
4,396✔
296
    this->lss_token_values.clear();
4,396✔
297
    this->lss_share_manager.invalidate_refs();
4,396✔
298
    if (flags & text_sub_source::RF_FULL) {
4,396✔
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
        auto sub_opts = subline_options{};
4,348✔
309
        sub_opts.scrub_invalid_utf8 = false;
4,348✔
310
        this->lss_token_value
311
            = this->lss_token_file->read_line(this->lss_token_line, sub_opts)
8,696✔
312
                  .map([](auto sbr) { return to_string(sbr); })
8,696✔
313
                  .unwrapOr({});
4,348✔
314
        if (this->lss_token_line->has_ansi()) {
4,348✔
315
            scrub_ansi_string(this->lss_token_value, &this->lss_token_attrs);
24✔
316
        }
317
    }
318
    this->lss_token_shift_start = 0;
4,396✔
319
    this->lss_token_shift_size = 0;
4,396✔
320

321
    auto format = this->lss_token_file->get_format();
4,396✔
322

323
    value_out = this->lss_token_value;
4,396✔
324

325
    auto& sbr = this->lss_token_values.lvv_sbr;
4,396✔
326

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

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

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

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

374
            char buffer[128];
375
            const char* fmt;
376
            ssize_t len;
377

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

401
            value_out.replace(
1,918✔
402
                time_range.lr_start, time_range.length(), buffer, len);
959✔
403
            this->lss_token_shift_start = time_range.lr_start;
959✔
404
            this->lss_token_shift_size = len - time_range.length();
959✔
405
        }
406
    }
407

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

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

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

495
    this->lss_in_value_for_line = false;
4,396✔
496

497
    return retval;
4,396✔
498
}
4,396✔
499

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

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

515
    value_out = this->lss_token_attrs;
4,396✔
516

517
    if ((row + 1) < (int) this->lss_filtered_index.size()) {
4,396✔
518
        next_line = this->find_line(this->at(vis_line_t(row + 1)));
4,204✔
519
    }
520

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

529
    const auto& line_values = this->lss_token_values;
4,396✔
530

531
    lr.lr_start = 0;
4,396✔
532
    lr.lr_end = -1;
4,396✔
533
    value_out.emplace_back(
4,396✔
534
        lr, SA_LEVEL.value(this->lss_token_line->get_msg_level()));
8,792✔
535

536
    lr.lr_start = time_offset_end;
4,396✔
537
    lr.lr_end = -1;
4,396✔
538

539
    if (!attrs.empty()) {
4,396✔
540
        value_out.emplace_back(lr, VC_STYLE.value(attrs));
15✔
541
    }
542

543
    if (this->lss_token_line->get_msg_level() == log_level_t::LEVEL_INVALID) {
4,396✔
544
        for (auto& token_attr : this->lss_token_attrs) {
49✔
545
            if (token_attr.sa_type != &SA_INVALID) {
28✔
546
                continue;
21✔
547
            }
548

549
            value_out.emplace_back(token_attr.sa_range,
7✔
550
                                   VC_ROLE.value(role_t::VCR_INVALID_MSG));
14✔
551
        }
552
    }
553

554
    for (const auto& line_value : line_values.lvv_values) {
45,693✔
555
        if ((!(this->lss_token_flags & RF_FULL)
92,427✔
556
             && line_value.lv_sub_offset
82,166✔
557
                 != this->lss_token_line->get_sub_offset())
41,083✔
558
            || !line_value.lv_origin.is_valid())
82,380✔
559
        {
560
            continue;
9,833✔
561
        }
562

563
        if (line_value.lv_meta.is_hidden()) {
31,464✔
564
            value_out.emplace_back(line_value.lv_origin,
144✔
565
                                   SA_HIDDEN.value(ui_icon_t::hidden));
288✔
566
        }
567

568
        if (!line_value.lv_meta.lvm_identifier
80,910✔
569
            || !line_value.lv_origin.is_valid())
31,464✔
570
        {
571
            continue;
17,982✔
572
        }
573

574
        value_out.emplace_back(line_value.lv_origin,
13,482✔
575
                               VC_ROLE.value(role_t::VCR_IDENTIFIER));
26,964✔
576
    }
577

578
    if (this->lss_token_shift_size) {
4,396✔
579
        shift_string_attrs(value_out,
668✔
580
                           this->lss_token_shift_start + 1,
668✔
581
                           this->lss_token_shift_size);
582
    }
583

584
    shift_string_attrs(value_out, 0, 1);
4,396✔
585

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

605
        if (!(this->lss_token_flags & RF_FULL)) {
4,396✔
606
            const auto& bv_search = bm[&textview_curses::BM_SEARCH];
4,348✔
607

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

617
    value_out.emplace_back(lr,
4,396✔
618
                           VC_STYLE.value(vc.attrs_for_ident(
8,792✔
619
                               this->lss_token_file->get_filename())));
4,396✔
620

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

627
        shift_string_attrs(value_out, 0, file_offset_end);
×
628

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

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

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

700
        shift_string_attrs(value_out, 0, time_offset_end);
210✔
701

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

706
        auto bar_role = role_t::VCR_NONE;
210✔
707

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

723
    lr.lr_start = 0;
4,396✔
724
    lr.lr_end = -1;
4,396✔
725
    value_out.emplace_back(lr, L_FILE.value(this->lss_token_file));
4,396✔
726
    value_out.emplace_back(
4,396✔
727
        lr, SA_FORMAT.value(this->lss_token_file->get_format()->get_name()));
8,792✔
728

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

741
        auto line_meta_opt = this->find_bookmark_metadata(vis_line_t(row));
4,396✔
742

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

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

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

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

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

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

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

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

856
struct logline_cmp {
857
    logline_cmp(logfile_sub_source& lc) : llss_controller(lc) {}
1,145✔
858

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

865
        return (*ll_lhs) < (*ll_rhs);
105,194✔
866
    }
867

868
    bool operator()(const logfile_sub_source::indexed_content& lhs,
×
869
                    const timeval& rhs) const
870
    {
871
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
×
872

873
        return *ll_lhs < rhs;
×
874
    }
875

876
    logfile_sub_source& llss_controller;
877
};
878

879
logfile_sub_source::rebuild_result
880
logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
4,462✔
881
{
882
    if (this->tss_view == nullptr) {
4,462✔
883
        return rebuild_result::rr_no_change;
124✔
884
    }
885

886
    this->lss_indexing_in_progress = true;
4,338✔
887
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
4,338✔
888

889
    iterator iter;
4,338✔
890
    size_t total_lines = 0;
4,338✔
891
    size_t est_remaining_lines = 0;
4,338✔
892
    auto all_time_ordered_formats = true;
4,338✔
893
    auto full_sort = this->lss_index.empty();
4,338✔
894
    int file_count = 0;
4,338✔
895
    auto force = std::exchange(this->lss_force_rebuild, false);
4,338✔
896
    auto retval = rebuild_result::rr_no_change;
4,338✔
897
    std::optional<timeval> lowest_tv = std::nullopt;
4,338✔
898
    auto search_start = 0_vl;
4,338✔
899

900
    if (force) {
4,338✔
901
        log_debug("forced to full rebuild");
481✔
902
        retval = rebuild_result::rr_full_rebuild;
481✔
903
        full_sort = true;
481✔
904
        this->tss_level_filtered_count = 0;
481✔
905
        this->lss_index.clear();
481✔
906
    }
907

908
    std::vector<size_t> file_order(this->lss_files.size());
4,338✔
909

910
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
7,338✔
911
        file_order[lpc] = lpc;
3,000✔
912
    }
913
    if (!this->lss_index.empty()) {
4,338✔
914
        std::stable_sort(file_order.begin(),
2,244✔
915
                         file_order.end(),
916
                         [this](const auto& left, const auto& right) {
250✔
917
                             const auto& left_ld = this->lss_files[left];
250✔
918
                             const auto& right_ld = this->lss_files[right];
250✔
919

920
                             if (left_ld->get_file_ptr() == nullptr) {
250✔
921
                                 return true;
×
922
                             }
923
                             if (right_ld->get_file_ptr() == nullptr) {
250✔
924
                                 return false;
3✔
925
                             }
926

927
                             return left_ld->get_file_ptr()->back()
247✔
928
                                 < right_ld->get_file_ptr()->back();
494✔
929
                         });
930
    }
931

932
    bool time_left = true;
4,338✔
933
    this->lss_all_timestamp_flags = 0;
4,338✔
934
    for (const auto file_index : file_order) {
7,338✔
935
        auto& ld = *(this->lss_files[file_index]);
3,000✔
936
        auto* lf = ld.get_file_ptr();
3,000✔
937

938
        if (lf == nullptr) {
3,000✔
939
            if (ld.ld_lines_indexed > 0) {
4✔
940
                log_debug("%zu: file closed, doing full rebuild",
1✔
941
                          ld.ld_file_index);
942
                force = true;
1✔
943
                retval = rebuild_result::rr_full_rebuild;
1✔
944
                full_sort = true;
1✔
945
            }
946
        } else {
947
            if (!lf->get_format_ptr()->lf_time_ordered) {
2,996✔
948
                all_time_ordered_formats = false;
183✔
949
            }
950
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
2,996✔
951
                log_debug("no time left, skipping %s",
2✔
952
                          lf->get_filename_as_string().c_str());
953
                time_left = false;
2✔
954
            }
955
            this->lss_all_timestamp_flags
2,996✔
956
                |= lf->get_format_ptr()->lf_timestamp_flags;
2,996✔
957

958
            if (!this->tss_view->is_paused() && time_left) {
2,996✔
959
                auto log_rebuild_res = lf->rebuild_index(deadline);
2,994✔
960

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

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

1042
            est_remaining_lines += lf->estimated_remaining_lines();
2,996✔
1043
        }
1044
    }
1045

1046
    if (!all_time_ordered_formats
4,338✔
1047
        && retval == rebuild_result::rr_partial_rebuild)
177✔
1048
    {
1049
        force = true;
×
1050
        full_sort = true;
×
1051
        retval = rebuild_result::rr_full_rebuild;
×
1052
    }
1053

1054
    if (this->lss_index.reserve(total_lines + est_remaining_lines)) {
4,338✔
1055
        // The index array was reallocated, just do a full sort/rebuild since
1056
        // it's been cleared out.
1057
        log_debug("expanding index capacity %zu", this->lss_index.ba_capacity);
637✔
1058
        force = true;
637✔
1059
        retval = rebuild_result::rr_full_rebuild;
637✔
1060
        full_sort = true;
637✔
1061
        this->tss_level_filtered_count = 0;
637✔
1062
    }
1063

1064
    auto& vis_bm = this->tss_view->get_bookmarks();
4,338✔
1065

1066
    if (force) {
4,338✔
1067
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,671✔
1068
             iter++)
538✔
1069
        {
1070
            (*iter)->ld_lines_indexed = 0;
538✔
1071
        }
1072

1073
        this->lss_index.clear();
1,133✔
1074
        this->lss_filtered_index.clear();
1,133✔
1075
        this->tss_level_filtered_count = 0;
1,133✔
1076
        this->lss_longest_line = 0;
1,133✔
1077
        this->lss_basename_width = 0;
1,133✔
1078
        this->lss_filename_width = 0;
1,133✔
1079
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,133✔
1080
        if (this->lss_index_delegate) {
1,133✔
1081
            this->lss_index_delegate->index_start(*this);
1,133✔
1082
        }
1083
    } else if (retval == rebuild_result::rr_partial_rebuild) {
3,205✔
1084
        size_t remaining = 0;
×
1085

1086
        log_debug("partial rebuild with lowest time: %ld",
×
1087
                  lowest_tv.value().tv_sec);
1088
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1089
             iter++)
×
1090
        {
1091
            logfile_data& ld = *(*iter);
×
1092
            auto* lf = ld.get_file_ptr();
×
1093

1094
            if (lf == nullptr) {
×
1095
                continue;
×
1096
            }
1097

1098
            require(lf->get_format_ptr()->lf_time_ordered);
×
1099

1100
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1101

1102
            if (line_iter) {
×
1103
                log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
×
1104
                          line_iter.value()->get_timeval().tv_sec,
1105
                          std::distance(lf->cbegin(), line_iter.value()),
1106
                          lf->size(),
1107
                          lf->get_filename_as_string().c_str());
1108
            }
1109
            ld.ld_lines_indexed
1110
                = std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
×
1111
            remaining += lf->size() - ld.ld_lines_indexed;
×
1112
        }
1113

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

1132
        if (this->lss_index_delegate) {
×
1133
            this->lss_index_delegate->index_start(*this);
×
1134
            for (const auto row_in_full_index : this->lss_filtered_index) {
×
1135
                auto cl = this->lss_index[row_in_full_index].value();
×
1136
                uint64_t line_number;
1137
                auto ld_iter = this->find_data(cl, line_number);
×
1138
                auto& ld = *ld_iter;
×
1139
                auto line_iter = ld->get_file_ptr()->begin() + line_number;
×
1140

1141
                this->lss_index_delegate->index_line(
×
1142
                    *this, ld->get_file_ptr(), line_iter);
1143
            }
1144
        }
1145
    }
1146

1147
    if (this->lss_index.empty() && !time_left) {
4,338✔
1148
        log_info("ran out of time, skipping rebuild");
×
1149
        // need to make sure we rebuild in case no new data comes in
1150
        this->lss_force_rebuild = true;
×
1151
        return rebuild_result::rr_appended_lines;
×
1152
    }
1153

1154
    if (retval != rebuild_result::rr_no_change || force) {
4,338✔
1155
        size_t index_size = 0, start_size = this->lss_index.size();
1,145✔
1156
        logline_cmp line_cmper(*this);
1,145✔
1157

1158
        for (auto& ld : this->lss_files) {
1,699✔
1159
            auto* lf = ld->get_file_ptr();
554✔
1160

1161
            if (lf == nullptr) {
554✔
1162
                continue;
1✔
1163
            }
1164
            this->lss_longest_line = std::max(
1,106✔
1165
                this->lss_longest_line, lf->get_longest_line_length() + 1);
553✔
1166
            this->lss_basename_width
1167
                = std::max(this->lss_basename_width,
1,106✔
1168
                           lf->get_unique_path().native().size());
553✔
1169
            this->lss_filename_width = std::max(
1,106✔
1170
                this->lss_filename_width, lf->get_filename().native().size());
553✔
1171
        }
1172

1173
        if (full_sort) {
1,145✔
1174
            log_trace("rebuild_index full sort");
1,145✔
1175
            for (auto& ld : this->lss_files) {
1,699✔
1176
                auto* lf = ld->get_file_ptr();
554✔
1177

1178
                if (lf == nullptr) {
554✔
1179
                    continue;
1✔
1180
                }
1181

1182
                for (size_t line_index = 0; line_index < lf->size();
14,829✔
1183
                     line_index++)
1184
                {
1185
                    const auto lf_iter
1186
                        = ld->get_file_ptr()->begin() + line_index;
14,276✔
1187
                    if (lf_iter->is_ignored()) {
14,276✔
1188
                        continue;
414✔
1189
                    }
1190

1191
                    content_line_t con_line(
1192
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
13,862✔
1193

1194
                    if (lf_iter->is_meta_marked()) {
13,862✔
1195
                        auto start_iter = lf_iter;
12✔
1196
                        while (start_iter->is_continued()) {
12✔
1197
                            --start_iter;
×
1198
                        }
1199
                        int start_index
1200
                            = start_iter - ld->get_file_ptr()->begin();
12✔
1201
                        content_line_t start_con_line(ld->ld_file_index
12✔
1202
                                                          * MAX_LINES_PER_FILE
12✔
1203
                                                      + start_index);
12✔
1204

1205
                        auto& line_meta
1206
                            = ld->get_file_ptr()
1207
                                  ->get_bookmark_metadata()[start_index];
12✔
1208
                        if (line_meta.has(bookmark_metadata::categories::notes))
12✔
1209
                        {
1210
                            this->lss_user_marks[&textview_curses::BM_META]
4✔
1211
                                .insert_once(start_con_line);
4✔
1212
                        }
1213
                        if (line_meta.has(
12✔
1214
                                bookmark_metadata::categories::partition))
1215
                        {
1216
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
8✔
1217
                                .insert_once(start_con_line);
8✔
1218
                        }
1219
                    }
1220
                    this->lss_index.push_back(
13,862✔
1221
                        indexed_content{con_line, lf_iter});
27,724✔
1222
                }
1223
            }
1224

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

UNCOV
1240
            for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
UNCOV
1241
                 iter++)
×
1242
            {
UNCOV
1243
                auto* ld = iter->get();
×
UNCOV
1244
                auto* lf = ld->get_file_ptr();
×
UNCOV
1245
                if (lf == nullptr) {
×
1246
                    continue;
×
1247
                }
1248

UNCOV
1249
                merge.add(ld, lf->begin() + ld->ld_lines_indexed, lf->end());
×
UNCOV
1250
                index_size += lf->size();
×
1251
            }
1252

UNCOV
1253
            file_off_t index_off = 0;
×
UNCOV
1254
            merge.execute();
×
UNCOV
1255
            if (this->lss_sorting_observer) {
×
1256
                this->lss_sorting_observer(*this, index_off, index_size);
×
1257
            }
UNCOV
1258
            log_trace("k-way merge");
×
1259
            for (;;) {
UNCOV
1260
                logfile::iterator lf_iter;
×
1261
                logfile_data* ld;
1262

UNCOV
1263
                if (!merge.get_top(ld, lf_iter)) {
×
UNCOV
1264
                    break;
×
1265
                }
1266

UNCOV
1267
                if (!lf_iter->is_ignored()) {
×
UNCOV
1268
                    int file_index = ld->ld_file_index;
×
UNCOV
1269
                    int line_index = lf_iter - ld->get_file_ptr()->begin();
×
1270

UNCOV
1271
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
UNCOV
1272
                                            + line_index);
×
1273

UNCOV
1274
                    if (lf_iter->is_meta_marked()) {
×
1275
                        auto start_iter = lf_iter;
×
1276
                        while (start_iter->is_continued()) {
×
1277
                            --start_iter;
×
1278
                        }
1279
                        int start_index
1280
                            = start_iter - ld->get_file_ptr()->begin();
×
1281
                        content_line_t start_con_line(
1282
                            file_index * MAX_LINES_PER_FILE + start_index);
×
1283

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

UNCOV
1303
                merge.next();
×
UNCOV
1304
                index_off += 1;
×
UNCOV
1305
                if (index_off % 100000 == 0 && this->lss_sorting_observer) {
×
1306
                    this->lss_sorting_observer(*this, index_off, index_size);
×
1307
                }
1308
            }
UNCOV
1309
            if (this->lss_sorting_observer) {
×
1310
                this->lss_sorting_observer(*this, index_size, index_size);
×
1311
            }
1312
        }
1313

1314
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,699✔
1315
             ++iter)
554✔
1316
        {
1317
            auto* lf = (*iter)->get_file_ptr();
554✔
1318

1319
            if (lf == nullptr) {
554✔
1320
                continue;
1✔
1321
            }
1322

1323
            (*iter)->ld_lines_indexed = lf->size();
553✔
1324
        }
1325

1326
        this->lss_filtered_index.reserve(this->lss_index.size());
1,145✔
1327

1328
        uint32_t filter_in_mask, filter_out_mask;
1329
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,145✔
1330

1331
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,145✔
1332
            this->lss_index_delegate->index_start(*this);
1,145✔
1333
        }
1334

1335
        log_trace("filtered index");
1,145✔
1336
        for (size_t index_index = start_size;
15,007✔
1337
             index_index < this->lss_index.size();
15,007✔
1338
             index_index++)
1339
        {
1340
            const auto cl = this->lss_index[index_index].value();
13,862✔
1341
            uint64_t line_number;
1342
            auto ld = this->find_data(cl, line_number);
13,862✔
1343

1344
            if (!(*ld)->is_visible()) {
13,862✔
1345
                continue;
×
1346
            }
1347

1348
            auto* lf = (*ld)->get_file_ptr();
13,862✔
1349
            auto line_iter = lf->begin() + line_number;
13,862✔
1350

1351
            if (line_iter->is_ignored()) {
13,862✔
1352
                continue;
×
1353
            }
1354

1355
            if (!this->tss_apply_filters
27,724✔
1356
                || (!(*ld)->ld_filter_state.excluded(
27,694✔
1357
                        filter_in_mask, filter_out_mask, line_number)
1358
                    && this->check_extra_filters(ld, line_iter)))
13,832✔
1359
            {
1360
                auto eval_res = this->eval_sql_filter(
1361
                    this->lss_marker_stmt.in(), ld, line_iter);
13,832✔
1362
                if (eval_res.isErr()) {
13,832✔
1363
                    line_iter->set_expr_mark(false);
×
1364
                } else {
1365
                    auto matched = eval_res.unwrap();
13,832✔
1366

1367
                    line_iter->set_expr_mark(matched);
13,832✔
1368
                    if (matched) {
13,832✔
1369
                        vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1370
                            vis_line_t(this->lss_filtered_index.size()));
×
1371
                        this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1372
                            .insert_once(cl);
×
1373
                    }
1374
                }
1375
                this->lss_filtered_index.push_back(index_index);
13,832✔
1376
                if (this->lss_index_delegate != nullptr) {
13,832✔
1377
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
13,832✔
1378
                }
1379
            }
13,832✔
1380
        }
1381

1382
        this->lss_indexing_in_progress = false;
1,145✔
1383

1384
        if (this->lss_index_delegate != nullptr) {
1,145✔
1385
            this->lss_index_delegate->index_complete(*this);
1,145✔
1386
        }
1387
    }
1388

1389
    switch (retval) {
4,338✔
1390
        case rebuild_result::rr_no_change:
3,193✔
1391
            break;
3,193✔
1392
        case rebuild_result::rr_full_rebuild:
1,133✔
1393
            log_debug("redoing search");
1,133✔
1394
            this->lss_index_generation += 1;
1,133✔
1395
            this->tss_view->reload_data();
1,133✔
1396
            this->tss_view->redo_search();
1,133✔
1397
            break;
1,133✔
1398
        case rebuild_result::rr_partial_rebuild:
×
1399
            log_debug("redoing search from: %d", (int) search_start);
×
1400
            this->lss_index_generation += 1;
×
1401
            this->tss_view->reload_data();
×
1402
            this->tss_view->search_new_data(search_start);
×
1403
            break;
×
1404
        case rebuild_result::rr_appended_lines:
12✔
1405
            this->tss_view->reload_data();
12✔
1406
            this->tss_view->search_new_data();
12✔
1407
            break;
12✔
1408
    }
1409

1410
    return retval;
4,338✔
1411
}
4,338✔
1412

1413
void
1414
logfile_sub_source::text_update_marks(vis_bookmarks& bm)
2,220✔
1415
{
1416
    logfile* last_file = nullptr;
2,220✔
1417
    vis_line_t vl;
2,220✔
1418

1419
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,220✔
1420
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,220✔
1421
    auto& bm_files = bm[&BM_FILES];
2,220✔
1422

1423
    bm_warnings.clear();
2,220✔
1424
    bm_errors.clear();
2,220✔
1425
    bm_files.clear();
2,220✔
1426

1427
    std::vector<const bookmark_type_t*> used_marks;
2,220✔
1428
    for (const auto* bmt :
11,100✔
1429
         {
1430
             &textview_curses::BM_USER,
1431
             &textview_curses::BM_USER_EXPR,
1432
             &textview_curses::BM_PARTITION,
1433
             &textview_curses::BM_META,
1434
         })
13,320✔
1435
    {
1436
        bm[bmt].clear();
8,880✔
1437
        if (!this->lss_user_marks[bmt].empty()) {
8,880✔
1438
            used_marks.emplace_back(bmt);
118✔
1439
        }
1440
    }
1441

1442
    for (; vl < (int) this->lss_filtered_index.size(); ++vl) {
19,597✔
1443
        const auto& orig_ic = this->lss_index[this->lss_filtered_index[vl]];
17,377✔
1444
        auto cl = orig_ic.value();
17,377✔
1445
        auto* lf = this->find_file_ptr(cl);
17,377✔
1446

1447
        for (const auto& bmt : used_marks) {
19,515✔
1448
            auto& user_mark = this->lss_user_marks[bmt];
2,138✔
1449
            if (user_mark.bv_tree.exists(orig_ic.value())) {
2,138✔
1450
                bm[bmt].insert_once(vl);
156✔
1451
            }
1452
        }
1453

1454
        if (lf != last_file) {
17,377✔
1455
            bm_files.insert_once(vl);
967✔
1456
        }
1457

1458
        switch (orig_ic.level()) {
17,377✔
1459
            case indexed_content::level_t::warning:
94✔
1460
                bm_warnings.insert_once(vl);
94✔
1461
                break;
94✔
1462

1463
            case indexed_content::level_t::error:
3,421✔
1464
                bm_errors.insert_once(vl);
3,421✔
1465
                break;
3,421✔
1466

1467
            default:
13,862✔
1468
                break;
13,862✔
1469
        }
1470

1471
        last_file = lf;
17,377✔
1472
    }
1473
}
2,220✔
1474

1475
void
1476
logfile_sub_source::text_filters_changed()
166✔
1477
{
1478
    this->lss_index_generation += 1;
166✔
1479
    this->tss_level_filtered_count = 0;
166✔
1480

1481
    if (this->lss_line_meta_changed) {
166✔
1482
        this->invalidate_sql_filter();
24✔
1483
        this->lss_line_meta_changed = false;
24✔
1484
    }
1485

1486
    for (auto& ld : *this) {
284✔
1487
        auto* lf = ld->get_file_ptr();
118✔
1488

1489
        if (lf != nullptr) {
118✔
1490
            ld->ld_filter_state.clear_deleted_filter_state();
118✔
1491
            lf->reobserve_from(lf->begin()
118✔
1492
                               + ld->ld_filter_state.get_min_count(lf->size()));
118✔
1493
        }
1494
    }
1495

1496
    if (this->lss_force_rebuild) {
166✔
1497
        return;
×
1498
    }
1499

1500
    auto& vis_bm = this->tss_view->get_bookmarks();
166✔
1501
    uint32_t filtered_in_mask, filtered_out_mask;
1502

1503
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
166✔
1504

1505
    if (this->lss_index_delegate != nullptr) {
166✔
1506
        this->lss_index_delegate->index_start(*this);
166✔
1507
    }
1508
    vis_bm[&textview_curses::BM_USER_EXPR].clear();
166✔
1509

1510
    this->lss_filtered_index.clear();
166✔
1511
    for (size_t index_index = 0; index_index < this->lss_index.size();
1,495✔
1512
         index_index++)
1513
    {
1514
        auto cl = this->lss_index[index_index].value();
1,329✔
1515
        uint64_t line_number;
1516
        auto ld = this->find_data(cl, line_number);
1,329✔
1517

1518
        if (!(*ld)->is_visible()) {
1,329✔
1519
            continue;
213✔
1520
        }
1521

1522
        auto lf = (*ld)->get_file_ptr();
1,116✔
1523
        auto line_iter = lf->begin() + line_number;
1,116✔
1524

1525
        if (!this->tss_apply_filters
2,232✔
1526
            || (!(*ld)->ld_filter_state.excluded(
2,095✔
1527
                    filtered_in_mask, filtered_out_mask, line_number)
1528
                && this->check_extra_filters(ld, line_iter)))
979✔
1529
        {
1530
            auto eval_res = this->eval_sql_filter(
1531
                this->lss_marker_stmt.in(), ld, line_iter);
937✔
1532
            if (eval_res.isErr()) {
937✔
1533
                line_iter->set_expr_mark(false);
×
1534
            } else {
1535
                auto matched = eval_res.unwrap();
937✔
1536

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

1552
    if (this->lss_index_delegate != nullptr) {
166✔
1553
        this->lss_index_delegate->index_complete(*this);
166✔
1554
    }
1555

1556
    if (this->tss_view != nullptr) {
166✔
1557
        this->tss_view->reload_data();
166✔
1558
        this->tss_view->redo_search();
166✔
1559
    }
1560
}
1561

1562
std::optional<json_string>
1563
logfile_sub_source::text_row_details(const textview_curses& tc)
29✔
1564
{
1565
    if (this->lss_index.empty()) {
29✔
1566
        log_trace("logfile_sub_source::text_row_details empty");
1✔
1567
        return std::nullopt;
1✔
1568
    }
1569

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

1580
                {
1581
                    yajlpp_map root(gen);
×
1582

1583
                    root.gen("value");
×
1584
                    root.gen(iter->second.ri_value);
×
1585
                }
1586

1587
                return json_string(gen);
×
1588
            }
1589
        }
1590
    }
1591

1592
    return std::nullopt;
28✔
1593
}
1594

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

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

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

1697
/**
1698
 * Functor for comparing the ld_file field of the logfile_data struct.
1699
 */
1700
struct logfile_data_eq {
1701
    explicit logfile_data_eq(std::shared_ptr<logfile> lf)
1,040✔
1702
        : lde_file(std::move(lf))
1,040✔
1703
    {
1704
    }
1,040✔
1705

1706
    bool operator()(
636✔
1707
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1708
    {
1709
        return this->lde_file == ld->get_file();
636✔
1710
    }
1711

1712
    std::shared_ptr<logfile> lde_file;
1713
};
1714

1715
bool
1716
logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
520✔
1717
{
1718
    iterator existing;
520✔
1719

1720
    require_lt(lf->size(), MAX_LINES_PER_FILE);
520✔
1721

1722
    existing = std::find_if(this->lss_files.begin(),
520✔
1723
                            this->lss_files.end(),
1724
                            logfile_data_eq(nullptr));
1,040✔
1725
    if (existing == this->lss_files.end()) {
520✔
1726
        if (this->lss_files.size() >= MAX_FILES) {
520✔
1727
            return false;
×
1728
        }
1729

1730
        auto ld = std::make_unique<logfile_data>(
1731
            this->lss_files.size(), this->get_filters(), lf);
520✔
1732
        ld->set_visibility(lf->get_open_options().loo_is_visible);
520✔
1733
        this->lss_files.push_back(std::move(ld));
520✔
1734
    } else {
520✔
1735
        (*existing)->set_file(lf);
×
1736
    }
1737

1738
    return true;
520✔
1739
}
1740

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

1750
        if (eval_res.isErr()) {
8✔
1751
            sqlite3_finalize(stmt);
1✔
1752
            return Err(eval_res.unwrapErr());
1✔
1753
        }
1754
    }
8✔
1755

1756
    for (auto& ld : *this) {
792✔
1757
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
17✔
1758
    }
1759

1760
    auto old_filter_iter = this->tss_filters.find(0);
775✔
1761
    if (stmt != nullptr) {
775✔
1762
        auto new_filter
1763
            = std::make_shared<sql_filter>(*this, std::move(stmt_str), stmt);
7✔
1764

1765
        if (old_filter_iter != this->tss_filters.end()) {
7✔
1766
            *old_filter_iter = new_filter;
×
1767
        } else {
1768
            this->tss_filters.add_filter(new_filter);
7✔
1769
        }
1770
    } else if (old_filter_iter != this->tss_filters.end()) {
775✔
1771
        this->tss_filters.delete_filter((*old_filter_iter)->get_id());
7✔
1772
    }
1773

1774
    return Ok();
775✔
1775
}
1776

1777
Result<void, lnav::console::user_message>
1778
logfile_sub_source::set_sql_marker(std::string stmt_str, sqlite3_stmt* stmt)
773✔
1779
{
1780
    static auto op = lnav_operation{"set_sql_marker"};
773✔
1781
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
773✔
1782
        auto top_cl = this->at(0_vl);
5✔
1783
        auto ld = this->find_data(top_cl);
5✔
1784
        auto eval_res
1785
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
5✔
1786

1787
        if (eval_res.isErr()) {
5✔
1788
            sqlite3_finalize(stmt);
×
1789
            return Err(eval_res.unwrapErr());
×
1790
        }
1791
    }
5✔
1792

1793
    auto op_guard = lnav_opid_guard::internal(op);
773✔
1794
    log_info("setting SQL marker: %s", stmt_str.c_str());
773✔
1795
    this->lss_marker_stmt_text = std::move(stmt_str);
773✔
1796
    this->lss_marker_stmt = stmt;
773✔
1797

1798
    if (this->tss_view == nullptr || this->lss_force_rebuild) {
773✔
1799
        log_info("skipping SQL marker update");
130✔
1800
        return Ok();
130✔
1801
    }
1802

1803
    auto& vis_bm = this->tss_view->get_bookmarks();
643✔
1804
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
643✔
1805
    auto& cl_marks_bv = this->lss_user_marks[&textview_curses::BM_USER_EXPR];
643✔
1806

1807
    expr_marks_bv.clear();
643✔
1808
    if (this->lss_index_delegate) {
643✔
1809
        this->lss_index_delegate->index_start(*this);
643✔
1810
    }
1811
    for (auto row = 0_vl; row < vis_line_t(this->lss_filtered_index.size());
1,076✔
1812
         row += 1_vl)
433✔
1813
    {
1814
        auto cl = this->at(row);
433✔
1815
        uint64_t line_number;
1816
        auto ld = this->find_data(cl, line_number);
433✔
1817

1818
        if (!(*ld)->is_visible()) {
433✔
1819
            continue;
1✔
1820
        }
1821
        auto ll = (*ld)->get_file()->begin() + line_number;
433✔
1822
        if (ll->is_continued() || ll->is_ignored()) {
433✔
1823
            continue;
1✔
1824
        }
1825
        auto eval_res
1826
            = this->eval_sql_filter(this->lss_marker_stmt.in(), ld, ll);
432✔
1827

1828
        if (eval_res.isErr()) {
432✔
1829
            ll->set_expr_mark(false);
×
1830
        } else {
1831
            auto matched = eval_res.unwrap();
432✔
1832

1833
            ll->set_expr_mark(matched);
432✔
1834
            if (matched) {
432✔
1835
                expr_marks_bv.insert_once(row);
22✔
1836
                cl_marks_bv.insert_once(cl);
22✔
1837
            }
1838
        }
1839
        if (this->lss_index_delegate) {
432✔
1840
            this->lss_index_delegate->index_line(
432✔
1841
                *this, (*ld)->get_file_ptr(), ll);
432✔
1842
        }
1843
    }
432✔
1844
    if (this->lss_index_delegate) {
643✔
1845
        this->lss_index_delegate->index_complete(*this);
643✔
1846
    }
1847
    log_info("SQL marker update complete");
643✔
1848

1849
    return Ok();
643✔
1850
}
773✔
1851

1852
Result<void, lnav::console::user_message>
1853
logfile_sub_source::set_preview_sql_filter(sqlite3_stmt* stmt)
768✔
1854
{
1855
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
768✔
1856
        auto top_cl = this->at(0_vl);
×
1857
        auto ld = this->find_data(top_cl);
×
1858
        auto eval_res
1859
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
×
1860

1861
        if (eval_res.isErr()) {
×
1862
            sqlite3_finalize(stmt);
×
1863
            return Err(eval_res.unwrapErr());
×
1864
        }
1865
    }
1866

1867
    this->lss_preview_filter_stmt = stmt;
768✔
1868

1869
    return Ok();
768✔
1870
}
1871

1872
Result<bool, lnav::console::user_message>
1873
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
15,387✔
1874
                                    iterator ld,
1875
                                    logfile::const_iterator ll)
1876
{
1877
    if (stmt == nullptr) {
15,387✔
1878
        return Ok(false);
29,544✔
1879
    }
1880

1881
    auto* lf = (*ld)->get_file_ptr();
615✔
1882
    char timestamp_buffer[64];
1883
    shared_buffer_ref raw_sbr;
615✔
1884
    logline_value_vector values;
615✔
1885
    auto& sbr = values.lvv_sbr;
615✔
1886
    lf->read_full_message(ll, sbr);
615✔
1887
    sbr.erase_ansi();
615✔
1888
    auto format = lf->get_format();
615✔
1889
    string_attrs_t sa;
615✔
1890
    auto line_number = std::distance(lf->cbegin(), ll);
615✔
1891
    format->annotate(lf, line_number, sa, values);
615✔
1892
    auto lffs = lf->get_format_file_state();
615✔
1893

1894
    sqlite3_reset(stmt);
615✔
1895
    sqlite3_clear_bindings(stmt);
615✔
1896

1897
    auto count = sqlite3_bind_parameter_count(stmt);
615✔
1898
    for (int lpc = 0; lpc < count; lpc++) {
1,242✔
1899
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
627✔
1900

1901
        if (name[0] == '$') {
627✔
1902
            const char* env_value;
1903

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

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

1979
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
1980

1981
                {
1982
                    yajlpp_array arr(gen);
1✔
1983

1984
                    for (const auto& str : meta.bm_tags) {
2✔
1985
                        arr.gen(str);
1✔
1986
                    }
1987
                }
1✔
1988

1989
                string_fragment sf = gen.to_string_fragment();
1✔
1990

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

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

2066
            if (res.isOk()) {
×
2067
                raw_sbr = res.unwrap();
×
2068
                sqlite3_bind_text(stmt,
×
2069
                                  lpc + 1,
2070
                                  raw_sbr.get_data(),
2071
                                  raw_sbr.length(),
×
2072
                                  SQLITE_STATIC);
2073
            }
2074
            continue;
×
2075
        }
2076
        for (const auto& lv : values.lvv_values) {
6,782✔
2077
            if (lv.lv_meta.lvm_name != &name[1]) {
6,777✔
2078
                continue;
6,201✔
2079
            }
2080

2081
            switch (lv.lv_meta.lvm_kind) {
576✔
2082
                case value_kind_t::VALUE_BOOLEAN:
×
2083
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
2084
                    break;
×
2085
                case value_kind_t::VALUE_FLOAT:
×
2086
                    sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
2087
                    break;
×
2088
                case value_kind_t::VALUE_INTEGER:
436✔
2089
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
436✔
2090
                    break;
436✔
2091
                case value_kind_t::VALUE_NULL:
×
2092
                    sqlite3_bind_null(stmt, lpc + 1);
×
2093
                    break;
×
2094
                default:
140✔
2095
                    sqlite3_bind_text(stmt,
140✔
2096
                                      lpc + 1,
2097
                                      lv.text_value(),
2098
                                      lv.text_length(),
140✔
2099
                                      SQLITE_TRANSIENT);
2100
                    break;
140✔
2101
            }
2102
            break;
576✔
2103
        }
2104
    }
2105

2106
    auto step_res = sqlite3_step(stmt);
615✔
2107

2108
    sqlite3_reset(stmt);
615✔
2109
    sqlite3_clear_bindings(stmt);
615✔
2110
    switch (step_res) {
615✔
2111
        case SQLITE_OK:
474✔
2112
        case SQLITE_DONE:
2113
            return Ok(false);
948✔
2114
        case SQLITE_ROW:
140✔
2115
            return Ok(true);
280✔
2116
        default:
1✔
2117
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2118
    }
2119
}
615✔
2120

2121
bool
2122
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
14,811✔
2123
{
2124
    auto retval = true;
14,811✔
2125

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

2149
    if (ll->get_msg_level() < this->tss_min_log_level) {
14,811✔
2150
        this->tss_level_filtered_count += 1;
2✔
2151
        retval = false;
2✔
2152
    }
2153

2154
    if (*ll < this->ttt_min_row_time) {
14,811✔
2155
        retval = false;
36✔
2156
    }
2157

2158
    if (!(*ll <= this->ttt_max_row_time)) {
14,811✔
2159
        retval = false;
4✔
2160
    }
2161

2162
    return retval;
14,811✔
2163
}
2164

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

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

2182
    auto cl = this->at(line);
148✔
2183

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

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

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

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

2224
        (*iter)->clear();
520✔
2225
        for (auto& bv : this->lss_user_marks) {
4,680✔
2226
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
4,160✔
2227
            auto mark_end
2228
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
4,160✔
2229
            auto file_range = bv.equal_range(mark_curr, mark_end);
4,160✔
2230

2231
            if (file_range.first != file_range.second) {
4,160✔
2232
                auto to_del = std::vector<content_line_t>{};
69✔
2233
                for (auto file_iter = file_range.first;
69✔
2234
                     file_iter != file_range.second;
178✔
2235
                     ++file_iter)
109✔
2236
                {
2237
                    to_del.emplace_back(*file_iter);
109✔
2238
                }
2239

2240
                for (auto cl : to_del) {
178✔
2241
                    bv.erase(cl);
109✔
2242
                }
2243
            }
69✔
2244
        }
2245

2246
        this->lss_force_rebuild = true;
520✔
2247
    }
2248
    while (!this->lss_files.empty()) {
1,040✔
2249
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
571✔
2250
            this->lss_files.pop_back();
520✔
2251
        } else {
2252
            break;
51✔
2253
        }
2254
    }
2255
    this->lss_token_file = nullptr;
520✔
2256
}
520✔
2257

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

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

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

2273
        auto vis_start = *vis_start_opt;
5✔
2274

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

2278
            if (cl == guess_cl) {
5✔
2279
                return vis_start;
5✔
2280
            }
2281

2282
            auto guess_line = this->find_line(guess_cl);
×
2283

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

2288
            ++vis_start;
×
2289
        }
2290
    }
2291

2292
    return std::nullopt;
×
2293
}
5✔
2294

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

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

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

2315
std::optional<std::shared_ptr<text_filter>>
2316
logfile_sub_source::get_sql_filter()
2,594✔
2317
{
2318
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
2,594✔
2319
               return filt->get_index() == 0
270✔
2320
                   && dynamic_cast<sql_filter*>(filt.get()) != nullptr;
270✔
2321
           })
2322
        | lnav::itertools::deref();
5,188✔
2323
}
2324

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

2333
    auto cl = this->llh_log_source.at(top);
98✔
2334

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

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

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

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

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

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

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

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

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

2369
    return std::nullopt;
×
2370
}
2371

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

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

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

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

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

2389
    return std::nullopt;
×
2390
}
2391

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

2400
    auto ls = ls_opt;
124✔
2401

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

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

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

2421
    return true;
47✔
2422
}
121✔
2423

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

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

2440
        {
2441
            md2attr_line mdal;
×
2442

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

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

2461
            md2attr_line mdal;
×
2462

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

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

2479
    return std::nullopt;
×
2480
}
2481

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

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

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

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

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

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

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

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

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

2549
    return retval;
11✔
2550
}
24✔
2551

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

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

2564
    return retval;
29✔
2565
}
×
2566

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2737
        auto tid_display = values.lvv_thread_id_value.has_value()
18✔
2738
            ? lnav::roles::identifier(values.lvv_thread_id_value.value())
1✔
2739
            : lnav::roles::hidden(ELLIPSIS);
19✔
2740
        crumbs.emplace_back(
18✔
2741
            values.lvv_thread_id_value.has_value()
18✔
2742
                ? values.lvv_thread_id_value.value()
53✔
2743
                : "",
2744
            attr_line_t()
18✔
2745
                .append(ui_icon_t::thread)
18✔
2746
                .append(" ")
18✔
2747
                .append(tid_display),
2748
            [this]() -> std::vector<breadcrumb::possibility> {
×
2749
                std::set<std::string> poss_strs;
11✔
2750

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

2758
                    for (const auto& pair : r_tid_map->ltis_tid_ranges) {
24✔
2759
                        poss_strs.emplace(pair.first.to_string());
13✔
2760
                    }
2761
                }
11✔
2762

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

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

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

2782
    {
2783
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
30✔
2784
          SET selection = ifnull(
2785
            (SELECT log_line FROM all_logs WHERE log_opid = $opid LIMIT 1),
2786
            (SELECT raise_error('Could not find opid: ' || $opid,
2787
                                'The corresponding log messages might have been filtered out')))
2788
          WHERE name = 'log'
2789
        )";
2790
        static const std::string ELLIPSIS = "\u22ef";
30✔
2791

2792
        auto opid_display = values.lvv_opid_value.has_value()
18✔
2793
            ? lnav::roles::identifier(values.lvv_opid_value.value())
9✔
2794
            : lnav::roles::hidden(ELLIPSIS);
27✔
2795
        crumbs.emplace_back(
36✔
2796
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
45✔
2797
                                              : "",
2798
            attr_line_t().append(opid_display),
18✔
2799
            [this]() -> std::vector<breadcrumb::possibility> {
×
2800
                std::unordered_set<std::string> poss_strs;
11✔
2801

2802
                for (const auto& file_data : this->lss_files) {
22✔
2803
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2804
                        continue;
×
2805
                    }
2806
                    safe::ReadAccess<logfile::safe_opid_state> r_opid_map(
2807
                        file_data->get_file_ptr()->get_opids());
11✔
2808

2809
                    poss_strs.reserve(poss_strs.size()
22✔
2810
                                      + r_opid_map->los_opid_ranges.size());
11✔
2811
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
783✔
2812
                        poss_strs.insert(pair.first.to_string());
772✔
2813
                    }
2814
                }
11✔
2815

2816
                std::vector<breadcrumb::possibility> retval;
11✔
2817
                retval.reserve(poss_strs.size());
11✔
2818

2819
                std::transform(poss_strs.begin(),
11✔
2820
                               poss_strs.end(),
2821
                               std::back_inserter(retval),
2822
                               [](const auto& opid_str) {
772✔
2823
                                   return breadcrumb::possibility(opid_str);
772✔
2824
                               });
2825

2826
                return retval;
22✔
2827
            },
11✔
2828
            [ec = this->lss_exec_context](const auto& opid) {
18✔
2829
                ec->execute_with(
×
2830
                    INTERNAL_SRC_LOC,
×
2831
                    MOVE_STMT,
2832
                    std::make_pair("opid", opid.template get<std::string>()));
2833
            });
×
2834
    }
18✔
2835

2836
    auto sf = string_fragment::from_str(al.get_string());
18✔
2837
    auto body_opt = get_string_attr(al.get_attrs(), SA_BODY);
18✔
2838
    auto nl_pos_opt = sf.find('\n');
18✔
2839
    auto msg_line_number = std::distance(msg_start_iter, line_pair.second);
18✔
2840
    auto line_from_top = line - msg_line_number;
18✔
2841
    if (body_opt && nl_pos_opt) {
18✔
2842
        if (this->lss_token_meta_line != file_line_number
18✔
2843
            || this->lss_token_meta_size != sf.length())
9✔
2844
        {
2845
            if (body_opt->saw_string_attr->sa_range.length() < 128 * 1024) {
3✔
2846
                this->lss_token_meta
2847
                    = lnav::document::discover(al)
3✔
2848
                          .over_range(
3✔
2849
                              body_opt.value().saw_string_attr->sa_range)
3✔
2850
                          .perform();
3✔
2851
                // XXX discover_structure() changes `al`, have to recompute
2852
                // stuff
2853
                sf = al.to_string_fragment();
3✔
2854
                body_opt = get_string_attr(al.get_attrs(), SA_BODY);
3✔
2855
            } else {
2856
                this->lss_token_meta = lnav::document::metadata{};
×
2857
            }
2858
            this->lss_token_meta_line = file_line_number;
3✔
2859
            this->lss_token_meta_size = sf.length();
3✔
2860
        }
2861

2862
        const auto initial_size = crumbs.size();
9✔
2863
        auto sf_body
2864
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
9✔
2865
                           body_opt->saw_string_attr->sa_range.lr_end);
9✔
2866
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
9✔
2867
        file_off_t line_end_offset = sf.length();
9✔
2868
        ssize_t line_number = 0;
9✔
2869

2870
        for (const auto& sf_line : sf_body.split_lines()) {
18✔
2871
            if (line_number >= msg_line_number) {
12✔
2872
                line_end_offset = line_offset + sf_line.length();
3✔
2873
                break;
3✔
2874
            }
2875
            line_number += 1;
9✔
2876
            line_offset += sf_line.length();
9✔
2877
        }
9✔
2878

2879
        this->lss_token_meta.m_sections_tree.visit_overlapping(
9✔
2880
            line_offset,
2881
            line_end_offset,
2882
            [this,
2✔
2883
             initial_size,
2884
             meta = &this->lss_token_meta,
9✔
2885
             &crumbs,
2886
             line_from_top](const auto& iv) {
2887
                auto path = crumbs | lnav::itertools::skip(initial_size)
6✔
2888
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
4✔
2889
                    | lnav::itertools::append(iv.value);
2✔
2890
                auto curr_node = lnav::document::hier_node::lookup_path(
2✔
2891
                    meta->m_sections_root.get(), path);
2✔
2892

2893
                crumbs.emplace_back(
4✔
2894
                    iv.value,
2✔
2895
                    [meta, path]() { return meta->possibility_provider(path); },
4✔
2896
                    [this, curr_node, path, line_from_top](const auto& key) {
4✔
2897
                        if (!curr_node) {
×
2898
                            return;
×
2899
                        }
2900
                        auto* parent_node = curr_node.value()->hn_parent;
×
2901
                        if (parent_node == nullptr) {
×
2902
                            return;
×
2903
                        }
2904
                        key.match(
2905
                            [parent_node](const std::string& str) {
×
2906
                                return parent_node->find_line_number(str);
×
2907
                            },
2908
                            [parent_node](size_t index) {
×
2909
                                return parent_node->find_line_number(index);
×
2910
                            })
2911
                            | [this, line_from_top](auto line_number) {
×
2912
                                  this->tss_view->set_selection(
×
2913
                                      vis_line_t(line_from_top + line_number));
×
2914
                              };
2915
                    });
2916
                if (curr_node
2✔
2917
                    && !curr_node.value()->hn_parent->is_named_only()) {
2✔
2918
                    auto node = lnav::document::hier_node::lookup_path(
×
2919
                        meta->m_sections_root.get(), path);
×
2920

2921
                    crumbs.back().c_expected_input
×
2922
                        = curr_node.value()
×
2923
                              ->hn_parent->hn_named_children.empty()
×
2924
                        ? breadcrumb::crumb::expected_input_t::index
×
2925
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
2926
                    crumbs.back().with_possible_range(
×
2927
                        node | lnav::itertools::map([](const auto hn) {
×
2928
                            return hn->hn_parent->hn_children.size();
×
2929
                        })
2930
                        | lnav::itertools::unwrap_or(size_t{0}));
×
2931
                }
2932
            });
2✔
2933

2934
        auto path = crumbs | lnav::itertools::skip(initial_size)
18✔
2935
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
18✔
2936
        auto node = lnav::document::hier_node::lookup_path(
9✔
2937
            this->lss_token_meta.m_sections_root.get(), path);
9✔
2938

2939
        if (node && !node.value()->hn_children.empty()) {
9✔
2940
            auto poss_provider = [curr_node = node.value()]() {
1✔
2941
                std::vector<breadcrumb::possibility> retval;
1✔
2942
                for (const auto& child : curr_node->hn_named_children) {
1✔
2943
                    retval.emplace_back(child.first);
×
2944
                }
2945
                return retval;
1✔
2946
            };
2947
            auto path_performer
2948
                = [this, curr_node = node.value(), line_from_top](
2✔
2949
                      const breadcrumb::crumb::key_t& value) {
2950
                      value.match(
×
2951
                          [curr_node](const std::string& str) {
×
2952
                              return curr_node->find_line_number(str);
×
2953
                          },
2954
                          [curr_node](size_t index) {
×
2955
                              return curr_node->find_line_number(index);
×
2956
                          })
2957
                          | [this, line_from_top](size_t line_number) {
×
2958
                                this->tss_view->set_selection(
×
2959
                                    vis_line_t(line_from_top + line_number));
×
2960
                            };
2961
                  };
1✔
2962
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
1✔
2963
            crumbs.back().c_expected_input
1✔
2964
                = node.value()->hn_named_children.empty()
2✔
2965
                ? breadcrumb::crumb::expected_input_t::index
1✔
2966
                : breadcrumb::crumb::expected_input_t::index_or_exact;
2967
        }
2968
    }
9✔
2969
}
18✔
2970

2971
void
2972
logfile_sub_source::quiesce()
42✔
2973
{
2974
    for (auto& ld : this->lss_files) {
84✔
2975
        auto* lf = ld->get_file_ptr();
42✔
2976

2977
        if (lf == nullptr) {
42✔
2978
            continue;
×
2979
        }
2980

2981
        lf->quiesce();
42✔
2982
    }
2983
}
42✔
2984

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

2992
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
2993
}
25✔
2994

2995
logfile_sub_source::bookmark_metadata_context
2996
logfile_sub_source::get_bookmark_metadata_context(
4,495✔
2997
    vis_line_t vl, bookmark_metadata::categories desired) const
2998
{
2999
    const auto& vb = this->tss_view->get_bookmarks();
4,495✔
3000
    const auto& bv = vb[desired == bookmark_metadata::categories::partition
3001
                            ? &textview_curses::BM_PARTITION
3002
                            : &textview_curses::BM_META];
4,495✔
3003
    auto vl_iter = bv.bv_tree.lower_bound(vl + 1_vl);
4,495✔
3004

3005
    std::optional<vis_line_t> next_line;
4,495✔
3006
    for (auto next_vl_iter = vl_iter; next_vl_iter != bv.bv_tree.end();
4,495✔
3007
         ++next_vl_iter)
×
3008
    {
3009
        auto bm_opt = this->find_bookmark_metadata(*next_vl_iter);
2✔
3010
        if (!bm_opt) {
2✔
3011
            continue;
×
3012
        }
3013

3014
        if (bm_opt.value()->has(desired)) {
2✔
3015
            next_line = *next_vl_iter;
2✔
3016
            break;
2✔
3017
        }
3018
    }
3019
    if (vl_iter == bv.bv_tree.begin()) {
4,495✔
3020
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
4,479✔
3021
    }
3022

3023
    --vl_iter;
16✔
3024
    while (true) {
3025
        auto bm_opt = this->find_bookmark_metadata(*vl_iter);
16✔
3026
        if (bm_opt) {
16✔
3027
            if (bm_opt.value()->has(desired)) {
13✔
3028
                return bookmark_metadata_context{
3029
                    *vl_iter, bm_opt.value(), next_line};
16✔
3030
            }
3031
        }
3032

3033
        if (vl_iter == bv.bv_tree.begin()) {
3✔
3034
            return bookmark_metadata_context{
3035
                std::nullopt, std::nullopt, next_line};
3✔
3036
        }
3037
        --vl_iter;
×
3038
    }
3039
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3040
}
3041

3042
std::optional<bookmark_metadata*>
3043
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
23,748✔
3044
{
3045
    auto line_pair = this->find_line_with_file(cl).value();
23,748✔
3046
    auto line_number = static_cast<uint32_t>(
3047
        std::distance(line_pair.first->begin(), line_pair.second));
23,748✔
3048

3049
    auto& bm = line_pair.first->get_bookmark_metadata();
23,748✔
3050
    auto bm_iter = bm.find(line_number);
23,748✔
3051
    if (bm_iter == bm.end()) {
23,748✔
3052
        return std::nullopt;
23,392✔
3053
    }
3054

3055
    return &bm_iter->second;
356✔
3056
}
23,748✔
3057

3058
void
3059
logfile_sub_source::erase_bookmark_metadata(content_line_t cl)
26✔
3060
{
3061
    auto line_pair = this->find_line_with_file(cl).value();
26✔
3062
    auto line_number = static_cast<uint32_t>(
3063
        std::distance(line_pair.first->begin(), line_pair.second));
26✔
3064

3065
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
3066
    auto bm_iter = bm.find(line_number);
26✔
3067
    if (bm_iter != bm.end()) {
26✔
3068
        bm.erase(bm_iter);
6✔
3069
    }
3070
}
26✔
3071

3072
void
3073
logfile_sub_source::clear_bookmark_metadata()
6✔
3074
{
3075
    for (auto& ld : *this) {
14✔
3076
        if (ld->get_file_ptr() == nullptr) {
8✔
3077
            continue;
×
3078
        }
3079

3080
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3081
    }
3082
}
6✔
3083

3084
void
3085
logfile_sub_source::increase_line_context()
×
3086
{
3087
    auto old_context = this->lss_line_context;
×
3088

3089
    switch (this->lss_line_context) {
×
3090
        case line_context_t::filename:
×
3091
            // nothing to do
3092
            break;
×
3093
        case line_context_t::basename:
×
3094
            this->lss_line_context = line_context_t::filename;
×
3095
            break;
×
3096
        case line_context_t::none:
×
3097
            this->lss_line_context = line_context_t::basename;
×
3098
            break;
×
3099
        case line_context_t::time_column:
×
3100
            this->lss_line_context = line_context_t::none;
×
3101
            break;
×
3102
    }
3103
    if (old_context != this->lss_line_context) {
×
3104
        this->clear_line_size_cache();
×
3105
    }
3106
}
3107

3108
bool
3109
logfile_sub_source::decrease_line_context()
×
3110
{
3111
    static const auto& cfg
3112
        = injector::get<const logfile_sub_source_ns::config&>();
×
3113
    auto old_context = this->lss_line_context;
×
3114

3115
    switch (this->lss_line_context) {
×
3116
        case line_context_t::filename:
×
3117
            this->lss_line_context = line_context_t::basename;
×
3118
            break;
×
3119
        case line_context_t::basename:
×
3120
            this->lss_line_context = line_context_t::none;
×
3121
            break;
×
3122
        case line_context_t::none:
×
3123
            if (cfg.c_time_column
×
3124
                != logfile_sub_source_ns::time_column_feature_t::Disabled)
3125
            {
3126
                this->lss_line_context = line_context_t::time_column;
×
3127
            }
3128
            break;
×
3129
        case line_context_t::time_column:
×
3130
            break;
×
3131
    }
3132
    if (old_context != this->lss_line_context) {
×
3133
        this->clear_line_size_cache();
×
3134

3135
        return true;
×
3136
    }
3137

3138
    return false;
×
3139
}
3140

3141
size_t
3142
logfile_sub_source::get_filename_offset() const
243✔
3143
{
3144
    switch (this->lss_line_context) {
243✔
3145
        case line_context_t::filename:
×
3146
            return this->lss_filename_width;
×
3147
        case line_context_t::basename:
×
3148
            return this->lss_basename_width;
×
3149
        default:
243✔
3150
            return 0;
243✔
3151
    }
3152
}
3153

3154
size_t
3155
logfile_sub_source::file_count() const
4,040✔
3156
{
3157
    size_t retval = 0;
4,040✔
3158
    const_iterator iter;
4,040✔
3159

3160
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,672✔
3161
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,632✔
3162
            retval += 1;
3,626✔
3163
        }
3164
    }
3165

3166
    return retval;
4,040✔
3167
}
3168

3169
size_t
3170
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3171
                                       int row,
3172
                                       text_sub_source::line_flags_t flags)
3173
{
3174
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3175

3176
    if (this->lss_line_size_cache[index].first != row) {
×
3177
        std::string value;
×
3178

3179
        this->text_value_for_line(tc, row, value, flags);
×
3180
        scrub_ansi_string(value, nullptr);
×
3181
        auto line_width = string_fragment::from_str(value).column_width();
×
3182
        if (this->lss_line_context == line_context_t::time_column) {
×
3183
            auto time_attr
3184
                = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
×
3185
            if (time_attr != this->lss_token_attrs.end()) {
×
3186
                line_width -= time_attr->sa_range.length();
×
3187
                auto format = this->lss_token_file->get_format();
×
3188
                if (format->lf_level_hideable) {
×
3189
                    auto level_attr
3190
                        = find_string_attr(this->lss_token_attrs, &L_LEVEL);
×
3191
                    if (level_attr != this->lss_token_attrs.end()) {
×
3192
                        line_width -= level_attr->sa_range.length();
×
3193
                    }
3194
                }
3195
            }
3196
        }
3197
        this->lss_line_size_cache[index].second = line_width;
×
3198
        this->lss_line_size_cache[index].first = row;
×
3199
    }
3200
    return this->lss_line_size_cache[index].second;
×
3201
}
3202

3203
int
3204
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
1✔
3205
{
3206
    int retval = 0;
1✔
3207

3208
    for (const auto& ld : this->lss_files) {
2✔
3209
        retval += ld->ld_filter_state.lfo_filter_state
1✔
3210
                      .tfs_filter_hits[filter_index];
1✔
3211
    }
3212

3213
    return retval;
1✔
3214
}
3215

3216
std::optional<vis_line_t>
3217
logfile_sub_source::row_for(const row_info& ri)
263✔
3218
{
3219
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
526✔
3220
                               this->lss_filtered_index.end(),
3221
                               ri.ri_time,
263✔
3222
                               filtered_logline_cmp(*this));
3223
    if (lb != this->lss_filtered_index.end()) {
263✔
3224
        auto first_lb = lb;
258✔
3225
        while (true) {
3226
            auto cl = this->lss_index[*lb].value();
272✔
3227
            if (content_line_t(ri.ri_id) == cl) {
272✔
3228
                first_lb = lb;
226✔
3229
                break;
226✔
3230
            }
3231
            auto ll = this->find_line(cl);
46✔
3232
            if (ll->get_timeval() != ri.ri_time) {
46✔
3233
                break;
32✔
3234
            }
3235
            auto next_lb = std::next(lb);
14✔
3236
            if (next_lb == this->lss_filtered_index.end()) {
14✔
3237
                break;
×
3238
            }
3239
            lb = next_lb;
14✔
3240
        }
14✔
3241

3242
        const auto dst
3243
            = std::distance(this->lss_filtered_index.begin(), first_lb);
258✔
3244
        return vis_line_t(dst);
258✔
3245
    }
3246

3247
    return std::nullopt;
5✔
3248
}
3249

3250
std::unique_ptr<logline_window>
3251
logfile_sub_source::window_at(vis_line_t start_vl, vis_line_t end_vl)
36✔
3252
{
3253
    return std::make_unique<logline_window>(*this, start_vl, end_vl);
36✔
3254
}
3255

3256
std::unique_ptr<logline_window>
3257
logfile_sub_source::window_at(vis_line_t start_vl)
206✔
3258
{
3259
    return std::make_unique<logline_window>(*this, start_vl, start_vl + 1_vl);
206✔
3260
}
3261

3262
std::unique_ptr<logline_window>
3263
logfile_sub_source::window_to_end(vis_line_t start_vl)
×
3264
{
3265
    return std::make_unique<logline_window>(
3266
        *this, start_vl, vis_line_t(this->text_line_count()));
×
3267
}
3268

3269
std::optional<vis_line_t>
3270
logfile_sub_source::row_for_anchor(const std::string& id)
3✔
3271
{
3272
    if (startswith(id, "#msg")) {
3✔
3273
        static const auto ANCHOR_RE
3274
            = lnav::pcre2pp::code::from_const(R"(#msg([0-9a-fA-F]+)-(.+))");
3✔
3275
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
3✔
3276

3277
        if (ANCHOR_RE.capture_from(id).into(md).found_p()) {
3✔
3278
            auto scan_res = scn::scan<int64_t>(md[1]->to_string_view(), "{:x}");
3✔
3279
            if (scan_res) {
3✔
3280
                auto ts_low = std::chrono::microseconds{scan_res->value()};
3✔
3281
                auto ts_high = ts_low + 1us;
3✔
3282

3283
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3284
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3285
                if (low_vl) {
3✔
3286
                    auto lw = this->window_at(
3287
                        low_vl.value(),
3✔
3288
                        high_vl.value_or(low_vl.value() + 1_vl));
6✔
3289

3290
                    for (const auto& li : *lw) {
3✔
3291
                        auto hash_res = li.get_line_hash();
3✔
3292
                        if (hash_res.isErr()) {
3✔
3293
                            auto errmsg = hash_res.unwrapErr();
×
3294

3295
                            log_error("unable to get line hash: %s",
×
3296
                                      errmsg.c_str());
3297
                            continue;
×
3298
                        }
3299

3300
                        auto hash = hash_res.unwrap();
3✔
3301
                        if (hash == md[2]) {
3✔
3302
                            return li.get_vis_line();
3✔
3303
                        }
3304
                    }
12✔
3305
                }
3✔
3306
            }
3307
        }
3308

3309
        return std::nullopt;
×
3310
    }
3311

3312
    auto& vb = this->tss_view->get_bookmarks();
×
3313
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3314

3315
    for (const auto& vl : bv.bv_tree) {
×
3316
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3317
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3318
            continue;
×
3319
        }
3320

3321
        const auto& name = meta_opt.value()->bm_name;
×
3322
        if (id == text_anchors::to_anchor_string(name)) {
×
3323
            return vl;
×
3324
        }
3325
    }
3326

3327
    return std::nullopt;
×
3328
}
3329

3330
std::optional<vis_line_t>
3331
logfile_sub_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
2✔
3332
{
3333
    if (vl < this->lss_filtered_index.size()) {
2✔
3334
        auto file_and_line_pair = this->find_line_with_file(vl);
2✔
3335
        if (file_and_line_pair) {
2✔
3336
            const auto& [lf, line] = file_and_line_pair.value();
2✔
3337
            if (line->is_continued()) {
2✔
3338
                auto retval = vl;
×
3339
                switch (dir) {
×
3340
                    case direction::prev: {
×
3341
                        auto first_line = line;
×
3342
                        while (first_line->is_continued()) {
×
3343
                            --first_line;
×
3344
                            retval -= 1_vl;
×
3345
                        }
3346
                        return retval;
×
3347
                    }
3348
                    case direction::next: {
×
3349
                        auto first_line = line;
×
3350
                        while (first_line->is_continued()) {
×
3351
                            ++first_line;
×
3352
                            retval += 1_vl;
×
3353
                        }
3354
                        return retval;
×
3355
                    }
3356
                }
3357
            }
3358
        }
3359
    }
2✔
3360

3361
    auto bmc = this->get_bookmark_metadata_context(
2✔
3362
        vl, bookmark_metadata::categories::partition);
3363
    switch (dir) {
2✔
3364
        case direction::prev: {
×
3365
            if (bmc.bmc_current && bmc.bmc_current.value() != vl) {
×
3366
                return bmc.bmc_current;
×
3367
            }
3368
            if (!bmc.bmc_current || bmc.bmc_current.value() == 0_vl) {
×
3369
                return 0_vl;
×
3370
            }
3371
            auto prev_bmc = this->get_bookmark_metadata_context(
×
3372
                bmc.bmc_current.value() - 1_vl,
×
3373
                bookmark_metadata::categories::partition);
3374
            if (!prev_bmc.bmc_current) {
×
3375
                return 0_vl;
×
3376
            }
3377
            return prev_bmc.bmc_current;
×
3378
        }
3379
        case direction::next:
2✔
3380
            return bmc.bmc_next_line;
2✔
3381
    }
3382
    return std::nullopt;
×
3383
}
3384

3385
std::optional<std::string>
3386
logfile_sub_source::anchor_for_row(vis_line_t vl)
79✔
3387
{
3388
    auto line_meta = this->get_bookmark_metadata_context(
79✔
3389
        vl, bookmark_metadata::categories::partition);
3390
    if (!line_meta.bmc_current_metadata) {
79✔
3391
        auto lw = window_at(vl);
76✔
3392

3393
        for (const auto& li : *lw) {
76✔
3394
            auto hash_res = li.get_line_hash();
76✔
3395
            if (hash_res.isErr()) {
76✔
3396
                auto errmsg = hash_res.unwrapErr();
×
3397
                log_error("unable to compute line hash: %s", errmsg.c_str());
×
3398
                break;
×
3399
            }
3400
            auto hash = hash_res.unwrap();
76✔
3401
            auto retval = fmt::format(
3402
                FMT_STRING("#msg{:016x}-{}"),
152✔
3403
                li.get_logline().get_time<std::chrono::microseconds>().count(),
76✔
3404
                hash);
×
3405

3406
            return retval;
76✔
3407
        }
304✔
3408

3409
        return std::nullopt;
×
3410
    }
76✔
3411

3412
    return text_anchors::to_anchor_string(
3✔
3413
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3414
}
3415

3416
std::unordered_set<std::string>
3417
logfile_sub_source::get_anchors()
×
3418
{
3419
    auto& vb = this->tss_view->get_bookmarks();
×
3420
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3421
    std::unordered_set<std::string> retval;
×
3422

3423
    for (const auto& vl : bv.bv_tree) {
×
3424
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3425
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3426
            continue;
×
3427
        }
3428

3429
        const auto& name = meta_opt.value()->bm_name;
×
3430
        retval.emplace(text_anchors::to_anchor_string(name));
×
3431
    }
3432

3433
    return retval;
×
3434
}
×
3435

3436
bool
3437
logfile_sub_source::text_handle_mouse(
×
3438
    textview_curses& tc,
3439
    const listview_curses::display_line_content_t& mouse_line,
3440
    mouse_event& me)
3441
{
3442
    if (mouse_line.is<listview_curses::static_overlay_content>()
×
3443
        && this->text_line_count() > 0)
×
3444
    {
3445
        auto top = tc.get_top();
×
3446
        if (top > 0) {
×
3447
            auto win = this->window_at(top - 1_vl);
×
3448
            for (const auto& li : *win) {
×
3449
                tc.set_top(li.get_vis_line());
×
3450
                tc.set_selection(li.get_vis_line());
×
3451
                return true;
×
3452
            }
3453
        }
3454
    }
3455

3456
    if (tc.get_overlay_selection()) {
×
3457
        auto nci = ncinput{};
×
3458
        if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 2, 4)) {
×
3459
            nci.id = ' ';
×
3460
            nci.eff_text[0] = ' ';
×
3461
            this->list_input_handle_key(tc, nci);
×
3462
        } else if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 5, 6)) {
×
3463
            nci.id = '#';
×
3464
            nci.eff_text[0] = '#';
×
3465
            this->list_input_handle_key(tc, nci);
×
3466
        }
3467
    }
3468
    return true;
×
3469
}
3470

3471
void
3472
logfile_sub_source::reload_config(error_reporter& reporter)
666✔
3473
{
3474
    static const auto& cfg
3475
        = injector::get<const logfile_sub_source_ns::config&>();
666✔
3476

3477
    switch (cfg.c_time_column) {
666✔
3478
        case logfile_sub_source_ns::time_column_feature_t::Default:
×
3479
            if (this->lss_line_context == line_context_t::none) {
×
3480
                this->lss_line_context = line_context_t::time_column;
×
3481
            }
3482
            break;
×
3483
        case logfile_sub_source_ns::time_column_feature_t::Disabled:
666✔
3484
            if (this->lss_line_context == line_context_t::time_column) {
666✔
3485
                this->lss_line_context = line_context_t::none;
×
3486
            }
3487
            break;
666✔
3488
        case logfile_sub_source_ns::time_column_feature_t::Enabled:
×
3489
            break;
×
3490
    }
3491
}
666✔
3492

3493
void
3494
logfile_sub_source::add_commands_for_session(
47✔
3495
    const std::function<void(const std::string&)>& receiver)
3496
{
3497
    text_sub_source::add_commands_for_session(receiver);
47✔
3498

3499
    auto mark_expr = this->get_sql_marker_text();
47✔
3500
    if (!mark_expr.empty()) {
47✔
3501
        receiver(fmt::format(FMT_STRING("mark-expr {}"), mark_expr));
4✔
3502
    }
3503
    auto filter_expr = this->get_sql_filter_text();
47✔
3504
    if (!filter_expr.empty()) {
47✔
3505
        receiver(fmt::format(FMT_STRING("filter-expr {}"), filter_expr));
×
3506
    }
3507

3508
    for (const auto& format : log_format::get_root_formats()) {
3,569✔
3509
        auto field_states = format->get_field_states();
3,522✔
3510

3511
        for (const auto& fs_pair : field_states) {
48,046✔
3512
            if (!fs_pair.second.lvm_user_hidden) {
44,524✔
3513
                continue;
44,522✔
3514
            }
3515

3516
            if (fs_pair.second.lvm_user_hidden.value()) {
2✔
3517
                receiver(fmt::format(FMT_STRING("hide-fields {}.{}"),
8✔
3518
                                     format->get_name().to_string(),
4✔
3519
                                     fs_pair.first.to_string()));
4✔
3520
            } else if (fs_pair.second.lvm_hidden) {
×
3521
                receiver(fmt::format(FMT_STRING("show-fields {}.{}"),
×
3522
                                     format->get_name().to_string(),
×
3523
                                     fs_pair.first.to_string()));
×
3524
            }
3525
        }
3526
    }
3,522✔
3527
}
47✔
3528

3529
void
3530
logfile_sub_source::update_filter_hash_state(hasher& h) const
8✔
3531
{
3532
    text_sub_source::update_filter_hash_state(h);
8✔
3533

3534
    for (const auto& ld : this->lss_files) {
10✔
3535
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
2✔
3536
            h.update(0);
×
3537
        } else {
3538
            h.update(1);
2✔
3539
        }
3540
    }
3541
    h.update(this->tss_min_log_level);
8✔
3542
    h.update(this->lss_marked_only);
8✔
3543
}
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