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

tstack / lnav / 17589970077-2502

09 Sep 2025 05:00PM UTC coverage: 65.196% (-5.0%) from 70.225%
17589970077-2502

push

github

tstack
[format] add fields for source file/line

Knowing the source file/line context in a log
message can help find log messages when using
log2src.

56 of 70 new or added lines in 2 files covered. (80.0%)

13954 existing lines in 210 files now uncovered.

45516 of 69814 relevant lines covered (65.2%)

404154.37 hits per line

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

63.25
/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 "md2attr_line.hh"
55
#include "ptimec.hh"
56
#include "scn/scan.h"
57
#include "shlex.hh"
58
#include "sql_util.hh"
59
#include "vtab_module.hh"
60
#include "yajlpp/yajlpp.hh"
61
#include "yajlpp/yajlpp_def.hh"
62

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
149
        return retval;
×
UNCOV
150
    });
×
151

UNCOV
152
    return retval;
×
153
}
154

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

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

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

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

187
    return retval;
22✔
UNCOV
188
}
×
189

190
struct filtered_logline_cmp {
191
    filtered_logline_cmp(const logfile_sub_source& lc) : llss_controller(lc) {}
240✔
192

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

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

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

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

220
    const logfile_sub_source& llss_controller;
221
};
222

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

235
    return std::nullopt;
4✔
236
}
237

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

250
    line_info retval;
2,782✔
251
    content_line_t line(0);
2,782✔
252

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

256
    line = this->at(vis_line_t(row));
2,782✔
257

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

280
    require_false(this->lss_in_value_for_line);
2,750✔
281

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

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

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

312
    auto format = this->lss_token_file->get_format();
2,750✔
313

314
    value_out = this->lss_token_value;
2,750✔
315

316
    auto& sbr = this->lss_token_values.lvv_sbr;
2,750✔
317

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

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

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

347
    std::optional<exttm> adjusted_tm;
2,750✔
348
    auto time_attr = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
2,750✔
349
    if (!this->lss_token_line->is_continued() && !format->lf_formatted_lines
3,792✔
350
        && (this->lss_token_file->is_time_adjusted()
842✔
351
            || ((format->lf_timestamp_flags & ETF_ZONE_SET
827✔
352
                 || format->lf_date_time.dts_default_zone != nullptr)
558✔
353
                && format->lf_date_time.dts_zoned_to_local)
353✔
354
            || format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
474✔
355
            || !(format->lf_timestamp_flags & ETF_DAY_SET)
474✔
356
            || !(format->lf_timestamp_flags & ETF_MONTH_SET))
453✔
357
        && format->lf_date_time.dts_fmt_lock != -1)
3,792✔
358
    {
359
        if (time_attr != this->lss_token_attrs.end()) {
333✔
360
            const auto time_range = time_attr->sa_range;
333✔
361
            const auto time_sf = string_fragment::from_str_range(
333✔
362
                this->lss_token_value, time_range.lr_start, time_range.lr_end);
333✔
363
            adjusted_tm = format->tm_for_display(this->lss_token_line, time_sf);
333✔
364

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

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

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

399
    // Insert space for the file/search-hit markers.
400
    value_out.insert(0, 1, ' ');
2,750✔
401
    this->lss_time_column_size = 0;
2,750✔
402
    if (this->lss_line_context == line_context_t::time_column) {
2,750✔
UNCOV
403
        if (time_attr != this->lss_token_attrs.end()) {
×
UNCOV
404
            this->lss_token_attrs.emplace_back(time_attr->sa_range,
×
UNCOV
405
                                               SA_REPLACED.value());
×
406

407
            const char* fmt;
UNCOV
408
            if (this->lss_all_timestamp_flags
×
UNCOV
409
                & (ETF_MICROS_SET | ETF_NANOS_SET))
×
410
            {
411
                fmt = "%H:%M:%S.%f";
×
412
            } else if (this->lss_all_timestamp_flags & ETF_MILLIS_SET) {
×
413
                fmt = "%H:%M:%S.%L";
×
414
            } else {
415
                fmt = "%H:%M:%S";
×
416
            }
417
            if (!adjusted_tm) {
×
UNCOV
418
                const auto time_range = time_attr->sa_range;
×
419
                const auto time_sf
420
                    = string_fragment::from_str_range(this->lss_token_value,
×
421
                                                      time_range.lr_start,
×
422
                                                      time_range.lr_end);
×
423
                adjusted_tm
424
                    = format->tm_for_display(this->lss_token_line, time_sf);
×
425
            }
426
            char buffer[128];
427
            this->lss_time_column_size
428
                = ftime_fmt(buffer, sizeof(buffer), fmt, adjusted_tm.value());
×
429
            if (this->tss_view->is_selectable()
×
430
                && this->tss_view->get_selection() == row)
×
431
            {
UNCOV
432
                buffer[this->lss_time_column_size] = ' ';
×
UNCOV
433
                buffer[this->lss_time_column_size + 1] = ' ';
×
UNCOV
434
                this->lss_time_column_size += 2;
×
435
            } else {
UNCOV
436
                constexpr char block[] = "\u258c ";
×
437

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

479
    if (this->tas_display_time_offset) {
2,750✔
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,750✔
486

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

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

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

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

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

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

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

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

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

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

533
    if (this->lss_token_line->get_msg_level() == log_level_t::LEVEL_INVALID) {
2,750✔
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) {
12,155✔
545
        if ((!(this->lss_token_flags & RF_FULL)
20,450✔
546
             && line_value.lv_sub_offset
18,366✔
547
                 != this->lss_token_line->get_sub_offset())
9,183✔
548
            || !line_value.lv_origin.is_valid())
18,588✔
549
        {
550
            continue;
1,640✔
551
        }
552

553
        if (line_value.lv_meta.is_hidden()) {
7,765✔
554
            value_out.emplace_back(line_value.lv_origin,
117✔
555
                                   SA_HIDDEN.value(ui_icon_t::hidden));
234✔
556
        }
557

558
        if (!line_value.lv_meta.lvm_identifier
20,077✔
559
            || !line_value.lv_origin.is_valid())
7,765✔
560
        {
561
            continue;
4,547✔
562
        }
563

564
        value_out.emplace_back(line_value.lv_origin,
3,218✔
565
                               VC_ROLE.value(role_t::VCR_IDENTIFIER));
6,436✔
566
    }
567

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

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

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

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

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

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

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

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

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

627
        ui_icon_t icon;
UNCOV
628
        switch (this->lss_token_line->get_msg_level()) {
×
UNCOV
629
            case LEVEL_TRACE:
×
UNCOV
630
                icon = ui_icon_t::log_level_trace;
×
UNCOV
631
                break;
×
UNCOV
632
            case LEVEL_DEBUG:
×
633
            case LEVEL_DEBUG2:
634
            case LEVEL_DEBUG3:
635
            case LEVEL_DEBUG4:
636
            case LEVEL_DEBUG5:
UNCOV
637
                icon = ui_icon_t::log_level_debug;
×
UNCOV
638
                break;
×
UNCOV
639
            case LEVEL_INFO:
×
UNCOV
640
                icon = ui_icon_t::log_level_info;
×
UNCOV
641
                break;
×
UNCOV
642
            case LEVEL_STATS:
×
UNCOV
643
                icon = ui_icon_t::log_level_stats;
×
UNCOV
644
                break;
×
UNCOV
645
            case LEVEL_NOTICE:
×
UNCOV
646
                icon = ui_icon_t::log_level_notice;
×
UNCOV
647
                break;
×
UNCOV
648
            case LEVEL_WARNING:
×
UNCOV
649
                icon = ui_icon_t::log_level_warning;
×
UNCOV
650
                break;
×
UNCOV
651
            case LEVEL_ERROR:
×
UNCOV
652
                icon = ui_icon_t::log_level_error;
×
UNCOV
653
                break;
×
UNCOV
654
            case LEVEL_CRITICAL:
×
UNCOV
655
                icon = ui_icon_t::log_level_critical;
×
UNCOV
656
                break;
×
UNCOV
657
            case LEVEL_FATAL:
×
UNCOV
658
                icon = ui_icon_t::log_level_fatal;
×
UNCOV
659
                break;
×
UNCOV
660
            default:
×
UNCOV
661
                icon = ui_icon_t::hidden;
×
UNCOV
662
                break;
×
663
        }
UNCOV
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;
×
UNCOV
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));
×
UNCOV
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(
×
UNCOV
681
                lr, VC_ROLE.value(role_t::VCR_TIME_COLUMN_TO_TEXT));
×
682
        }
683
    }
684

685
    if (this->tas_display_time_offset) {
2,750✔
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,750✔
714
    lr.lr_end = -1;
2,750✔
715
    value_out.emplace_back(lr, L_FILE.value(this->lss_token_file));
2,750✔
716
    value_out.emplace_back(
2,750✔
717
        lr, SA_FORMAT.value(this->lss_token_file->get_format()->get_name()));
5,500✔
718

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

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

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

740
    if (this->lss_time_column_size == 0) {
2,750✔
741
        if (this->lss_token_file->is_time_adjusted()) {
2,750✔
742
            auto time_range = find_string_attr_range(value_out, &L_TIMESTAMP);
17✔
743

744
            if (time_range.lr_end != -1) {
17✔
745
                value_out.emplace_back(
15✔
746
                    time_range, VC_ROLE.value(role_t::VCR_ADJUSTED_TIME));
30✔
747
            }
748
        } else if (this->lss_token_line->is_time_skewed()) {
2,733✔
749
            auto time_range = find_string_attr_range(value_out, &L_TIMESTAMP);
8✔
750

751
            if (time_range.lr_end != -1) {
8✔
752
                value_out.emplace_back(time_range,
8✔
753
                                       VC_ROLE.value(role_t::VCR_SKEWED_TIME));
16✔
754
            }
755
        }
756
    }
757

758
    if (!this->lss_token_line->is_continued()) {
2,750✔
759
        if (this->lss_preview_filter_stmt != nullptr) {
1,042✔
UNCOV
760
            auto color = styling::color_unit::EMPTY;
×
761
            auto eval_res
762
                = this->eval_sql_filter(this->lss_preview_filter_stmt.in(),
763
                                        this->lss_token_file_data,
UNCOV
764
                                        this->lss_token_line);
×
UNCOV
765
            if (eval_res.isErr()) {
×
UNCOV
766
                color = palette_color{
×
UNCOV
767
                    lnav::enums::to_underlying(ansi_color::yellow)};
×
UNCOV
768
                value_out.emplace_back(
×
UNCOV
769
                    line_range{0, -1},
×
UNCOV
770
                    SA_ERROR.value(
×
UNCOV
771
                        eval_res.unwrapErr().to_attr_line().get_string()));
×
772
            } else {
UNCOV
773
                auto matched = eval_res.unwrap();
×
774

UNCOV
775
                if (matched) {
×
UNCOV
776
                    color = palette_color{
×
UNCOV
777
                        lnav::enums::to_underlying(ansi_color::green)};
×
778
                } else {
UNCOV
779
                    color = palette_color{
×
UNCOV
780
                        lnav::enums::to_underlying(ansi_color::red)};
×
UNCOV
781
                    value_out.emplace_back(
×
UNCOV
782
                        line_range{0, 1},
×
UNCOV
783
                        VC_STYLE.value(text_attrs::with_blink()));
×
784
                }
785
            }
UNCOV
786
            value_out.emplace_back(line_range{0, 1},
×
UNCOV
787
                                   VC_BACKGROUND.value(color));
×
788
        }
789

790
        auto sql_filter_opt = this->get_sql_filter();
1,042✔
791
        if (sql_filter_opt) {
1,042✔
792
            auto* sf = (sql_filter*) sql_filter_opt.value().get();
6✔
793
            auto eval_res = this->eval_sql_filter(sf->sf_filter_stmt.in(),
794
                                                  this->lss_token_file_data,
795
                                                  this->lss_token_line);
6✔
796
            if (eval_res.isErr()) {
6✔
797
                auto msg = fmt::format(
UNCOV
798
                    FMT_STRING(
×
799
                        "filter expression evaluation failed with -- {}"),
UNCOV
800
                    eval_res.unwrapErr().to_attr_line().get_string());
×
UNCOV
801
                auto cu = styling::color_unit::from_palette(palette_color{
×
UNCOV
802
                    lnav::enums::to_underlying(ansi_color::yellow)});
×
UNCOV
803
                value_out.emplace_back(line_range{0, -1}, SA_ERROR.value(msg));
×
UNCOV
804
                value_out.emplace_back(line_range{0, 1},
×
UNCOV
805
                                       VC_BACKGROUND.value(cu));
×
806
            }
807
        }
6✔
808
    }
1,042✔
809
}
2,750✔
810

811
struct logline_cmp {
812
    logline_cmp(logfile_sub_source& lc) : llss_controller(lc) {}
1,050✔
813

814
    bool operator()(const logfile_sub_source::indexed_content& lhs,
93,008✔
815
                    const logfile_sub_source::indexed_content& rhs) const
816
    {
817
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
93,008✔
818
        const auto* ll_rhs = this->llss_controller.find_line(rhs.value());
93,008✔
819

820
        return (*ll_lhs) < (*ll_rhs);
93,008✔
821
    }
822

823
    bool operator()(const uint32_t& lhs, const uint32_t& rhs) const
824
    {
825
        auto cl_lhs = llss_controller.lss_index[lhs].value();
826
        auto cl_rhs = llss_controller.lss_index[rhs].value();
827
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
828
        const auto* ll_rhs = this->llss_controller.find_line(cl_rhs);
829

830
        return (*ll_lhs) < (*ll_rhs);
831
    }
832
#if 0
833
        bool operator()(const indexed_content &lhs, const indexed_content &rhs)
834
        {
835
            logline *ll_lhs = this->llss_controller.find_line(lhs.ic_value);
836
            logline *ll_rhs = this->llss_controller.find_line(rhs.ic_value);
837

838
            return (*ll_lhs) < (*ll_rhs);
839
        }
840
#endif
841

842
#if 0
843
    bool operator()(const content_line_t& lhs, const time_t& rhs) const
844
    {
845
        logline* ll_lhs = this->llss_controller.find_line(lhs);
846

847
        return *ll_lhs < rhs;
848
    }
849
#endif
850

851
    bool operator()(const logfile_sub_source::indexed_content& lhs,
×
852
                    const struct timeval& rhs) const
853
    {
UNCOV
854
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
×
855

UNCOV
856
        return *ll_lhs < rhs;
×
857
    }
858

859
    logfile_sub_source& llss_controller;
860
};
861

862
logfile_sub_source::rebuild_result
863
logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
4,029✔
864
{
865
    if (this->tss_view == nullptr) {
4,029✔
866
        return rebuild_result::rr_no_change;
114✔
867
    }
868

869
    this->lss_indexing_in_progress = true;
3,915✔
870
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
3,915✔
871

872
    iterator iter;
3,915✔
873
    size_t total_lines = 0;
3,915✔
874
    size_t est_remaining_lines = 0;
3,915✔
875
    auto all_time_ordered_formats = true;
3,915✔
876
    auto full_sort = this->lss_index.empty();
3,915✔
877
    int file_count = 0;
3,915✔
878
    auto force = std::exchange(this->lss_force_rebuild, false);
3,915✔
879
    auto retval = rebuild_result::rr_no_change;
3,915✔
880
    std::optional<timeval> lowest_tv = std::nullopt;
3,915✔
881
    auto search_start = 0_vl;
3,915✔
882

883
    if (force) {
3,915✔
884
        log_debug("forced to full rebuild");
444✔
885
        retval = rebuild_result::rr_full_rebuild;
444✔
886
        full_sort = true;
444✔
887
    }
888

889
    std::vector<size_t> file_order(this->lss_files.size());
3,915✔
890

891
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
6,697✔
892
        file_order[lpc] = lpc;
2,782✔
893
    }
894
    if (!this->lss_index.empty()) {
3,915✔
895
        std::stable_sort(file_order.begin(),
2,515✔
896
                         file_order.end(),
897
                         [this](const auto& left, const auto& right) {
241✔
898
                             const auto& left_ld = this->lss_files[left];
241✔
899
                             const auto& right_ld = this->lss_files[right];
241✔
900

901
                             if (left_ld->get_file_ptr() == nullptr) {
241✔
UNCOV
902
                                 return true;
×
903
                             }
904
                             if (right_ld->get_file_ptr() == nullptr) {
241✔
905
                                 return false;
4✔
906
                             }
907

908
                             return left_ld->get_file_ptr()->back()
237✔
909
                                 < right_ld->get_file_ptr()->back();
474✔
910
                         });
911
    }
912

913
    bool time_left = true;
3,915✔
914
    this->lss_all_timestamp_flags = 0;
3,915✔
915
    for (const auto file_index : file_order) {
6,697✔
916
        auto& ld = *(this->lss_files[file_index]);
2,782✔
917
        auto* lf = ld.get_file_ptr();
2,782✔
918

919
        if (lf == nullptr) {
2,782✔
920
            if (ld.ld_lines_indexed > 0) {
4✔
921
                log_debug("%d: file closed, doing full rebuild",
1✔
922
                          ld.ld_file_index);
923
                force = true;
1✔
924
                retval = rebuild_result::rr_full_rebuild;
1✔
925
                full_sort = true;
1✔
926
            }
927
        } else {
928
            if (!lf->get_format_ptr()->lf_time_ordered) {
2,778✔
929
                all_time_ordered_formats = false;
161✔
930
            }
931
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
2,778✔
UNCOV
932
                log_debug("no time left, skipping %s",
×
933
                          lf->get_filename_as_string().c_str());
934
                time_left = false;
×
935
            }
936
            this->lss_all_timestamp_flags
2,778✔
937
                |= lf->get_format_ptr()->lf_timestamp_flags;
2,778✔
938

939
            if (!this->tss_view->is_paused() && time_left) {
2,778✔
940
                auto log_rebuild_res = lf->rebuild_index(deadline);
2,778✔
941

942
                if (ld.ld_lines_indexed < lf->size()
2,778✔
943
                    && log_rebuild_res
2,778✔
944
                        == logfile::rebuild_result_t::NO_NEW_LINES)
945
                {
946
                    // This is a bit awkward... if the logfile indexing was
947
                    // complete before being added to us, we need to adjust
948
                    // the rebuild result to make it look like new lines
949
                    // were added.
950
                    log_rebuild_res = logfile::rebuild_result_t::NEW_LINES;
50✔
951
                }
952
                switch (log_rebuild_res) {
2,778✔
953
                    case logfile::rebuild_result_t::NO_NEW_LINES:
2,275✔
954
                        break;
2,275✔
955
                    case logfile::rebuild_result_t::NEW_LINES:
452✔
956
                        if (retval == rebuild_result::rr_no_change) {
452✔
957
                            retval = rebuild_result::rr_appended_lines;
403✔
958
                        }
959
                        log_debug("new lines for %s:%d",
452✔
960
                                  lf->get_filename_as_string().c_str(),
961
                                  lf->size());
962
                        if (!this->lss_index.empty()
452✔
963
                            && lf->size() > ld.ld_lines_indexed)
452✔
964
                        {
UNCOV
965
                            auto& new_file_line = (*lf)[ld.ld_lines_indexed];
×
966
                            auto cl = this->lss_index.back().value();
×
UNCOV
967
                            auto* last_indexed_line = this->find_line(cl);
×
968

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

1023
            est_remaining_lines += lf->estimated_remaining_lines();
2,778✔
1024
        }
1025
    }
1026

1027
    if (this->lss_index.reserve(total_lines + est_remaining_lines)) {
3,915✔
1028
        // The index array was reallocated, just do a full sort/rebuild since
1029
        // it's been cleared out.
1030
        log_debug("expanding index capacity %zu", this->lss_index.ba_capacity);
579✔
1031
        force = true;
579✔
1032
        retval = rebuild_result::rr_full_rebuild;
579✔
1033
        full_sort = true;
579✔
1034
    }
1035

1036
    auto& vis_bm = this->tss_view->get_bookmarks();
3,915✔
1037

1038
    if (force) {
3,915✔
1039
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,526✔
1040
             iter++)
490✔
1041
        {
1042
            (*iter)->ld_lines_indexed = 0;
490✔
1043
        }
1044

1045
        this->lss_index.clear();
1,036✔
1046
        this->lss_filtered_index.clear();
1,036✔
1047
        this->lss_longest_line = 0;
1,036✔
1048
        this->lss_basename_width = 0;
1,036✔
1049
        this->lss_filename_width = 0;
1,036✔
1050
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,036✔
1051
        if (this->lss_index_delegate) {
1,036✔
1052
            this->lss_index_delegate->index_start(*this);
1,036✔
1053
        }
1054
    } else if (retval == rebuild_result::rr_partial_rebuild) {
2,879✔
UNCOV
1055
        size_t remaining = 0;
×
1056

UNCOV
1057
        log_debug("partial rebuild with lowest time: %ld",
×
1058
                  lowest_tv.value().tv_sec);
UNCOV
1059
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
UNCOV
1060
             iter++)
×
1061
        {
UNCOV
1062
            logfile_data& ld = *(*iter);
×
UNCOV
1063
            auto* lf = ld.get_file_ptr();
×
1064

UNCOV
1065
            if (lf == nullptr) {
×
UNCOV
1066
                continue;
×
1067
            }
1068

UNCOV
1069
            require(lf->get_format_ptr()->lf_time_ordered);
×
1070

UNCOV
1071
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1072

UNCOV
1073
            if (line_iter) {
×
UNCOV
1074
                log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
×
1075
                          line_iter.value()->get_timeval().tv_sec,
1076
                          std::distance(lf->cbegin(), line_iter.value()),
1077
                          lf->size(),
1078
                          lf->get_filename_as_string().c_str());
1079
            }
1080
            ld.ld_lines_indexed
1081
                = std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
×
1082
            remaining += lf->size() - ld.ld_lines_indexed;
×
1083
        }
1084

UNCOV
1085
        auto* row_iter = std::lower_bound(this->lss_index.begin(),
×
1086
                                          this->lss_index.end(),
1087
                                          lowest_tv.value(),
×
1088
                                          logline_cmp(*this));
UNCOV
1089
        this->lss_index.shrink_to(
×
1090
            std::distance(this->lss_index.begin(), row_iter));
×
1091
        log_debug("new index size %ld/%ld; remain %ld",
×
1092
                  this->lss_index.ba_size,
1093
                  this->lss_index.ba_capacity,
1094
                  remaining);
UNCOV
1095
        auto filt_row_iter = std::lower_bound(this->lss_filtered_index.begin(),
×
1096
                                              this->lss_filtered_index.end(),
UNCOV
1097
                                              lowest_tv.value(),
×
1098
                                              filtered_logline_cmp(*this));
1099
        this->lss_filtered_index.resize(
×
1100
            std::distance(this->lss_filtered_index.begin(), filt_row_iter));
×
UNCOV
1101
        search_start = vis_line_t(this->lss_filtered_index.size());
×
1102

1103
        if (this->lss_index_delegate) {
×
1104
            this->lss_index_delegate->index_start(*this);
×
1105
            for (const auto row_in_full_index : this->lss_filtered_index) {
×
UNCOV
1106
                auto cl = this->lss_index[row_in_full_index].value();
×
1107
                uint64_t line_number;
1108
                auto ld_iter = this->find_data(cl, line_number);
×
UNCOV
1109
                auto& ld = *ld_iter;
×
1110
                auto line_iter = ld->get_file_ptr()->begin() + line_number;
×
1111

1112
                this->lss_index_delegate->index_line(
×
1113
                    *this, ld->get_file_ptr(), line_iter);
1114
            }
1115
        }
1116
    }
1117

1118
    if (this->lss_index.empty() && !time_left) {
3,915✔
UNCOV
1119
        log_info("ran out of time, skipping rebuild");
×
1120
        // need to make sure we rebuild in case no new data comes in
UNCOV
1121
        this->lss_force_rebuild = true;
×
1122
        return rebuild_result::rr_appended_lines;
×
1123
    }
1124

1125
    if (retval != rebuild_result::rr_no_change || force) {
3,915✔
1126
        size_t index_size = 0, start_size = this->lss_index.size();
1,050✔
1127
        logline_cmp line_cmper(*this);
1,050✔
1128

1129
        for (auto& ld : this->lss_files) {
1,563✔
1130
            auto* lf = ld->get_file_ptr();
513✔
1131

1132
            if (lf == nullptr) {
513✔
1133
                continue;
1✔
1134
            }
1135
            this->lss_longest_line = std::max(
1,024✔
1136
                this->lss_longest_line, lf->get_longest_line_length() + 1);
512✔
1137
            this->lss_basename_width
1138
                = std::max(this->lss_basename_width,
1,024✔
1139
                           lf->get_unique_path().native().size());
512✔
1140
            this->lss_filename_width = std::max(
1,024✔
1141
                this->lss_filename_width, lf->get_filename().native().size());
512✔
1142
        }
1143

1144
        if (full_sort) {
1,050✔
1145
            log_trace("rebuild_index full sort");
1,050✔
1146
            for (auto& ld : this->lss_files) {
1,563✔
1147
                auto* lf = ld->get_file_ptr();
513✔
1148

1149
                if (lf == nullptr) {
513✔
1150
                    continue;
1✔
1151
                }
1152

1153
                for (size_t line_index = 0; line_index < lf->size();
12,729✔
1154
                     line_index++)
1155
                {
1156
                    const auto lf_iter
1157
                        = ld->get_file_ptr()->begin() + line_index;
12,217✔
1158
                    if (lf_iter->is_ignored()) {
12,217✔
1159
                        continue;
233✔
1160
                    }
1161

1162
                    content_line_t con_line(
1163
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
11,984✔
1164

1165
                    if (lf_iter->is_meta_marked()) {
11,984✔
1166
                        auto start_iter = lf_iter;
11✔
1167
                        while (start_iter->is_continued()) {
11✔
UNCOV
1168
                            --start_iter;
×
1169
                        }
1170
                        int start_index
1171
                            = start_iter - ld->get_file_ptr()->begin();
11✔
1172
                        content_line_t start_con_line(ld->ld_file_index
11✔
1173
                                                          * MAX_LINES_PER_FILE
11✔
1174
                                                      + start_index);
11✔
1175

1176
                        auto& line_meta
1177
                            = ld->get_file_ptr()
1178
                                  ->get_bookmark_metadata()[start_index];
11✔
1179
                        if (line_meta.has(bookmark_metadata::categories::notes))
11✔
1180
                        {
1181
                            this->lss_user_marks[&textview_curses::BM_META]
3✔
1182
                                .insert_once(start_con_line);
3✔
1183
                        }
1184
                        if (line_meta.has(
11✔
1185
                                bookmark_metadata::categories::partition))
1186
                        {
1187
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
8✔
1188
                                .insert_once(start_con_line);
8✔
1189
                        }
1190
                    }
1191
                    this->lss_index.push_back(
11,984✔
1192
                        indexed_content{con_line, lf_iter});
23,968✔
1193
                }
1194
            }
1195

1196
            // XXX get rid of this full sort on the initial run, it's not
1197
            // needed unless the file is not in time-order
1198
            if (this->lss_sorting_observer) {
1,050✔
UNCOV
1199
                this->lss_sorting_observer(*this, 0, this->lss_index.size());
×
1200
            }
1201
            std::sort(
1,050✔
1202
                this->lss_index.begin(), this->lss_index.end(), line_cmper);
1203
            if (this->lss_sorting_observer) {
1,050✔
1204
                this->lss_sorting_observer(
×
UNCOV
1205
                    *this, this->lss_index.size(), this->lss_index.size());
×
1206
            }
1207
        } else {
1208
            kmerge_tree_c<logline, logfile_data, logfile::iterator> merge(
UNCOV
1209
                file_count);
×
1210

UNCOV
1211
            for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
UNCOV
1212
                 iter++)
×
1213
            {
UNCOV
1214
                auto* ld = iter->get();
×
UNCOV
1215
                auto* lf = ld->get_file_ptr();
×
UNCOV
1216
                if (lf == nullptr) {
×
UNCOV
1217
                    continue;
×
1218
                }
1219

UNCOV
1220
                merge.add(ld, lf->begin() + ld->ld_lines_indexed, lf->end());
×
UNCOV
1221
                index_size += lf->size();
×
1222
            }
1223

UNCOV
1224
            file_off_t index_off = 0;
×
UNCOV
1225
            merge.execute();
×
UNCOV
1226
            if (this->lss_sorting_observer) {
×
UNCOV
1227
                this->lss_sorting_observer(*this, index_off, index_size);
×
1228
            }
UNCOV
1229
            log_trace("k-way merge");
×
1230
            for (;;) {
UNCOV
1231
                logfile::iterator lf_iter;
×
1232
                logfile_data* ld;
1233

1234
                if (!merge.get_top(ld, lf_iter)) {
×
1235
                    break;
×
1236
                }
1237

1238
                if (!lf_iter->is_ignored()) {
×
1239
                    int file_index = ld->ld_file_index;
×
1240
                    int line_index = lf_iter - ld->get_file_ptr()->begin();
×
1241

1242
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
UNCOV
1243
                                            + line_index);
×
1244

UNCOV
1245
                    if (lf_iter->is_meta_marked()) {
×
UNCOV
1246
                        auto start_iter = lf_iter;
×
UNCOV
1247
                        while (start_iter->is_continued()) {
×
UNCOV
1248
                            --start_iter;
×
1249
                        }
1250
                        int start_index
UNCOV
1251
                            = start_iter - ld->get_file_ptr()->begin();
×
1252
                        content_line_t start_con_line(
UNCOV
1253
                            file_index * MAX_LINES_PER_FILE + start_index);
×
1254

1255
                        auto& line_meta
1256
                            = ld->get_file_ptr()
UNCOV
1257
                                  ->get_bookmark_metadata()[start_index];
×
UNCOV
1258
                        if (line_meta.has(bookmark_metadata::categories::notes))
×
1259
                        {
UNCOV
1260
                            this->lss_user_marks[&textview_curses::BM_META]
×
UNCOV
1261
                                .insert_once(start_con_line);
×
1262
                        }
UNCOV
1263
                        if (line_meta.has(
×
1264
                                bookmark_metadata::categories::partition))
1265
                        {
UNCOV
1266
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
×
UNCOV
1267
                                .insert_once(start_con_line);
×
1268
                        }
1269
                    }
UNCOV
1270
                    this->lss_index.push_back(
×
UNCOV
1271
                        indexed_content{con_line, lf_iter});
×
1272
                }
1273

UNCOV
1274
                merge.next();
×
UNCOV
1275
                index_off += 1;
×
UNCOV
1276
                if (index_off % 100000 == 0 && this->lss_sorting_observer) {
×
UNCOV
1277
                    this->lss_sorting_observer(*this, index_off, index_size);
×
1278
                }
1279
            }
UNCOV
1280
            if (this->lss_sorting_observer) {
×
UNCOV
1281
                this->lss_sorting_observer(*this, index_size, index_size);
×
1282
            }
1283
        }
1284

1285
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,563✔
1286
             ++iter)
513✔
1287
        {
1288
            auto* lf = (*iter)->get_file_ptr();
513✔
1289

1290
            if (lf == nullptr) {
513✔
1291
                continue;
1✔
1292
            }
1293

1294
            (*iter)->ld_lines_indexed = lf->size();
512✔
1295
        }
1296

1297
        this->lss_filtered_index.reserve(this->lss_index.size());
1,050✔
1298

1299
        uint32_t filter_in_mask, filter_out_mask;
1300
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,050✔
1301

1302
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,050✔
1303
            this->lss_index_delegate->index_start(*this);
1,050✔
1304
        }
1305

1306
        log_trace("filtered index");
1,050✔
1307
        for (size_t index_index = start_size;
13,034✔
1308
             index_index < this->lss_index.size();
13,034✔
1309
             index_index++)
1310
        {
1311
            const auto cl = this->lss_index[index_index].value();
11,984✔
1312
            uint64_t line_number;
1313
            auto ld = this->find_data(cl, line_number);
11,984✔
1314

1315
            if (!(*ld)->is_visible()) {
11,984✔
UNCOV
1316
                continue;
×
1317
            }
1318

1319
            auto* lf = (*ld)->get_file_ptr();
11,984✔
1320
            auto line_iter = lf->begin() + line_number;
11,984✔
1321

1322
            if (line_iter->is_ignored()) {
11,984✔
UNCOV
1323
                continue;
×
1324
            }
1325

1326
            if (!this->tss_apply_filters
23,968✔
1327
                || (!(*ld)->ld_filter_state.excluded(
23,958✔
1328
                        filter_in_mask, filter_out_mask, line_number)
1329
                    && this->check_extra_filters(ld, line_iter)))
11,974✔
1330
            {
1331
                auto eval_res = this->eval_sql_filter(
1332
                    this->lss_marker_stmt.in(), ld, line_iter);
11,974✔
1333
                if (eval_res.isErr()) {
11,974✔
UNCOV
1334
                    line_iter->set_expr_mark(false);
×
1335
                } else {
1336
                    auto matched = eval_res.unwrap();
11,974✔
1337

1338
                    if (matched) {
11,974✔
UNCOV
1339
                        line_iter->set_expr_mark(true);
×
UNCOV
1340
                        vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
UNCOV
1341
                            vis_line_t(this->lss_filtered_index.size()));
×
1342
                    } else {
1343
                        line_iter->set_expr_mark(false);
11,974✔
1344
                    }
1345
                }
1346
                this->lss_filtered_index.push_back(index_index);
11,974✔
1347
                if (this->lss_index_delegate != nullptr) {
11,974✔
1348
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
11,974✔
1349
                }
1350
            }
11,974✔
1351
        }
1352

1353
        this->lss_indexing_in_progress = false;
1,050✔
1354

1355
        if (this->lss_index_delegate != nullptr) {
1,050✔
1356
            this->lss_index_delegate->index_complete(*this);
1,050✔
1357
        }
1358
    }
1359

1360
    switch (retval) {
3,915✔
1361
        case rebuild_result::rr_no_change:
2,865✔
1362
            break;
2,865✔
1363
        case rebuild_result::rr_full_rebuild:
1,036✔
1364
            log_debug("redoing search");
1,036✔
1365
            this->lss_index_generation += 1;
1,036✔
1366
            this->tss_view->reload_data();
1,036✔
1367
            this->tss_view->redo_search();
1,036✔
1368
            break;
1,036✔
UNCOV
1369
        case rebuild_result::rr_partial_rebuild:
×
UNCOV
1370
            log_debug("redoing search from: %d", (int) search_start);
×
UNCOV
1371
            this->lss_index_generation += 1;
×
UNCOV
1372
            this->tss_view->reload_data();
×
UNCOV
1373
            this->tss_view->search_new_data(search_start);
×
UNCOV
1374
            break;
×
1375
        case rebuild_result::rr_appended_lines:
14✔
1376
            this->tss_view->reload_data();
14✔
1377
            this->tss_view->search_new_data();
14✔
1378
            break;
14✔
1379
    }
1380

1381
    return retval;
3,915✔
1382
}
3,915✔
1383

1384
void
1385
logfile_sub_source::text_update_marks(vis_bookmarks& bm)
2,458✔
1386
{
1387
    logfile* last_file = nullptr;
2,458✔
1388
    vis_line_t vl;
2,458✔
1389

1390
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,458✔
1391
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,458✔
1392
    auto& bm_files = bm[&BM_FILES];
2,458✔
1393

1394
    bm_warnings.clear();
2,458✔
1395
    bm_errors.clear();
2,458✔
1396
    bm_files.clear();
2,458✔
1397

1398
    std::vector<const bookmark_type_t*> used_marks;
2,458✔
1399
    for (const auto* bmt : {
12,290✔
1400
             &textview_curses::BM_USER,
1401
             &textview_curses::BM_USER_EXPR,
1402
             &textview_curses::BM_PARTITION,
1403
             &textview_curses::BM_META,
1404
         })
14,748✔
1405
    {
1406
        bm[bmt].clear();
9,832✔
1407
        if (!this->lss_user_marks[bmt].empty()) {
9,832✔
1408
            used_marks.emplace_back(bmt);
112✔
1409
        }
1410
    }
1411

1412
    for (; vl < (int) this->lss_filtered_index.size(); ++vl) {
16,233✔
1413
        const auto& orig_ic = this->lss_index[this->lss_filtered_index[vl]];
13,775✔
1414
        auto cl = orig_ic.value();
13,775✔
1415
        auto* lf = this->find_file_ptr(cl);
13,775✔
1416

1417
        for (const auto& bmt : used_marks) {
15,237✔
1418
            auto& user_mark = this->lss_user_marks[bmt];
1,462✔
1419
            if (user_mark.bv_tree.exists(orig_ic.value())) {
1,462✔
1420
                bm[bmt].insert_once(vl);
125✔
1421

1422
                if (bmt == &textview_curses::BM_USER) {
125✔
1423
                    auto ll = lf->begin() + cl;
40✔
1424

1425
                    ll->set_mark(true);
40✔
1426
                }
1427
            }
1428
        }
1429

1430
        if (lf != last_file) {
13,775✔
1431
            bm_files.insert_once(vl);
896✔
1432
        }
1433

1434
        switch (orig_ic.level()) {
13,775✔
1435
            case indexed_content::level_t::warning:
58✔
1436
                bm_warnings.insert_once(vl);
58✔
1437
                break;
58✔
1438

1439
            case indexed_content::level_t::error:
995✔
1440
                bm_errors.insert_once(vl);
995✔
1441
                break;
995✔
1442

1443
            default:
12,722✔
1444
                break;
12,722✔
1445
        }
1446

1447
        last_file = lf;
13,775✔
1448
    }
1449
}
2,458✔
1450

1451
void
1452
logfile_sub_source::text_filters_changed()
151✔
1453
{
1454
    this->lss_index_generation += 1;
151✔
1455

1456
    if (this->lss_line_meta_changed) {
151✔
1457
        this->invalidate_sql_filter();
20✔
1458
        this->lss_line_meta_changed = false;
20✔
1459
    }
1460

1461
    for (auto& ld : *this) {
257✔
1462
        auto* lf = ld->get_file_ptr();
106✔
1463

1464
        if (lf != nullptr) {
106✔
1465
            ld->ld_filter_state.clear_deleted_filter_state();
106✔
1466
            lf->reobserve_from(lf->begin()
106✔
1467
                               + ld->ld_filter_state.get_min_count(lf->size()));
106✔
1468
        }
1469
    }
1470

1471
    if (this->lss_force_rebuild) {
151✔
1472
        return;
×
1473
    }
1474

1475
    auto& vis_bm = this->tss_view->get_bookmarks();
151✔
1476
    uint32_t filtered_in_mask, filtered_out_mask;
1477

1478
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
151✔
1479

1480
    if (this->lss_index_delegate != nullptr) {
151✔
1481
        this->lss_index_delegate->index_start(*this);
151✔
1482
    }
1483
    vis_bm[&textview_curses::BM_USER_EXPR].clear();
151✔
1484

1485
    this->lss_filtered_index.clear();
151✔
1486
    for (size_t index_index = 0; index_index < this->lss_index.size();
964✔
1487
         index_index++)
1488
    {
1489
        auto cl = this->lss_index[index_index].value();
813✔
1490
        uint64_t line_number;
1491
        auto ld = this->find_data(cl, line_number);
813✔
1492

1493
        if (!(*ld)->is_visible()) {
813✔
1494
            continue;
210✔
1495
        }
1496

1497
        auto lf = (*ld)->get_file_ptr();
603✔
1498
        auto line_iter = lf->begin() + line_number;
603✔
1499

1500
        if (!this->tss_apply_filters
1,206✔
1501
            || (!(*ld)->ld_filter_state.excluded(
1,090✔
1502
                    filtered_in_mask, filtered_out_mask, line_number)
1503
                && this->check_extra_filters(ld, line_iter)))
487✔
1504
        {
1505
            auto eval_res = this->eval_sql_filter(
1506
                this->lss_marker_stmt.in(), ld, line_iter);
478✔
1507
            if (eval_res.isErr()) {
478✔
UNCOV
1508
                line_iter->set_expr_mark(false);
×
1509
            } else {
1510
                auto matched = eval_res.unwrap();
478✔
1511

1512
                if (matched) {
478✔
UNCOV
1513
                    line_iter->set_expr_mark(true);
×
UNCOV
1514
                    vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
UNCOV
1515
                        vis_line_t(this->lss_filtered_index.size()));
×
1516
                } else {
1517
                    line_iter->set_expr_mark(false);
478✔
1518
                }
1519
            }
1520
            this->lss_filtered_index.push_back(index_index);
478✔
1521
            if (this->lss_index_delegate != nullptr) {
478✔
1522
                this->lss_index_delegate->index_line(*this, lf, line_iter);
478✔
1523
            }
1524
        }
478✔
1525
    }
1526

1527
    if (this->lss_index_delegate != nullptr) {
151✔
1528
        this->lss_index_delegate->index_complete(*this);
151✔
1529
    }
1530

1531
    if (this->tss_view != nullptr) {
151✔
1532
        this->tss_view->reload_data();
151✔
1533
        this->tss_view->redo_search();
151✔
1534
    }
1535
}
1536

1537
std::optional<json_string>
1538
logfile_sub_source::text_row_details(const textview_curses& tc)
25✔
1539
{
1540
    if (this->lss_index.empty()) {
25✔
1541
        log_trace("logfile_sub_source::text_row_details empty");
1✔
1542
        return std::nullopt;
1✔
1543
    }
1544

1545
    auto ov_sel = tc.get_overlay_selection();
24✔
1546
    if (ov_sel.has_value()) {
24✔
1547
        auto* fos
UNCOV
1548
            = dynamic_cast<field_overlay_source*>(tc.get_overlay_source());
×
UNCOV
1549
        auto iter = fos->fos_row_to_field_meta.find(ov_sel.value());
×
UNCOV
1550
        if (iter != fos->fos_row_to_field_meta.end()) {
×
UNCOV
1551
            auto find_res = this->find_line_with_file(tc.get_top());
×
UNCOV
1552
            if (find_res) {
×
UNCOV
1553
                yajlpp_gen gen;
×
1554

1555
                {
UNCOV
1556
                    yajlpp_map root(gen);
×
1557

UNCOV
1558
                    root.gen("value");
×
UNCOV
1559
                    root.gen(iter->second.ri_value);
×
1560
                }
1561

UNCOV
1562
                return json_string(gen);
×
1563
            }
1564
        }
1565
    }
1566

1567
    return std::nullopt;
24✔
1568
}
1569

1570
bool
UNCOV
1571
logfile_sub_source::list_input_handle_key(listview_curses& lv,
×
1572
                                          const ncinput& ch)
1573
{
UNCOV
1574
    switch (ch.eff_text[0]) {
×
UNCOV
1575
        case ' ': {
×
UNCOV
1576
            auto ov_vl = lv.get_overlay_selection();
×
UNCOV
1577
            if (ov_vl) {
×
UNCOV
1578
                auto* fos = dynamic_cast<field_overlay_source*>(
×
UNCOV
1579
                    lv.get_overlay_source());
×
UNCOV
1580
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
UNCOV
1581
                if (iter != fos->fos_row_to_field_meta.end()
×
UNCOV
1582
                    && iter->second.ri_meta)
×
1583
                {
UNCOV
1584
                    auto find_res = this->find_line_with_file(lv.get_top());
×
UNCOV
1585
                    if (find_res) {
×
UNCOV
1586
                        auto file_and_line = find_res.value();
×
UNCOV
1587
                        auto* format = file_and_line.first->get_format_ptr();
×
1588
                        auto fstates = format->get_field_states();
×
1589
                        auto state_iter
UNCOV
1590
                            = fstates.find(iter->second.ri_meta->lvm_name);
×
UNCOV
1591
                        if (state_iter != fstates.end()) {
×
UNCOV
1592
                            format->hide_field(iter->second.ri_meta->lvm_name,
×
UNCOV
1593
                                               !state_iter->second.is_hidden());
×
UNCOV
1594
                            lv.set_needs_update();
×
1595
                        }
1596
                    }
1597
                }
UNCOV
1598
                return true;
×
1599
            }
UNCOV
1600
            return false;
×
1601
        }
UNCOV
1602
        case '#': {
×
UNCOV
1603
            auto ov_vl = lv.get_overlay_selection();
×
UNCOV
1604
            if (ov_vl) {
×
UNCOV
1605
                auto* fos = dynamic_cast<field_overlay_source*>(
×
UNCOV
1606
                    lv.get_overlay_source());
×
UNCOV
1607
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
UNCOV
1608
                if (iter != fos->fos_row_to_field_meta.end()
×
UNCOV
1609
                    && iter->second.ri_meta)
×
1610
                {
UNCOV
1611
                    const auto& meta = iter->second.ri_meta.value();
×
UNCOV
1612
                    std::string cmd;
×
1613

UNCOV
1614
                    switch (meta.to_chart_type()) {
×
UNCOV
1615
                        case chart_type_t::none:
×
UNCOV
1616
                            break;
×
UNCOV
1617
                        case chart_type_t::hist: {
×
1618
                            auto prql = fmt::format(
UNCOV
1619
                                FMT_STRING(
×
1620
                                    "from {} | stats.hist {} slice:'1h'"),
UNCOV
1621
                                meta.lvm_format.value()->get_name(),
×
UNCOV
1622
                                meta.lvm_name);
×
UNCOV
1623
                            cmd = fmt::format(FMT_STRING(":prompt sql ; '{}'"),
×
1624
                                              shlex::escape(prql));
×
UNCOV
1625
                            break;
×
1626
                        }
UNCOV
1627
                        case chart_type_t::spectro:
×
UNCOV
1628
                            cmd = fmt::format(FMT_STRING(":spectrogram {}"),
×
UNCOV
1629
                                              meta.lvm_name);
×
UNCOV
1630
                            break;
×
1631
                    }
UNCOV
1632
                    if (!cmd.empty()) {
×
UNCOV
1633
                        this->lss_exec_context
×
UNCOV
1634
                            ->with_provenance(exec_context::mouse_input{})
×
UNCOV
1635
                            ->execute(INTERNAL_SRC_LOC, cmd);
×
1636
                    }
1637
                }
UNCOV
1638
                return true;
×
1639
            }
UNCOV
1640
            return false;
×
1641
        }
UNCOV
1642
        case 'h':
×
1643
        case 'H':
1644
        case NCKEY_LEFT:
UNCOV
1645
            if (lv.get_left() == 0) {
×
UNCOV
1646
                this->increase_line_context();
×
UNCOV
1647
                lv.set_needs_update();
×
UNCOV
1648
                return true;
×
1649
            }
UNCOV
1650
            break;
×
1651
        case 'l':
×
1652
        case 'L':
1653
        case NCKEY_RIGHT:
1654
            if (this->decrease_line_context()) {
×
UNCOV
1655
                lv.set_needs_update();
×
1656
                return true;
×
1657
            }
1658
            break;
×
1659
    }
UNCOV
1660
    return false;
×
1661
}
1662

1663
std::optional<
1664
    std::pair<grep_proc_source<vis_line_t>*, grep_proc_sink<vis_line_t>*>>
1665
logfile_sub_source::get_grepper()
9✔
1666
{
1667
    return std::make_pair(
18✔
UNCOV
1668
        (grep_proc_source<vis_line_t>*) &this->lss_meta_grepper,
×
1669
        (grep_proc_sink<vis_line_t>*) &this->lss_meta_grepper);
9✔
1670
}
1671

1672
/**
1673
 * Functor for comparing the ld_file field of the logfile_data struct.
1674
 */
1675
struct logfile_data_eq {
1676
    explicit logfile_data_eq(std::shared_ptr<logfile> lf)
964✔
1677
        : lde_file(std::move(lf))
964✔
1678
    {
1679
    }
964✔
1680

1681
    bool operator()(
596✔
1682
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1683
    {
1684
        return this->lde_file == ld->get_file();
596✔
1685
    }
1686

1687
    std::shared_ptr<logfile> lde_file;
1688
};
1689

1690
bool
1691
logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
482✔
1692
{
1693
    iterator existing;
482✔
1694

1695
    require_lt(lf->size(), MAX_LINES_PER_FILE);
482✔
1696

1697
    existing = std::find_if(this->lss_files.begin(),
482✔
1698
                            this->lss_files.end(),
1699
                            logfile_data_eq(nullptr));
964✔
1700
    if (existing == this->lss_files.end()) {
482✔
1701
        if (this->lss_files.size() >= MAX_FILES) {
482✔
UNCOV
1702
            return false;
×
1703
        }
1704

1705
        auto ld = std::make_unique<logfile_data>(
1706
            this->lss_files.size(), this->get_filters(), lf);
482✔
1707
        ld->set_visibility(lf->get_open_options().loo_is_visible);
482✔
1708
        this->lss_files.push_back(std::move(ld));
482✔
1709
    } else {
482✔
UNCOV
1710
        (*existing)->set_file(lf);
×
1711
    }
1712

1713
    return true;
482✔
1714
}
1715

1716
Result<void, lnav::console::user_message>
1717
logfile_sub_source::set_sql_filter(std::string stmt_str, sqlite3_stmt* stmt)
705✔
1718
{
1719
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
705✔
1720
        auto top_cl = this->at(0_vl);
6✔
1721
        auto ld = this->find_data(top_cl);
6✔
1722
        auto eval_res
1723
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
6✔
1724

1725
        if (eval_res.isErr()) {
6✔
1726
            sqlite3_finalize(stmt);
1✔
1727
            return Err(eval_res.unwrapErr());
1✔
1728
        }
1729
    }
6✔
1730

1731
    for (auto& ld : *this) {
718✔
1732
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
14✔
1733
    }
1734

1735
    auto old_filter = this->get_sql_filter();
704✔
1736
    if (stmt != nullptr) {
704✔
1737
        auto new_filter
1738
            = std::make_shared<sql_filter>(*this, std::move(stmt_str), stmt);
5✔
1739

1740
        if (old_filter) {
5✔
1741
            auto existing_iter = std::find(this->tss_filters.begin(),
×
1742
                                           this->tss_filters.end(),
1743
                                           old_filter.value());
×
1744
            *existing_iter = new_filter;
×
1745
        } else {
1746
            this->tss_filters.add_filter(new_filter);
5✔
1747
        }
1748
    } else if (old_filter) {
704✔
1749
        this->tss_filters.delete_filter(old_filter.value()->get_id());
5✔
1750
    }
1751

1752
    return Ok();
704✔
1753
}
704✔
1754

1755
Result<void, lnav::console::user_message>
1756
logfile_sub_source::set_sql_marker(std::string stmt_str, sqlite3_stmt* stmt)
701✔
1757
{
1758
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
701✔
1759
        auto top_cl = this->at(0_vl);
2✔
1760
        auto ld = this->find_data(top_cl);
2✔
1761
        auto eval_res
1762
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
2✔
1763

1764
        if (eval_res.isErr()) {
2✔
UNCOV
1765
            sqlite3_finalize(stmt);
×
UNCOV
1766
            return Err(eval_res.unwrapErr());
×
1767
        }
1768
    }
2✔
1769

1770
    this->lss_marker_stmt_text = std::move(stmt_str);
701✔
1771
    this->lss_marker_stmt = stmt;
701✔
1772

1773
    if (this->tss_view == nullptr || this->lss_force_rebuild) {
701✔
1774
        return Ok();
120✔
1775
    }
1776

1777
    auto& vis_bm = this->tss_view->get_bookmarks();
581✔
1778
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
581✔
1779

1780
    expr_marks_bv.clear();
581✔
1781
    if (this->lss_index_delegate) {
581✔
1782
        this->lss_index_delegate->index_start(*this);
581✔
1783
    }
1784
    for (auto row = 0_vl; row < vis_line_t(this->lss_filtered_index.size());
588✔
1785
         row += 1_vl)
7✔
1786
    {
1787
        auto cl = this->at(row);
7✔
1788
        auto ld = this->find_data(cl);
7✔
1789

1790
        if (!(*ld)->is_visible()) {
7✔
1791
            continue;
1✔
1792
        }
1793
        auto ll = (*ld)->get_file()->begin() + cl;
7✔
1794
        if (ll->is_continued() || ll->is_ignored()) {
7✔
1795
            continue;
1✔
1796
        }
1797
        auto eval_res
1798
            = this->eval_sql_filter(this->lss_marker_stmt.in(), ld, ll);
6✔
1799

1800
        if (eval_res.isErr()) {
6✔
UNCOV
1801
            ll->set_expr_mark(false);
×
1802
        } else {
1803
            auto matched = eval_res.unwrap();
6✔
1804

1805
            if (matched) {
6✔
1806
                ll->set_expr_mark(true);
4✔
1807
                expr_marks_bv.insert_once(row);
4✔
1808
            } else {
1809
                ll->set_expr_mark(false);
2✔
1810
            }
1811
        }
1812
        if (this->lss_index_delegate) {
6✔
1813
            this->lss_index_delegate->index_line(
6✔
1814
                *this, (*ld)->get_file_ptr(), ll);
6✔
1815
        }
1816
    }
6✔
1817
    if (this->lss_index_delegate) {
581✔
1818
        this->lss_index_delegate->index_complete(*this);
581✔
1819
    }
1820

1821
    return Ok();
581✔
1822
}
1823

1824
Result<void, lnav::console::user_message>
1825
logfile_sub_source::set_preview_sql_filter(sqlite3_stmt* stmt)
697✔
1826
{
1827
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
697✔
UNCOV
1828
        auto top_cl = this->at(0_vl);
×
UNCOV
1829
        auto ld = this->find_data(top_cl);
×
1830
        auto eval_res
UNCOV
1831
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
×
1832

UNCOV
1833
        if (eval_res.isErr()) {
×
UNCOV
1834
            sqlite3_finalize(stmt);
×
UNCOV
1835
            return Err(eval_res.unwrapErr());
×
1836
        }
1837
    }
1838

1839
    this->lss_preview_filter_stmt = stmt;
697✔
1840

1841
    return Ok();
697✔
1842
}
1843

1844
Result<bool, lnav::console::user_message>
1845
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
12,497✔
1846
                                    iterator ld,
1847
                                    logfile::const_iterator ll)
1848
{
1849
    if (stmt == nullptr) {
12,497✔
1850
        return Ok(false);
24,904✔
1851
    }
1852

1853
    auto* lf = (*ld)->get_file_ptr();
45✔
1854
    char timestamp_buffer[64];
1855
    shared_buffer_ref raw_sbr;
45✔
1856
    logline_value_vector values;
45✔
1857
    auto& sbr = values.lvv_sbr;
45✔
1858
    lf->read_full_message(ll, sbr);
45✔
1859
    sbr.erase_ansi();
45✔
1860
    auto format = lf->get_format();
45✔
1861
    string_attrs_t sa;
45✔
1862
    auto line_number = std::distance(lf->cbegin(), ll);
45✔
1863
    format->annotate(lf, line_number, sa, values);
45✔
1864

1865
    sqlite3_reset(stmt);
45✔
1866
    sqlite3_clear_bindings(stmt);
45✔
1867

1868
    auto count = sqlite3_bind_parameter_count(stmt);
45✔
1869
    for (int lpc = 0; lpc < count; lpc++) {
93✔
1870
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
48✔
1871

1872
        if (name[0] == '$') {
48✔
1873
            const char* env_value;
1874

1875
            if ((env_value = getenv(&name[1])) != nullptr) {
1✔
1876
                sqlite3_bind_text(stmt, lpc + 1, env_value, -1, SQLITE_STATIC);
1✔
1877
            }
1878
            continue;
1✔
1879
        }
1✔
1880
        if (strcmp(name, ":log_level") == 0) {
47✔
1881
            auto lvl = ll->get_level_name();
3✔
1882
            sqlite3_bind_text(
3✔
1883
                stmt, lpc + 1, lvl.data(), lvl.length(), SQLITE_STATIC);
1884
            continue;
3✔
1885
        }
3✔
1886
        if (strcmp(name, ":log_time") == 0) {
44✔
UNCOV
1887
            auto len = sql_strftime(timestamp_buffer,
×
1888
                                    sizeof(timestamp_buffer),
UNCOV
1889
                                    ll->get_timeval(),
×
1890
                                    'T');
UNCOV
1891
            sqlite3_bind_text(
×
1892
                stmt, lpc + 1, timestamp_buffer, len, SQLITE_STATIC);
UNCOV
1893
            continue;
×
1894
        }
1895
        if (strcmp(name, ":log_time_msecs") == 0) {
44✔
1896
            sqlite3_bind_int64(
1✔
1897
                stmt,
1898
                lpc + 1,
1899
                ll->get_time<std::chrono::milliseconds>().count());
1✔
1900
            continue;
1✔
1901
        }
1902
        if (strcmp(name, ":log_mark") == 0) {
43✔
UNCOV
1903
            sqlite3_bind_int(stmt, lpc + 1, ll->is_marked());
×
UNCOV
1904
            continue;
×
1905
        }
1906
        if (strcmp(name, ":log_comment") == 0) {
43✔
UNCOV
1907
            const auto& bm = lf->get_bookmark_metadata();
×
1908
            auto line_number
UNCOV
1909
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
UNCOV
1910
            auto bm_iter = bm.find(line_number);
×
UNCOV
1911
            if (bm_iter != bm.end() && !bm_iter->second.bm_comment.empty()) {
×
UNCOV
1912
                const auto& meta = bm_iter->second;
×
UNCOV
1913
                sqlite3_bind_text(stmt,
×
1914
                                  lpc + 1,
1915
                                  meta.bm_comment.c_str(),
UNCOV
1916
                                  meta.bm_comment.length(),
×
1917
                                  SQLITE_STATIC);
1918
            }
UNCOV
1919
            continue;
×
1920
        }
1921
        if (strcmp(name, ":log_annotations") == 0) {
43✔
UNCOV
1922
            const auto& bm = lf->get_bookmark_metadata();
×
1923
            auto line_number
UNCOV
1924
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
UNCOV
1925
            auto bm_iter = bm.find(line_number);
×
UNCOV
1926
            if (bm_iter != bm.end()
×
UNCOV
1927
                && !bm_iter->second.bm_annotations.la_pairs.empty())
×
1928
            {
UNCOV
1929
                const auto& meta = bm_iter->second;
×
1930
                auto anno_str = logmsg_annotations_handlers.to_string(
UNCOV
1931
                    meta.bm_annotations);
×
1932

UNCOV
1933
                sqlite3_bind_text(stmt,
×
1934
                                  lpc + 1,
1935
                                  anno_str.c_str(),
UNCOV
1936
                                  anno_str.length(),
×
1937
                                  SQLITE_TRANSIENT);
1938
            }
UNCOV
1939
            continue;
×
1940
        }
1941
        if (strcmp(name, ":log_tags") == 0) {
43✔
1942
            const auto& bm = lf->get_bookmark_metadata();
9✔
1943
            auto line_number
1944
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
9✔
1945
            auto bm_iter = bm.find(line_number);
9✔
1946
            if (bm_iter != bm.end() && !bm_iter->second.bm_tags.empty()) {
9✔
1947
                const auto& meta = bm_iter->second;
1✔
1948
                yajlpp_gen gen;
1✔
1949

1950
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
1951

1952
                {
1953
                    yajlpp_array arr(gen);
1✔
1954

1955
                    for (const auto& str : meta.bm_tags) {
2✔
1956
                        arr.gen(str);
1✔
1957
                    }
1958
                }
1✔
1959

1960
                string_fragment sf = gen.to_string_fragment();
1✔
1961

1962
                sqlite3_bind_text(
1✔
1963
                    stmt, lpc + 1, sf.data(), sf.length(), SQLITE_TRANSIENT);
1964
            }
1✔
1965
            continue;
9✔
1966
        }
9✔
1967
        if (strcmp(name, ":log_format") == 0) {
34✔
1968
            const auto format_name = format->get_name();
3✔
1969
            sqlite3_bind_text(stmt,
3✔
1970
                              lpc + 1,
1971
                              format_name.get(),
1972
                              format_name.size(),
3✔
1973
                              SQLITE_STATIC);
1974
            continue;
3✔
1975
        }
3✔
1976
        if (strcmp(name, ":log_format_regex") == 0) {
31✔
UNCOV
1977
            const auto pat_name = format->get_pattern_name(line_number);
×
UNCOV
1978
            sqlite3_bind_text(
×
UNCOV
1979
                stmt, lpc + 1, pat_name.get(), pat_name.size(), SQLITE_STATIC);
×
UNCOV
1980
            continue;
×
1981
        }
1982
        if (strcmp(name, ":log_path") == 0) {
31✔
UNCOV
1983
            const auto& filename = lf->get_filename();
×
UNCOV
1984
            sqlite3_bind_text(stmt,
×
1985
                              lpc + 1,
1986
                              filename.c_str(),
UNCOV
1987
                              filename.native().length(),
×
1988
                              SQLITE_STATIC);
UNCOV
1989
            continue;
×
1990
        }
1991
        if (strcmp(name, ":log_unique_path") == 0) {
31✔
UNCOV
1992
            const auto& filename = lf->get_unique_path();
×
UNCOV
1993
            sqlite3_bind_text(stmt,
×
1994
                              lpc + 1,
1995
                              filename.c_str(),
UNCOV
1996
                              filename.native().length(),
×
1997
                              SQLITE_STATIC);
UNCOV
1998
            continue;
×
1999
        }
2000
        if (strcmp(name, ":log_text") == 0) {
31✔
2001
            sqlite3_bind_text(
4✔
2002
                stmt, lpc + 1, sbr.get_data(), sbr.length(), SQLITE_STATIC);
4✔
2003
            continue;
4✔
2004
        }
2005
        if (strcmp(name, ":log_body") == 0) {
27✔
2006
            auto body_attr_opt = get_string_attr(sa, SA_BODY);
10✔
2007
            if (body_attr_opt) {
10✔
2008
                const auto& sar
2009
                    = body_attr_opt.value().saw_string_attr->sa_range;
10✔
2010

2011
                sqlite3_bind_text(stmt,
20✔
2012
                                  lpc + 1,
2013
                                  sbr.get_data_at(sar.lr_start),
10✔
2014
                                  sar.length(),
2015
                                  SQLITE_STATIC);
2016
            } else {
UNCOV
2017
                sqlite3_bind_null(stmt, lpc + 1);
×
2018
            }
2019
            continue;
10✔
2020
        }
10✔
2021
        if (strcmp(name, ":log_opid") == 0) {
17✔
UNCOV
2022
            if (values.lvv_opid_value) {
×
UNCOV
2023
                sqlite3_bind_text(stmt,
×
2024
                                  lpc + 1,
2025
                                  values.lvv_opid_value->c_str(),
UNCOV
2026
                                  values.lvv_opid_value->length(),
×
2027
                                  SQLITE_STATIC);
2028
            } else {
UNCOV
2029
                sqlite3_bind_null(stmt, lpc + 1);
×
2030
            }
UNCOV
2031
            continue;
×
2032
        }
2033
        if (strcmp(name, ":log_raw_text") == 0) {
17✔
UNCOV
2034
            auto res = lf->read_raw_message(ll);
×
2035

UNCOV
2036
            if (res.isOk()) {
×
2037
                raw_sbr = res.unwrap();
×
UNCOV
2038
                sqlite3_bind_text(stmt,
×
2039
                                  lpc + 1,
2040
                                  raw_sbr.get_data(),
UNCOV
2041
                                  raw_sbr.length(),
×
2042
                                  SQLITE_STATIC);
2043
            }
UNCOV
2044
            continue;
×
2045
        }
2046
        for (const auto& lv : values.lvv_values) {
129✔
2047
            if (lv.lv_meta.lvm_name != &name[1]) {
129✔
2048
                continue;
112✔
2049
            }
2050

2051
            switch (lv.lv_meta.lvm_kind) {
17✔
2052
                case value_kind_t::VALUE_BOOLEAN:
×
UNCOV
2053
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
UNCOV
2054
                    break;
×
2055
                case value_kind_t::VALUE_FLOAT:
×
UNCOV
2056
                    sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
UNCOV
2057
                    break;
×
2058
                case value_kind_t::VALUE_INTEGER:
12✔
2059
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
12✔
2060
                    break;
12✔
UNCOV
2061
                case value_kind_t::VALUE_NULL:
×
UNCOV
2062
                    sqlite3_bind_null(stmt, lpc + 1);
×
UNCOV
2063
                    break;
×
2064
                default:
5✔
2065
                    sqlite3_bind_text(stmt,
5✔
2066
                                      lpc + 1,
2067
                                      lv.text_value(),
2068
                                      lv.text_length(),
5✔
2069
                                      SQLITE_TRANSIENT);
2070
                    break;
5✔
2071
            }
2072
            break;
17✔
2073
        }
2074
    }
2075

2076
    auto step_res = sqlite3_step(stmt);
45✔
2077

2078
    sqlite3_reset(stmt);
45✔
2079
    sqlite3_clear_bindings(stmt);
45✔
2080
    switch (step_res) {
45✔
2081
        case SQLITE_OK:
14✔
2082
        case SQLITE_DONE:
2083
            return Ok(false);
28✔
2084
        case SQLITE_ROW:
30✔
2085
            return Ok(true);
60✔
2086
        default:
1✔
2087
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2088
    }
2089
}
45✔
2090

2091
bool
2092
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
12,461✔
2093
{
2094
    if (this->lss_marked_only) {
12,461✔
2095
        auto found_mark = ll->is_marked() || ll->is_expr_marked();
6✔
2096
        auto to_start_ll = ll;
6✔
2097
        while (!found_mark && to_start_ll->is_continued()) {
6✔
UNCOV
2098
            if (to_start_ll->is_marked() || to_start_ll->is_expr_marked()) {
×
UNCOV
2099
                found_mark = true;
×
2100
            }
UNCOV
2101
            --to_start_ll;
×
2102
        }
2103
        auto to_end_ll = std::next(ll);
6✔
2104
        while (!found_mark && to_end_ll != (*ld)->get_file_ptr()->end()
10✔
2105
               && to_end_ll->is_continued())
11✔
2106
        {
2107
            if (to_end_ll->is_marked() || to_end_ll->is_expr_marked()) {
1✔
2108
                found_mark = true;
1✔
2109
            }
2110
            ++to_end_ll;
1✔
2111
        }
2112
        if (!found_mark) {
6✔
2113
            return false;
3✔
2114
        }
2115
    }
2116

2117
    if (ll->get_msg_level() < this->lss_min_log_level) {
12,458✔
2118
        return false;
2✔
2119
    }
2120

2121
    if (*ll < this->ttt_min_row_time) {
12,456✔
2122
        return false;
3✔
2123
    }
2124

2125
    if (!(*ll <= this->ttt_max_row_time)) {
12,453✔
2126
        return false;
4✔
2127
    }
2128

2129
    return true;
12,449✔
2130
}
2131

2132
void
2133
logfile_sub_source::invalidate_sql_filter()
20✔
2134
{
2135
    for (auto& ld : *this) {
40✔
2136
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
20✔
2137
    }
2138
}
20✔
2139

2140
void
2141
logfile_sub_source::text_mark(const bookmark_type_t* bm,
130✔
2142
                              vis_line_t line,
2143
                              bool added)
2144
{
2145
    if (line >= (int) this->lss_index.size()) {
130✔
UNCOV
2146
        return;
×
2147
    }
2148

2149
    auto cl = this->at(line);
130✔
2150

2151
    if (bm == &textview_curses::BM_USER) {
130✔
2152
        auto* ll = this->find_line(cl);
49✔
2153

2154
        ll->set_mark(added);
49✔
2155
    }
2156
    auto lb = this->lss_user_marks[bm].bv_tree.lower_bound(cl);
130✔
2157
    if (added) {
130✔
2158
        if (lb == this->lss_user_marks[bm].bv_tree.end() || *lb != cl) {
65✔
2159
            this->lss_user_marks[bm].bv_tree.insert(cl);
60✔
2160
        }
2161
    } else if (lb != this->lss_user_marks[bm].bv_tree.end() && *lb == cl) {
65✔
2162
        this->lss_user_marks[bm].bv_tree.erase(lb);
7✔
2163
    }
2164
    if (bm == &textview_curses::BM_META
130✔
2165
        && this->lss_meta_grepper.gps_proc != nullptr)
16✔
2166
    {
2167
        this->tss_view->search_range(line, line + 1_vl);
1✔
2168
        this->tss_view->search_new_data();
1✔
2169
    }
2170
}
2171

2172
void
2173
logfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
46✔
2174
{
2175
    if (bm == &textview_curses::BM_USER) {
46✔
2176
        for (auto iter = this->lss_user_marks[bm].bv_tree.begin();
6✔
2177
             iter != this->lss_user_marks[bm].bv_tree.end();
6✔
UNCOV
2178
             ++iter)
×
2179
        {
UNCOV
2180
            this->find_line(*iter)->set_mark(false);
×
2181
        }
2182
    }
2183
    this->lss_user_marks[bm].clear();
46✔
2184
}
46✔
2185

2186
void
2187
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
482✔
2188
{
2189
    auto iter = std::find_if(
482✔
2190
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
964✔
2191
    if (iter != this->lss_files.end()) {
482✔
2192
        int file_index = iter - this->lss_files.begin();
482✔
2193

2194
        (*iter)->clear();
482✔
2195
        for (auto& bv : this->lss_user_marks) {
4,338✔
2196
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
3,856✔
2197
            auto mark_end
2198
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
3,856✔
2199
            auto file_range = bv.equal_range(mark_curr, mark_end);
3,856✔
2200

2201
            if (file_range.first != file_range.second) {
3,856✔
2202
                auto to_del = std::vector<content_line_t>{};
61✔
2203
                for (auto file_iter = file_range.first;
61✔
2204
                     file_iter != file_range.second;
145✔
2205
                     ++file_iter)
84✔
2206
                {
2207
                    to_del.emplace_back(*file_iter);
84✔
2208
                }
2209

2210
                for (auto cl : to_del) {
145✔
2211
                    bv.bv_tree.erase(cl);
84✔
2212
                }
2213
            }
61✔
2214
        }
2215

2216
        this->lss_force_rebuild = true;
482✔
2217
    }
2218
    while (!this->lss_files.empty()) {
964✔
2219
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
532✔
2220
            this->lss_files.pop_back();
482✔
2221
        } else {
2222
            break;
50✔
2223
        }
2224
    }
2225
    this->lss_token_file = nullptr;
482✔
2226
}
482✔
2227

2228
std::optional<vis_line_t>
2229
logfile_sub_source::find_from_content(content_line_t cl)
5✔
2230
{
2231
    content_line_t line = cl;
5✔
2232
    std::shared_ptr<logfile> lf = this->find(line);
5✔
2233

2234
    if (lf != nullptr) {
5✔
2235
        auto ll_iter = lf->begin() + line;
5✔
2236
        auto& ll = *ll_iter;
5✔
2237
        auto vis_start_opt = this->find_from_time(ll.get_timeval());
5✔
2238

2239
        if (!vis_start_opt) {
5✔
UNCOV
2240
            return std::nullopt;
×
2241
        }
2242

2243
        auto vis_start = *vis_start_opt;
5✔
2244

2245
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2246
            content_line_t guess_cl = this->at(vis_start);
5✔
2247

2248
            if (cl == guess_cl) {
5✔
2249
                return vis_start;
5✔
2250
            }
2251

2252
            auto guess_line = this->find_line(guess_cl);
×
2253

UNCOV
2254
            if (!guess_line || ll < *guess_line) {
×
2255
                return std::nullopt;
×
2256
            }
2257

UNCOV
2258
            ++vis_start;
×
2259
        }
2260
    }
2261

2262
    return std::nullopt;
×
2263
}
5✔
2264

2265
void
2266
logfile_sub_source::reload_index_delegate()
589✔
2267
{
2268
    if (this->lss_index_delegate == nullptr) {
589✔
UNCOV
2269
        return;
×
2270
    }
2271

2272
    this->lss_index_delegate->index_start(*this);
589✔
2273
    for (const auto index : this->lss_filtered_index) {
817✔
2274
        auto cl = this->lss_index[index].value();
228✔
2275
        uint64_t line_number;
2276
        auto ld = this->find_data(cl, line_number);
228✔
2277
        std::shared_ptr<logfile> lf = (*ld)->get_file();
228✔
2278

2279
        this->lss_index_delegate->index_line(
228✔
2280
            *this, lf.get(), lf->begin() + line_number);
228✔
2281
    }
228✔
2282
    this->lss_index_delegate->index_complete(*this);
589✔
2283
}
2284

2285
std::optional<std::shared_ptr<text_filter>>
2286
logfile_sub_source::get_sql_filter()
1,752✔
2287
{
2288
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
1,752✔
2289
               return filt->get_index() == 0;
309✔
2290
           })
2291
        | lnav::itertools::deref();
3,504✔
2292
}
2293

2294
void
2295
log_location_history::loc_history_append(vis_line_t top)
88✔
2296
{
2297
    if (top >= vis_line_t(this->llh_log_source.text_line_count())) {
88✔
2298
        return;
2✔
2299
    }
2300

2301
    content_line_t cl = this->llh_log_source.at(top);
86✔
2302

2303
    auto iter = this->llh_history.begin();
86✔
2304
    iter += this->llh_history.size() - this->lh_history_position;
86✔
2305
    this->llh_history.erase_from(iter);
86✔
2306
    this->lh_history_position = 0;
86✔
2307
    this->llh_history.push_back(cl);
86✔
2308
}
2309

2310
std::optional<vis_line_t>
2311
log_location_history::loc_history_back(vis_line_t current_top)
2✔
2312
{
2313
    while (this->lh_history_position < this->llh_history.size()) {
2✔
2314
        auto iter = this->llh_history.rbegin();
2✔
2315

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

2318
        if (this->lh_history_position == 0 && vis_for_pos != current_top) {
2✔
2319
            return vis_for_pos;
2✔
2320
        }
2321

2322
        if ((this->lh_history_position + 1) >= this->llh_history.size()) {
2✔
UNCOV
2323
            break;
×
2324
        }
2325

2326
        this->lh_history_position += 1;
2✔
2327

2328
        iter += this->lh_history_position;
2✔
2329

2330
        vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2331

2332
        if (vis_for_pos) {
2✔
2333
            return vis_for_pos;
2✔
2334
        }
2335
    }
2336

UNCOV
2337
    return std::nullopt;
×
2338
}
2339

2340
std::optional<vis_line_t>
2341
log_location_history::loc_history_forward(vis_line_t current_top)
1✔
2342
{
2343
    while (this->lh_history_position > 0) {
1✔
2344
        this->lh_history_position -= 1;
1✔
2345

2346
        auto iter = this->llh_history.rbegin();
1✔
2347

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

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

2352
        if (vis_for_pos) {
1✔
2353
            return vis_for_pos;
1✔
2354
        }
2355
    }
2356

UNCOV
2357
    return std::nullopt;
×
2358
}
2359

2360
bool
2361
sql_filter::matches(std::optional<line_source> ls_opt,
19✔
2362
                    const shared_buffer_ref& line)
2363
{
2364
    if (!ls_opt) {
19✔
UNCOV
2365
        return false;
×
2366
    }
2367

2368
    auto ls = ls_opt;
19✔
2369

2370
    if (!ls->ls_line->is_message()) {
19✔
2371
        return false;
1✔
2372
    }
2373
    if (this->sf_filter_stmt == nullptr) {
18✔
UNCOV
2374
        return false;
×
2375
    }
2376

2377
    auto lfp = ls->ls_file.shared_from_this();
18✔
2378
    auto ld = this->sf_log_source.find_data_i(lfp);
18✔
2379
    if (ld == this->sf_log_source.end()) {
18✔
2380
        return false;
×
2381
    }
2382

2383
    auto eval_res = this->sf_log_source.eval_sql_filter(
18✔
2384
        this->sf_filter_stmt, ld, ls->ls_line);
18✔
2385
    if (eval_res.unwrapOr(true)) {
18✔
2386
        return false;
12✔
2387
    }
2388

2389
    return true;
6✔
2390
}
18✔
2391

2392
std::string
2393
sql_filter::to_command() const
×
2394
{
2395
    return fmt::format(FMT_STRING("filter-expr {}"), this->lf_id);
×
2396
}
2397

2398
std::optional<line_info>
UNCOV
2399
logfile_sub_source::meta_grepper::grep_value_for_line(vis_line_t line,
×
2400
                                                      std::string& value_out)
2401
{
UNCOV
2402
    auto line_meta_opt = this->lmg_source.find_bookmark_metadata(line);
×
UNCOV
2403
    if (!line_meta_opt) {
×
UNCOV
2404
        value_out.clear();
×
2405
    } else {
UNCOV
2406
        auto& bm = *(line_meta_opt.value());
×
2407

2408
        {
UNCOV
2409
            md2attr_line mdal;
×
2410

UNCOV
2411
            auto parse_res = md4cpp::parse(bm.bm_comment, mdal);
×
UNCOV
2412
            if (parse_res.isOk()) {
×
2413
                value_out.append(parse_res.unwrap().get_string());
×
2414
            } else {
UNCOV
2415
                value_out.append(bm.bm_comment);
×
2416
            }
2417
        }
2418

2419
        value_out.append("\x1c");
×
2420
        for (const auto& tag : bm.bm_tags) {
×
2421
            value_out.append(tag);
×
UNCOV
2422
            value_out.append("\x1c");
×
2423
        }
UNCOV
2424
        value_out.append("\x1c");
×
UNCOV
2425
        for (const auto& pair : bm.bm_annotations.la_pairs) {
×
UNCOV
2426
            value_out.append(pair.first);
×
UNCOV
2427
            value_out.append("\x1c");
×
2428

UNCOV
2429
            md2attr_line mdal;
×
2430

2431
            auto parse_res = md4cpp::parse(pair.second, mdal);
×
2432
            if (parse_res.isOk()) {
×
2433
                value_out.append(parse_res.unwrap().get_string());
×
2434
            } else {
UNCOV
2435
                value_out.append(pair.second);
×
2436
            }
UNCOV
2437
            value_out.append("\x1c");
×
2438
        }
UNCOV
2439
        value_out.append("\x1c");
×
UNCOV
2440
        value_out.append(bm.bm_opid);
×
2441
    }
2442

2443
    if (!this->lmg_done) {
×
UNCOV
2444
        return line_info{};
×
2445
    }
2446

UNCOV
2447
    return std::nullopt;
×
2448
}
2449

2450
vis_line_t
UNCOV
2451
logfile_sub_source::meta_grepper::grep_initial_line(vis_line_t start,
×
2452
                                                    vis_line_t highest)
2453
{
UNCOV
2454
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
UNCOV
2455
    auto& bv = bm[&textview_curses::BM_META];
×
2456

UNCOV
2457
    if (bv.empty()) {
×
UNCOV
2458
        return -1_vl;
×
2459
    }
2460
    return *bv.bv_tree.begin();
×
2461
}
2462

2463
void
2464
logfile_sub_source::meta_grepper::grep_next_line(vis_line_t& line)
×
2465
{
UNCOV
2466
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
UNCOV
2467
    auto& bv = bm[&textview_curses::BM_META];
×
2468

UNCOV
2469
    auto line_opt = bv.next(vis_line_t(line));
×
UNCOV
2470
    if (!line_opt) {
×
UNCOV
2471
        this->lmg_done = true;
×
2472
    }
UNCOV
2473
    line = line_opt.value_or(-1_vl);
×
2474
}
2475

2476
void
2477
logfile_sub_source::meta_grepper::grep_begin(grep_proc<vis_line_t>& gp,
32✔
2478
                                             vis_line_t start,
2479
                                             vis_line_t stop)
2480
{
2481
    this->lmg_source.quiesce();
32✔
2482

2483
    this->lmg_source.tss_view->grep_begin(gp, start, stop);
32✔
2484
}
32✔
2485

2486
void
2487
logfile_sub_source::meta_grepper::grep_end(grep_proc<vis_line_t>& gp)
23✔
2488
{
2489
    this->lmg_source.tss_view->grep_end(gp);
23✔
2490
}
23✔
2491

2492
void
2493
logfile_sub_source::meta_grepper::grep_match(grep_proc<vis_line_t>& gp,
3✔
2494
                                             vis_line_t line)
2495
{
2496
    this->lmg_source.tss_view->grep_match(gp, line);
3✔
2497
}
3✔
2498

2499
logline_window::iterator
2500
logline_window::begin()
160✔
2501
{
2502
    if (this->lw_start_line < 0_vl) {
160✔
2503
        return this->end();
×
2504
    }
2505

2506
    auto retval = iterator{this->lw_source, this->lw_start_line};
160✔
2507
    while (!retval->is_valid() && retval != this->end()) {
160✔
UNCOV
2508
        ++retval;
×
2509
    }
2510

2511
    return retval;
160✔
2512
}
160✔
2513

2514
logline_window::iterator
2515
logline_window::end()
117✔
2516
{
2517
    auto vl = this->lw_end_line;
117✔
2518
    while (vl < vis_line_t(this->lw_source.text_line_count())) {
154✔
2519
        const auto& line = this->lw_source.find_line(this->lw_source.at(vl));
118✔
2520
        if (line->is_message()) {
118✔
2521
            break;
81✔
2522
        }
2523
        ++vl;
37✔
2524
    }
2525

2526
    return {this->lw_source, vl};
234✔
2527
}
2528

2529
logline_window::logmsg_info::logmsg_info(logfile_sub_source& lss, vis_line_t vl)
277✔
2530
    : li_source(lss), li_line(vl)
277✔
2531
{
2532
    if (this->li_line < vis_line_t(this->li_source.text_line_count())) {
277✔
2533
        while (true) {
2534
            auto pair_opt = this->li_source.find_line_with_file(vl);
244✔
2535
            if (!pair_opt) {
244✔
2536
                break;
×
2537
            }
2538

2539
            auto& [lf, ll] = pair_opt.value();
244✔
2540
            if (ll->is_message()) {
244✔
2541
                this->li_file = lf.get();
241✔
2542
                this->li_logline = ll;
241✔
2543
                this->li_line_number
2544
                    = std::distance(this->li_file->begin(), this->li_logline);
241✔
2545
                break;
241✔
2546
            }
2547
            --vl;
3✔
2548
        }
247✔
2549
    }
2550
}
277✔
2551

2552
bool
2553
logline_window::logmsg_info::is_valid() const
160✔
2554
{
2555
    return this->li_file != nullptr;
160✔
2556
}
2557

2558
void
2559
logline_window::logmsg_info::next_msg()
201✔
2560
{
2561
    this->li_file = nullptr;
201✔
2562
    this->li_logline = logfile::iterator{};
201✔
2563
    this->li_string_attrs.clear();
201✔
2564
    this->li_line_values.clear();
201✔
2565
    ++this->li_line;
201✔
2566
    while (this->li_line < vis_line_t(this->li_source.text_line_count())) {
201✔
2567
        auto pair_opt = this->li_source.find_line_with_file(this->li_line);
198✔
2568

2569
        if (!pair_opt) {
198✔
UNCOV
2570
            break;
×
2571
        }
2572

2573
        auto line_pair = pair_opt.value();
198✔
2574
        if (line_pair.second->is_message()) {
198✔
2575
            this->li_file = line_pair.first.get();
198✔
2576
            this->li_logline = line_pair.second;
198✔
2577
            this->li_line_number
2578
                = std::distance(this->li_file->begin(), this->li_logline);
198✔
2579
            break;
198✔
2580
        }
UNCOV
2581
        ++this->li_line;
×
2582
    }
396✔
2583
}
201✔
2584

2585
void
2586
logline_window::logmsg_info::prev_msg()
×
2587
{
2588
    this->li_file = nullptr;
×
UNCOV
2589
    this->li_logline = logfile::iterator{};
×
2590
    this->li_string_attrs.clear();
×
UNCOV
2591
    this->li_line_values.clear();
×
UNCOV
2592
    while (this->li_line > 0) {
×
UNCOV
2593
        --this->li_line;
×
UNCOV
2594
        auto pair_opt = this->li_source.find_line_with_file(this->li_line);
×
2595

UNCOV
2596
        if (!pair_opt) {
×
UNCOV
2597
            break;
×
2598
        }
2599

UNCOV
2600
        auto line_pair = pair_opt.value();
×
UNCOV
2601
        if (line_pair.second->is_message()) {
×
UNCOV
2602
            this->li_file = line_pair.first.get();
×
UNCOV
2603
            this->li_logline = line_pair.second;
×
2604
            this->li_line_number
UNCOV
2605
                = std::distance(this->li_file->begin(), this->li_logline);
×
UNCOV
2606
            break;
×
2607
        }
2608
    }
2609
}
2610

2611
std::optional<bookmark_metadata*>
2612
logline_window::logmsg_info::get_metadata() const
212✔
2613
{
2614
    auto line_number = std::distance(this->li_file->begin(), this->li_logline);
212✔
2615
    auto& bm = this->li_file->get_bookmark_metadata();
212✔
2616
    auto bm_iter = bm.find(line_number);
212✔
2617
    if (bm_iter == bm.end()) {
212✔
2618
        return std::nullopt;
212✔
2619
    }
UNCOV
2620
    return &bm_iter->second;
×
2621
}
2622

2623
Result<auto_buffer, std::string>
2624
logline_window::logmsg_info::get_line_hash() const
112✔
2625
{
2626
    auto fr = this->li_file->get_file_range(this->li_logline, false);
112✔
2627
    auto sbr = TRY(this->li_file->read_range(fr));
112✔
2628
    auto outbuf = auto_buffer::alloc(3 + hasher::STRING_SIZE);
112✔
2629
    outbuf.push_back('v');
112✔
2630
    outbuf.push_back('1');
112✔
2631
    outbuf.push_back(':');
112✔
2632
    hasher line_hasher;
112✔
2633
    line_hasher.update(sbr.get_data(), sbr.length())
112✔
2634
        .update(this->get_file_line_number())
112✔
2635
        .to_string(outbuf);
112✔
2636

2637
    return Ok(std::move(outbuf));
112✔
2638
}
112✔
2639

UNCOV
2640
logline_window::logmsg_info::metadata_edit_guard::~metadata_edit_guard()
×
2641
{
2642
    auto line_number = std::distance(this->meg_logmsg_info.li_file->begin(),
×
UNCOV
2643
                                     this->meg_logmsg_info.li_logline);
×
UNCOV
2644
    auto& bm = this->meg_logmsg_info.li_file->get_bookmark_metadata();
×
UNCOV
2645
    auto bm_iter = bm.find(line_number);
×
2646
    if (bm_iter != bm.end()
×
UNCOV
2647
        && bm_iter->second.empty(bookmark_metadata::categories::any))
×
2648
    {
UNCOV
2649
        bm.erase(bm_iter);
×
2650
    }
2651
}
2652

2653
bookmark_metadata&
UNCOV
2654
logline_window::logmsg_info::metadata_edit_guard::operator*()
×
2655
{
UNCOV
2656
    auto line_number = std::distance(this->meg_logmsg_info.li_file->begin(),
×
UNCOV
2657
                                     this->meg_logmsg_info.li_logline);
×
UNCOV
2658
    auto& bm = this->meg_logmsg_info.li_file->get_bookmark_metadata();
×
UNCOV
2659
    return bm[line_number];
×
2660
}
2661

2662
size_t
UNCOV
2663
logline_window::logmsg_info::get_line_count() const
×
2664
{
UNCOV
2665
    size_t retval = 1;
×
UNCOV
2666
    auto iter = std::next(this->li_logline);
×
UNCOV
2667
    while (iter != this->li_file->end() && iter->is_continued()) {
×
UNCOV
2668
        ++iter;
×
UNCOV
2669
        retval += 1;
×
2670
    }
2671

UNCOV
2672
    return retval;
×
2673
}
2674

2675
void
2676
logline_window::logmsg_info::load_msg() const
261✔
2677
{
2678
    if (!this->li_string_attrs.empty()) {
261✔
2679
        return;
25✔
2680
    }
2681

2682
    auto format = this->li_file->get_format();
236✔
2683
    this->li_file->read_full_message(this->li_logline,
236✔
2684
                                     this->li_line_values.lvv_sbr);
236✔
2685
    if (this->li_line_values.lvv_sbr.get_metadata().m_has_ansi) {
236✔
2686
        auto* writable_data = this->li_line_values.lvv_sbr.get_writable_data();
×
2687
        auto str
UNCOV
2688
            = std::string{writable_data, this->li_line_values.lvv_sbr.length()};
×
UNCOV
2689
        scrub_ansi_string(str, &this->li_string_attrs);
×
UNCOV
2690
        this->li_line_values.lvv_sbr.get_metadata().m_has_ansi = false;
×
2691
    }
2692
    format->annotate(this->li_file,
236✔
2693
                     std::distance(this->li_file->begin(), this->li_logline),
236✔
2694
                     this->li_string_attrs,
236✔
2695
                     this->li_line_values,
236✔
2696
                     false);
2697

2698
    if (!this->li_line_values.lvv_opid_value) {
236✔
2699
        auto bm_opt = this->get_metadata();
212✔
2700
        if (bm_opt && !bm_opt.value()->bm_opid.empty()) {
212✔
UNCOV
2701
            this->li_line_values.lvv_opid_value = bm_opt.value()->bm_opid;
×
2702
            this->li_line_values.lvv_opid_provenance
UNCOV
2703
                = logline_value_vector::opid_provenance::user;
×
2704
        }
2705
    }
2706
}
236✔
2707

2708
std::string
UNCOV
2709
logline_window::logmsg_info::to_string(const struct line_range& lr) const
×
2710
{
2711
    this->load_msg();
×
2712

2713
    return this->li_line_values.lvv_sbr
UNCOV
2714
        .to_string_fragment(lr.lr_start, lr.length())
×
UNCOV
2715
        .to_string();
×
2716
}
2717

2718
logline_window::iterator&
2719
logline_window::iterator::operator++()
201✔
2720
{
2721
    this->i_info.next_msg();
201✔
2722

2723
    return *this;
201✔
2724
}
2725

2726
logline_window::iterator&
UNCOV
2727
logline_window::iterator::operator--()
×
2728
{
UNCOV
2729
    this->i_info.prev_msg();
×
2730

UNCOV
2731
    return *this;
×
2732
}
2733

2734
static std::vector<breadcrumb::possibility>
2735
timestamp_poss()
10✔
2736
{
2737
    const static std::vector<breadcrumb::possibility> retval = {
2738
        breadcrumb::possibility{"-1 day"},
2739
        breadcrumb::possibility{"-1h"},
2740
        breadcrumb::possibility{"-30m"},
2741
        breadcrumb::possibility{"-15m"},
2742
        breadcrumb::possibility{"-5m"},
2743
        breadcrumb::possibility{"-1m"},
2744
        breadcrumb::possibility{"+1m"},
2745
        breadcrumb::possibility{"+5m"},
2746
        breadcrumb::possibility{"+15m"},
2747
        breadcrumb::possibility{"+30m"},
2748
        breadcrumb::possibility{"+1h"},
2749
        breadcrumb::possibility{"+1 day"},
2750
    };
80✔
2751

2752
    return retval;
10✔
2753
}
20✔
2754

2755
static attr_line_t
2756
to_display(const std::shared_ptr<logfile>& lf)
20✔
2757
{
2758
    attr_line_t retval;
20✔
2759

2760
    if (lf->get_open_options().loo_piper) {
20✔
UNCOV
2761
        if (!lf->get_open_options().loo_piper->is_finished()) {
×
UNCOV
2762
            retval.append("\u21bb "_list_glyph);
×
2763
        }
2764
    }
2765
    retval.append(lf->get_unique_path());
20✔
2766

2767
    return retval;
20✔
UNCOV
2768
}
×
2769

2770
void
2771
logfile_sub_source::text_crumbs_for_line(int line,
10✔
2772
                                         std::vector<breadcrumb::crumb>& crumbs)
2773
{
2774
    text_sub_source::text_crumbs_for_line(line, crumbs);
10✔
2775

2776
    if (this->lss_filtered_index.empty()) {
10✔
2777
        return;
×
2778
    }
2779

2780
    auto vl = vis_line_t(line);
10✔
2781
    auto bmc = this->get_bookmark_metadata_context(
10✔
2782
        vl, bookmark_metadata::categories::partition);
2783
    if (bmc.bmc_current_metadata) {
10✔
2784
        const auto& name = bmc.bmc_current_metadata.value()->bm_name;
1✔
2785
        auto key = text_anchors::to_anchor_string(name);
1✔
2786
        auto display = attr_line_t()
1✔
2787
                           .append("\u2291 "_symbol)
1✔
2788
                           .append(lnav::roles::variable(name))
2✔
2789
                           .move();
1✔
2790
        crumbs.emplace_back(
1✔
2791
            key,
2792
            display,
2793
            [this]() -> std::vector<breadcrumb::possibility> {
×
2794
                auto& vb = this->tss_view->get_bookmarks();
1✔
2795
                const auto& bv = vb[&textview_curses::BM_PARTITION];
1✔
2796
                std::vector<breadcrumb::possibility> retval;
1✔
2797

2798
                for (const auto& vl : bv.bv_tree) {
2✔
2799
                    auto meta_opt = this->find_bookmark_metadata(vl);
1✔
2800
                    if (!meta_opt || meta_opt.value()->bm_name.empty()) {
1✔
UNCOV
2801
                        continue;
×
2802
                    }
2803

2804
                    const auto& name = meta_opt.value()->bm_name;
1✔
2805
                    retval.emplace_back(text_anchors::to_anchor_string(name),
1✔
2806
                                        name);
2807
                }
2808

2809
                return retval;
1✔
UNCOV
2810
            },
×
2811
            [ec = this->lss_exec_context](const auto& part) {
1✔
UNCOV
2812
                auto cmd = fmt::format(FMT_STRING(":goto {}"),
×
2813
                                       part.template get<std::string>());
UNCOV
2814
                ec->execute(INTERNAL_SRC_LOC, cmd);
×
UNCOV
2815
            });
×
2816
    }
1✔
2817

2818
    auto line_pair_opt = this->find_line_with_file(vl);
10✔
2819
    if (!line_pair_opt) {
10✔
UNCOV
2820
        return;
×
2821
    }
2822
    auto line_pair = line_pair_opt.value();
10✔
2823
    auto& lf = line_pair.first;
10✔
2824
    auto format = lf->get_format();
10✔
2825
    char ts[64];
2826

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

2829
    crumbs.emplace_back(std::string(ts),
10✔
2830
                        timestamp_poss,
2831
                        [ec = this->lss_exec_context](const auto& ts) {
10✔
2832
                            auto cmd
×
2833
                                = fmt::format(FMT_STRING(":goto {}"),
×
2834
                                              ts.template get<std::string>());
2835
                            ec->execute(INTERNAL_SRC_LOC, cmd);
×
2836
                        });
×
2837
    crumbs.back().c_expected_input
10✔
2838
        = breadcrumb::crumb::expected_input_t::anything;
10✔
2839
    crumbs.back().c_search_placeholder = "(Enter an absolute or relative time)";
10✔
2840

2841
    auto format_name = format->get_name().to_string();
10✔
2842

2843
    crumbs.emplace_back(
10✔
2844
        format_name,
2845
        attr_line_t().append(format_name),
10✔
UNCOV
2846
        [this]() -> std::vector<breadcrumb::possibility> {
×
2847
            return this->lss_files
10✔
2848
                | lnav::itertools::filter_in([](const auto& file_data) {
20✔
2849
                       return file_data->is_visible();
10✔
2850
                   })
2851
                | lnav::itertools::map(&logfile_data::get_file_ptr)
30✔
2852
                | lnav::itertools::map(&logfile::get_format_name)
30✔
2853
                | lnav::itertools::unique()
30✔
2854
                | lnav::itertools::map([](const auto& elem) {
40✔
2855
                       return breadcrumb::possibility{
2856
                           elem.to_string(),
2857
                       };
10✔
2858
                   })
2859
                | lnav::itertools::to_vector();
30✔
2860
        },
2861
        [ec = this->lss_exec_context](const auto& format_name) {
10✔
2862
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2863
     SET selection = ifnull(
2864
         (SELECT log_line FROM all_logs WHERE log_format = $format_name LIMIT 1),
2865
         (SELECT raise_error(
2866
            'Could not find format: ' || $format_name,
2867
            'The corresponding log messages might have been filtered out'))
2868
       )
2869
     WHERE name = 'log'
2870
)";
2871

UNCOV
2872
            ec->execute_with(
×
UNCOV
2873
                INTERNAL_SRC_LOC,
×
2874
                MOVE_STMT,
2875
                std::make_pair("format_name",
2876
                               format_name.template get<std::string>()));
UNCOV
2877
        });
×
2878

2879
    auto msg_start_iter = lf->message_start(line_pair.second);
10✔
2880
    auto file_line_number = std::distance(lf->begin(), msg_start_iter);
10✔
2881
    crumbs.emplace_back(
20✔
2882
        lf->get_unique_path(),
10✔
2883
        to_display(lf).appendf(FMT_STRING("[{:L}]"), file_line_number),
40✔
UNCOV
2884
        [this]() -> std::vector<breadcrumb::possibility> {
×
2885
            return this->lss_files
10✔
2886
                | lnav::itertools::filter_in([](const auto& file_data) {
20✔
2887
                       return file_data->is_visible();
10✔
2888
                   })
2889
                | lnav::itertools::map([](const auto& file_data) {
20✔
2890
                       return breadcrumb::possibility{
2891
                           file_data->get_file_ptr()->get_unique_path(),
10✔
2892
                           to_display(file_data->get_file()),
2893
                       };
10✔
2894
                   });
20✔
2895
        },
2896
        [ec = this->lss_exec_context](const auto& uniq_path) {
10✔
2897
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2898
     SET selection = ifnull(
2899
          (SELECT log_line FROM all_logs WHERE log_unique_path = $uniq_path LIMIT 1),
2900
          (SELECT raise_error(
2901
            'Could not find file: ' || $uniq_path,
2902
            'The corresponding log messages might have been filtered out'))
2903
         )
2904
     WHERE name = 'log'
2905
)";
2906

UNCOV
2907
            ec->execute_with(
×
UNCOV
2908
                INTERNAL_SRC_LOC,
×
2909
                MOVE_STMT,
2910
                std::make_pair("uniq_path",
2911
                               uniq_path.template get<std::string>()));
UNCOV
2912
        });
×
2913

2914
    shared_buffer sb;
10✔
2915
    logline_value_vector values;
10✔
2916
    auto& sbr = values.lvv_sbr;
10✔
2917

2918
    lf->read_full_message(msg_start_iter, sbr);
10✔
2919
    attr_line_t al(to_string(sbr));
10✔
2920
    if (!sbr.get_metadata().m_valid_utf) {
10✔
2921
        scrub_to_utf8(&al.al_string[0], al.al_string.length());
×
2922
    }
2923
    if (sbr.get_metadata().m_has_ansi) {
10✔
2924
        // bleh
UNCOV
2925
        scrub_ansi_string(al.get_string(), &al.al_attrs);
×
UNCOV
2926
        sbr.share(sb, al.al_string.data(), al.al_string.size());
×
2927
    }
2928
    format->annotate(lf.get(), file_line_number, al.get_attrs(), values);
10✔
2929

2930
    {
2931
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
20✔
2932
          SET selection = ifnull(
2933
            (SELECT log_line FROM all_logs WHERE log_opid = $opid LIMIT 1),
2934
            (SELECT raise_error('Could not find opid: ' || $opid,
2935
                                'The corresponding log messages might have been filtered out')))
2936
          WHERE name = 'log'
2937
        )";
2938
        static const std::string ELLIPSIS = "\u22ef";
20✔
2939

2940
        auto opid_display = values.lvv_opid_value.has_value()
10✔
2941
            ? lnav::roles::identifier(values.lvv_opid_value.value())
9✔
2942
            : lnav::roles::hidden(ELLIPSIS);
19✔
2943
        crumbs.emplace_back(
20✔
2944
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
21✔
2945
                                              : "",
2946
            attr_line_t().append(opid_display),
10✔
UNCOV
2947
            [this]() -> std::vector<breadcrumb::possibility> {
×
2948
                std::set<std::string> poss_strs;
10✔
2949

2950
                for (const auto& file_data : this->lss_files) {
20✔
2951
                    if (file_data->get_file_ptr() == nullptr) {
10✔
UNCOV
2952
                        continue;
×
2953
                    }
2954
                    safe::ReadAccess<logfile::safe_opid_state> r_opid_map(
2955
                        file_data->get_file_ptr()->get_opids());
10✔
2956

2957
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
719✔
2958
                        poss_strs.emplace(pair.first.to_string());
709✔
2959
                    }
2960
                }
10✔
2961

2962
                std::vector<breadcrumb::possibility> retval;
10✔
2963

2964
                std::transform(poss_strs.begin(),
10✔
2965
                               poss_strs.end(),
2966
                               std::back_inserter(retval),
2967
                               [](const auto& opid_str) {
709✔
2968
                                   return breadcrumb::possibility(opid_str);
709✔
2969
                               });
2970

2971
                return retval;
20✔
2972
            },
10✔
2973
            [ec = this->lss_exec_context](const auto& opid) {
10✔
UNCOV
2974
                ec->execute_with(
×
2975
                    INTERNAL_SRC_LOC,
×
2976
                    MOVE_STMT,
2977
                    std::make_pair("opid", opid.template get<std::string>()));
2978
            });
×
2979
    }
10✔
2980

2981
    auto sf = string_fragment::from_str(al.get_string());
10✔
2982
    auto body_opt = get_string_attr(al.get_attrs(), SA_BODY);
10✔
2983
    auto nl_pos_opt = sf.find('\n');
10✔
2984
    auto msg_line_number = std::distance(msg_start_iter, line_pair.second);
10✔
2985
    auto line_from_top = line - msg_line_number;
10✔
2986
    if (body_opt && nl_pos_opt) {
10✔
2987
        if (this->lss_token_meta_line != file_line_number
2✔
2988
            || this->lss_token_meta_size != sf.length())
1✔
2989
        {
2990
            if (body_opt->saw_string_attr->sa_range.length() < 128 * 1024) {
1✔
2991
                this->lss_token_meta
2992
                    = lnav::document::discover(al)
1✔
2993
                          .over_range(
1✔
2994
                              body_opt.value().saw_string_attr->sa_range)
1✔
2995
                          .perform();
1✔
2996
                // XXX discover_structure() changes `al`, have to recompute
2997
                // stuff
2998
                sf = al.to_string_fragment();
1✔
2999
                body_opt = get_string_attr(al.get_attrs(), SA_BODY);
1✔
3000
            } else {
UNCOV
3001
                this->lss_token_meta = lnav::document::metadata{};
×
3002
            }
3003
            this->lss_token_meta_line = file_line_number;
1✔
3004
            this->lss_token_meta_size = sf.length();
1✔
3005
        }
3006

3007
        const auto initial_size = crumbs.size();
1✔
3008
        auto sf_body
3009
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
1✔
3010
                           body_opt->saw_string_attr->sa_range.lr_end);
1✔
3011
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
1✔
3012
        file_off_t line_end_offset = sf.length();
1✔
3013
        ssize_t line_number = 0;
1✔
3014

3015
        for (const auto& sf_line : sf_body.split_lines()) {
4✔
3016
            if (line_number >= msg_line_number) {
4✔
3017
                line_end_offset = line_offset + sf_line.length();
1✔
3018
                break;
1✔
3019
            }
3020
            line_number += 1;
3✔
3021
            line_offset += sf_line.length();
3✔
3022
        }
1✔
3023

3024
        this->lss_token_meta.m_sections_tree.visit_overlapping(
1✔
3025
            line_offset,
3026
            line_end_offset,
3027
            [this,
2✔
3028
             initial_size,
3029
             meta = &this->lss_token_meta,
1✔
3030
             &crumbs,
3031
             line_from_top](const auto& iv) {
3032
                auto path = crumbs | lnav::itertools::skip(initial_size)
6✔
3033
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
4✔
3034
                    | lnav::itertools::append(iv.value);
2✔
3035
                auto curr_node = lnav::document::hier_node::lookup_path(
2✔
3036
                    meta->m_sections_root.get(), path);
2✔
3037

3038
                crumbs.emplace_back(
4✔
3039
                    iv.value,
2✔
3040
                    [meta, path]() { return meta->possibility_provider(path); },
4✔
3041
                    [this, curr_node, path, line_from_top](const auto& key) {
4✔
3042
                        if (!curr_node) {
×
3043
                            return;
×
3044
                        }
UNCOV
3045
                        auto* parent_node = curr_node.value()->hn_parent;
×
UNCOV
3046
                        if (parent_node == nullptr) {
×
UNCOV
3047
                            return;
×
3048
                        }
3049
                        key.match(
UNCOV
3050
                            [parent_node](const std::string& str) {
×
3051
                                return parent_node->find_line_number(str);
×
3052
                            },
UNCOV
3053
                            [parent_node](size_t index) {
×
UNCOV
3054
                                return parent_node->find_line_number(index);
×
3055
                            })
UNCOV
3056
                            | [this, line_from_top](auto line_number) {
×
3057
                                  this->tss_view->set_selection(
×
3058
                                      vis_line_t(line_from_top + line_number));
×
3059
                              };
3060
                    });
3061
                if (curr_node && !curr_node.value()->hn_parent->is_named_only())
2✔
3062
                {
3063
                    auto node = lnav::document::hier_node::lookup_path(
×
3064
                        meta->m_sections_root.get(), path);
×
3065

3066
                    crumbs.back().c_expected_input
×
UNCOV
3067
                        = curr_node.value()
×
UNCOV
3068
                              ->hn_parent->hn_named_children.empty()
×
UNCOV
3069
                        ? breadcrumb::crumb::expected_input_t::index
×
3070
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
UNCOV
3071
                    crumbs.back().with_possible_range(
×
UNCOV
3072
                        node | lnav::itertools::map([](const auto hn) {
×
UNCOV
3073
                            return hn->hn_parent->hn_children.size();
×
3074
                        })
UNCOV
3075
                        | lnav::itertools::unwrap_or(size_t{0}));
×
3076
                }
3077
            });
2✔
3078

3079
        auto path = crumbs | lnav::itertools::skip(initial_size)
2✔
3080
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
2✔
3081
        auto node = lnav::document::hier_node::lookup_path(
1✔
3082
            this->lss_token_meta.m_sections_root.get(), path);
1✔
3083

3084
        if (node && !node.value()->hn_children.empty()) {
1✔
3085
            auto poss_provider = [curr_node = node.value()]() {
1✔
3086
                std::vector<breadcrumb::possibility> retval;
1✔
3087
                for (const auto& child : curr_node->hn_named_children) {
1✔
UNCOV
3088
                    retval.emplace_back(child.first);
×
3089
                }
3090
                return retval;
1✔
3091
            };
3092
            auto path_performer
3093
                = [this, curr_node = node.value(), line_from_top](
2✔
3094
                      const breadcrumb::crumb::key_t& value) {
UNCOV
3095
                      value.match(
×
UNCOV
3096
                          [curr_node](const std::string& str) {
×
UNCOV
3097
                              return curr_node->find_line_number(str);
×
3098
                          },
UNCOV
3099
                          [curr_node](size_t index) {
×
UNCOV
3100
                              return curr_node->find_line_number(index);
×
3101
                          })
UNCOV
3102
                          | [this, line_from_top](size_t line_number) {
×
UNCOV
3103
                                this->tss_view->set_selection(
×
UNCOV
3104
                                    vis_line_t(line_from_top + line_number));
×
3105
                            };
3106
                  };
1✔
3107
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
1✔
3108
            crumbs.back().c_expected_input
1✔
3109
                = node.value()->hn_named_children.empty()
2✔
3110
                ? breadcrumb::crumb::expected_input_t::index
1✔
3111
                : breadcrumb::crumb::expected_input_t::index_or_exact;
3112
        }
3113
    }
1✔
3114
}
10✔
3115

3116
void
3117
logfile_sub_source::quiesce()
60✔
3118
{
3119
    for (auto& ld : this->lss_files) {
102✔
3120
        auto* lf = ld->get_file_ptr();
42✔
3121

3122
        if (lf == nullptr) {
42✔
3123
            continue;
×
3124
        }
3125

3126
        lf->quiesce();
42✔
3127
    }
3128
}
60✔
3129

3130
bookmark_metadata&
3131
logfile_sub_source::get_bookmark_metadata(content_line_t cl)
25✔
3132
{
3133
    auto line_pair = this->find_line_with_file(cl).value();
25✔
3134
    auto line_number = static_cast<uint32_t>(
3135
        std::distance(line_pair.first->begin(), line_pair.second));
25✔
3136

3137
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
3138
}
25✔
3139

3140
logfile_sub_source::bookmark_metadata_context
3141
logfile_sub_source::get_bookmark_metadata_context(
2,824✔
3142
    vis_line_t vl, bookmark_metadata::categories desired) const
3143
{
3144
    const auto& vb = this->tss_view->get_bookmarks();
2,824✔
3145
    const auto& bv = vb[desired == bookmark_metadata::categories::partition
3146
                            ? &textview_curses::BM_PARTITION
3147
                            : &textview_curses::BM_META];
2,824✔
3148
    auto vl_iter = bv.bv_tree.lower_bound(vl + 1_vl);
2,824✔
3149

3150
    std::optional<vis_line_t> next_line;
2,824✔
3151
    for (auto next_vl_iter = vl_iter; next_vl_iter != bv.bv_tree.end();
2,824✔
UNCOV
3152
         ++next_vl_iter)
×
3153
    {
3154
        auto bm_opt = this->find_bookmark_metadata(*next_vl_iter);
2✔
3155
        if (!bm_opt) {
2✔
UNCOV
3156
            continue;
×
3157
        }
3158

3159
        if (bm_opt.value()->has(desired)) {
2✔
3160
            next_line = *next_vl_iter;
2✔
3161
            break;
2✔
3162
        }
3163
    }
3164
    if (vl_iter == bv.bv_tree.begin()) {
2,824✔
3165
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
2,808✔
3166
    }
3167

3168
    --vl_iter;
16✔
3169
    while (true) {
3170
        auto bm_opt = this->find_bookmark_metadata(*vl_iter);
16✔
3171
        if (bm_opt) {
16✔
3172
            if (bm_opt.value()->has(desired)) {
13✔
3173
                return bookmark_metadata_context{
3174
                    *vl_iter, bm_opt.value(), next_line};
16✔
3175
            }
3176
        }
3177

3178
        if (vl_iter == bv.bv_tree.begin()) {
3✔
3179
            return bookmark_metadata_context{
3180
                std::nullopt, std::nullopt, next_line};
3✔
3181
        }
UNCOV
3182
        --vl_iter;
×
3183
    }
3184
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3185
}
3186

3187
std::optional<bookmark_metadata*>
3188
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
22,106✔
3189
{
3190
    auto line_pair = this->find_line_with_file(cl).value();
22,106✔
3191
    auto line_number = static_cast<uint32_t>(
3192
        std::distance(line_pair.first->begin(), line_pair.second));
22,106✔
3193

3194
    auto& bm = line_pair.first->get_bookmark_metadata();
22,106✔
3195
    auto bm_iter = bm.find(line_number);
22,106✔
3196
    if (bm_iter == bm.end()) {
22,106✔
3197
        return std::nullopt;
21,719✔
3198
    }
3199

3200
    return &bm_iter->second;
387✔
3201
}
22,106✔
3202

3203
void
3204
logfile_sub_source::erase_bookmark_metadata(content_line_t cl)
26✔
3205
{
3206
    auto line_pair = this->find_line_with_file(cl).value();
26✔
3207
    auto line_number = static_cast<uint32_t>(
3208
        std::distance(line_pair.first->begin(), line_pair.second));
26✔
3209

3210
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
3211
    auto bm_iter = bm.find(line_number);
26✔
3212
    if (bm_iter != bm.end()) {
26✔
3213
        bm.erase(bm_iter);
6✔
3214
    }
3215
}
26✔
3216

3217
void
3218
logfile_sub_source::clear_bookmark_metadata()
6✔
3219
{
3220
    for (auto& ld : *this) {
14✔
3221
        if (ld->get_file_ptr() == nullptr) {
8✔
UNCOV
3222
            continue;
×
3223
        }
3224

3225
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3226
    }
3227
}
6✔
3228

3229
void
UNCOV
3230
logfile_sub_source::increase_line_context()
×
3231
{
UNCOV
3232
    auto old_context = this->lss_line_context;
×
3233

UNCOV
3234
    switch (this->lss_line_context) {
×
UNCOV
3235
        case line_context_t::filename:
×
3236
            // nothing to do
UNCOV
3237
            break;
×
UNCOV
3238
        case line_context_t::basename:
×
UNCOV
3239
            this->lss_line_context = line_context_t::filename;
×
UNCOV
3240
            break;
×
UNCOV
3241
        case line_context_t::none:
×
UNCOV
3242
            this->lss_line_context = line_context_t::basename;
×
UNCOV
3243
            break;
×
UNCOV
3244
        case line_context_t::time_column:
×
UNCOV
3245
            this->lss_line_context = line_context_t::none;
×
UNCOV
3246
            break;
×
3247
    }
UNCOV
3248
    if (old_context != this->lss_line_context) {
×
UNCOV
3249
        this->clear_line_size_cache();
×
3250
    }
3251
}
3252

3253
bool
UNCOV
3254
logfile_sub_source::decrease_line_context()
×
3255
{
3256
    static const auto& cfg
UNCOV
3257
        = injector::get<const logfile_sub_source_ns::config&>();
×
UNCOV
3258
    auto old_context = this->lss_line_context;
×
3259

UNCOV
3260
    switch (this->lss_line_context) {
×
UNCOV
3261
        case line_context_t::filename:
×
UNCOV
3262
            this->lss_line_context = line_context_t::basename;
×
UNCOV
3263
            break;
×
UNCOV
3264
        case line_context_t::basename:
×
UNCOV
3265
            this->lss_line_context = line_context_t::none;
×
UNCOV
3266
            break;
×
UNCOV
3267
        case line_context_t::none:
×
UNCOV
3268
            if (cfg.c_time_column
×
3269
                != logfile_sub_source_ns::time_column_feature_t::Disabled)
3270
            {
UNCOV
3271
                this->lss_line_context = line_context_t::time_column;
×
3272
            }
UNCOV
3273
            break;
×
UNCOV
3274
        case line_context_t::time_column:
×
UNCOV
3275
            break;
×
3276
    }
UNCOV
3277
    if (old_context != this->lss_line_context) {
×
UNCOV
3278
        this->clear_line_size_cache();
×
3279

UNCOV
3280
        return true;
×
3281
    }
3282

UNCOV
3283
    return false;
×
3284
}
3285

3286
size_t
3287
logfile_sub_source::get_filename_offset() const
254✔
3288
{
3289
    switch (this->lss_line_context) {
254✔
UNCOV
3290
        case line_context_t::filename:
×
UNCOV
3291
            return this->lss_filename_width;
×
UNCOV
3292
        case line_context_t::basename:
×
UNCOV
3293
            return this->lss_basename_width;
×
3294
        default:
254✔
3295
            return 0;
254✔
3296
    }
3297
}
3298

3299
size_t
UNCOV
3300
logfile_sub_source::file_count() const
×
3301
{
UNCOV
3302
    size_t retval = 0;
×
UNCOV
3303
    const_iterator iter;
×
3304

UNCOV
3305
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
×
UNCOV
3306
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
×
UNCOV
3307
            retval += 1;
×
3308
        }
3309
    }
3310

UNCOV
3311
    return retval;
×
3312
}
3313

3314
size_t
UNCOV
3315
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3316
                                       int row,
3317
                                       text_sub_source::line_flags_t flags)
3318
{
UNCOV
3319
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3320

UNCOV
3321
    if (this->lss_line_size_cache[index].first != row) {
×
UNCOV
3322
        std::string value;
×
3323

UNCOV
3324
        this->text_value_for_line(tc, row, value, flags);
×
UNCOV
3325
        scrub_ansi_string(value, nullptr);
×
UNCOV
3326
        auto line_width = string_fragment::from_str(value).column_width();
×
UNCOV
3327
        if (this->lss_line_context == line_context_t::time_column) {
×
3328
            auto time_attr
UNCOV
3329
                = find_string_attr(this->lss_token_attrs, &L_TIMESTAMP);
×
UNCOV
3330
            if (time_attr != this->lss_token_attrs.end()) {
×
UNCOV
3331
                line_width -= time_attr->sa_range.length();
×
UNCOV
3332
                auto format = this->lss_token_file->get_format();
×
UNCOV
3333
                if (format->lf_level_hideable) {
×
3334
                    auto level_attr
UNCOV
3335
                        = find_string_attr(this->lss_token_attrs, &L_LEVEL);
×
UNCOV
3336
                    if (level_attr != this->lss_token_attrs.end()) {
×
UNCOV
3337
                        line_width -= level_attr->sa_range.length();
×
3338
                    }
3339
                }
3340
            }
3341
        }
UNCOV
3342
        this->lss_line_size_cache[index].second = line_width;
×
UNCOV
3343
        this->lss_line_size_cache[index].first = row;
×
3344
    }
UNCOV
3345
    return this->lss_line_size_cache[index].second;
×
3346
}
3347

3348
int
3349
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
1✔
3350
{
3351
    int retval = 0;
1✔
3352

3353
    for (const auto& ld : this->lss_files) {
2✔
3354
        retval += ld->ld_filter_state.lfo_filter_state
1✔
3355
                      .tfs_filter_hits[filter_index];
1✔
3356
    }
3357

3358
    return retval;
1✔
3359
}
3360

3361
std::optional<vis_line_t>
3362
logfile_sub_source::row_for(const row_info& ri)
213✔
3363
{
3364
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
426✔
3365
                               this->lss_filtered_index.end(),
3366
                               ri.ri_time,
213✔
3367
                               filtered_logline_cmp(*this));
3368
    if (lb != this->lss_filtered_index.end()) {
213✔
3369
        auto first_lb = lb;
208✔
3370
        while (true) {
3371
            auto cl = this->lss_index[*lb].value();
216✔
3372
            if (content_line_t(ri.ri_id) == cl) {
216✔
3373
                first_lb = lb;
181✔
3374
                break;
181✔
3375
            }
3376
            auto ll = this->find_line(cl);
35✔
3377
            if (ll->get_timeval() != ri.ri_time) {
35✔
3378
                break;
27✔
3379
            }
3380
            auto next_lb = std::next(lb);
8✔
3381
            if (next_lb == this->lss_filtered_index.end()) {
8✔
UNCOV
3382
                break;
×
3383
            }
3384
            lb = next_lb;
8✔
3385
        }
8✔
3386

3387
        const auto dst
3388
            = std::distance(this->lss_filtered_index.begin(), first_lb);
208✔
3389
        return vis_line_t(dst);
208✔
3390
    }
3391

3392
    return std::nullopt;
5✔
3393
}
3394

3395
std::optional<vis_line_t>
3396
logfile_sub_source::row_for_anchor(const std::string& id)
3✔
3397
{
3398
    if (startswith(id, "#msg")) {
3✔
3399
        static const auto ANCHOR_RE
3400
            = lnav::pcre2pp::code::from_const(R"(#msg([0-9a-fA-F]+)-(.+))");
3✔
3401
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
3✔
3402

3403
        if (ANCHOR_RE.capture_from(id).into(md).found_p()) {
3✔
3404
            auto scan_res = scn::scan<int64_t>(md[1]->to_string_view(), "{:x}");
3✔
3405
            if (scan_res) {
3✔
3406
                auto ts_low = std::chrono::microseconds{scan_res->value()};
3✔
3407
                auto ts_high = ts_low + 1us;
3✔
3408

3409
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3410
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3411
                if (low_vl) {
3✔
3412
                    auto lw = this->window_at(
3✔
3413
                        low_vl.value(),
3✔
3414
                        high_vl.value_or(low_vl.value() + 1_vl));
3✔
3415

3416
                    for (const auto& li : lw) {
3✔
3417
                        auto hash_res = li.get_line_hash();
3✔
3418
                        if (hash_res.isErr()) {
3✔
UNCOV
3419
                            auto errmsg = hash_res.unwrapErr();
×
3420

UNCOV
3421
                            log_error("unable to get line hash: %s",
×
3422
                                      errmsg.c_str());
UNCOV
3423
                            continue;
×
3424
                        }
3425

3426
                        auto hash = hash_res.unwrap();
3✔
3427
                        if (hash == md[2]) {
3✔
3428
                            return li.get_vis_line();
3✔
3429
                        }
3430
                    }
12✔
3431
                }
3432
            }
3433
        }
3434

UNCOV
3435
        return std::nullopt;
×
3436
    }
3437

UNCOV
3438
    auto& vb = this->tss_view->get_bookmarks();
×
UNCOV
3439
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3440

UNCOV
3441
    for (const auto& vl : bv.bv_tree) {
×
UNCOV
3442
        auto meta_opt = this->find_bookmark_metadata(vl);
×
UNCOV
3443
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
UNCOV
3444
            continue;
×
3445
        }
3446

UNCOV
3447
        const auto& name = meta_opt.value()->bm_name;
×
UNCOV
3448
        if (id == text_anchors::to_anchor_string(name)) {
×
UNCOV
3449
            return vl;
×
3450
        }
3451
    }
3452

UNCOV
3453
    return std::nullopt;
×
3454
}
3455

3456
std::optional<vis_line_t>
3457
logfile_sub_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
2✔
3458
{
3459
    auto bmc = this->get_bookmark_metadata_context(
2✔
3460
        vl, bookmark_metadata::categories::partition);
3461
    switch (dir) {
2✔
UNCOV
3462
        case text_anchors::direction::prev: {
×
UNCOV
3463
            if (bmc.bmc_current && bmc.bmc_current.value() != vl) {
×
UNCOV
3464
                return bmc.bmc_current;
×
3465
            }
UNCOV
3466
            if (!bmc.bmc_current || bmc.bmc_current.value() == 0_vl) {
×
UNCOV
3467
                return 0_vl;
×
3468
            }
UNCOV
3469
            auto prev_bmc = this->get_bookmark_metadata_context(
×
UNCOV
3470
                bmc.bmc_current.value() - 1_vl,
×
3471
                bookmark_metadata::categories::partition);
UNCOV
3472
            if (!prev_bmc.bmc_current) {
×
UNCOV
3473
                return 0_vl;
×
3474
            }
UNCOV
3475
            return prev_bmc.bmc_current;
×
3476
        }
3477
        case text_anchors::direction::next:
2✔
3478
            return bmc.bmc_next_line;
2✔
3479
    }
UNCOV
3480
    return std::nullopt;
×
3481
}
3482

3483
std::optional<std::string>
3484
logfile_sub_source::anchor_for_row(vis_line_t vl)
62✔
3485
{
3486
    auto line_meta = this->get_bookmark_metadata_context(
62✔
3487
        vl, bookmark_metadata::categories::partition);
3488
    if (!line_meta.bmc_current_metadata) {
62✔
3489
        auto lw = window_at(vl);
59✔
3490

3491
        for (const auto& li : lw) {
59✔
3492
            auto hash_res = li.get_line_hash();
59✔
3493
            if (hash_res.isErr()) {
59✔
UNCOV
3494
                auto errmsg = hash_res.unwrapErr();
×
UNCOV
3495
                log_error("unable to compute line hash: %s", errmsg.c_str());
×
UNCOV
3496
                break;
×
3497
            }
3498
            auto hash = hash_res.unwrap();
59✔
3499
            auto retval = fmt::format(
3500
                FMT_STRING("#msg{:016x}-{}"),
118✔
3501
                li.get_logline().get_time<std::chrono::microseconds>().count(),
59✔
UNCOV
3502
                hash);
×
3503

3504
            return retval;
59✔
3505
        }
236✔
3506

UNCOV
3507
        return std::nullopt;
×
3508
    }
3509

3510
    return text_anchors::to_anchor_string(
3✔
3511
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3512
}
3513

3514
std::unordered_set<std::string>
UNCOV
3515
logfile_sub_source::get_anchors()
×
3516
{
UNCOV
3517
    auto& vb = this->tss_view->get_bookmarks();
×
UNCOV
3518
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
UNCOV
3519
    std::unordered_set<std::string> retval;
×
3520

UNCOV
3521
    for (const auto& vl : bv.bv_tree) {
×
UNCOV
3522
        auto meta_opt = this->find_bookmark_metadata(vl);
×
UNCOV
3523
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
UNCOV
3524
            continue;
×
3525
        }
3526

UNCOV
3527
        const auto& name = meta_opt.value()->bm_name;
×
UNCOV
3528
        retval.emplace(text_anchors::to_anchor_string(name));
×
3529
    }
3530

UNCOV
3531
    return retval;
×
UNCOV
3532
}
×
3533

3534
bool
UNCOV
3535
logfile_sub_source::text_handle_mouse(
×
3536
    textview_curses& tc,
3537
    const listview_curses::display_line_content_t& mouse_line,
3538
    mouse_event& me)
3539
{
UNCOV
3540
    if (tc.get_overlay_selection()) {
×
UNCOV
3541
        auto nci = ncinput{};
×
UNCOV
3542
        if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 2, 4)) {
×
UNCOV
3543
            nci.id = ' ';
×
UNCOV
3544
            nci.eff_text[0] = ' ';
×
UNCOV
3545
            this->list_input_handle_key(tc, nci);
×
UNCOV
3546
        } else if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 5, 6)) {
×
UNCOV
3547
            nci.id = '#';
×
UNCOV
3548
            nci.eff_text[0] = '#';
×
UNCOV
3549
            this->list_input_handle_key(tc, nci);
×
3550
        }
3551
    }
UNCOV
3552
    return true;
×
3553
}
3554

3555
void
3556
logfile_sub_source::reload_config(error_reporter& reporter)
607✔
3557
{
3558
    static const auto& cfg
3559
        = injector::get<const logfile_sub_source_ns::config&>();
607✔
3560

3561
    switch (cfg.c_time_column) {
607✔
UNCOV
3562
        case logfile_sub_source_ns::time_column_feature_t::Default:
×
UNCOV
3563
            if (this->lss_line_context == line_context_t::none) {
×
UNCOV
3564
                this->lss_line_context = line_context_t::time_column;
×
3565
            }
UNCOV
3566
            break;
×
3567
        case logfile_sub_source_ns::time_column_feature_t::Disabled:
607✔
3568
            if (this->lss_line_context == line_context_t::time_column) {
607✔
UNCOV
3569
                this->lss_line_context = line_context_t::none;
×
3570
            }
3571
            break;
607✔
UNCOV
3572
        case logfile_sub_source_ns::time_column_feature_t::Enabled:
×
UNCOV
3573
            break;
×
3574
    }
3575
}
607✔
3576

3577
void
UNCOV
3578
logfile_sub_source::update_filter_hash_state(hasher& h) const
×
3579
{
UNCOV
3580
    text_sub_source::update_filter_hash_state(h);
×
3581

UNCOV
3582
    for (const auto& ld : this->lss_files) {
×
UNCOV
3583
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
×
UNCOV
3584
            h.update(0);
×
3585
        } else {
UNCOV
3586
            h.update(1);
×
3587
        }
3588
    }
UNCOV
3589
    h.update(this->lss_min_log_level);
×
UNCOV
3590
    h.update(this->lss_marked_only);
×
3591
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc