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

tstack / lnav / 20284884426-2753

16 Dec 2025 10:23PM UTC coverage: 68.23% (-0.7%) from 68.903%
20284884426-2753

push

github

tstack
[log] show invalid utf hex dump in log view too

25 of 25 new or added lines in 2 files covered. (100.0%)

503 existing lines in 33 files now uncovered.

51170 of 74996 relevant lines covered (68.23%)

433797.6 hits per line

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

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

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

39
#include "logfile_sub_source.hh"
40

41
#include <sqlite3.h>
42

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

156
        return retval;
×
157
    });
×
158

159
    return retval;
×
160
}
161

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

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

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

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

194
    return retval;
24✔
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
        auto ll_lhs = this->llss_controller.find_line(cl_lhs);
205
        auto ll_rhs = this->llss_controller.find_line(cl_rhs);
206

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

216
    bool operator()(const uint32_t& lhs, const timeval& rhs) const
1,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,311✔
247
                                        int row,
248
                                        std::string& value_out,
249
                                        line_flags_t flags)
250
{
251
    if (this->lss_indexing_in_progress) {
4,311✔
252
        value_out = "";
×
253
        this->lss_token_attrs.clear();
×
254
        return {};
×
255
    }
256

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

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

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

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

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

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

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

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

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

327
    sbr.share(this->lss_share_manager,
4,279✔
328
              (char*) this->lss_token_value.c_str(),
329
              this->lss_token_value.size());
330
    format->annotate(this->lss_token_file.get(),
8,558✔
331
                     line,
332
                     this->lss_token_attrs,
4,279✔
333
                     this->lss_token_values);
4,279✔
334
    if (flags & RF_REWRITE) {
4,279✔
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,279✔
353
        this->lss_token_attrs.emplace_back(lr, SA_ORIGINAL_LINE.value());
4,279✔
354
    }
355

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

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

378
            if (format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
957✔
379
                || !(format->lf_timestamp_flags & ETF_DAY_SET)
323✔
380
                || !(format->lf_timestamp_flags & ETF_MONTH_SET))
1,280✔
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(
634✔
395
                    buffer,
396
                    sizeof(buffer),
397
                    format->get_timestamp_formats(),
398
                    adjusted_tm.value());
317✔
399
            }
400

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

408
    // Insert space for the file/search-hit markers.
409
    value_out.insert(0, 1, ' ');
4,279✔
410
    this->lss_time_column_size = 0;
4,279✔
411
    if (this->lss_line_context == line_context_t::time_column) {
4,279✔
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,279✔
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,279✔
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,279✔
496

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

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

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

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

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

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

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

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

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

539
    if (!attrs.empty()) {
4,279✔
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,279✔
544
        for (auto& token_attr : this->lss_token_attrs) {
37✔
545
            if (token_attr.sa_type != &SA_INVALID) {
20✔
546
                continue;
17✔
547
            }
548

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

554
    for (const auto& line_value : line_values.lvv_values) {
45,553✔
555
        if ((!(this->lss_token_flags & RF_FULL)
92,377✔
556
             && line_value.lv_sub_offset
82,104✔
557
                 != this->lss_token_line->get_sub_offset())
41,052✔
558
            || !line_value.lv_origin.is_valid())
82,326✔
559
        {
560
            continue;
9,829✔
561
        }
562

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

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

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

578
    if (this->lss_token_shift_size) {
4,279✔
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,279✔
585

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

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

608
            if (bv_search.bv_tree.exists(vis_line_t(row))) {
4,231✔
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,279✔
618
                           VC_STYLE.value(vc.attrs_for_ident(
8,558✔
619
                               this->lss_token_file->get_filename())));
4,279✔
620

621
    if (this->lss_line_context < line_context_t::none) {
4,279✔
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,279✔
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,279✔
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,279✔
724
    lr.lr_end = -1;
4,279✔
725
    value_out.emplace_back(lr, L_FILE.value(this->lss_token_file));
4,279✔
726
    value_out.emplace_back(
4,279✔
727
        lr, SA_FORMAT.value(this->lss_token_file->get_format()->get_name()));
8,558✔
728

729
    {
730
        auto line_meta_context = this->get_bookmark_metadata_context(
4,279✔
731
            vis_line_t(row + 1), bookmark_metadata::categories::partition);
732
        if (line_meta_context.bmc_current_metadata) {
4,279✔
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,279✔
742

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

750
    auto src_file_attr = get_string_attr(value_out, SA_SRC_FILE);
4,279✔
751
    if (src_file_attr) {
4,279✔
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,279✔
764
        if (this->lss_token_file->is_time_adjusted()) {
4,279✔
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,262✔
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,279✔
782
        && this->lss_token_line->get_msg_level()
4,279✔
783
            < this->tss_preview_min_log_level)
4,279✔
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,279✔
790
        && this->lss_token_line->get_timeval() < this->ttt_preview_min_time)
4,279✔
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,279✔
797
        && this->ttt_preview_max_time < this->lss_token_line->get_timeval())
4,279✔
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,279✔
804
        if (this->lss_preview_filter_stmt != nullptr) {
2,535✔
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,535✔
836
        if (sql_filter_opt) {
2,535✔
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,535✔
854
}
855

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

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

865
        return (*ll_lhs) < (*ll_rhs);
104,977✔
866
    }
867

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

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

883
            return (*ll_lhs) < (*ll_rhs);
884
        }
885
#endif
886

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

892
        return *ll_lhs < rhs;
893
    }
894
#endif
895

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

901
        return *ll_lhs < rhs;
×
902
    }
903

904
    logfile_sub_source& llss_controller;
905
};
906

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

914
    this->lss_indexing_in_progress = true;
4,327✔
915
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
4,327✔
916

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

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

936
    std::vector<size_t> file_order(this->lss_files.size());
4,327✔
937

938
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
7,319✔
939
        file_order[lpc] = lpc;
2,992✔
940
    }
941
    if (!this->lss_index.empty()) {
4,327✔
942
        std::stable_sort(file_order.begin(),
2,237✔
943
                         file_order.end(),
944
                         [this](const auto& left, const auto& right) {
249✔
945
                             const auto& left_ld = this->lss_files[left];
249✔
946
                             const auto& right_ld = this->lss_files[right];
249✔
947

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

955
                             return left_ld->get_file_ptr()->back()
246✔
956
                                 < right_ld->get_file_ptr()->back();
492✔
957
                         });
958
    }
959

960
    bool time_left = true;
4,327✔
961
    this->lss_all_timestamp_flags = 0;
4,327✔
962
    for (const auto file_index : file_order) {
7,319✔
963
        auto& ld = *(this->lss_files[file_index]);
2,992✔
964
        auto* lf = ld.get_file_ptr();
2,992✔
965

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

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

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

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

1070
            est_remaining_lines += lf->estimated_remaining_lines();
2,988✔
1071
        }
1072
    }
1073

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

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

1092
    auto& vis_bm = this->tss_view->get_bookmarks();
4,327✔
1093

1094
    if (force) {
4,327✔
1095
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,667✔
1096
             iter++)
537✔
1097
        {
1098
            (*iter)->ld_lines_indexed = 0;
537✔
1099
        }
1100

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

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

1122
            if (lf == nullptr) {
×
1123
                continue;
×
1124
            }
1125

1126
            require(lf->get_format_ptr()->lf_time_ordered);
×
1127

1128
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1129

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

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

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

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

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

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

1186
        for (auto& ld : this->lss_files) {
1,695✔
1187
            auto* lf = ld->get_file_ptr();
553✔
1188

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

1201
        if (full_sort) {
1,142✔
1202
            log_trace("rebuild_index full sort");
1,142✔
1203
            for (auto& ld : this->lss_files) {
1,695✔
1204
                auto* lf = ld->get_file_ptr();
553✔
1205

1206
                if (lf == nullptr) {
553✔
1207
                    continue;
1✔
1208
                }
1209

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

1219
                    content_line_t con_line(
1220
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
13,813✔
1221

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

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

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

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

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

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

1291
                if (!merge.get_top(ld, lf_iter)) {
×
1292
                    break;
×
1293
                }
1294

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

1299
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
1300
                                            + line_index);
×
1301

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

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

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

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

1347
            if (lf == nullptr) {
553✔
1348
                continue;
1✔
1349
            }
1350

1351
            (*iter)->ld_lines_indexed = lf->size();
552✔
1352
        }
1353

1354
        this->lss_filtered_index.reserve(this->lss_index.size());
1,142✔
1355

1356
        uint32_t filter_in_mask, filter_out_mask;
1357
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,142✔
1358

1359
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,142✔
1360
            this->lss_index_delegate->index_start(*this);
1,142✔
1361
        }
1362

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

1372
            if (!(*ld)->is_visible()) {
13,813✔
1373
                continue;
×
1374
            }
1375

1376
            auto* lf = (*ld)->get_file_ptr();
13,813✔
1377
            auto line_iter = lf->begin() + line_number;
13,813✔
1378

1379
            if (line_iter->is_ignored()) {
13,813✔
1380
                continue;
×
1381
            }
1382

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

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

1410
        this->lss_indexing_in_progress = false;
1,142✔
1411

1412
        if (this->lss_index_delegate != nullptr) {
1,142✔
1413
            this->lss_index_delegate->index_complete(*this);
1,142✔
1414
        }
1415
    }
1416

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

1438
    return retval;
4,327✔
1439
}
4,327✔
1440

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

1447
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,213✔
1448
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,213✔
1449
    auto& bm_files = bm[&BM_FILES];
2,213✔
1450

1451
    bm_warnings.clear();
2,213✔
1452
    bm_errors.clear();
2,213✔
1453
    bm_files.clear();
2,213✔
1454

1455
    std::vector<const bookmark_type_t*> used_marks;
2,213✔
1456
    for (const auto* bmt :
11,065✔
1457
         {
1458
             &textview_curses::BM_USER,
1459
             &textview_curses::BM_USER_EXPR,
1460
             &textview_curses::BM_PARTITION,
1461
             &textview_curses::BM_META,
1462
         })
13,278✔
1463
    {
1464
        bm[bmt].clear();
8,852✔
1465
        if (!this->lss_user_marks[bmt].empty()) {
8,852✔
1466
            used_marks.emplace_back(bmt);
117✔
1467
        }
1468
    }
1469

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

1475
        for (const auto& bmt : used_marks) {
19,430✔
1476
            auto& user_mark = this->lss_user_marks[bmt];
2,102✔
1477
            if (user_mark.bv_tree.exists(orig_ic.value())) {
2,102✔
1478
                bm[bmt].insert_once(vl);
155✔
1479
            }
1480
        }
1481

1482
        if (lf != last_file) {
17,328✔
1483
            bm_files.insert_once(vl);
966✔
1484
        }
1485

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

1491
            case indexed_content::level_t::error:
3,420✔
1492
                bm_errors.insert_once(vl);
3,420✔
1493
                break;
3,420✔
1494

1495
            default:
13,814✔
1496
                break;
13,814✔
1497
        }
1498

1499
        last_file = lf;
17,328✔
1500
    }
1501
}
2,213✔
1502

1503
void
1504
logfile_sub_source::text_filters_changed()
165✔
1505
{
1506
    this->lss_index_generation += 1;
165✔
1507
    this->tss_level_filtered_count = 0;
165✔
1508

1509
    if (this->lss_line_meta_changed) {
165✔
1510
        this->invalidate_sql_filter();
24✔
1511
        this->lss_line_meta_changed = false;
24✔
1512
    }
1513

1514
    for (auto& ld : *this) {
283✔
1515
        auto* lf = ld->get_file_ptr();
118✔
1516

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

1524
    if (this->lss_force_rebuild) {
165✔
1525
        return;
×
1526
    }
1527

1528
    auto& vis_bm = this->tss_view->get_bookmarks();
165✔
1529
    uint32_t filtered_in_mask, filtered_out_mask;
1530

1531
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
165✔
1532

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

1538
    this->lss_filtered_index.clear();
165✔
1539
    for (size_t index_index = 0; index_index < this->lss_index.size();
1,493✔
1540
         index_index++)
1541
    {
1542
        auto cl = this->lss_index[index_index].value();
1,328✔
1543
        uint64_t line_number;
1544
        auto ld = this->find_data(cl, line_number);
1,328✔
1545

1546
        if (!(*ld)->is_visible()) {
1,328✔
1547
            continue;
213✔
1548
        }
1549

1550
        auto lf = (*ld)->get_file_ptr();
1,115✔
1551
        auto line_iter = lf->begin() + line_number;
1,115✔
1552

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

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

1580
    if (this->lss_index_delegate != nullptr) {
165✔
1581
        this->lss_index_delegate->index_complete(*this);
165✔
1582
    }
1583

1584
    if (this->tss_view != nullptr) {
165✔
1585
        this->tss_view->reload_data();
165✔
1586
        this->tss_view->redo_search();
165✔
1587
    }
1588
}
1589

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

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

1608
                {
1609
                    yajlpp_map root(gen);
×
1610

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

1615
                return json_string(gen);
×
1616
            }
1617
        }
1618
    }
1619

1620
    return std::nullopt;
26✔
1621
}
1622

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

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

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

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

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

1740
    std::shared_ptr<logfile> lde_file;
1741
};
1742

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

1748
    require_lt(lf->size(), MAX_LINES_PER_FILE);
519✔
1749

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

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

1766
    return true;
519✔
1767
}
1768

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

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

1784
    for (auto& ld : *this) {
790✔
1785
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
17✔
1786
    }
1787

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

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

1802
    return Ok();
773✔
1803
}
1804

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

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

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

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

1831
    auto& vis_bm = this->tss_view->get_bookmarks();
641✔
1832
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
641✔
1833
    auto& cl_marks_bv = this->lss_user_marks[&textview_curses::BM_USER_EXPR];
641✔
1834

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

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

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

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

1877
    return Ok();
641✔
1878
}
771✔
1879

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

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

1895
    this->lss_preview_filter_stmt = stmt;
766✔
1896

1897
    return Ok();
766✔
1898
}
1899

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

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

1922
    sqlite3_reset(stmt);
606✔
1923
    sqlite3_clear_bindings(stmt);
606✔
1924

1925
    auto count = sqlite3_bind_parameter_count(stmt);
606✔
1926
    for (int lpc = 0; lpc < count; lpc++) {
1,215✔
1927
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
609✔
1928

1929
        if (name[0] == '$') {
609✔
1930
            const char* env_value;
1931

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

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

2007
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
2008

2009
                {
2010
                    yajlpp_array arr(gen);
1✔
2011

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

2017
                string_fragment sf = gen.to_string_fragment();
1✔
2018

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

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

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

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

2134
    auto step_res = sqlite3_step(stmt);
606✔
2135

2136
    sqlite3_reset(stmt);
606✔
2137
    sqlite3_clear_bindings(stmt);
606✔
2138
    switch (step_res) {
606✔
2139
        case SQLITE_OK:
465✔
2140
        case SQLITE_DONE:
2141
            return Ok(false);
930✔
2142
        case SQLITE_ROW:
140✔
2143
            return Ok(true);
280✔
2144
        default:
1✔
2145
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2146
    }
2147
}
606✔
2148

2149
bool
2150
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
14,762✔
2151
{
2152
    auto retval = true;
14,762✔
2153

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

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

2182
    if (*ll < this->ttt_min_row_time) {
14,762✔
2183
        retval = false;
36✔
2184
    }
2185

2186
    if (!(*ll <= this->ttt_max_row_time)) {
14,762✔
2187
        retval = false;
4✔
2188
    }
2189

2190
    return retval;
14,762✔
2191
}
2192

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

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

2210
    auto cl = this->at(line);
148✔
2211

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

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

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

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

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

2259
            if (file_range.first != file_range.second) {
4,152✔
2260
                auto to_del = std::vector<content_line_t>{};
68✔
2261
                for (auto file_iter = file_range.first;
68✔
2262
                     file_iter != file_range.second;
176✔
2263
                     ++file_iter)
108✔
2264
                {
2265
                    to_del.emplace_back(*file_iter);
108✔
2266
                }
2267

2268
                for (auto cl : to_del) {
176✔
2269
                    bv.erase(cl);
108✔
2270
                }
2271
            }
68✔
2272
        }
2273

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

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

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

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

2301
        auto vis_start = *vis_start_opt;
5✔
2302

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

2306
            if (cl == guess_cl) {
5✔
2307
                return vis_start;
5✔
2308
            }
2309

2310
            auto guess_line = this->find_line(guess_cl);
×
2311

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

2316
            ++vis_start;
×
2317
        }
2318
    }
2319

2320
    return std::nullopt;
×
2321
}
5✔
2322

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

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

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

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

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

2361
    auto cl = this->llh_log_source.at(top);
95✔
2362

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

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

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

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

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

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

2388
        iter += this->lh_history_position;
2✔
2389

2390
        vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2391

2392
        if (vis_for_pos) {
2✔
2393
            return vis_for_pos;
2✔
2394
        }
2395
    }
2396

2397
    return std::nullopt;
×
2398
}
2399

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

2406
        auto iter = this->llh_history.rbegin();
1✔
2407

2408
        iter += this->lh_history_position;
1✔
2409

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

2412
        if (vis_for_pos) {
1✔
2413
            return vis_for_pos;
1✔
2414
        }
2415
    }
2416

2417
    return std::nullopt;
×
2418
}
2419

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

2428
    auto ls = ls_opt;
124✔
2429

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

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

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

2449
    return true;
47✔
2450
}
121✔
2451

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

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

2468
        {
2469
            md2attr_line mdal;
×
2470

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

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

2489
            md2attr_line mdal;
×
2490

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

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

2507
    return std::nullopt;
×
2508
}
2509

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

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

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

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

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

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

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

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

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

2577
    return retval;
10✔
2578
}
20✔
2579

2580
static attr_line_t
2581
to_display(const std::shared_ptr<logfile>& lf)
20✔
2582
{
2583
    attr_line_t retval;
20✔
2584

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

2592
    return retval;
20✔
2593
}
×
2594

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

2601
    if (this->lss_filtered_index.empty()) {
18✔
2602
        return;
8✔
2603
    }
2604

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

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

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

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

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

2652
    sql_strftime(ts, sizeof(ts), line_pair.second->get_timeval(), 'T');
10✔
2653

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

2666
    auto format_name = format->get_name().to_string();
10✔
2667

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

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

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

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

2739
    shared_buffer sb;
10✔
2740
    logline_value_vector values;
10✔
2741
    auto& sbr = values.lvv_sbr;
10✔
2742

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

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

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

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

2786
                    for (const auto& pair : r_tid_map->ltis_tid_ranges) {
22✔
2787
                        poss_strs.emplace(pair.first.to_string());
12✔
2788
                    }
2789
                }
10✔
2790

2791
                std::vector<breadcrumb::possibility> retval;
10✔
2792

2793
                std::transform(poss_strs.begin(),
10✔
2794
                               poss_strs.end(),
2795
                               std::back_inserter(retval),
2796
                               [](const auto& tid_str) {
12✔
2797
                                   return breadcrumb::possibility(tid_str);
12✔
2798
                               });
2799

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

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

2820
        auto opid_display = values.lvv_opid_value.has_value()
10✔
2821
            ? lnav::roles::identifier(values.lvv_opid_value.value())
9✔
2822
            : lnav::roles::hidden(ELLIPSIS);
19✔
2823
        crumbs.emplace_back(
20✔
2824
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
21✔
2825
                                              : "",
2826
            attr_line_t().append(opid_display),
10✔
2827
            [this]() -> std::vector<breadcrumb::possibility> {
×
2828
                std::unordered_set<std::string> poss_strs;
10✔
2829

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

2837
                    poss_strs.reserve(poss_strs.size()
20✔
2838
                                      + r_opid_map->los_opid_ranges.size());
10✔
2839
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
782✔
2840
                        poss_strs.insert(pair.first.to_string());
772✔
2841
                    }
2842
                }
10✔
2843

2844
                std::vector<breadcrumb::possibility> retval;
10✔
2845
                retval.reserve(poss_strs.size());
10✔
2846

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

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

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

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

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

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

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

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

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

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

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

3005
        if (lf == nullptr) {
42✔
3006
            continue;
×
3007
        }
3008

3009
        lf->quiesce();
42✔
3010
    }
3011
}
42✔
3012

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

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

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

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

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

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

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

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

3077
    auto& bm = line_pair.first->get_bookmark_metadata();
23,159✔
3078
    auto bm_iter = bm.find(line_number);
23,159✔
3079
    if (bm_iter == bm.end()) {
23,159✔
3080
        return std::nullopt;
22,805✔
3081
    }
3082

3083
    return &bm_iter->second;
354✔
3084
}
23,159✔
3085

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

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

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

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

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

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

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

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

3163
        return true;
×
3164
    }
3165

3166
    return false;
×
3167
}
3168

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

3182
size_t
3183
logfile_sub_source::file_count() const
4,026✔
3184
{
3185
    size_t retval = 0;
4,026✔
3186
    const_iterator iter;
4,026✔
3187

3188
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,646✔
3189
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,620✔
3190
            retval += 1;
3,614✔
3191
        }
3192
    }
3193

3194
    return retval;
4,026✔
3195
}
3196

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

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

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

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

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

3241
    return retval;
1✔
3242
}
3243

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

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

3275
    return std::nullopt;
5✔
3276
}
3277

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

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

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

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

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

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

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

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

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

3337
        return std::nullopt;
×
3338
    }
3339

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

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

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

3355
    return std::nullopt;
×
3356
}
3357

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

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

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

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

3434
            return retval;
74✔
3435
        }
296✔
3436

3437
        return std::nullopt;
×
3438
    }
74✔
3439

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

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

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

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

3461
    return retval;
×
3462
}
×
3463

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

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

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

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

3521
void
3522
logfile_sub_source::add_commands_for_session(
45✔
3523
    const std::function<void(const std::string&)>& receiver)
3524
{
3525
    text_sub_source::add_commands_for_session(receiver);
45✔
3526

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

3536
    for (const auto& format : log_format::get_root_formats()) {
3,352✔
3537
        auto field_states = format->get_field_states();
3,307✔
3538

3539
        for (const auto& fs_pair : field_states) {
45,104✔
3540
            if (!fs_pair.second.lvm_user_hidden) {
41,797✔
3541
                continue;
41,795✔
3542
            }
3543

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

3557
void
3558
logfile_sub_source::update_filter_hash_state(hasher& h) const
5✔
3559
{
3560
    text_sub_source::update_filter_hash_state(h);
5✔
3561

3562
    for (const auto& ld : this->lss_files) {
5✔
UNCOV
3563
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
×
3564
            h.update(0);
×
3565
        } else {
UNCOV
3566
            h.update(1);
×
3567
        }
3568
    }
3569
    h.update(this->tss_min_log_level);
5✔
3570
    h.update(this->lss_marked_only);
5✔
3571
}
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc