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

tstack / lnav / 18602397746-2581

17 Oct 2025 07:07PM UTC coverage: 69.26% (-0.6%) from 69.886%
18602397746-2581

push

github

tstack
[log-view] if the top message is not fully in view, display the first line

82 of 105 new or added lines in 5 files covered. (78.1%)

481 existing lines in 35 files now uncovered.

50183 of 72456 relevant lines covered (69.26%)

415322.24 hits per line

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

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

30
#include <algorithm>
31
#include <chrono>
32
#include <future>
33

34
#include "logfile_sub_source.hh"
35

36
#include <sqlite3.h>
37

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

150
        return retval;
×
151
    });
×
152

153
    return retval;
×
154
}
155

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

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

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

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

188
    return retval;
22✔
189
}
×
190

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

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

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

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

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

221
    const logfile_sub_source& llss_controller;
222
};
223

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

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

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

251
    line_info retval;
2,937✔
252
    content_line_t line(0);
2,937✔
253

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

257
    line = this->at(vis_line_t(row));
2,937✔
258

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

281
    require_false(this->lss_in_value_for_line);
2,905✔
282

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

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

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

313
    auto format = this->lss_token_file->get_format();
2,905✔
314

315
    value_out = this->lss_token_value;
2,905✔
316

317
    auto& sbr = this->lss_token_values.lvv_sbr;
2,905✔
318

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

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

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

348
    std::optional<exttm> adjusted_tm;
2,905✔
349
    auto time_attr = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
2,905✔
350
    if (!this->lss_token_line->is_continued() && !format->lf_formatted_lines
4,096✔
351
        && (this->lss_token_file->is_time_adjusted()
958✔
352
            || ((format->lf_timestamp_flags & ETF_ZONE_SET
943✔
353
                 || format->lf_date_time.dts_default_zone != nullptr)
602✔
354
                && format->lf_date_time.dts_zoned_to_local)
425✔
355
            || format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
518✔
356
            || !(format->lf_timestamp_flags & ETF_DAY_SET)
518✔
357
            || !(format->lf_timestamp_flags & ETF_MONTH_SET))
497✔
358
        && format->lf_date_time.dts_fmt_lock != -1)
4,096✔
359
    {
360
        if (time_attr != this->lss_token_attrs.end()) {
405✔
361
            const auto time_range = time_attr->sa_range;
405✔
362
            const auto time_sf = string_fragment::from_str_range(
405✔
363
                this->lss_token_value, time_range.lr_start, time_range.lr_end);
405✔
364
            adjusted_tm = format->tm_for_display(this->lss_token_line, time_sf);
405✔
365

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

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

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

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

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

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

485
    this->lss_in_value_for_line = false;
2,905✔
486

487
    return retval;
2,905✔
488
}
2,905✔
489

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

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

505
    value_out = this->lss_token_attrs;
2,905✔
506

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

511
    if (next_line != nullptr
2,905✔
512
        && (day_num(next_line->get_time<std::chrono::seconds>().count())
5,629✔
513
            > day_num(this->lss_token_line->get_time<std::chrono::seconds>()
5,629✔
514
                          .count())))
515
    {
516
        attrs |= text_attrs::style::underline;
21✔
517
    }
518

519
    const auto& line_values = this->lss_token_values;
2,905✔
520

521
    lr.lr_start = 0;
2,905✔
522
    lr.lr_end = -1;
2,905✔
523
    value_out.emplace_back(
2,905✔
524
        lr, SA_LEVEL.value(this->lss_token_line->get_msg_level()));
5,810✔
525

526
    lr.lr_start = time_offset_end;
2,905✔
527
    lr.lr_end = -1;
2,905✔
528

529
    if (!attrs.empty()) {
2,905✔
530
        value_out.emplace_back(lr, VC_STYLE.value(attrs));
21✔
531
    }
532

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

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

544
    for (const auto& line_value : line_values.lvv_values) {
13,678✔
545
        if ((!(this->lss_token_flags & RF_FULL)
23,430✔
546
             && line_value.lv_sub_offset
21,102✔
547
                 != this->lss_token_line->get_sub_offset())
10,551✔
548
            || !line_value.lv_origin.is_valid())
21,324✔
549
        {
550
            continue;
1,884✔
551
        }
552

553
        if (line_value.lv_meta.is_hidden()) {
8,889✔
554
            value_out.emplace_back(line_value.lv_origin,
153✔
555
                                   SA_HIDDEN.value(ui_icon_t::hidden));
306✔
556
        }
557

558
        if (!line_value.lv_meta.lvm_identifier
22,825✔
559
            || !line_value.lv_origin.is_valid())
8,889✔
560
        {
561
            continue;
5,047✔
562
        }
563

564
        value_out.emplace_back(line_value.lv_origin,
3,842✔
565
                               VC_ROLE.value(role_t::VCR_IDENTIFIER));
7,684✔
566
    }
567

568
    if (this->lss_token_shift_size) {
2,905✔
569
        shift_string_attrs(value_out,
61✔
570
                           this->lss_token_shift_start + 1,
61✔
571
                           this->lss_token_shift_size);
572
    }
573

574
    shift_string_attrs(value_out, 0, 1);
2,905✔
575

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

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

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

607
    value_out.emplace_back(lr,
2,905✔
608
                           VC_STYLE.value(vc.attrs_for_ident(
5,810✔
609
                               this->lss_token_file->get_filename())));
2,905✔
610

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

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

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

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

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

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

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

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

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

713
    lr.lr_start = 0;
2,905✔
714
    lr.lr_end = -1;
2,905✔
715
    value_out.emplace_back(lr, L_FILE.value(this->lss_token_file));
2,905✔
716
    value_out.emplace_back(
2,905✔
717
        lr, SA_FORMAT.value(this->lss_token_file->get_format()->get_name()));
5,810✔
718

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

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

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

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

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

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

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

771
    if (!this->lss_token_line->is_continued()) {
2,905✔
772
        if (this->lss_preview_filter_stmt != nullptr) {
1,191✔
773
            auto color = styling::color_unit::EMPTY;
×
774
            auto eval_res
775
                = this->eval_sql_filter(this->lss_preview_filter_stmt.in(),
776
                                        this->lss_token_file_data,
777
                                        this->lss_token_line);
×
778
            if (eval_res.isErr()) {
×
779
                color = palette_color{
×
780
                    lnav::enums::to_underlying(ansi_color::yellow)};
×
781
                value_out.emplace_back(
×
782
                    line_range{0, -1},
×
783
                    SA_ERROR.value(
×
784
                        eval_res.unwrapErr().to_attr_line().get_string()));
×
785
            } else {
786
                auto matched = eval_res.unwrap();
×
787

788
                if (matched) {
×
789
                    color = palette_color{
×
790
                        lnav::enums::to_underlying(ansi_color::green)};
×
791
                } else {
792
                    color = palette_color{
×
793
                        lnav::enums::to_underlying(ansi_color::red)};
×
794
                    value_out.emplace_back(
×
795
                        line_range{0, 1},
×
796
                        VC_STYLE.value(text_attrs::with_blink()));
×
797
                }
798
            }
799
            value_out.emplace_back(line_range{0, 1},
×
800
                                   VC_BACKGROUND.value(color));
×
801
        }
802

803
        auto sql_filter_opt = this->get_sql_filter();
1,191✔
804
        if (sql_filter_opt) {
1,191✔
805
            auto* sf = (sql_filter*) sql_filter_opt.value().get();
36✔
806
            auto eval_res = this->eval_sql_filter(sf->sf_filter_stmt.in(),
807
                                                  this->lss_token_file_data,
808
                                                  this->lss_token_line);
36✔
809
            if (eval_res.isErr()) {
36✔
810
                auto msg = fmt::format(
811
                    FMT_STRING(
×
812
                        "filter expression evaluation failed with -- {}"),
813
                    eval_res.unwrapErr().to_attr_line().get_string());
×
814
                auto cu = styling::color_unit::from_palette(palette_color{
×
815
                    lnav::enums::to_underlying(ansi_color::yellow)});
816
                value_out.emplace_back(line_range{0, -1}, SA_ERROR.value(msg));
×
817
                value_out.emplace_back(line_range{0, 1},
×
818
                                       VC_BACKGROUND.value(cu));
×
819
            }
820
        }
36✔
821
    }
1,191✔
822
}
823

824
struct logline_cmp {
825
    logline_cmp(logfile_sub_source& lc) : llss_controller(lc) {}
1,085✔
826

827
    bool operator()(const logfile_sub_source::indexed_content& lhs,
93,633✔
828
                    const logfile_sub_source::indexed_content& rhs) const
829
    {
830
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
93,633✔
831
        const auto* ll_rhs = this->llss_controller.find_line(rhs.value());
93,633✔
832

833
        return (*ll_lhs) < (*ll_rhs);
93,633✔
834
    }
835

836
    bool operator()(const uint32_t& lhs, const uint32_t& rhs) const
837
    {
838
        auto cl_lhs = llss_controller.lss_index[lhs].value();
839
        auto cl_rhs = llss_controller.lss_index[rhs].value();
840
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
841
        const auto* ll_rhs = this->llss_controller.find_line(cl_rhs);
842

843
        return (*ll_lhs) < (*ll_rhs);
844
    }
845
#if 0
846
        bool operator()(const indexed_content &lhs, const indexed_content &rhs)
847
        {
848
            logline *ll_lhs = this->llss_controller.find_line(lhs.ic_value);
849
            logline *ll_rhs = this->llss_controller.find_line(rhs.ic_value);
850

851
            return (*ll_lhs) < (*ll_rhs);
852
        }
853
#endif
854

855
#if 0
856
    bool operator()(const content_line_t& lhs, const time_t& rhs) const
857
    {
858
        logline* ll_lhs = this->llss_controller.find_line(lhs);
859

860
        return *ll_lhs < rhs;
861
    }
862
#endif
863

864
    bool operator()(const logfile_sub_source::indexed_content& lhs,
×
865
                    const struct timeval& rhs) const
866
    {
867
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
×
868

869
        return *ll_lhs < rhs;
×
870
    }
871

872
    logfile_sub_source& llss_controller;
873
};
874

875
logfile_sub_source::rebuild_result
876
logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
4,183✔
877
{
878
    if (this->tss_view == nullptr) {
4,183✔
879
        return rebuild_result::rr_no_change;
122✔
880
    }
881

882
    this->lss_indexing_in_progress = true;
4,061✔
883
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
4,061✔
884

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

896
    if (force) {
4,061✔
897
        log_debug("forced to full rebuild");
456✔
898
        retval = rebuild_result::rr_full_rebuild;
456✔
899
        full_sort = true;
456✔
900
    }
901

902
    std::vector<size_t> file_order(this->lss_files.size());
4,061✔
903

904
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
6,920✔
905
        file_order[lpc] = lpc;
2,859✔
906
    }
907
    if (!this->lss_index.empty()) {
4,061✔
908
        std::stable_sort(file_order.begin(),
2,592✔
909
                         file_order.end(),
910
                         [this](const auto& left, const auto& right) {
241✔
911
                             const auto& left_ld = this->lss_files[left];
241✔
912
                             const auto& right_ld = this->lss_files[right];
241✔
913

914
                             if (left_ld->get_file_ptr() == nullptr) {
241✔
915
                                 return true;
×
916
                             }
917
                             if (right_ld->get_file_ptr() == nullptr) {
241✔
918
                                 return false;
4✔
919
                             }
920

921
                             return left_ld->get_file_ptr()->back()
237✔
922
                                 < right_ld->get_file_ptr()->back();
474✔
923
                         });
924
    }
925

926
    bool time_left = true;
4,061✔
927
    this->lss_all_timestamp_flags = 0;
4,061✔
928
    for (const auto file_index : file_order) {
6,920✔
929
        auto& ld = *(this->lss_files[file_index]);
2,859✔
930
        auto* lf = ld.get_file_ptr();
2,859✔
931

932
        if (lf == nullptr) {
2,859✔
933
            if (ld.ld_lines_indexed > 0) {
4✔
934
                log_debug("%d: file closed, doing full rebuild",
1✔
935
                          ld.ld_file_index);
936
                force = true;
1✔
937
                retval = rebuild_result::rr_full_rebuild;
1✔
938
                full_sort = true;
1✔
939
            }
940
        } else {
941
            if (!lf->get_format_ptr()->lf_time_ordered) {
2,855✔
942
                all_time_ordered_formats = false;
161✔
943
            }
944
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
2,855✔
UNCOV
945
                log_debug("no time left, skipping %s",
×
946
                          lf->get_filename_as_string().c_str());
UNCOV
947
                time_left = false;
×
948
            }
949
            this->lss_all_timestamp_flags
2,855✔
950
                |= lf->get_format_ptr()->lf_timestamp_flags;
2,855✔
951

952
            if (!this->tss_view->is_paused() && time_left) {
2,855✔
953
                auto log_rebuild_res = lf->rebuild_index(deadline);
2,855✔
954

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

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

1036
            est_remaining_lines += lf->estimated_remaining_lines();
2,855✔
1037
        }
1038
    }
1039

1040
    if (!all_time_ordered_formats
4,061✔
1041
        && retval == rebuild_result::rr_partial_rebuild)
155✔
1042
    {
1043
        force = true;
×
1044
        full_sort = true;
×
1045
        retval = rebuild_result::rr_full_rebuild;
×
1046
    }
1047

1048
    if (this->lss_index.reserve(total_lines + est_remaining_lines)) {
4,061✔
1049
        // The index array was reallocated, just do a full sort/rebuild since
1050
        // it's been cleared out.
1051
        log_debug("expanding index capacity %zu", this->lss_index.ba_capacity);
601✔
1052
        force = true;
601✔
1053
        retval = rebuild_result::rr_full_rebuild;
601✔
1054
        full_sort = true;
601✔
1055
    }
1056

1057
    auto& vis_bm = this->tss_view->get_bookmarks();
4,061✔
1058

1059
    if (force) {
4,061✔
1060
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,574✔
1061
             iter++)
503✔
1062
        {
1063
            (*iter)->ld_lines_indexed = 0;
503✔
1064
        }
1065

1066
        this->lss_index.clear();
1,071✔
1067
        this->lss_filtered_index.clear();
1,071✔
1068
        this->lss_longest_line = 0;
1,071✔
1069
        this->lss_basename_width = 0;
1,071✔
1070
        this->lss_filename_width = 0;
1,071✔
1071
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,071✔
1072
        if (this->lss_index_delegate) {
1,071✔
1073
            this->lss_index_delegate->index_start(*this);
1,071✔
1074
        }
1075
    } else if (retval == rebuild_result::rr_partial_rebuild) {
2,990✔
1076
        size_t remaining = 0;
×
1077

1078
        log_debug("partial rebuild with lowest time: %ld",
×
1079
                  lowest_tv.value().tv_sec);
1080
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1081
             iter++)
×
1082
        {
1083
            logfile_data& ld = *(*iter);
×
1084
            auto* lf = ld.get_file_ptr();
×
1085

1086
            if (lf == nullptr) {
×
1087
                continue;
×
1088
            }
1089

1090
            require(lf->get_format_ptr()->lf_time_ordered);
×
1091

1092
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1093

1094
            if (line_iter) {
×
1095
                log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
×
1096
                          line_iter.value()->get_timeval().tv_sec,
1097
                          std::distance(lf->cbegin(), line_iter.value()),
1098
                          lf->size(),
1099
                          lf->get_filename_as_string().c_str());
1100
            }
1101
            ld.ld_lines_indexed
1102
                = std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
×
1103
            remaining += lf->size() - ld.ld_lines_indexed;
×
1104
        }
1105

1106
        auto* row_iter = std::lower_bound(this->lss_index.begin(),
×
1107
                                          this->lss_index.end(),
1108
                                          lowest_tv.value(),
×
1109
                                          logline_cmp(*this));
1110
        this->lss_index.shrink_to(
×
1111
            std::distance(this->lss_index.begin(), row_iter));
×
1112
        log_debug("new index size %ld/%ld; remain %ld",
×
1113
                  this->lss_index.ba_size,
1114
                  this->lss_index.ba_capacity,
1115
                  remaining);
1116
        auto filt_row_iter = std::lower_bound(this->lss_filtered_index.begin(),
×
1117
                                              this->lss_filtered_index.end(),
1118
                                              lowest_tv.value(),
×
1119
                                              filtered_logline_cmp(*this));
1120
        this->lss_filtered_index.resize(
×
1121
            std::distance(this->lss_filtered_index.begin(), filt_row_iter));
×
1122
        search_start = vis_line_t(this->lss_filtered_index.size());
×
1123

1124
        if (this->lss_index_delegate) {
×
1125
            this->lss_index_delegate->index_start(*this);
×
1126
            for (const auto row_in_full_index : this->lss_filtered_index) {
×
1127
                auto cl = this->lss_index[row_in_full_index].value();
×
1128
                uint64_t line_number;
1129
                auto ld_iter = this->find_data(cl, line_number);
×
1130
                auto& ld = *ld_iter;
×
1131
                auto line_iter = ld->get_file_ptr()->begin() + line_number;
×
1132

1133
                this->lss_index_delegate->index_line(
×
1134
                    *this, ld->get_file_ptr(), line_iter);
1135
            }
1136
        }
1137
    }
1138

1139
    if (this->lss_index.empty() && !time_left) {
4,061✔
1140
        log_info("ran out of time, skipping rebuild");
×
1141
        // need to make sure we rebuild in case no new data comes in
1142
        this->lss_force_rebuild = true;
×
1143
        return rebuild_result::rr_appended_lines;
×
1144
    }
1145

1146
    if (retval != rebuild_result::rr_no_change || force) {
4,061✔
1147
        size_t index_size = 0, start_size = this->lss_index.size();
1,085✔
1148
        logline_cmp line_cmper(*this);
1,085✔
1149

1150
        for (auto& ld : this->lss_files) {
1,611✔
1151
            auto* lf = ld->get_file_ptr();
526✔
1152

1153
            if (lf == nullptr) {
526✔
1154
                continue;
1✔
1155
            }
1156
            this->lss_longest_line = std::max(
1,050✔
1157
                this->lss_longest_line, lf->get_longest_line_length() + 1);
525✔
1158
            this->lss_basename_width
1159
                = std::max(this->lss_basename_width,
1,050✔
1160
                           lf->get_unique_path().native().size());
525✔
1161
            this->lss_filename_width = std::max(
1,050✔
1162
                this->lss_filename_width, lf->get_filename().native().size());
525✔
1163
        }
1164

1165
        if (full_sort) {
1,085✔
1166
            log_trace("rebuild_index full sort");
1,085✔
1167
            for (auto& ld : this->lss_files) {
1,611✔
1168
                auto* lf = ld->get_file_ptr();
526✔
1169

1170
                if (lf == nullptr) {
526✔
1171
                    continue;
1✔
1172
                }
1173

1174
                for (size_t line_index = 0; line_index < lf->size();
12,918✔
1175
                     line_index++)
1176
                {
1177
                    const auto lf_iter
1178
                        = ld->get_file_ptr()->begin() + line_index;
12,393✔
1179
                    if (lf_iter->is_ignored()) {
12,393✔
1180
                        continue;
233✔
1181
                    }
1182

1183
                    content_line_t con_line(
1184
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
12,160✔
1185

1186
                    if (lf_iter->is_meta_marked()) {
12,160✔
1187
                        auto start_iter = lf_iter;
11✔
1188
                        while (start_iter->is_continued()) {
11✔
1189
                            --start_iter;
×
1190
                        }
1191
                        int start_index
1192
                            = start_iter - ld->get_file_ptr()->begin();
11✔
1193
                        content_line_t start_con_line(ld->ld_file_index
11✔
1194
                                                          * MAX_LINES_PER_FILE
11✔
1195
                                                      + start_index);
11✔
1196

1197
                        auto& line_meta
1198
                            = ld->get_file_ptr()
1199
                                  ->get_bookmark_metadata()[start_index];
11✔
1200
                        if (line_meta.has(bookmark_metadata::categories::notes))
11✔
1201
                        {
1202
                            this->lss_user_marks[&textview_curses::BM_META]
3✔
1203
                                .insert_once(start_con_line);
3✔
1204
                        }
1205
                        if (line_meta.has(
11✔
1206
                                bookmark_metadata::categories::partition))
1207
                        {
1208
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
8✔
1209
                                .insert_once(start_con_line);
8✔
1210
                        }
1211
                    }
1212
                    this->lss_index.push_back(
12,160✔
1213
                        indexed_content{con_line, lf_iter});
24,320✔
1214
                }
1215
            }
1216

1217
            // XXX get rid of this full sort on the initial run, it's not
1218
            // needed unless the file is not in time-order
1219
            if (this->lss_sorting_observer) {
1,085✔
1220
                this->lss_sorting_observer(*this, 0, this->lss_index.size());
3✔
1221
            }
1222
            std::sort(
1,085✔
1223
                this->lss_index.begin(), this->lss_index.end(), line_cmper);
1224
            if (this->lss_sorting_observer) {
1,085✔
1225
                this->lss_sorting_observer(
6✔
1226
                    *this, this->lss_index.size(), this->lss_index.size());
3✔
1227
            }
1228
        } else {
1229
            kmerge_tree_c<logline, logfile_data, logfile::iterator> merge(
1230
                file_count);
×
1231

1232
            for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1233
                 iter++)
×
1234
            {
1235
                auto* ld = iter->get();
×
1236
                auto* lf = ld->get_file_ptr();
×
1237
                if (lf == nullptr) {
×
1238
                    continue;
×
1239
                }
1240

1241
                merge.add(ld, lf->begin() + ld->ld_lines_indexed, lf->end());
×
1242
                index_size += lf->size();
×
1243
            }
1244

1245
            file_off_t index_off = 0;
×
1246
            merge.execute();
×
1247
            if (this->lss_sorting_observer) {
×
1248
                this->lss_sorting_observer(*this, index_off, index_size);
×
1249
            }
1250
            log_trace("k-way merge");
×
1251
            for (;;) {
1252
                logfile::iterator lf_iter;
×
1253
                logfile_data* ld;
1254

1255
                if (!merge.get_top(ld, lf_iter)) {
×
1256
                    break;
×
1257
                }
1258

1259
                if (!lf_iter->is_ignored()) {
×
1260
                    int file_index = ld->ld_file_index;
×
1261
                    int line_index = lf_iter - ld->get_file_ptr()->begin();
×
1262

1263
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
1264
                                            + line_index);
×
1265

1266
                    if (lf_iter->is_meta_marked()) {
×
1267
                        auto start_iter = lf_iter;
×
1268
                        while (start_iter->is_continued()) {
×
1269
                            --start_iter;
×
1270
                        }
1271
                        int start_index
1272
                            = start_iter - ld->get_file_ptr()->begin();
×
1273
                        content_line_t start_con_line(
1274
                            file_index * MAX_LINES_PER_FILE + start_index);
×
1275

1276
                        auto& line_meta
1277
                            = ld->get_file_ptr()
1278
                                  ->get_bookmark_metadata()[start_index];
×
1279
                        if (line_meta.has(bookmark_metadata::categories::notes))
×
1280
                        {
1281
                            this->lss_user_marks[&textview_curses::BM_META]
×
1282
                                .insert_once(start_con_line);
×
1283
                        }
1284
                        if (line_meta.has(
×
1285
                                bookmark_metadata::categories::partition))
1286
                        {
1287
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
×
1288
                                .insert_once(start_con_line);
×
1289
                        }
1290
                    }
1291
                    this->lss_index.push_back(
×
1292
                        indexed_content{con_line, lf_iter});
×
1293
                }
1294

1295
                merge.next();
×
1296
                index_off += 1;
×
1297
                if (index_off % 100000 == 0 && this->lss_sorting_observer) {
×
1298
                    this->lss_sorting_observer(*this, index_off, index_size);
×
1299
                }
1300
            }
1301
            if (this->lss_sorting_observer) {
×
1302
                this->lss_sorting_observer(*this, index_size, index_size);
×
1303
            }
1304
        }
1305

1306
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,611✔
1307
             ++iter)
526✔
1308
        {
1309
            auto* lf = (*iter)->get_file_ptr();
526✔
1310

1311
            if (lf == nullptr) {
526✔
1312
                continue;
1✔
1313
            }
1314

1315
            (*iter)->ld_lines_indexed = lf->size();
525✔
1316
        }
1317

1318
        this->lss_filtered_index.reserve(this->lss_index.size());
1,085✔
1319

1320
        uint32_t filter_in_mask, filter_out_mask;
1321
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,085✔
1322

1323
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,085✔
1324
            this->lss_index_delegate->index_start(*this);
1,085✔
1325
        }
1326

1327
        log_trace("filtered index");
1,085✔
1328
        for (size_t index_index = start_size;
13,245✔
1329
             index_index < this->lss_index.size();
13,245✔
1330
             index_index++)
1331
        {
1332
            const auto cl = this->lss_index[index_index].value();
12,160✔
1333
            uint64_t line_number;
1334
            auto ld = this->find_data(cl, line_number);
12,160✔
1335

1336
            if (!(*ld)->is_visible()) {
12,160✔
1337
                continue;
×
1338
            }
1339

1340
            auto* lf = (*ld)->get_file_ptr();
12,160✔
1341
            auto line_iter = lf->begin() + line_number;
12,160✔
1342

1343
            if (line_iter->is_ignored()) {
12,160✔
1344
                continue;
×
1345
            }
1346

1347
            if (!this->tss_apply_filters
24,320✔
1348
                || (!(*ld)->ld_filter_state.excluded(
24,290✔
1349
                        filter_in_mask, filter_out_mask, line_number)
1350
                    && this->check_extra_filters(ld, line_iter)))
12,130✔
1351
            {
1352
                auto eval_res = this->eval_sql_filter(
1353
                    this->lss_marker_stmt.in(), ld, line_iter);
12,130✔
1354
                if (eval_res.isErr()) {
12,130✔
1355
                    line_iter->set_expr_mark(false);
×
1356
                } else {
1357
                    auto matched = eval_res.unwrap();
12,130✔
1358

1359
                    if (matched) {
12,130✔
1360
                        line_iter->set_expr_mark(true);
×
1361
                        vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1362
                            vis_line_t(this->lss_filtered_index.size()));
×
1363
                    } else {
1364
                        line_iter->set_expr_mark(false);
12,130✔
1365
                    }
1366
                }
1367
                this->lss_filtered_index.push_back(index_index);
12,130✔
1368
                if (this->lss_index_delegate != nullptr) {
12,130✔
1369
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
12,130✔
1370
                }
1371
            }
12,130✔
1372
        }
1373

1374
        this->lss_indexing_in_progress = false;
1,085✔
1375

1376
        if (this->lss_index_delegate != nullptr) {
1,085✔
1377
            this->lss_index_delegate->index_complete(*this);
1,085✔
1378
        }
1379
    }
1380

1381
    switch (retval) {
4,061✔
1382
        case rebuild_result::rr_no_change:
2,976✔
1383
            break;
2,976✔
1384
        case rebuild_result::rr_full_rebuild:
1,071✔
1385
            log_debug("redoing search");
1,071✔
1386
            this->lss_index_generation += 1;
1,071✔
1387
            this->tss_view->reload_data();
1,071✔
1388
            this->tss_view->redo_search();
1,071✔
1389
            break;
1,071✔
1390
        case rebuild_result::rr_partial_rebuild:
×
1391
            log_debug("redoing search from: %d", (int) search_start);
×
1392
            this->lss_index_generation += 1;
×
1393
            this->tss_view->reload_data();
×
1394
            this->tss_view->search_new_data(search_start);
×
1395
            break;
×
1396
        case rebuild_result::rr_appended_lines:
14✔
1397
            this->tss_view->reload_data();
14✔
1398
            this->tss_view->search_new_data();
14✔
1399
            break;
14✔
1400
    }
1401

1402
    return retval;
4,061✔
1403
}
4,061✔
1404

1405
void
1406
logfile_sub_source::text_update_marks(vis_bookmarks& bm)
2,096✔
1407
{
1408
    logfile* last_file = nullptr;
2,096✔
1409
    vis_line_t vl;
2,096✔
1410

1411
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,096✔
1412
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,096✔
1413
    auto& bm_files = bm[&BM_FILES];
2,096✔
1414

1415
    bm_warnings.clear();
2,096✔
1416
    bm_errors.clear();
2,096✔
1417
    bm_files.clear();
2,096✔
1418

1419
    std::vector<const bookmark_type_t*> used_marks;
2,096✔
1420
    for (const auto* bmt : {
10,480✔
1421
             &textview_curses::BM_USER,
1422
             &textview_curses::BM_USER_EXPR,
1423
             &textview_curses::BM_PARTITION,
1424
             &textview_curses::BM_META,
1425
         })
12,576✔
1426
    {
1427
        bm[bmt].clear();
8,384✔
1428
        if (!this->lss_user_marks[bmt].empty()) {
8,384✔
1429
            used_marks.emplace_back(bmt);
112✔
1430
        }
1431
    }
1432

1433
    for (; vl < (int) this->lss_filtered_index.size(); ++vl) {
16,147✔
1434
        const auto& orig_ic = this->lss_index[this->lss_filtered_index[vl]];
14,051✔
1435
        auto cl = orig_ic.value();
14,051✔
1436
        auto* lf = this->find_file_ptr(cl);
14,051✔
1437

1438
        for (const auto& bmt : used_marks) {
15,513✔
1439
            auto& user_mark = this->lss_user_marks[bmt];
1,462✔
1440
            if (user_mark.bv_tree.exists(orig_ic.value())) {
1,462✔
1441
                bm[bmt].insert_once(vl);
125✔
1442

1443
                if (bmt == &textview_curses::BM_USER) {
125✔
1444
                    auto ll = lf->begin() + cl;
40✔
1445

1446
                    ll->set_mark(true);
40✔
1447
                }
1448
            }
1449
        }
1450

1451
        if (lf != last_file) {
14,051✔
1452
            bm_files.insert_once(vl);
919✔
1453
        }
1454

1455
        switch (orig_ic.level()) {
14,051✔
1456
            case indexed_content::level_t::warning:
73✔
1457
                bm_warnings.insert_once(vl);
73✔
1458
                break;
73✔
1459

1460
            case indexed_content::level_t::error:
1,027✔
1461
                bm_errors.insert_once(vl);
1,027✔
1462
                break;
1,027✔
1463

1464
            default:
12,951✔
1465
                break;
12,951✔
1466
        }
1467

1468
        last_file = lf;
14,051✔
1469
    }
1470
}
2,096✔
1471

1472
void
1473
logfile_sub_source::text_filters_changed()
158✔
1474
{
1475
    this->lss_index_generation += 1;
158✔
1476

1477
    if (this->lss_line_meta_changed) {
158✔
1478
        this->invalidate_sql_filter();
24✔
1479
        this->lss_line_meta_changed = false;
24✔
1480
    }
1481

1482
    for (auto& ld : *this) {
269✔
1483
        auto* lf = ld->get_file_ptr();
111✔
1484

1485
        if (lf != nullptr) {
111✔
1486
            ld->ld_filter_state.clear_deleted_filter_state();
111✔
1487
            lf->reobserve_from(lf->begin()
111✔
1488
                               + ld->ld_filter_state.get_min_count(lf->size()));
111✔
1489
        }
1490
    }
1491

1492
    if (this->lss_force_rebuild) {
158✔
1493
        return;
×
1494
    }
1495

1496
    auto& vis_bm = this->tss_view->get_bookmarks();
158✔
1497
    uint32_t filtered_in_mask, filtered_out_mask;
1498

1499
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
158✔
1500

1501
    if (this->lss_index_delegate != nullptr) {
158✔
1502
        this->lss_index_delegate->index_start(*this);
158✔
1503
    }
1504
    vis_bm[&textview_curses::BM_USER_EXPR].clear();
158✔
1505

1506
    this->lss_filtered_index.clear();
158✔
1507
    for (size_t index_index = 0; index_index < this->lss_index.size();
1,051✔
1508
         index_index++)
1509
    {
1510
        auto cl = this->lss_index[index_index].value();
893✔
1511
        uint64_t line_number;
1512
        auto ld = this->find_data(cl, line_number);
893✔
1513

1514
        if (!(*ld)->is_visible()) {
893✔
1515
            continue;
210✔
1516
        }
1517

1518
        auto lf = (*ld)->get_file_ptr();
683✔
1519
        auto line_iter = lf->begin() + line_number;
683✔
1520

1521
        if (!this->tss_apply_filters
1,366✔
1522
            || (!(*ld)->ld_filter_state.excluded(
1,230✔
1523
                    filtered_in_mask, filtered_out_mask, line_number)
1524
                && this->check_extra_filters(ld, line_iter)))
547✔
1525
        {
1526
            auto eval_res = this->eval_sql_filter(
1527
                this->lss_marker_stmt.in(), ld, line_iter);
538✔
1528
            if (eval_res.isErr()) {
538✔
1529
                line_iter->set_expr_mark(false);
×
1530
            } else {
1531
                auto matched = eval_res.unwrap();
538✔
1532

1533
                if (matched) {
538✔
1534
                    line_iter->set_expr_mark(true);
×
1535
                    vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1536
                        vis_line_t(this->lss_filtered_index.size()));
×
1537
                } else {
1538
                    line_iter->set_expr_mark(false);
538✔
1539
                }
1540
            }
1541
            this->lss_filtered_index.push_back(index_index);
538✔
1542
            if (this->lss_index_delegate != nullptr) {
538✔
1543
                this->lss_index_delegate->index_line(*this, lf, line_iter);
538✔
1544
            }
1545
        }
538✔
1546
    }
1547

1548
    if (this->lss_index_delegate != nullptr) {
158✔
1549
        this->lss_index_delegate->index_complete(*this);
158✔
1550
    }
1551

1552
    if (this->tss_view != nullptr) {
158✔
1553
        this->tss_view->reload_data();
158✔
1554
        this->tss_view->redo_search();
158✔
1555
    }
1556
}
1557

1558
std::optional<json_string>
1559
logfile_sub_source::text_row_details(const textview_curses& tc)
25✔
1560
{
1561
    if (this->lss_index.empty()) {
25✔
1562
        log_trace("logfile_sub_source::text_row_details empty");
1✔
1563
        return std::nullopt;
1✔
1564
    }
1565

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

1576
                {
1577
                    yajlpp_map root(gen);
×
1578

1579
                    root.gen("value");
×
1580
                    root.gen(iter->second.ri_value);
×
1581
                }
1582

1583
                return json_string(gen);
×
1584
            }
1585
        }
1586
    }
1587

1588
    return std::nullopt;
24✔
1589
}
1590

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

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

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

1693
/**
1694
 * Functor for comparing the ld_file field of the logfile_data struct.
1695
 */
1696
struct logfile_data_eq {
1697
    explicit logfile_data_eq(std::shared_ptr<logfile> lf)
988✔
1698
        : lde_file(std::move(lf))
988✔
1699
    {
1700
    }
988✔
1701

1702
    bool operator()(
608✔
1703
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1704
    {
1705
        return this->lde_file == ld->get_file();
608✔
1706
    }
1707

1708
    std::shared_ptr<logfile> lde_file;
1709
};
1710

1711
bool
1712
logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
494✔
1713
{
1714
    iterator existing;
494✔
1715

1716
    require_lt(lf->size(), MAX_LINES_PER_FILE);
494✔
1717

1718
    existing = std::find_if(this->lss_files.begin(),
494✔
1719
                            this->lss_files.end(),
1720
                            logfile_data_eq(nullptr));
988✔
1721
    if (existing == this->lss_files.end()) {
494✔
1722
        if (this->lss_files.size() >= MAX_FILES) {
494✔
1723
            return false;
×
1724
        }
1725

1726
        auto ld = std::make_unique<logfile_data>(
1727
            this->lss_files.size(), this->get_filters(), lf);
494✔
1728
        ld->set_visibility(lf->get_open_options().loo_is_visible);
494✔
1729
        this->lss_files.push_back(std::move(ld));
494✔
1730
    } else {
494✔
1731
        (*existing)->set_file(lf);
×
1732
    }
1733

1734
    return true;
494✔
1735
}
1736

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

1746
        if (eval_res.isErr()) {
7✔
1747
            sqlite3_finalize(stmt);
1✔
1748
            return Err(eval_res.unwrapErr());
1✔
1749
        }
1750
    }
7✔
1751

1752
    for (auto& ld : *this) {
750✔
1753
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
15✔
1754
    }
1755

1756
    auto old_filter = this->get_sql_filter();
735✔
1757
    if (stmt != nullptr) {
735✔
1758
        auto new_filter
1759
            = std::make_shared<sql_filter>(*this, std::move(stmt_str), stmt);
6✔
1760

1761
        if (old_filter) {
6✔
1762
            auto existing_iter = std::find(this->tss_filters.begin(),
×
1763
                                           this->tss_filters.end(),
1764
                                           old_filter.value());
×
1765
            *existing_iter = new_filter;
×
1766
        } else {
1767
            this->tss_filters.add_filter(new_filter);
6✔
1768
        }
1769
    } else if (old_filter) {
735✔
1770
        this->tss_filters.delete_filter(old_filter.value()->get_id());
6✔
1771
    }
1772

1773
    return Ok();
735✔
1774
}
735✔
1775

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

1785
        if (eval_res.isErr()) {
2✔
1786
            sqlite3_finalize(stmt);
×
1787
            return Err(eval_res.unwrapErr());
×
1788
        }
1789
    }
2✔
1790

1791
    this->lss_marker_stmt_text = std::move(stmt_str);
731✔
1792
    this->lss_marker_stmt = stmt;
731✔
1793

1794
    if (this->tss_view == nullptr || this->lss_force_rebuild) {
731✔
1795
        return Ok();
128✔
1796
    }
1797

1798
    auto& vis_bm = this->tss_view->get_bookmarks();
603✔
1799
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
603✔
1800

1801
    expr_marks_bv.clear();
603✔
1802
    if (this->lss_index_delegate) {
603✔
1803
        this->lss_index_delegate->index_start(*this);
603✔
1804
    }
1805
    for (auto row = 0_vl; row < vis_line_t(this->lss_filtered_index.size());
610✔
1806
         row += 1_vl)
7✔
1807
    {
1808
        auto cl = this->at(row);
7✔
1809
        auto ld = this->find_data(cl);
7✔
1810

1811
        if (!(*ld)->is_visible()) {
7✔
1812
            continue;
1✔
1813
        }
1814
        auto ll = (*ld)->get_file()->begin() + cl;
7✔
1815
        if (ll->is_continued() || ll->is_ignored()) {
7✔
1816
            continue;
1✔
1817
        }
1818
        auto eval_res
1819
            = this->eval_sql_filter(this->lss_marker_stmt.in(), ld, ll);
6✔
1820

1821
        if (eval_res.isErr()) {
6✔
1822
            ll->set_expr_mark(false);
×
1823
        } else {
1824
            auto matched = eval_res.unwrap();
6✔
1825

1826
            if (matched) {
6✔
1827
                ll->set_expr_mark(true);
4✔
1828
                expr_marks_bv.insert_once(row);
4✔
1829
            } else {
1830
                ll->set_expr_mark(false);
2✔
1831
            }
1832
        }
1833
        if (this->lss_index_delegate) {
6✔
1834
            this->lss_index_delegate->index_line(
6✔
1835
                *this, (*ld)->get_file_ptr(), ll);
6✔
1836
        }
1837
    }
6✔
1838
    if (this->lss_index_delegate) {
603✔
1839
        this->lss_index_delegate->index_complete(*this);
603✔
1840
    }
1841

1842
    return Ok();
603✔
1843
}
1844

1845
Result<void, lnav::console::user_message>
1846
logfile_sub_source::set_preview_sql_filter(sqlite3_stmt* stmt)
746✔
1847
{
1848
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
746✔
1849
        auto top_cl = this->at(0_vl);
×
1850
        auto ld = this->find_data(top_cl);
×
1851
        auto eval_res
1852
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
×
1853

1854
        if (eval_res.isErr()) {
×
1855
            sqlite3_finalize(stmt);
×
1856
            return Err(eval_res.unwrapErr());
×
1857
        }
1858
    }
1859

1860
    this->lss_preview_filter_stmt = stmt;
746✔
1861

1862
    return Ok();
746✔
1863
}
1864

1865
Result<bool, lnav::console::user_message>
1866
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
12,844✔
1867
                                    iterator ld,
1868
                                    logfile::const_iterator ll)
1869
{
1870
    if (stmt == nullptr) {
12,844✔
1871
        return Ok(false);
25,336✔
1872
    }
1873

1874
    auto* lf = (*ld)->get_file_ptr();
176✔
1875
    char timestamp_buffer[64];
1876
    shared_buffer_ref raw_sbr;
176✔
1877
    logline_value_vector values;
176✔
1878
    auto& sbr = values.lvv_sbr;
176✔
1879
    lf->read_full_message(ll, sbr);
176✔
1880
    sbr.erase_ansi();
176✔
1881
    auto format = lf->get_format();
176✔
1882
    string_attrs_t sa;
176✔
1883
    auto line_number = std::distance(lf->cbegin(), ll);
176✔
1884
    format->annotate(lf, line_number, sa, values);
176✔
1885

1886
    sqlite3_reset(stmt);
176✔
1887
    sqlite3_clear_bindings(stmt);
176✔
1888

1889
    auto count = sqlite3_bind_parameter_count(stmt);
176✔
1890
    for (int lpc = 0; lpc < count; lpc++) {
355✔
1891
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
179✔
1892

1893
        if (name[0] == '$') {
179✔
1894
            const char* env_value;
1895

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

1954
                sqlite3_bind_text(stmt,
×
1955
                                  lpc + 1,
1956
                                  anno_str.c_str(),
1957
                                  anno_str.length(),
×
1958
                                  SQLITE_TRANSIENT);
1959
            }
1960
            continue;
×
1961
        }
1962
        if (strcmp(name, ":log_tags") == 0) {
174✔
1963
            const auto& bm = lf->get_bookmark_metadata();
9✔
1964
            auto line_number
1965
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
9✔
1966
            auto bm_iter = bm.find(line_number);
9✔
1967
            if (bm_iter != bm.end() && !bm_iter->second.bm_tags.empty()) {
9✔
1968
                const auto& meta = bm_iter->second;
1✔
1969
                yajlpp_gen gen;
1✔
1970

1971
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
1972

1973
                {
1974
                    yajlpp_array arr(gen);
1✔
1975

1976
                    for (const auto& str : meta.bm_tags) {
2✔
1977
                        arr.gen(str);
1✔
1978
                    }
1979
                }
1✔
1980

1981
                string_fragment sf = gen.to_string_fragment();
1✔
1982

1983
                sqlite3_bind_text(
1✔
1984
                    stmt, lpc + 1, sf.data(), sf.length(), SQLITE_TRANSIENT);
1985
            }
1✔
1986
            continue;
9✔
1987
        }
9✔
1988
        if (strcmp(name, ":log_format") == 0) {
165✔
1989
            const auto format_name = format->get_name();
3✔
1990
            sqlite3_bind_text(stmt,
3✔
1991
                              lpc + 1,
1992
                              format_name.get(),
1993
                              format_name.size(),
3✔
1994
                              SQLITE_STATIC);
1995
            continue;
3✔
1996
        }
3✔
1997
        if (strcmp(name, ":log_format_regex") == 0) {
162✔
1998
            const auto pat_name = format->get_pattern_name(line_number);
×
1999
            sqlite3_bind_text(
×
2000
                stmt, lpc + 1, pat_name.get(), pat_name.size(), SQLITE_STATIC);
×
2001
            continue;
×
2002
        }
2003
        if (strcmp(name, ":log_path") == 0) {
162✔
2004
            const auto& filename = lf->get_filename();
×
2005
            sqlite3_bind_text(stmt,
×
2006
                              lpc + 1,
2007
                              filename.c_str(),
2008
                              filename.native().length(),
×
2009
                              SQLITE_STATIC);
2010
            continue;
×
2011
        }
2012
        if (strcmp(name, ":log_unique_path") == 0) {
162✔
2013
            const auto& filename = lf->get_unique_path();
×
2014
            sqlite3_bind_text(stmt,
×
2015
                              lpc + 1,
2016
                              filename.c_str(),
2017
                              filename.native().length(),
×
2018
                              SQLITE_STATIC);
2019
            continue;
×
2020
        }
2021
        if (strcmp(name, ":log_text") == 0) {
162✔
2022
            sqlite3_bind_text(
4✔
2023
                stmt, lpc + 1, sbr.get_data(), sbr.length(), SQLITE_STATIC);
4✔
2024
            continue;
4✔
2025
        }
2026
        if (strcmp(name, ":log_body") == 0) {
158✔
2027
            auto body_attr_opt = get_string_attr(sa, SA_BODY);
10✔
2028
            if (body_attr_opt) {
10✔
2029
                const auto& sar
2030
                    = body_attr_opt.value().saw_string_attr->sa_range;
10✔
2031

2032
                sqlite3_bind_text(stmt,
20✔
2033
                                  lpc + 1,
2034
                                  sbr.get_data_at(sar.lr_start),
10✔
2035
                                  sar.length(),
2036
                                  SQLITE_STATIC);
2037
            } else {
2038
                sqlite3_bind_null(stmt, lpc + 1);
×
2039
            }
2040
            continue;
10✔
2041
        }
10✔
2042
        if (strcmp(name, ":log_opid") == 0) {
148✔
2043
            if (values.lvv_opid_value) {
×
2044
                sqlite3_bind_text(stmt,
×
2045
                                  lpc + 1,
2046
                                  values.lvv_opid_value->c_str(),
2047
                                  values.lvv_opid_value->length(),
×
2048
                                  SQLITE_STATIC);
2049
            } else {
2050
                sqlite3_bind_null(stmt, lpc + 1);
×
2051
            }
2052
            continue;
×
2053
        }
2054
        if (strcmp(name, ":log_raw_text") == 0) {
148✔
2055
            auto res = lf->read_raw_message(ll);
×
2056

2057
            if (res.isOk()) {
×
2058
                raw_sbr = res.unwrap();
×
2059
                sqlite3_bind_text(stmt,
×
2060
                                  lpc + 1,
2061
                                  raw_sbr.get_data(),
2062
                                  raw_sbr.length(),
×
2063
                                  SQLITE_STATIC);
2064
            }
2065
            continue;
×
2066
        }
2067
        for (const auto& lv : values.lvv_values) {
391✔
2068
            if (lv.lv_meta.lvm_name != &name[1]) {
391✔
2069
                continue;
243✔
2070
            }
2071

2072
            switch (lv.lv_meta.lvm_kind) {
148✔
2073
                case value_kind_t::VALUE_BOOLEAN:
×
2074
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
2075
                    break;
×
2076
                case value_kind_t::VALUE_FLOAT:
×
2077
                    sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
2078
                    break;
×
2079
                case value_kind_t::VALUE_INTEGER:
12✔
2080
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
12✔
2081
                    break;
12✔
2082
                case value_kind_t::VALUE_NULL:
×
2083
                    sqlite3_bind_null(stmt, lpc + 1);
×
2084
                    break;
×
2085
                default:
136✔
2086
                    sqlite3_bind_text(stmt,
136✔
2087
                                      lpc + 1,
2088
                                      lv.text_value(),
2089
                                      lv.text_length(),
136✔
2090
                                      SQLITE_TRANSIENT);
2091
                    break;
136✔
2092
            }
2093
            break;
148✔
2094
        }
2095
    }
2096

2097
    auto step_res = sqlite3_step(stmt);
176✔
2098

2099
    sqlite3_reset(stmt);
176✔
2100
    sqlite3_clear_bindings(stmt);
176✔
2101
    switch (step_res) {
176✔
2102
        case SQLITE_OK:
55✔
2103
        case SQLITE_DONE:
2104
            return Ok(false);
110✔
2105
        case SQLITE_ROW:
120✔
2106
            return Ok(true);
240✔
2107
        default:
1✔
2108
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2109
    }
2110
}
176✔
2111

2112
bool
2113
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
12,677✔
2114
{
2115
    if (this->lss_marked_only) {
12,677✔
2116
        auto found_mark = ll->is_marked() || ll->is_expr_marked();
6✔
2117
        auto to_start_ll = ll;
6✔
2118
        while (!found_mark && to_start_ll->is_continued()) {
6✔
2119
            if (to_start_ll->is_marked() || to_start_ll->is_expr_marked()) {
×
2120
                found_mark = true;
×
2121
            }
2122
            --to_start_ll;
×
2123
        }
2124
        auto to_end_ll = std::next(ll);
6✔
2125
        while (!found_mark && to_end_ll != (*ld)->get_file_ptr()->end()
10✔
2126
               && to_end_ll->is_continued())
11✔
2127
        {
2128
            if (to_end_ll->is_marked() || to_end_ll->is_expr_marked()) {
1✔
2129
                found_mark = true;
1✔
2130
            }
2131
            ++to_end_ll;
1✔
2132
        }
2133
        if (!found_mark) {
6✔
2134
            return false;
3✔
2135
        }
2136
    }
2137

2138
    if (ll->get_msg_level() < this->lss_min_log_level) {
12,674✔
2139
        return false;
2✔
2140
    }
2141

2142
    if (*ll < this->ttt_min_row_time) {
12,672✔
2143
        return false;
3✔
2144
    }
2145

2146
    if (!(*ll <= this->ttt_max_row_time)) {
12,669✔
2147
        return false;
4✔
2148
    }
2149

2150
    return true;
12,665✔
2151
}
2152

2153
void
2154
logfile_sub_source::invalidate_sql_filter()
24✔
2155
{
2156
    for (auto& ld : *this) {
48✔
2157
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
24✔
2158
    }
2159
}
24✔
2160

2161
void
2162
logfile_sub_source::text_mark(const bookmark_type_t* bm,
146✔
2163
                              vis_line_t line,
2164
                              bool added)
2165
{
2166
    if (line >= (int) this->lss_index.size()) {
146✔
2167
        return;
×
2168
    }
2169

2170
    auto cl = this->at(line);
146✔
2171

2172
    if (bm == &textview_curses::BM_USER) {
146✔
2173
        auto* ll = this->find_line(cl);
57✔
2174

2175
        ll->set_mark(added);
57✔
2176
    }
2177
    auto lb = this->lss_user_marks[bm].bv_tree.lower_bound(cl);
146✔
2178
    if (added) {
146✔
2179
        if (lb == this->lss_user_marks[bm].bv_tree.end() || *lb != cl) {
65✔
2180
            this->lss_user_marks[bm].bv_tree.insert(cl);
60✔
2181
        }
2182
    } else if (lb != this->lss_user_marks[bm].bv_tree.end() && *lb == cl) {
81✔
2183
        this->lss_user_marks[bm].bv_tree.erase(lb);
7✔
2184
    }
2185
    if (bm == &textview_curses::BM_META
146✔
2186
        && this->lss_meta_grepper.gps_proc != nullptr)
16✔
2187
    {
2188
        this->tss_view->search_range(line, line + 1_vl);
1✔
2189
        this->tss_view->search_new_data();
1✔
2190
    }
2191
}
2192

2193
void
2194
logfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
37✔
2195
{
2196
    if (bm == &textview_curses::BM_USER) {
37✔
2197
        for (auto iter = this->lss_user_marks[bm].bv_tree.begin();
6✔
2198
             iter != this->lss_user_marks[bm].bv_tree.end();
6✔
2199
             ++iter)
×
2200
        {
2201
            this->find_line(*iter)->set_mark(false);
×
2202
        }
2203
    }
2204
    this->lss_user_marks[bm].clear();
37✔
2205
}
37✔
2206

2207
void
2208
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
494✔
2209
{
2210
    auto iter = std::find_if(
494✔
2211
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
988✔
2212
    if (iter != this->lss_files.end()) {
494✔
2213
        int file_index = iter - this->lss_files.begin();
494✔
2214

2215
        (*iter)->clear();
494✔
2216
        for (auto& bv : this->lss_user_marks) {
4,446✔
2217
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
3,952✔
2218
            auto mark_end
2219
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
3,952✔
2220
            auto file_range = bv.equal_range(mark_curr, mark_end);
3,952✔
2221

2222
            if (file_range.first != file_range.second) {
3,952✔
2223
                auto to_del = std::vector<content_line_t>{};
61✔
2224
                for (auto file_iter = file_range.first;
61✔
2225
                     file_iter != file_range.second;
145✔
2226
                     ++file_iter)
84✔
2227
                {
2228
                    to_del.emplace_back(*file_iter);
84✔
2229
                }
2230

2231
                for (auto cl : to_del) {
145✔
2232
                    bv.bv_tree.erase(cl);
84✔
2233
                }
2234
            }
61✔
2235
        }
2236

2237
        this->lss_force_rebuild = true;
494✔
2238
    }
2239
    while (!this->lss_files.empty()) {
988✔
2240
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
544✔
2241
            this->lss_files.pop_back();
494✔
2242
        } else {
2243
            break;
50✔
2244
        }
2245
    }
2246
    this->lss_token_file = nullptr;
494✔
2247
}
494✔
2248

2249
std::optional<vis_line_t>
2250
logfile_sub_source::find_from_content(content_line_t cl)
5✔
2251
{
2252
    content_line_t line = cl;
5✔
2253
    std::shared_ptr<logfile> lf = this->find(line);
5✔
2254

2255
    if (lf != nullptr) {
5✔
2256
        auto ll_iter = lf->begin() + line;
5✔
2257
        auto& ll = *ll_iter;
5✔
2258
        auto vis_start_opt = this->find_from_time(ll.get_timeval());
5✔
2259

2260
        if (!vis_start_opt) {
5✔
2261
            return std::nullopt;
×
2262
        }
2263

2264
        auto vis_start = *vis_start_opt;
5✔
2265

2266
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2267
            content_line_t guess_cl = this->at(vis_start);
5✔
2268

2269
            if (cl == guess_cl) {
5✔
2270
                return vis_start;
5✔
2271
            }
2272

2273
            auto guess_line = this->find_line(guess_cl);
×
2274

2275
            if (!guess_line || ll < *guess_line) {
×
2276
                return std::nullopt;
×
2277
            }
2278

2279
            ++vis_start;
×
2280
        }
2281
    }
2282

2283
    return std::nullopt;
×
2284
}
5✔
2285

2286
void
2287
logfile_sub_source::reload_index_delegate()
611✔
2288
{
2289
    if (this->lss_index_delegate == nullptr) {
611✔
2290
        return;
×
2291
    }
2292

2293
    this->lss_index_delegate->index_start(*this);
611✔
2294
    for (const auto index : this->lss_filtered_index) {
839✔
2295
        auto cl = this->lss_index[index].value();
228✔
2296
        uint64_t line_number;
2297
        auto ld = this->find_data(cl, line_number);
228✔
2298
        std::shared_ptr<logfile> lf = (*ld)->get_file();
228✔
2299

2300
        this->lss_index_delegate->index_line(
228✔
2301
            *this, lf.get(), lf->begin() + line_number);
228✔
2302
    }
228✔
2303
    this->lss_index_delegate->index_complete(*this);
611✔
2304
}
2305

2306
std::optional<std::shared_ptr<text_filter>>
2307
logfile_sub_source::get_sql_filter()
1,932✔
2308
{
2309
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
1,932✔
2310
               return filt->get_index() == 0;
347✔
2311
           })
2312
        | lnav::itertools::deref();
3,864✔
2313
}
2314

2315
void
2316
log_location_history::loc_history_append(vis_line_t top)
93✔
2317
{
2318
    if (top < 0_vl || top >= vis_line_t(this->llh_log_source.text_line_count()))
93✔
2319
    {
2320
        return;
3✔
2321
    }
2322

2323
    auto cl = this->llh_log_source.at(top);
90✔
2324

2325
    auto iter = this->llh_history.begin();
90✔
2326
    iter += this->llh_history.size() - this->lh_history_position;
90✔
2327
    this->llh_history.erase_from(iter);
90✔
2328
    this->lh_history_position = 0;
90✔
2329
    this->llh_history.push_back(cl);
90✔
2330
}
2331

2332
std::optional<vis_line_t>
2333
log_location_history::loc_history_back(vis_line_t current_top)
2✔
2334
{
2335
    while (this->lh_history_position < this->llh_history.size()) {
2✔
2336
        auto iter = this->llh_history.rbegin();
2✔
2337

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

2340
        if (this->lh_history_position == 0 && vis_for_pos != current_top) {
2✔
2341
            return vis_for_pos;
2✔
2342
        }
2343

2344
        if ((this->lh_history_position + 1) >= this->llh_history.size()) {
2✔
2345
            break;
×
2346
        }
2347

2348
        this->lh_history_position += 1;
2✔
2349

2350
        iter += this->lh_history_position;
2✔
2351

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

2354
        if (vis_for_pos) {
2✔
2355
            return vis_for_pos;
2✔
2356
        }
2357
    }
2358

2359
    return std::nullopt;
×
2360
}
2361

2362
std::optional<vis_line_t>
2363
log_location_history::loc_history_forward(vis_line_t current_top)
1✔
2364
{
2365
    while (this->lh_history_position > 0) {
1✔
2366
        this->lh_history_position -= 1;
1✔
2367

2368
        auto iter = this->llh_history.rbegin();
1✔
2369

2370
        iter += this->lh_history_position;
1✔
2371

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

2374
        if (vis_for_pos) {
1✔
2375
            return vis_for_pos;
1✔
2376
        }
2377
    }
2378

2379
    return std::nullopt;
×
2380
}
2381

2382
bool
2383
sql_filter::matches(std::optional<line_source> ls_opt,
121✔
2384
                    const shared_buffer_ref& line)
2385
{
2386
    if (!ls_opt) {
121✔
2387
        return false;
×
2388
    }
2389

2390
    auto ls = ls_opt;
121✔
2391

2392
    if (!ls->ls_line->is_message()) {
121✔
2393
        return false;
3✔
2394
    }
2395
    if (this->sf_filter_stmt == nullptr) {
118✔
2396
        return false;
×
2397
    }
2398

2399
    auto lfp = ls->ls_file.shared_from_this();
118✔
2400
    auto ld = this->sf_log_source.find_data_i(lfp);
118✔
2401
    if (ld == this->sf_log_source.end()) {
118✔
2402
        return false;
×
2403
    }
2404

2405
    auto eval_res = this->sf_log_source.eval_sql_filter(
118✔
2406
        this->sf_filter_stmt, ld, ls->ls_line);
118✔
2407
    if (eval_res.unwrapOr(true)) {
118✔
2408
        return false;
72✔
2409
    }
2410

2411
    return true;
46✔
2412
}
118✔
2413

2414
std::string
2415
sql_filter::to_command() const
×
2416
{
2417
    return fmt::format(FMT_STRING("filter-expr {}"), this->lf_id);
×
2418
}
2419

2420
std::optional<line_info>
2421
logfile_sub_source::meta_grepper::grep_value_for_line(vis_line_t line,
×
2422
                                                      std::string& value_out)
2423
{
2424
    auto line_meta_opt = this->lmg_source.find_bookmark_metadata(line);
×
2425
    if (!line_meta_opt) {
×
2426
        value_out.clear();
×
2427
    } else {
2428
        auto& bm = *(line_meta_opt.value());
×
2429

2430
        {
2431
            md2attr_line mdal;
×
2432

2433
            auto parse_res = md4cpp::parse(bm.bm_comment, mdal);
×
2434
            if (parse_res.isOk()) {
×
2435
                value_out.append(parse_res.unwrap().get_string());
×
2436
            } else {
2437
                value_out.append(bm.bm_comment);
×
2438
            }
2439
        }
2440

2441
        value_out.append("\x1c");
×
2442
        for (const auto& tag : bm.bm_tags) {
×
2443
            value_out.append(tag);
×
2444
            value_out.append("\x1c");
×
2445
        }
2446
        value_out.append("\x1c");
×
2447
        for (const auto& pair : bm.bm_annotations.la_pairs) {
×
2448
            value_out.append(pair.first);
×
2449
            value_out.append("\x1c");
×
2450

2451
            md2attr_line mdal;
×
2452

2453
            auto parse_res = md4cpp::parse(pair.second, mdal);
×
2454
            if (parse_res.isOk()) {
×
2455
                value_out.append(parse_res.unwrap().get_string());
×
2456
            } else {
2457
                value_out.append(pair.second);
×
2458
            }
2459
            value_out.append("\x1c");
×
2460
        }
2461
        value_out.append("\x1c");
×
2462
        value_out.append(bm.bm_opid);
×
2463
    }
2464

2465
    if (!this->lmg_done) {
×
2466
        return line_info{};
×
2467
    }
2468

2469
    return std::nullopt;
×
2470
}
2471

2472
vis_line_t
2473
logfile_sub_source::meta_grepper::grep_initial_line(vis_line_t start,
×
2474
                                                    vis_line_t highest)
2475
{
2476
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2477
    auto& bv = bm[&textview_curses::BM_META];
×
2478

2479
    if (bv.empty()) {
×
2480
        return -1_vl;
×
2481
    }
2482
    return *bv.bv_tree.begin();
×
2483
}
2484

2485
void
2486
logfile_sub_source::meta_grepper::grep_next_line(vis_line_t& line)
×
2487
{
2488
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2489
    auto& bv = bm[&textview_curses::BM_META];
×
2490

2491
    auto line_opt = bv.next(vis_line_t(line));
×
2492
    if (!line_opt) {
×
2493
        this->lmg_done = true;
×
2494
    }
2495
    line = line_opt.value_or(-1_vl);
×
2496
}
2497

2498
void
2499
logfile_sub_source::meta_grepper::grep_begin(grep_proc<vis_line_t>& gp,
23✔
2500
                                             vis_line_t start,
2501
                                             vis_line_t stop)
2502
{
2503
    this->lmg_source.quiesce();
23✔
2504

2505
    this->lmg_source.tss_view->grep_begin(gp, start, stop);
23✔
2506
}
23✔
2507

2508
void
2509
logfile_sub_source::meta_grepper::grep_end(grep_proc<vis_line_t>& gp)
23✔
2510
{
2511
    this->lmg_source.tss_view->grep_end(gp);
23✔
2512
}
23✔
2513

2514
void
2515
logfile_sub_source::meta_grepper::grep_match(grep_proc<vis_line_t>& gp,
3✔
2516
                                             vis_line_t line)
2517
{
2518
    this->lmg_source.tss_view->grep_match(gp, line);
3✔
2519
}
3✔
2520

2521
static std::vector<breadcrumb::possibility>
2522
timestamp_poss()
10✔
2523
{
2524
    const static std::vector<breadcrumb::possibility> retval = {
2525
        breadcrumb::possibility{"-1 day"},
2526
        breadcrumb::possibility{"-1h"},
2527
        breadcrumb::possibility{"-30m"},
2528
        breadcrumb::possibility{"-15m"},
2529
        breadcrumb::possibility{"-5m"},
2530
        breadcrumb::possibility{"-1m"},
2531
        breadcrumb::possibility{"+1m"},
2532
        breadcrumb::possibility{"+5m"},
2533
        breadcrumb::possibility{"+15m"},
2534
        breadcrumb::possibility{"+30m"},
2535
        breadcrumb::possibility{"+1h"},
2536
        breadcrumb::possibility{"+1 day"},
2537
    };
80✔
2538

2539
    return retval;
10✔
2540
}
20✔
2541

2542
static attr_line_t
2543
to_display(const std::shared_ptr<logfile>& lf)
20✔
2544
{
2545
    attr_line_t retval;
20✔
2546

2547
    if (lf->get_open_options().loo_piper) {
20✔
2548
        if (!lf->get_open_options().loo_piper->is_finished()) {
×
2549
            retval.append("\u21bb "_list_glyph);
×
2550
        }
2551
    }
2552
    retval.append(lf->get_unique_path());
20✔
2553

2554
    return retval;
20✔
2555
}
×
2556

2557
void
2558
logfile_sub_source::text_crumbs_for_line(int line,
10✔
2559
                                         std::vector<breadcrumb::crumb>& crumbs)
2560
{
2561
    text_sub_source::text_crumbs_for_line(line, crumbs);
10✔
2562

2563
    if (this->lss_filtered_index.empty()) {
10✔
2564
        return;
×
2565
    }
2566

2567
    auto vl = vis_line_t(line);
10✔
2568
    auto bmc = this->get_bookmark_metadata_context(
10✔
2569
        vl, bookmark_metadata::categories::partition);
2570
    if (bmc.bmc_current_metadata) {
10✔
2571
        const auto& name = bmc.bmc_current_metadata.value()->bm_name;
1✔
2572
        auto key = text_anchors::to_anchor_string(name);
1✔
2573
        auto display = attr_line_t()
1✔
2574
                           .append("\u2291 "_symbol)
1✔
2575
                           .append(lnav::roles::variable(name))
2✔
2576
                           .move();
1✔
2577
        crumbs.emplace_back(
1✔
2578
            key,
2579
            display,
2580
            [this]() -> std::vector<breadcrumb::possibility> {
×
2581
                auto& vb = this->tss_view->get_bookmarks();
1✔
2582
                const auto& bv = vb[&textview_curses::BM_PARTITION];
1✔
2583
                std::vector<breadcrumb::possibility> retval;
1✔
2584

2585
                for (const auto& vl : bv.bv_tree) {
2✔
2586
                    auto meta_opt = this->find_bookmark_metadata(vl);
1✔
2587
                    if (!meta_opt || meta_opt.value()->bm_name.empty()) {
1✔
2588
                        continue;
×
2589
                    }
2590

2591
                    const auto& name = meta_opt.value()->bm_name;
1✔
2592
                    retval.emplace_back(text_anchors::to_anchor_string(name),
1✔
2593
                                        name);
2594
                }
2595

2596
                return retval;
1✔
2597
            },
×
2598
            [ec = this->lss_exec_context](const auto& part) {
1✔
2599
                auto cmd = fmt::format(FMT_STRING(":goto {}"),
×
2600
                                       part.template get<std::string>());
2601
                ec->execute(INTERNAL_SRC_LOC, cmd);
×
2602
            });
×
2603
    }
1✔
2604

2605
    auto line_pair_opt = this->find_line_with_file(vl);
10✔
2606
    if (!line_pair_opt) {
10✔
2607
        return;
×
2608
    }
2609
    auto line_pair = line_pair_opt.value();
10✔
2610
    auto& lf = line_pair.first;
10✔
2611
    auto format = lf->get_format();
10✔
2612
    char ts[64];
2613

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

2616
    crumbs.emplace_back(std::string(ts),
10✔
2617
                        timestamp_poss,
2618
                        [ec = this->lss_exec_context](const auto& ts) {
10✔
2619
                            auto cmd
×
2620
                                = fmt::format(FMT_STRING(":goto {}"),
×
2621
                                              ts.template get<std::string>());
2622
                            ec->execute(INTERNAL_SRC_LOC, cmd);
×
2623
                        });
×
2624
    crumbs.back().c_expected_input
10✔
2625
        = breadcrumb::crumb::expected_input_t::anything;
10✔
2626
    crumbs.back().c_search_placeholder = "(Enter an absolute or relative time)";
10✔
2627

2628
    auto format_name = format->get_name().to_string();
10✔
2629

2630
    crumbs.emplace_back(
10✔
2631
        format_name,
2632
        attr_line_t().append(format_name),
10✔
2633
        [this]() -> std::vector<breadcrumb::possibility> {
×
2634
            return this->lss_files
10✔
2635
                | lnav::itertools::filter_in([](const auto& file_data) {
20✔
2636
                       return file_data->is_visible();
10✔
2637
                   })
2638
                | lnav::itertools::map(&logfile_data::get_file_ptr)
30✔
2639
                | lnav::itertools::map(&logfile::get_format_name)
30✔
2640
                | lnav::itertools::unique()
30✔
2641
                | lnav::itertools::map([](const auto& elem) {
40✔
2642
                       return breadcrumb::possibility{
2643
                           elem.to_string(),
2644
                       };
10✔
2645
                   })
2646
                | lnav::itertools::to_vector();
30✔
2647
        },
2648
        [ec = this->lss_exec_context](const auto& format_name) {
10✔
2649
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2650
     SET selection = ifnull(
2651
         (SELECT log_line FROM all_logs WHERE log_format = $format_name LIMIT 1),
2652
         (SELECT raise_error(
2653
            'Could not find format: ' || $format_name,
2654
            'The corresponding log messages might have been filtered out'))
2655
       )
2656
     WHERE name = 'log'
2657
)";
2658

2659
            ec->execute_with(
×
2660
                INTERNAL_SRC_LOC,
×
2661
                MOVE_STMT,
2662
                std::make_pair("format_name",
2663
                               format_name.template get<std::string>()));
2664
        });
×
2665

2666
    auto msg_start_iter = lf->message_start(line_pair.second);
10✔
2667
    auto file_line_number = std::distance(lf->begin(), msg_start_iter);
10✔
2668
    crumbs.emplace_back(
20✔
2669
        lf->get_unique_path(),
10✔
2670
        to_display(lf).appendf(FMT_STRING("[{:L}]"), file_line_number),
40✔
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([](const auto& file_data) {
20✔
2677
                       return breadcrumb::possibility{
2678
                           file_data->get_file_ptr()->get_unique_path(),
10✔
2679
                           to_display(file_data->get_file()),
2680
                       };
10✔
2681
                   });
20✔
2682
        },
2683
        [ec = this->lss_exec_context](const auto& uniq_path) {
10✔
2684
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2685
     SET selection = ifnull(
2686
          (SELECT log_line FROM all_logs WHERE log_unique_path = $uniq_path LIMIT 1),
2687
          (SELECT raise_error(
2688
            'Could not find file: ' || $uniq_path,
2689
            'The corresponding log messages might have been filtered out'))
2690
         )
2691
     WHERE name = 'log'
2692
)";
2693

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

2701
    shared_buffer sb;
10✔
2702
    logline_value_vector values;
10✔
2703
    auto& sbr = values.lvv_sbr;
10✔
2704

2705
    lf->read_full_message(msg_start_iter, sbr);
10✔
2706
    attr_line_t al(to_string(sbr));
10✔
2707
    if (!sbr.get_metadata().m_valid_utf) {
10✔
2708
        scrub_to_utf8(&al.al_string[0], al.al_string.length());
×
2709
    }
2710
    if (sbr.get_metadata().m_has_ansi) {
10✔
2711
        // bleh
2712
        scrub_ansi_string(al.get_string(), &al.al_attrs);
×
2713
        sbr.share(sb, al.al_string.data(), al.al_string.size());
×
2714
    }
2715
    format->annotate(lf.get(), file_line_number, al.get_attrs(), values);
10✔
2716

2717
    {
2718
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
20✔
2719
          SET selection = ifnull(
2720
            (SELECT log_line FROM all_logs WHERE log_opid = $opid LIMIT 1),
2721
            (SELECT raise_error('Could not find opid: ' || $opid,
2722
                                'The corresponding log messages might have been filtered out')))
2723
          WHERE name = 'log'
2724
        )";
2725
        static const std::string ELLIPSIS = "\u22ef";
20✔
2726

2727
        auto opid_display = values.lvv_opid_value.has_value()
10✔
2728
            ? lnav::roles::identifier(values.lvv_opid_value.value())
9✔
2729
            : lnav::roles::hidden(ELLIPSIS);
19✔
2730
        crumbs.emplace_back(
20✔
2731
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
21✔
2732
                                              : "",
2733
            attr_line_t().append(opid_display),
10✔
2734
            [this]() -> std::vector<breadcrumb::possibility> {
×
2735
                std::set<std::string> poss_strs;
10✔
2736

2737
                for (const auto& file_data : this->lss_files) {
20✔
2738
                    if (file_data->get_file_ptr() == nullptr) {
10✔
2739
                        continue;
×
2740
                    }
2741
                    safe::ReadAccess<logfile::safe_opid_state> r_opid_map(
2742
                        file_data->get_file_ptr()->get_opids());
10✔
2743

2744
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
719✔
2745
                        poss_strs.emplace(pair.first.to_string());
709✔
2746
                    }
2747
                }
10✔
2748

2749
                std::vector<breadcrumb::possibility> retval;
10✔
2750

2751
                std::transform(poss_strs.begin(),
10✔
2752
                               poss_strs.end(),
2753
                               std::back_inserter(retval),
2754
                               [](const auto& opid_str) {
709✔
2755
                                   return breadcrumb::possibility(opid_str);
709✔
2756
                               });
2757

2758
                return retval;
20✔
2759
            },
10✔
2760
            [ec = this->lss_exec_context](const auto& opid) {
10✔
2761
                ec->execute_with(
×
2762
                    INTERNAL_SRC_LOC,
×
2763
                    MOVE_STMT,
2764
                    std::make_pair("opid", opid.template get<std::string>()));
2765
            });
×
2766
    }
10✔
2767

2768
    auto sf = string_fragment::from_str(al.get_string());
10✔
2769
    auto body_opt = get_string_attr(al.get_attrs(), SA_BODY);
10✔
2770
    auto nl_pos_opt = sf.find('\n');
10✔
2771
    auto msg_line_number = std::distance(msg_start_iter, line_pair.second);
10✔
2772
    auto line_from_top = line - msg_line_number;
10✔
2773
    if (body_opt && nl_pos_opt) {
10✔
2774
        if (this->lss_token_meta_line != file_line_number
2✔
2775
            || this->lss_token_meta_size != sf.length())
1✔
2776
        {
2777
            if (body_opt->saw_string_attr->sa_range.length() < 128 * 1024) {
1✔
2778
                this->lss_token_meta
2779
                    = lnav::document::discover(al)
1✔
2780
                          .over_range(
1✔
2781
                              body_opt.value().saw_string_attr->sa_range)
1✔
2782
                          .perform();
1✔
2783
                // XXX discover_structure() changes `al`, have to recompute
2784
                // stuff
2785
                sf = al.to_string_fragment();
1✔
2786
                body_opt = get_string_attr(al.get_attrs(), SA_BODY);
1✔
2787
            } else {
2788
                this->lss_token_meta = lnav::document::metadata{};
×
2789
            }
2790
            this->lss_token_meta_line = file_line_number;
1✔
2791
            this->lss_token_meta_size = sf.length();
1✔
2792
        }
2793

2794
        const auto initial_size = crumbs.size();
1✔
2795
        auto sf_body
2796
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
1✔
2797
                           body_opt->saw_string_attr->sa_range.lr_end);
1✔
2798
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
1✔
2799
        file_off_t line_end_offset = sf.length();
1✔
2800
        ssize_t line_number = 0;
1✔
2801

2802
        for (const auto& sf_line : sf_body.split_lines()) {
4✔
2803
            if (line_number >= msg_line_number) {
4✔
2804
                line_end_offset = line_offset + sf_line.length();
1✔
2805
                break;
1✔
2806
            }
2807
            line_number += 1;
3✔
2808
            line_offset += sf_line.length();
3✔
2809
        }
1✔
2810

2811
        this->lss_token_meta.m_sections_tree.visit_overlapping(
1✔
2812
            line_offset,
2813
            line_end_offset,
2814
            [this,
2✔
2815
             initial_size,
2816
             meta = &this->lss_token_meta,
1✔
2817
             &crumbs,
2818
             line_from_top](const auto& iv) {
2819
                auto path = crumbs | lnav::itertools::skip(initial_size)
6✔
2820
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
4✔
2821
                    | lnav::itertools::append(iv.value);
2✔
2822
                auto curr_node = lnav::document::hier_node::lookup_path(
2✔
2823
                    meta->m_sections_root.get(), path);
2✔
2824

2825
                crumbs.emplace_back(
4✔
2826
                    iv.value,
2✔
2827
                    [meta, path]() { return meta->possibility_provider(path); },
4✔
2828
                    [this, curr_node, path, line_from_top](const auto& key) {
4✔
2829
                        if (!curr_node) {
×
2830
                            return;
×
2831
                        }
2832
                        auto* parent_node = curr_node.value()->hn_parent;
×
2833
                        if (parent_node == nullptr) {
×
2834
                            return;
×
2835
                        }
2836
                        key.match(
2837
                            [parent_node](const std::string& str) {
×
2838
                                return parent_node->find_line_number(str);
×
2839
                            },
2840
                            [parent_node](size_t index) {
×
2841
                                return parent_node->find_line_number(index);
×
2842
                            })
2843
                            | [this, line_from_top](auto line_number) {
×
2844
                                  this->tss_view->set_selection(
×
2845
                                      vis_line_t(line_from_top + line_number));
×
2846
                              };
2847
                    });
2848
                if (curr_node && !curr_node.value()->hn_parent->is_named_only())
2✔
2849
                {
2850
                    auto node = lnav::document::hier_node::lookup_path(
×
2851
                        meta->m_sections_root.get(), path);
×
2852

2853
                    crumbs.back().c_expected_input
×
2854
                        = curr_node.value()
×
2855
                              ->hn_parent->hn_named_children.empty()
×
2856
                        ? breadcrumb::crumb::expected_input_t::index
×
2857
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
2858
                    crumbs.back().with_possible_range(
×
2859
                        node | lnav::itertools::map([](const auto hn) {
×
2860
                            return hn->hn_parent->hn_children.size();
×
2861
                        })
2862
                        | lnav::itertools::unwrap_or(size_t{0}));
×
2863
                }
2864
            });
2✔
2865

2866
        auto path = crumbs | lnav::itertools::skip(initial_size)
2✔
2867
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
2✔
2868
        auto node = lnav::document::hier_node::lookup_path(
1✔
2869
            this->lss_token_meta.m_sections_root.get(), path);
1✔
2870

2871
        if (node && !node.value()->hn_children.empty()) {
1✔
2872
            auto poss_provider = [curr_node = node.value()]() {
1✔
2873
                std::vector<breadcrumb::possibility> retval;
1✔
2874
                for (const auto& child : curr_node->hn_named_children) {
1✔
2875
                    retval.emplace_back(child.first);
×
2876
                }
2877
                return retval;
1✔
2878
            };
2879
            auto path_performer
2880
                = [this, curr_node = node.value(), line_from_top](
2✔
2881
                      const breadcrumb::crumb::key_t& value) {
2882
                      value.match(
×
2883
                          [curr_node](const std::string& str) {
×
2884
                              return curr_node->find_line_number(str);
×
2885
                          },
2886
                          [curr_node](size_t index) {
×
2887
                              return curr_node->find_line_number(index);
×
2888
                          })
2889
                          | [this, line_from_top](size_t line_number) {
×
2890
                                this->tss_view->set_selection(
×
2891
                                    vis_line_t(line_from_top + line_number));
×
2892
                            };
2893
                  };
1✔
2894
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
1✔
2895
            crumbs.back().c_expected_input
1✔
2896
                = node.value()->hn_named_children.empty()
2✔
2897
                ? breadcrumb::crumb::expected_input_t::index
1✔
2898
                : breadcrumb::crumb::expected_input_t::index_or_exact;
2899
        }
2900
    }
1✔
2901
}
10✔
2902

2903
void
2904
logfile_sub_source::quiesce()
42✔
2905
{
2906
    for (auto& ld : this->lss_files) {
84✔
2907
        auto* lf = ld->get_file_ptr();
42✔
2908

2909
        if (lf == nullptr) {
42✔
2910
            continue;
×
2911
        }
2912

2913
        lf->quiesce();
42✔
2914
    }
2915
}
42✔
2916

2917
bookmark_metadata&
2918
logfile_sub_source::get_bookmark_metadata(content_line_t cl)
25✔
2919
{
2920
    auto line_pair = this->find_line_with_file(cl).value();
25✔
2921
    auto line_number = static_cast<uint32_t>(
2922
        std::distance(line_pair.first->begin(), line_pair.second));
25✔
2923

2924
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
2925
}
25✔
2926

2927
logfile_sub_source::bookmark_metadata_context
2928
logfile_sub_source::get_bookmark_metadata_context(
2,987✔
2929
    vis_line_t vl, bookmark_metadata::categories desired) const
2930
{
2931
    const auto& vb = this->tss_view->get_bookmarks();
2,987✔
2932
    const auto& bv = vb[desired == bookmark_metadata::categories::partition
2933
                            ? &textview_curses::BM_PARTITION
2934
                            : &textview_curses::BM_META];
2,987✔
2935
    auto vl_iter = bv.bv_tree.lower_bound(vl + 1_vl);
2,987✔
2936

2937
    std::optional<vis_line_t> next_line;
2,987✔
2938
    for (auto next_vl_iter = vl_iter; next_vl_iter != bv.bv_tree.end();
2,987✔
2939
         ++next_vl_iter)
×
2940
    {
2941
        auto bm_opt = this->find_bookmark_metadata(*next_vl_iter);
3✔
2942
        if (!bm_opt) {
3✔
2943
            continue;
×
2944
        }
2945

2946
        if (bm_opt.value()->has(desired)) {
3✔
2947
            next_line = *next_vl_iter;
3✔
2948
            break;
3✔
2949
        }
2950
    }
2951
    if (vl_iter == bv.bv_tree.begin()) {
2,987✔
2952
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
2,959✔
2953
    }
2954

2955
    --vl_iter;
28✔
2956
    while (true) {
2957
        auto bm_opt = this->find_bookmark_metadata(*vl_iter);
28✔
2958
        if (bm_opt) {
28✔
2959
            if (bm_opt.value()->has(desired)) {
19✔
2960
                return bookmark_metadata_context{
2961
                    *vl_iter, bm_opt.value(), next_line};
28✔
2962
            }
2963
        }
2964

2965
        if (vl_iter == bv.bv_tree.begin()) {
9✔
2966
            return bookmark_metadata_context{
2967
                std::nullopt, std::nullopt, next_line};
9✔
2968
        }
2969
        --vl_iter;
×
2970
    }
2971
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
2972
}
2973

2974
std::optional<bookmark_metadata*>
2975
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
19,679✔
2976
{
2977
    auto line_pair = this->find_line_with_file(cl).value();
19,679✔
2978
    auto line_number = static_cast<uint32_t>(
2979
        std::distance(line_pair.first->begin(), line_pair.second));
19,679✔
2980

2981
    auto& bm = line_pair.first->get_bookmark_metadata();
19,679✔
2982
    auto bm_iter = bm.find(line_number);
19,679✔
2983
    if (bm_iter == bm.end()) {
19,679✔
2984
        return std::nullopt;
19,293✔
2985
    }
2986

2987
    return &bm_iter->second;
386✔
2988
}
19,679✔
2989

2990
void
2991
logfile_sub_source::erase_bookmark_metadata(content_line_t cl)
26✔
2992
{
2993
    auto line_pair = this->find_line_with_file(cl).value();
26✔
2994
    auto line_number = static_cast<uint32_t>(
2995
        std::distance(line_pair.first->begin(), line_pair.second));
26✔
2996

2997
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
2998
    auto bm_iter = bm.find(line_number);
26✔
2999
    if (bm_iter != bm.end()) {
26✔
3000
        bm.erase(bm_iter);
6✔
3001
    }
3002
}
26✔
3003

3004
void
3005
logfile_sub_source::clear_bookmark_metadata()
6✔
3006
{
3007
    for (auto& ld : *this) {
14✔
3008
        if (ld->get_file_ptr() == nullptr) {
8✔
3009
            continue;
×
3010
        }
3011

3012
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3013
    }
3014
}
6✔
3015

3016
void
3017
logfile_sub_source::increase_line_context()
×
3018
{
3019
    auto old_context = this->lss_line_context;
×
3020

3021
    switch (this->lss_line_context) {
×
3022
        case line_context_t::filename:
×
3023
            // nothing to do
3024
            break;
×
3025
        case line_context_t::basename:
×
3026
            this->lss_line_context = line_context_t::filename;
×
3027
            break;
×
3028
        case line_context_t::none:
×
3029
            this->lss_line_context = line_context_t::basename;
×
3030
            break;
×
3031
        case line_context_t::time_column:
×
3032
            this->lss_line_context = line_context_t::none;
×
3033
            break;
×
3034
    }
3035
    if (old_context != this->lss_line_context) {
×
3036
        this->clear_line_size_cache();
×
3037
    }
3038
}
3039

3040
bool
3041
logfile_sub_source::decrease_line_context()
×
3042
{
3043
    static const auto& cfg
3044
        = injector::get<const logfile_sub_source_ns::config&>();
×
3045
    auto old_context = this->lss_line_context;
×
3046

3047
    switch (this->lss_line_context) {
×
3048
        case line_context_t::filename:
×
3049
            this->lss_line_context = line_context_t::basename;
×
3050
            break;
×
3051
        case line_context_t::basename:
×
3052
            this->lss_line_context = line_context_t::none;
×
3053
            break;
×
3054
        case line_context_t::none:
×
3055
            if (cfg.c_time_column
×
3056
                != logfile_sub_source_ns::time_column_feature_t::Disabled)
3057
            {
3058
                this->lss_line_context = line_context_t::time_column;
×
3059
            }
3060
            break;
×
3061
        case line_context_t::time_column:
×
3062
            break;
×
3063
    }
3064
    if (old_context != this->lss_line_context) {
×
3065
        this->clear_line_size_cache();
×
3066

3067
        return true;
×
3068
    }
3069

3070
    return false;
×
3071
}
3072

3073
size_t
3074
logfile_sub_source::get_filename_offset() const
238✔
3075
{
3076
    switch (this->lss_line_context) {
238✔
3077
        case line_context_t::filename:
×
3078
            return this->lss_filename_width;
×
3079
        case line_context_t::basename:
×
3080
            return this->lss_basename_width;
×
3081
        default:
238✔
3082
            return 0;
238✔
3083
    }
3084
}
3085

3086
size_t
3087
logfile_sub_source::file_count() const
581✔
3088
{
3089
    size_t retval = 0;
581✔
3090
    const_iterator iter;
581✔
3091

3092
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
581✔
3093
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
×
3094
            retval += 1;
×
3095
        }
3096
    }
3097

3098
    return retval;
581✔
3099
}
3100

3101
size_t
3102
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3103
                                       int row,
3104
                                       text_sub_source::line_flags_t flags)
3105
{
3106
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3107

3108
    if (this->lss_line_size_cache[index].first != row) {
×
3109
        std::string value;
×
3110

3111
        this->text_value_for_line(tc, row, value, flags);
×
3112
        scrub_ansi_string(value, nullptr);
×
3113
        auto line_width = string_fragment::from_str(value).column_width();
×
3114
        if (this->lss_line_context == line_context_t::time_column) {
×
3115
            auto time_attr
3116
                = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
×
3117
            if (time_attr != this->lss_token_attrs.end()) {
×
3118
                line_width -= time_attr->sa_range.length();
×
3119
                auto format = this->lss_token_file->get_format();
×
3120
                if (format->lf_level_hideable) {
×
3121
                    auto level_attr
3122
                        = find_string_attr(this->lss_token_attrs, &L_LEVEL);
×
3123
                    if (level_attr != this->lss_token_attrs.end()) {
×
3124
                        line_width -= level_attr->sa_range.length();
×
3125
                    }
3126
                }
3127
            }
3128
        }
3129
        this->lss_line_size_cache[index].second = line_width;
×
3130
        this->lss_line_size_cache[index].first = row;
×
3131
    }
3132
    return this->lss_line_size_cache[index].second;
×
3133
}
3134

3135
int
3136
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
2✔
3137
{
3138
    int retval = 0;
2✔
3139

3140
    for (const auto& ld : this->lss_files) {
3✔
3141
        retval += ld->ld_filter_state.lfo_filter_state
1✔
3142
                      .tfs_filter_hits[filter_index];
1✔
3143
    }
3144

3145
    return retval;
2✔
3146
}
3147

3148
std::optional<vis_line_t>
3149
logfile_sub_source::row_for(const row_info& ri)
236✔
3150
{
3151
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
472✔
3152
                               this->lss_filtered_index.end(),
3153
                               ri.ri_time,
236✔
3154
                               filtered_logline_cmp(*this));
3155
    if (lb != this->lss_filtered_index.end()) {
236✔
3156
        auto first_lb = lb;
231✔
3157
        while (true) {
3158
            auto cl = this->lss_index[*lb].value();
251✔
3159
            if (content_line_t(ri.ri_id) == cl) {
251✔
3160
                first_lb = lb;
197✔
3161
                break;
197✔
3162
            }
3163
            auto ll = this->find_line(cl);
54✔
3164
            if (ll->get_timeval() != ri.ri_time) {
54✔
3165
                break;
34✔
3166
            }
3167
            auto next_lb = std::next(lb);
20✔
3168
            if (next_lb == this->lss_filtered_index.end()) {
20✔
3169
                break;
×
3170
            }
3171
            lb = next_lb;
20✔
3172
        }
20✔
3173

3174
        const auto dst
3175
            = std::distance(this->lss_filtered_index.begin(), first_lb);
231✔
3176
        return vis_line_t(dst);
231✔
3177
    }
3178

3179
    return std::nullopt;
5✔
3180
}
3181

3182
std::unique_ptr<logline_window>
3183
logfile_sub_source::window_at(vis_line_t start_vl, vis_line_t end_vl)
35✔
3184
{
3185
    return std::make_unique<logline_window>(*this, start_vl, end_vl);
35✔
3186
}
3187

3188
std::unique_ptr<logline_window>
3189
logfile_sub_source::window_at(vis_line_t start_vl)
188✔
3190
{
3191
    return std::make_unique<logline_window>(*this, start_vl, start_vl + 1_vl);
188✔
3192
}
3193

3194
std::unique_ptr<logline_window>
3195
logfile_sub_source::window_to_end(vis_line_t start_vl)
×
3196
{
3197
    return std::make_unique<logline_window>(
3198
        *this, start_vl, vis_line_t(this->text_line_count()));
×
3199
}
3200

3201
std::optional<vis_line_t>
3202
logfile_sub_source::row_for_anchor(const std::string& id)
3✔
3203
{
3204
    if (startswith(id, "#msg")) {
3✔
3205
        static const auto ANCHOR_RE
3206
            = lnav::pcre2pp::code::from_const(R"(#msg([0-9a-fA-F]+)-(.+))");
3✔
3207
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
3✔
3208

3209
        if (ANCHOR_RE.capture_from(id).into(md).found_p()) {
3✔
3210
            auto scan_res = scn::scan<int64_t>(md[1]->to_string_view(), "{:x}");
3✔
3211
            if (scan_res) {
3✔
3212
                auto ts_low = std::chrono::microseconds{scan_res->value()};
3✔
3213
                auto ts_high = ts_low + 1us;
3✔
3214

3215
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3216
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3217
                if (low_vl) {
3✔
3218
                    auto lw = this->window_at(
3219
                        low_vl.value(),
3✔
3220
                        high_vl.value_or(low_vl.value() + 1_vl));
6✔
3221

3222
                    for (const auto& li : *lw) {
3✔
3223
                        auto hash_res = li.get_line_hash();
3✔
3224
                        if (hash_res.isErr()) {
3✔
3225
                            auto errmsg = hash_res.unwrapErr();
×
3226

3227
                            log_error("unable to get line hash: %s",
×
3228
                                      errmsg.c_str());
3229
                            continue;
×
3230
                        }
3231

3232
                        auto hash = hash_res.unwrap();
3✔
3233
                        if (hash == md[2]) {
3✔
3234
                            return li.get_vis_line();
3✔
3235
                        }
3236
                    }
12✔
3237
                }
3✔
3238
            }
3239
        }
3240

3241
        return std::nullopt;
×
3242
    }
3243

3244
    auto& vb = this->tss_view->get_bookmarks();
×
3245
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3246

3247
    for (const auto& vl : bv.bv_tree) {
×
3248
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3249
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3250
            continue;
×
3251
        }
3252

3253
        const auto& name = meta_opt.value()->bm_name;
×
3254
        if (id == text_anchors::to_anchor_string(name)) {
×
3255
            return vl;
×
3256
        }
3257
    }
3258

3259
    return std::nullopt;
×
3260
}
3261

3262
std::optional<vis_line_t>
3263
logfile_sub_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
2✔
3264
{
3265
    if (vl < this->lss_filtered_index.size()) {
2✔
3266
        auto file_and_line_pair = this->find_line_with_file(vl);
2✔
3267
        if (file_and_line_pair) {
2✔
3268
            const auto& [lf, line] = file_and_line_pair.value();
2✔
3269
            if (line->is_continued()) {
2✔
NEW
3270
                auto retval = vl;
×
NEW
3271
                switch (dir) {
×
NEW
3272
                    case direction::prev: {
×
NEW
3273
                        auto first_line = line;
×
NEW
3274
                        while (first_line->is_continued()) {
×
NEW
3275
                            --first_line;
×
NEW
3276
                            retval -= 1_vl;
×
3277
                        }
NEW
3278
                        return retval;
×
3279
                    }
NEW
3280
                    case direction::next: {
×
NEW
3281
                        auto first_line = line;
×
NEW
3282
                        while (first_line->is_continued()) {
×
NEW
3283
                            ++first_line;
×
NEW
3284
                            retval += 1_vl;
×
3285
                        }
NEW
3286
                        return retval;
×
3287
                    }
3288
                }
3289
            }
3290
        }
3291
    }
2✔
3292

3293
    auto bmc = this->get_bookmark_metadata_context(
2✔
3294
        vl, bookmark_metadata::categories::partition);
3295
    switch (dir) {
2✔
NEW
3296
        case direction::prev: {
×
3297
            if (bmc.bmc_current && bmc.bmc_current.value() != vl) {
×
3298
                return bmc.bmc_current;
×
3299
            }
3300
            if (!bmc.bmc_current || bmc.bmc_current.value() == 0_vl) {
×
3301
                return 0_vl;
×
3302
            }
3303
            auto prev_bmc = this->get_bookmark_metadata_context(
×
3304
                bmc.bmc_current.value() - 1_vl,
×
3305
                bookmark_metadata::categories::partition);
3306
            if (!prev_bmc.bmc_current) {
×
3307
                return 0_vl;
×
3308
            }
3309
            return prev_bmc.bmc_current;
×
3310
        }
3311
        case direction::next:
2✔
3312
            return bmc.bmc_next_line;
2✔
3313
    }
3314
    return std::nullopt;
×
3315
}
3316

3317
std::optional<std::string>
3318
logfile_sub_source::anchor_for_row(vis_line_t vl)
70✔
3319
{
3320
    auto line_meta = this->get_bookmark_metadata_context(
70✔
3321
        vl, bookmark_metadata::categories::partition);
3322
    if (!line_meta.bmc_current_metadata) {
70✔
3323
        auto lw = window_at(vl);
67✔
3324

3325
        for (const auto& li : *lw) {
67✔
3326
            auto hash_res = li.get_line_hash();
67✔
3327
            if (hash_res.isErr()) {
67✔
3328
                auto errmsg = hash_res.unwrapErr();
×
3329
                log_error("unable to compute line hash: %s", errmsg.c_str());
×
3330
                break;
×
3331
            }
3332
            auto hash = hash_res.unwrap();
67✔
3333
            auto retval = fmt::format(
3334
                FMT_STRING("#msg{:016x}-{}"),
134✔
3335
                li.get_logline().get_time<std::chrono::microseconds>().count(),
67✔
3336
                hash);
×
3337

3338
            return retval;
67✔
3339
        }
268✔
3340

3341
        return std::nullopt;
×
3342
    }
67✔
3343

3344
    return text_anchors::to_anchor_string(
3✔
3345
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3346
}
3347

3348
std::unordered_set<std::string>
3349
logfile_sub_source::get_anchors()
×
3350
{
3351
    auto& vb = this->tss_view->get_bookmarks();
×
3352
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3353
    std::unordered_set<std::string> retval;
×
3354

3355
    for (const auto& vl : bv.bv_tree) {
×
3356
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3357
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3358
            continue;
×
3359
        }
3360

3361
        const auto& name = meta_opt.value()->bm_name;
×
3362
        retval.emplace(text_anchors::to_anchor_string(name));
×
3363
    }
3364

3365
    return retval;
×
3366
}
×
3367

3368
bool
3369
logfile_sub_source::text_handle_mouse(
×
3370
    textview_curses& tc,
3371
    const listview_curses::display_line_content_t& mouse_line,
3372
    mouse_event& me)
3373
{
3374
    if (tc.get_overlay_selection()) {
×
3375
        auto nci = ncinput{};
×
3376
        if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 2, 4)) {
×
3377
            nci.id = ' ';
×
3378
            nci.eff_text[0] = ' ';
×
3379
            this->list_input_handle_key(tc, nci);
×
3380
        } else if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 5, 6)) {
×
3381
            nci.id = '#';
×
3382
            nci.eff_text[0] = '#';
×
3383
            this->list_input_handle_key(tc, nci);
×
3384
        }
3385
    }
3386
    return true;
×
3387
}
3388

3389
void
3390
logfile_sub_source::reload_config(error_reporter& reporter)
630✔
3391
{
3392
    static const auto& cfg
3393
        = injector::get<const logfile_sub_source_ns::config&>();
630✔
3394

3395
    switch (cfg.c_time_column) {
630✔
3396
        case logfile_sub_source_ns::time_column_feature_t::Default:
×
3397
            if (this->lss_line_context == line_context_t::none) {
×
3398
                this->lss_line_context = line_context_t::time_column;
×
3399
            }
3400
            break;
×
3401
        case logfile_sub_source_ns::time_column_feature_t::Disabled:
630✔
3402
            if (this->lss_line_context == line_context_t::time_column) {
630✔
3403
                this->lss_line_context = line_context_t::none;
×
3404
            }
3405
            break;
630✔
3406
        case logfile_sub_source_ns::time_column_feature_t::Enabled:
×
3407
            break;
×
3408
    }
3409
}
630✔
3410

3411
void
3412
logfile_sub_source::update_filter_hash_state(hasher& h) const
29✔
3413
{
3414
    text_sub_source::update_filter_hash_state(h);
29✔
3415

3416
    for (const auto& ld : this->lss_files) {
29✔
UNCOV
3417
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
×
3418
            h.update(0);
×
3419
        } else {
UNCOV
3420
            h.update(1);
×
3421
        }
3422
    }
3423
    h.update(this->lss_min_log_level);
29✔
3424
    h.update(this->lss_marked_only);
29✔
3425
}
29✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc