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

tstack / lnav / 19833402571-2724

01 Dec 2025 06:33PM UTC coverage: 68.86% (-0.001%) from 68.861%
19833402571-2724

push

github

tstack
[breadcrumb] add thread ID to breadcrumb bar

173 of 219 new or added lines in 14 files covered. (79.0%)

4 existing lines in 3 files now uncovered.

51293 of 74489 relevant lines covered (68.86%)

435674.56 hits per line

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

64.18
/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
#include <optional>
34
#include <string>
35
#include <unordered_set>
36
#include <vector>
37

38
#include "logfile_sub_source.hh"
39

40
#include <sqlite3.h>
41

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

155
        return retval;
×
156
    });
×
157

158
    return retval;
×
159
}
160

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

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

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

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

193
    return retval;
25✔
194
}
×
195

196
struct filtered_logline_cmp {
197
    filtered_logline_cmp(const logfile_sub_source& lc) : llss_controller(lc) {}
347✔
198

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

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

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

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

226
    const logfile_sub_source& llss_controller;
227
};
228

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

241
    return std::nullopt;
17✔
242
}
243

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

256
    line_info retval;
4,044✔
257
    content_line_t line(0);
4,044✔
258

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

262
    line = this->at(vis_line_t(row));
4,044✔
263

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

286
    require_false(this->lss_in_value_for_line);
4,012✔
287

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

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

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

318
    auto format = this->lss_token_file->get_format();
4,012✔
319

320
    value_out = this->lss_token_value;
4,012✔
321

322
    auto& sbr = this->lss_token_values.lvv_sbr;
4,012✔
323

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

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

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

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

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

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

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

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

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

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

492
    this->lss_in_value_for_line = false;
4,012✔
493

494
    return retval;
4,012✔
495
}
4,012✔
496

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

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

512
    value_out = this->lss_token_attrs;
4,012✔
513

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

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

526
    const auto& line_values = this->lss_token_values;
4,012✔
527

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

533
    lr.lr_start = time_offset_end;
4,012✔
534
    lr.lr_end = -1;
4,012✔
535

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

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

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

551
    for (const auto& line_value : line_values.lvv_values) {
34,360✔
552
        if ((!(this->lss_token_flags & RF_FULL)
66,973✔
553
             && line_value.lv_sub_offset
60,252✔
554
                 != this->lss_token_line->get_sub_offset())
30,126✔
555
            || !line_value.lv_origin.is_valid())
60,474✔
556
        {
557
            continue;
6,277✔
558
        }
559

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

565
        if (!line_value.lv_meta.lvm_identifier
62,438✔
566
            || !line_value.lv_origin.is_valid())
24,071✔
567
        {
568
            continue;
14,296✔
569
        }
570

571
        value_out.emplace_back(line_value.lv_origin,
9,775✔
572
                               VC_ROLE.value(role_t::VCR_IDENTIFIER));
19,550✔
573
    }
574

575
    if (this->lss_token_shift_size) {
4,012✔
576
        shift_string_attrs(value_out,
290✔
577
                           this->lss_token_shift_start + 1,
290✔
578
                           this->lss_token_shift_size);
579
    }
580

581
    shift_string_attrs(value_out, 0, 1);
4,012✔
582

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

602
        if (!(this->lss_token_flags & RF_FULL)) {
4,012✔
603
            const auto& bv_search = bm[&textview_curses::BM_SEARCH];
3,964✔
604

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

614
    value_out.emplace_back(lr,
4,012✔
615
                           VC_STYLE.value(vc.attrs_for_ident(
8,024✔
616
                               this->lss_token_file->get_filename())));
4,012✔
617

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

809
                if (matched) {
×
810
                    color = palette_color{
×
811
                        lnav::enums::to_underlying(ansi_color::green)};
×
812
                } else {
813
                    color = palette_color{
×
814
                        lnav::enums::to_underlying(ansi_color::red)};
×
815
                    value_out.emplace_back(
×
816
                        line_range{0, 1},
×
817
                        VC_STYLE.value(text_attrs::with_blink()));
×
818
                }
819
            }
820
            value_out.emplace_back(line_range{0, 1},
×
821
                                   VC_BACKGROUND.value(color));
×
822
        }
823

824
        auto sql_filter_opt = this->get_sql_filter();
2,158✔
825
        if (sql_filter_opt) {
2,158✔
826
            auto* sf = (sql_filter*) sql_filter_opt.value().get();
36✔
827
            auto eval_res = this->eval_sql_filter(sf->sf_filter_stmt.in(),
828
                                                  this->lss_token_file_data,
829
                                                  this->lss_token_line);
36✔
830
            if (eval_res.isErr()) {
36✔
831
                auto msg = fmt::format(
832
                    FMT_STRING(
×
833
                        "filter expression evaluation failed with -- {}"),
834
                    eval_res.unwrapErr().to_attr_line().get_string());
×
835
                auto cu = styling::color_unit::from_palette(palette_color{
×
836
                    lnav::enums::to_underlying(ansi_color::yellow)});
837
                value_out.emplace_back(line_range{0, -1}, SA_ERROR.value(msg));
×
838
                value_out.emplace_back(line_range{0, 1},
×
839
                                       VC_BACKGROUND.value(cu));
×
840
            }
841
        }
36✔
842
    }
2,158✔
843
}
844

845
struct logline_cmp {
846
    logline_cmp(logfile_sub_source& lc) : llss_controller(lc) {}
1,138✔
847

848
    bool operator()(const logfile_sub_source::indexed_content& lhs,
105,014✔
849
                    const logfile_sub_source::indexed_content& rhs) const
850
    {
851
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
105,014✔
852
        const auto* ll_rhs = this->llss_controller.find_line(rhs.value());
105,014✔
853

854
        return (*ll_lhs) < (*ll_rhs);
105,014✔
855
    }
856

857
    bool operator()(const uint32_t& lhs, const uint32_t& rhs) const
858
    {
859
        auto cl_lhs = llss_controller.lss_index[lhs].value();
860
        auto cl_rhs = llss_controller.lss_index[rhs].value();
861
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
862
        const auto* ll_rhs = this->llss_controller.find_line(cl_rhs);
863

864
        return (*ll_lhs) < (*ll_rhs);
865
    }
866
#if 0
867
        bool operator()(const indexed_content &lhs, const indexed_content &rhs)
868
        {
869
            logline *ll_lhs = this->llss_controller.find_line(lhs.ic_value);
870
            logline *ll_rhs = this->llss_controller.find_line(rhs.ic_value);
871

872
            return (*ll_lhs) < (*ll_rhs);
873
        }
874
#endif
875

876
#if 0
877
    bool operator()(const content_line_t& lhs, const time_t& rhs) const
878
    {
879
        logline* ll_lhs = this->llss_controller.find_line(lhs);
880

881
        return *ll_lhs < rhs;
882
    }
883
#endif
884

885
    bool operator()(const logfile_sub_source::indexed_content& lhs,
×
886
                    const struct timeval& rhs) const
887
    {
888
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
×
889

890
        return *ll_lhs < rhs;
×
891
    }
892

893
    logfile_sub_source& llss_controller;
894
};
895

896
logfile_sub_source::rebuild_result
897
logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
4,431✔
898
{
899
    if (this->tss_view == nullptr) {
4,431✔
900
        return rebuild_result::rr_no_change;
124✔
901
    }
902

903
    this->lss_indexing_in_progress = true;
4,307✔
904
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
4,307✔
905

906
    iterator iter;
4,307✔
907
    size_t total_lines = 0;
4,307✔
908
    size_t est_remaining_lines = 0;
4,307✔
909
    auto all_time_ordered_formats = true;
4,307✔
910
    auto full_sort = this->lss_index.empty();
4,307✔
911
    int file_count = 0;
4,307✔
912
    auto force = std::exchange(this->lss_force_rebuild, false);
4,307✔
913
    auto retval = rebuild_result::rr_no_change;
4,307✔
914
    std::optional<timeval> lowest_tv = std::nullopt;
4,307✔
915
    auto search_start = 0_vl;
4,307✔
916

917
    if (force) {
4,307✔
918
        log_debug("forced to full rebuild");
480✔
919
        retval = rebuild_result::rr_full_rebuild;
480✔
920
        full_sort = true;
480✔
921
        this->lss_index.clear();
480✔
922
    }
923

924
    std::vector<size_t> file_order(this->lss_files.size());
4,307✔
925

926
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
7,301✔
927
        file_order[lpc] = lpc;
2,994✔
928
    }
929
    if (!this->lss_index.empty()) {
4,307✔
930
        std::stable_sort(file_order.begin(),
2,233✔
931
                         file_order.end(),
932
                         [this](const auto& left, const auto& right) {
259✔
933
                             const auto& left_ld = this->lss_files[left];
259✔
934
                             const auto& right_ld = this->lss_files[right];
259✔
935

936
                             if (left_ld->get_file_ptr() == nullptr) {
259✔
937
                                 return true;
×
938
                             }
939
                             if (right_ld->get_file_ptr() == nullptr) {
259✔
940
                                 return false;
3✔
941
                             }
942

943
                             return left_ld->get_file_ptr()->back()
256✔
944
                                 < right_ld->get_file_ptr()->back();
512✔
945
                         });
946
    }
947

948
    bool time_left = true;
4,307✔
949
    this->lss_all_timestamp_flags = 0;
4,307✔
950
    for (const auto file_index : file_order) {
7,301✔
951
        auto& ld = *(this->lss_files[file_index]);
2,994✔
952
        auto* lf = ld.get_file_ptr();
2,994✔
953

954
        if (lf == nullptr) {
2,994✔
955
            if (ld.ld_lines_indexed > 0) {
4✔
956
                log_debug("%zu: file closed, doing full rebuild",
1✔
957
                          ld.ld_file_index);
958
                force = true;
1✔
959
                retval = rebuild_result::rr_full_rebuild;
1✔
960
                full_sort = true;
1✔
961
            }
962
        } else {
963
            if (!lf->get_format_ptr()->lf_time_ordered) {
2,990✔
964
                all_time_ordered_formats = false;
191✔
965
            }
966
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
2,990✔
967
                log_debug("no time left, skipping %s",
2✔
968
                          lf->get_filename_as_string().c_str());
969
                time_left = false;
2✔
970
            }
971
            this->lss_all_timestamp_flags
2,990✔
972
                |= lf->get_format_ptr()->lf_timestamp_flags;
2,990✔
973

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

977
                if (ld.ld_lines_indexed < lf->size()
2,988✔
978
                    && log_rebuild_res
2,988✔
979
                        == logfile::rebuild_result_t::NO_NEW_LINES)
980
                {
981
                    // This is a bit awkward... if the logfile indexing was
982
                    // complete before being added to us, we need to adjust
983
                    // the rebuild result to make it look like new lines
984
                    // were added.
985
                    log_rebuild_res = logfile::rebuild_result_t::NEW_LINES;
54✔
986
                }
987
                switch (log_rebuild_res) {
2,988✔
988
                    case logfile::rebuild_result_t::NO_NEW_LINES:
2,442✔
989
                        break;
2,442✔
990
                    case logfile::rebuild_result_t::NEW_LINES:
486✔
991
                        if (retval == rebuild_result::rr_no_change) {
486✔
992
                            retval = rebuild_result::rr_appended_lines;
433✔
993
                        }
994
                        log_debug("new lines for %s:%zu",
486✔
995
                                  lf->get_filename_as_string().c_str(),
996
                                  lf->size());
997
                        if (!this->lss_index.empty()
486✔
998
                            && lf->size() > ld.ld_lines_indexed)
486✔
999
                        {
1000
                            auto& new_file_line = (*lf)[ld.ld_lines_indexed];
×
1001
                            auto cl = this->lss_index.back().value();
×
1002
                            auto* last_indexed_line = this->find_line(cl);
×
1003

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

1058
            est_remaining_lines += lf->estimated_remaining_lines();
2,990✔
1059
        }
1060
    }
1061

1062
    if (!all_time_ordered_formats
4,307✔
1063
        && retval == rebuild_result::rr_partial_rebuild)
185✔
1064
    {
1065
        force = true;
×
1066
        full_sort = true;
×
1067
        retval = rebuild_result::rr_full_rebuild;
×
1068
    }
1069

1070
    if (this->lss_index.reserve(total_lines + est_remaining_lines)) {
4,307✔
1071
        // The index array was reallocated, just do a full sort/rebuild since
1072
        // it's been cleared out.
1073
        log_debug("expanding index capacity %zu", this->lss_index.ba_capacity);
633✔
1074
        force = true;
633✔
1075
        retval = rebuild_result::rr_full_rebuild;
633✔
1076
        full_sort = true;
633✔
1077
    }
1078

1079
    auto& vis_bm = this->tss_view->get_bookmarks();
4,307✔
1080

1081
    if (force) {
4,307✔
1082
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,671✔
1083
             iter++)
543✔
1084
        {
1085
            (*iter)->ld_lines_indexed = 0;
543✔
1086
        }
1087

1088
        this->lss_index.clear();
1,128✔
1089
        this->lss_filtered_index.clear();
1,128✔
1090
        this->lss_longest_line = 0;
1,128✔
1091
        this->lss_basename_width = 0;
1,128✔
1092
        this->lss_filename_width = 0;
1,128✔
1093
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,128✔
1094
        if (this->lss_index_delegate) {
1,128✔
1095
            this->lss_index_delegate->index_start(*this);
1,128✔
1096
        }
1097
    } else if (retval == rebuild_result::rr_partial_rebuild) {
3,179✔
1098
        size_t remaining = 0;
×
1099

1100
        log_debug("partial rebuild with lowest time: %ld",
×
1101
                  lowest_tv.value().tv_sec);
1102
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1103
             iter++)
×
1104
        {
1105
            logfile_data& ld = *(*iter);
×
1106
            auto* lf = ld.get_file_ptr();
×
1107

1108
            if (lf == nullptr) {
×
1109
                continue;
×
1110
            }
1111

1112
            require(lf->get_format_ptr()->lf_time_ordered);
×
1113

1114
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1115

1116
            if (line_iter) {
×
1117
                log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
×
1118
                          line_iter.value()->get_timeval().tv_sec,
1119
                          std::distance(lf->cbegin(), line_iter.value()),
1120
                          lf->size(),
1121
                          lf->get_filename_as_string().c_str());
1122
            }
1123
            ld.ld_lines_indexed
1124
                = std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
×
1125
            remaining += lf->size() - ld.ld_lines_indexed;
×
1126
        }
1127

1128
        auto* row_iter = std::lower_bound(this->lss_index.begin(),
×
1129
                                          this->lss_index.end(),
1130
                                          lowest_tv.value(),
×
1131
                                          logline_cmp(*this));
1132
        this->lss_index.shrink_to(
×
1133
            std::distance(this->lss_index.begin(), row_iter));
×
1134
        log_debug("new index size %ld/%ld; remain %ld",
×
1135
                  this->lss_index.ba_size,
1136
                  this->lss_index.ba_capacity,
1137
                  remaining);
1138
        auto filt_row_iter = std::lower_bound(this->lss_filtered_index.begin(),
×
1139
                                              this->lss_filtered_index.end(),
1140
                                              lowest_tv.value(),
×
1141
                                              filtered_logline_cmp(*this));
1142
        this->lss_filtered_index.resize(
×
1143
            std::distance(this->lss_filtered_index.begin(), filt_row_iter));
×
1144
        search_start = vis_line_t(this->lss_filtered_index.size());
×
1145

1146
        if (this->lss_index_delegate) {
×
1147
            this->lss_index_delegate->index_start(*this);
×
1148
            for (const auto row_in_full_index : this->lss_filtered_index) {
×
1149
                auto cl = this->lss_index[row_in_full_index].value();
×
1150
                uint64_t line_number;
1151
                auto ld_iter = this->find_data(cl, line_number);
×
1152
                auto& ld = *ld_iter;
×
1153
                auto line_iter = ld->get_file_ptr()->begin() + line_number;
×
1154

1155
                this->lss_index_delegate->index_line(
×
1156
                    *this, ld->get_file_ptr(), line_iter);
1157
            }
1158
        }
1159
    }
1160

1161
    if (this->lss_index.empty() && !time_left) {
4,307✔
1162
        log_info("ran out of time, skipping rebuild");
×
1163
        // need to make sure we rebuild in case no new data comes in
1164
        this->lss_force_rebuild = true;
×
1165
        return rebuild_result::rr_appended_lines;
×
1166
    }
1167

1168
    if (retval != rebuild_result::rr_no_change || force) {
4,307✔
1169
        size_t index_size = 0, start_size = this->lss_index.size();
1,138✔
1170
        logline_cmp line_cmper(*this);
1,138✔
1171

1172
        for (auto& ld : this->lss_files) {
1,694✔
1173
            auto* lf = ld->get_file_ptr();
556✔
1174

1175
            if (lf == nullptr) {
556✔
1176
                continue;
1✔
1177
            }
1178
            this->lss_longest_line = std::max(
1,110✔
1179
                this->lss_longest_line, lf->get_longest_line_length() + 1);
555✔
1180
            this->lss_basename_width
1181
                = std::max(this->lss_basename_width,
1,110✔
1182
                           lf->get_unique_path().native().size());
555✔
1183
            this->lss_filename_width = std::max(
1,110✔
1184
                this->lss_filename_width, lf->get_filename().native().size());
555✔
1185
        }
1186

1187
        if (full_sort) {
1,138✔
1188
            log_trace("rebuild_index full sort");
1,138✔
1189
            for (auto& ld : this->lss_files) {
1,694✔
1190
                auto* lf = ld->get_file_ptr();
556✔
1191

1192
                if (lf == nullptr) {
556✔
1193
                    continue;
1✔
1194
                }
1195

1196
                for (size_t line_index = 0; line_index < lf->size();
14,802✔
1197
                     line_index++)
1198
                {
1199
                    const auto lf_iter
1200
                        = ld->get_file_ptr()->begin() + line_index;
14,247✔
1201
                    if (lf_iter->is_ignored()) {
14,247✔
1202
                        continue;
417✔
1203
                    }
1204

1205
                    content_line_t con_line(
1206
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
13,830✔
1207

1208
                    if (lf_iter->is_meta_marked()) {
13,830✔
1209
                        auto start_iter = lf_iter;
12✔
1210
                        while (start_iter->is_continued()) {
12✔
1211
                            --start_iter;
×
1212
                        }
1213
                        int start_index
1214
                            = start_iter - ld->get_file_ptr()->begin();
12✔
1215
                        content_line_t start_con_line(ld->ld_file_index
12✔
1216
                                                          * MAX_LINES_PER_FILE
12✔
1217
                                                      + start_index);
12✔
1218

1219
                        auto& line_meta
1220
                            = ld->get_file_ptr()
1221
                                  ->get_bookmark_metadata()[start_index];
12✔
1222
                        if (line_meta.has(bookmark_metadata::categories::notes))
12✔
1223
                        {
1224
                            this->lss_user_marks[&textview_curses::BM_META]
4✔
1225
                                .insert_once(start_con_line);
4✔
1226
                        }
1227
                        if (line_meta.has(
12✔
1228
                                bookmark_metadata::categories::partition))
1229
                        {
1230
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
8✔
1231
                                .insert_once(start_con_line);
8✔
1232
                        }
1233
                    }
1234
                    this->lss_index.push_back(
13,830✔
1235
                        indexed_content{con_line, lf_iter});
27,660✔
1236
                }
1237
            }
1238

1239
            // XXX get rid of this full sort on the initial run, it's not
1240
            // needed unless the file is not in time-order
1241
            if (this->lss_sorting_observer) {
1,138✔
1242
                this->lss_sorting_observer(*this, 0, this->lss_index.size());
3✔
1243
            }
1244
            std::sort(
1,138✔
1245
                this->lss_index.begin(), this->lss_index.end(), line_cmper);
1246
            if (this->lss_sorting_observer) {
1,138✔
1247
                this->lss_sorting_observer(
6✔
1248
                    *this, this->lss_index.size(), this->lss_index.size());
3✔
1249
            }
1250
        } else {
1251
            kmerge_tree_c<logline, logfile_data, logfile::iterator> merge(
1252
                file_count);
×
1253

1254
            for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1255
                 iter++)
×
1256
            {
1257
                auto* ld = iter->get();
×
1258
                auto* lf = ld->get_file_ptr();
×
1259
                if (lf == nullptr) {
×
1260
                    continue;
×
1261
                }
1262

1263
                merge.add(ld, lf->begin() + ld->ld_lines_indexed, lf->end());
×
1264
                index_size += lf->size();
×
1265
            }
1266

1267
            file_off_t index_off = 0;
×
1268
            merge.execute();
×
1269
            if (this->lss_sorting_observer) {
×
1270
                this->lss_sorting_observer(*this, index_off, index_size);
×
1271
            }
1272
            log_trace("k-way merge");
×
1273
            for (;;) {
1274
                logfile::iterator lf_iter;
×
1275
                logfile_data* ld;
1276

1277
                if (!merge.get_top(ld, lf_iter)) {
×
1278
                    break;
×
1279
                }
1280

1281
                if (!lf_iter->is_ignored()) {
×
1282
                    int file_index = ld->ld_file_index;
×
1283
                    int line_index = lf_iter - ld->get_file_ptr()->begin();
×
1284

1285
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
1286
                                            + line_index);
×
1287

1288
                    if (lf_iter->is_meta_marked()) {
×
1289
                        auto start_iter = lf_iter;
×
1290
                        while (start_iter->is_continued()) {
×
1291
                            --start_iter;
×
1292
                        }
1293
                        int start_index
1294
                            = start_iter - ld->get_file_ptr()->begin();
×
1295
                        content_line_t start_con_line(
1296
                            file_index * MAX_LINES_PER_FILE + start_index);
×
1297

1298
                        auto& line_meta
1299
                            = ld->get_file_ptr()
1300
                                  ->get_bookmark_metadata()[start_index];
×
1301
                        if (line_meta.has(bookmark_metadata::categories::notes))
×
1302
                        {
1303
                            this->lss_user_marks[&textview_curses::BM_META]
×
1304
                                .insert_once(start_con_line);
×
1305
                        }
1306
                        if (line_meta.has(
×
1307
                                bookmark_metadata::categories::partition))
1308
                        {
1309
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
×
1310
                                .insert_once(start_con_line);
×
1311
                        }
1312
                    }
1313
                    this->lss_index.push_back(
×
1314
                        indexed_content{con_line, lf_iter});
×
1315
                }
1316

1317
                merge.next();
×
1318
                index_off += 1;
×
1319
                if (index_off % 100000 == 0 && this->lss_sorting_observer) {
×
1320
                    this->lss_sorting_observer(*this, index_off, index_size);
×
1321
                }
1322
            }
1323
            if (this->lss_sorting_observer) {
×
1324
                this->lss_sorting_observer(*this, index_size, index_size);
×
1325
            }
1326
        }
1327

1328
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,694✔
1329
             ++iter)
556✔
1330
        {
1331
            auto* lf = (*iter)->get_file_ptr();
556✔
1332

1333
            if (lf == nullptr) {
556✔
1334
                continue;
1✔
1335
            }
1336

1337
            (*iter)->ld_lines_indexed = lf->size();
555✔
1338
        }
1339

1340
        this->lss_filtered_index.reserve(this->lss_index.size());
1,138✔
1341

1342
        uint32_t filter_in_mask, filter_out_mask;
1343
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,138✔
1344

1345
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,138✔
1346
            this->lss_index_delegate->index_start(*this);
1,138✔
1347
        }
1348

1349
        log_trace("filtered index");
1,138✔
1350
        for (size_t index_index = start_size;
14,968✔
1351
             index_index < this->lss_index.size();
14,968✔
1352
             index_index++)
1353
        {
1354
            const auto cl = this->lss_index[index_index].value();
13,830✔
1355
            uint64_t line_number;
1356
            auto ld = this->find_data(cl, line_number);
13,830✔
1357

1358
            if (!(*ld)->is_visible()) {
13,830✔
1359
                continue;
×
1360
            }
1361

1362
            auto* lf = (*ld)->get_file_ptr();
13,830✔
1363
            auto line_iter = lf->begin() + line_number;
13,830✔
1364

1365
            if (line_iter->is_ignored()) {
13,830✔
1366
                continue;
×
1367
            }
1368

1369
            if (!this->tss_apply_filters
27,660✔
1370
                || (!(*ld)->ld_filter_state.excluded(
27,632✔
1371
                        filter_in_mask, filter_out_mask, line_number)
1372
                    && this->check_extra_filters(ld, line_iter)))
13,802✔
1373
            {
1374
                auto eval_res = this->eval_sql_filter(
1375
                    this->lss_marker_stmt.in(), ld, line_iter);
13,802✔
1376
                if (eval_res.isErr()) {
13,802✔
1377
                    line_iter->set_expr_mark(false);
×
1378
                } else {
1379
                    auto matched = eval_res.unwrap();
13,802✔
1380

1381
                    line_iter->set_expr_mark(matched);
13,802✔
1382
                    if (matched) {
13,802✔
1383
                        vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1384
                            vis_line_t(this->lss_filtered_index.size()));
×
1385
                        this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1386
                            .insert_once(cl);
×
1387
                    }
1388
                }
1389
                this->lss_filtered_index.push_back(index_index);
13,802✔
1390
                if (this->lss_index_delegate != nullptr) {
13,802✔
1391
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
13,802✔
1392
                }
1393
            }
13,802✔
1394
        }
1395

1396
        this->lss_indexing_in_progress = false;
1,138✔
1397

1398
        if (this->lss_index_delegate != nullptr) {
1,138✔
1399
            this->lss_index_delegate->index_complete(*this);
1,138✔
1400
        }
1401
    }
1402

1403
    switch (retval) {
4,307✔
1404
        case rebuild_result::rr_no_change:
3,169✔
1405
            break;
3,169✔
1406
        case rebuild_result::rr_full_rebuild:
1,128✔
1407
            log_debug("redoing search");
1,128✔
1408
            this->lss_index_generation += 1;
1,128✔
1409
            this->tss_view->reload_data();
1,128✔
1410
            this->tss_view->redo_search();
1,128✔
1411
            break;
1,128✔
1412
        case rebuild_result::rr_partial_rebuild:
×
1413
            log_debug("redoing search from: %d", (int) search_start);
×
1414
            this->lss_index_generation += 1;
×
1415
            this->tss_view->reload_data();
×
1416
            this->tss_view->search_new_data(search_start);
×
1417
            break;
×
1418
        case rebuild_result::rr_appended_lines:
10✔
1419
            this->tss_view->reload_data();
10✔
1420
            this->tss_view->search_new_data();
10✔
1421
            break;
10✔
1422
    }
1423

1424
    return retval;
4,307✔
1425
}
4,307✔
1426

1427
void
1428
logfile_sub_source::text_update_marks(vis_bookmarks& bm)
2,207✔
1429
{
1430
    logfile* last_file = nullptr;
2,207✔
1431
    vis_line_t vl;
2,207✔
1432

1433
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,207✔
1434
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,207✔
1435
    auto& bm_files = bm[&BM_FILES];
2,207✔
1436

1437
    bm_warnings.clear();
2,207✔
1438
    bm_errors.clear();
2,207✔
1439
    bm_files.clear();
2,207✔
1440

1441
    std::vector<const bookmark_type_t*> used_marks;
2,207✔
1442
    for (const auto* bmt : {
11,035✔
1443
             &textview_curses::BM_USER,
1444
             &textview_curses::BM_USER_EXPR,
1445
             &textview_curses::BM_PARTITION,
1446
             &textview_curses::BM_META,
1447
         })
13,242✔
1448
    {
1449
        bm[bmt].clear();
8,828✔
1450
        if (!this->lss_user_marks[bmt].empty()) {
8,828✔
1451
            used_marks.emplace_back(bmt);
118✔
1452
        }
1453
    }
1454

1455
    for (; vl < (int) this->lss_filtered_index.size(); ++vl) {
19,555✔
1456
        const auto& orig_ic = this->lss_index[this->lss_filtered_index[vl]];
17,348✔
1457
        auto cl = orig_ic.value();
17,348✔
1458
        auto* lf = this->find_file_ptr(cl);
17,348✔
1459

1460
        for (const auto& bmt : used_marks) {
19,486✔
1461
            auto& user_mark = this->lss_user_marks[bmt];
2,138✔
1462
            if (user_mark.bv_tree.exists(orig_ic.value())) {
2,138✔
1463
                bm[bmt].insert_once(vl);
156✔
1464
            }
1465
        }
1466

1467
        if (lf != last_file) {
17,348✔
1468
            bm_files.insert_once(vl);
970✔
1469
        }
1470

1471
        switch (orig_ic.level()) {
17,348✔
1472
            case indexed_content::level_t::warning:
94✔
1473
                bm_warnings.insert_once(vl);
94✔
1474
                break;
94✔
1475

1476
            case indexed_content::level_t::error:
3,421✔
1477
                bm_errors.insert_once(vl);
3,421✔
1478
                break;
3,421✔
1479

1480
            default:
13,833✔
1481
                break;
13,833✔
1482
        }
1483

1484
        last_file = lf;
17,348✔
1485
    }
1486
}
2,207✔
1487

1488
void
1489
logfile_sub_source::text_filters_changed()
166✔
1490
{
1491
    this->lss_index_generation += 1;
166✔
1492

1493
    if (this->lss_line_meta_changed) {
166✔
1494
        this->invalidate_sql_filter();
24✔
1495
        this->lss_line_meta_changed = false;
24✔
1496
    }
1497

1498
    for (auto& ld : *this) {
285✔
1499
        auto* lf = ld->get_file_ptr();
119✔
1500

1501
        if (lf != nullptr) {
119✔
1502
            ld->ld_filter_state.clear_deleted_filter_state();
119✔
1503
            lf->reobserve_from(lf->begin()
119✔
1504
                               + ld->ld_filter_state.get_min_count(lf->size()));
119✔
1505
        }
1506
    }
1507

1508
    if (this->lss_force_rebuild) {
166✔
1509
        return;
×
1510
    }
1511

1512
    auto& vis_bm = this->tss_view->get_bookmarks();
166✔
1513
    uint32_t filtered_in_mask, filtered_out_mask;
1514

1515
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
166✔
1516

1517
    if (this->lss_index_delegate != nullptr) {
166✔
1518
        this->lss_index_delegate->index_start(*this);
166✔
1519
    }
1520
    vis_bm[&textview_curses::BM_USER_EXPR].clear();
166✔
1521

1522
    this->lss_filtered_index.clear();
166✔
1523
    for (size_t index_index = 0; index_index < this->lss_index.size();
1,497✔
1524
         index_index++)
1525
    {
1526
        auto cl = this->lss_index[index_index].value();
1,331✔
1527
        uint64_t line_number;
1528
        auto ld = this->find_data(cl, line_number);
1,331✔
1529

1530
        if (!(*ld)->is_visible()) {
1,331✔
1531
            continue;
213✔
1532
        }
1533

1534
        auto lf = (*ld)->get_file_ptr();
1,118✔
1535
        auto line_iter = lf->begin() + line_number;
1,118✔
1536

1537
        if (!this->tss_apply_filters
2,236✔
1538
            || (!(*ld)->ld_filter_state.excluded(
2,098✔
1539
                    filtered_in_mask, filtered_out_mask, line_number)
1540
                && this->check_extra_filters(ld, line_iter)))
980✔
1541
        {
1542
            auto eval_res = this->eval_sql_filter(
1543
                this->lss_marker_stmt.in(), ld, line_iter);
938✔
1544
            if (eval_res.isErr()) {
938✔
1545
                line_iter->set_expr_mark(false);
×
1546
            } else {
1547
                auto matched = eval_res.unwrap();
938✔
1548

1549
                line_iter->set_expr_mark(matched);
938✔
1550
                if (matched) {
938✔
1551
                    vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1552
                        vis_line_t(this->lss_filtered_index.size()));
×
1553
                    this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1554
                        .insert_once(cl);
×
1555
                }
1556
            }
1557
            this->lss_filtered_index.push_back(index_index);
938✔
1558
            if (this->lss_index_delegate != nullptr) {
938✔
1559
                this->lss_index_delegate->index_line(*this, lf, line_iter);
938✔
1560
            }
1561
        }
938✔
1562
    }
1563

1564
    if (this->lss_index_delegate != nullptr) {
166✔
1565
        this->lss_index_delegate->index_complete(*this);
166✔
1566
    }
1567

1568
    if (this->tss_view != nullptr) {
166✔
1569
        this->tss_view->reload_data();
166✔
1570
        this->tss_view->redo_search();
166✔
1571
    }
1572
}
1573

1574
std::optional<json_string>
1575
logfile_sub_source::text_row_details(const textview_curses& tc)
28✔
1576
{
1577
    if (this->lss_index.empty()) {
28✔
1578
        log_trace("logfile_sub_source::text_row_details empty");
1✔
1579
        return std::nullopt;
1✔
1580
    }
1581

1582
    auto ov_sel = tc.get_overlay_selection();
27✔
1583
    if (ov_sel.has_value()) {
27✔
1584
        auto* fos
1585
            = dynamic_cast<field_overlay_source*>(tc.get_overlay_source());
×
1586
        auto iter = fos->fos_row_to_field_meta.find(ov_sel.value());
×
1587
        if (iter != fos->fos_row_to_field_meta.end()) {
×
1588
            auto find_res = this->find_line_with_file(tc.get_top());
×
1589
            if (find_res) {
×
1590
                yajlpp_gen gen;
×
1591

1592
                {
1593
                    yajlpp_map root(gen);
×
1594

1595
                    root.gen("value");
×
1596
                    root.gen(iter->second.ri_value);
×
1597
                }
1598

1599
                return json_string(gen);
×
1600
            }
1601
        }
1602
    }
1603

1604
    return std::nullopt;
27✔
1605
}
1606

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

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

1700
std::optional<
1701
    std::pair<grep_proc_source<vis_line_t>*, grep_proc_sink<vis_line_t>*>>
1702
logfile_sub_source::get_grepper()
9✔
1703
{
1704
    return std::make_pair(
18✔
1705
        (grep_proc_source<vis_line_t>*) &this->lss_meta_grepper,
×
1706
        (grep_proc_sink<vis_line_t>*) &this->lss_meta_grepper);
9✔
1707
}
1708

1709
/**
1710
 * Functor for comparing the ld_file field of the logfile_data struct.
1711
 */
1712
struct logfile_data_eq {
1713
    explicit logfile_data_eq(std::shared_ptr<logfile> lf)
1,044✔
1714
        : lde_file(std::move(lf))
1,044✔
1715
    {
1716
    }
1,044✔
1717

1718
    bool operator()(
650✔
1719
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1720
    {
1721
        return this->lde_file == ld->get_file();
650✔
1722
    }
1723

1724
    std::shared_ptr<logfile> lde_file;
1725
};
1726

1727
bool
1728
logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
522✔
1729
{
1730
    iterator existing;
522✔
1731

1732
    require_lt(lf->size(), MAX_LINES_PER_FILE);
522✔
1733

1734
    existing = std::find_if(this->lss_files.begin(),
522✔
1735
                            this->lss_files.end(),
1736
                            logfile_data_eq(nullptr));
1,044✔
1737
    if (existing == this->lss_files.end()) {
522✔
1738
        if (this->lss_files.size() >= MAX_FILES) {
522✔
1739
            return false;
×
1740
        }
1741

1742
        auto ld = std::make_unique<logfile_data>(
1743
            this->lss_files.size(), this->get_filters(), lf);
522✔
1744
        ld->set_visibility(lf->get_open_options().loo_is_visible);
522✔
1745
        this->lss_files.push_back(std::move(ld));
522✔
1746
    } else {
522✔
1747
        (*existing)->set_file(lf);
×
1748
    }
1749

1750
    return true;
522✔
1751
}
1752

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

1762
        if (eval_res.isErr()) {
8✔
1763
            sqlite3_finalize(stmt);
1✔
1764
            return Err(eval_res.unwrapErr());
1✔
1765
        }
1766
    }
8✔
1767

1768
    for (auto& ld : *this) {
788✔
1769
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
17✔
1770
    }
1771

1772
    auto old_filter = this->get_sql_filter();
771✔
1773
    if (stmt != nullptr) {
771✔
1774
        auto new_filter
1775
            = std::make_shared<sql_filter>(*this, std::move(stmt_str), stmt);
7✔
1776

1777
        if (old_filter) {
7✔
1778
            auto existing_iter = std::find(this->tss_filters.begin(),
×
1779
                                           this->tss_filters.end(),
1780
                                           old_filter.value());
×
1781
            *existing_iter = new_filter;
×
1782
        } else {
1783
            this->tss_filters.add_filter(new_filter);
7✔
1784
        }
1785
    } else if (old_filter) {
771✔
1786
        this->tss_filters.delete_filter(old_filter.value()->get_id());
7✔
1787
    }
1788

1789
    return Ok();
771✔
1790
}
771✔
1791

1792
Result<void, lnav::console::user_message>
1793
logfile_sub_source::set_sql_marker(std::string stmt_str, sqlite3_stmt* stmt)
769✔
1794
{
1795
    static auto op = lnav_operation{"set_sql_marker"};
769✔
1796
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
769✔
1797
        auto top_cl = this->at(0_vl);
5✔
1798
        auto ld = this->find_data(top_cl);
5✔
1799
        auto eval_res
1800
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
5✔
1801

1802
        if (eval_res.isErr()) {
5✔
1803
            sqlite3_finalize(stmt);
×
1804
            return Err(eval_res.unwrapErr());
×
1805
        }
1806
    }
5✔
1807

1808
    auto op_guard = lnav_opid_guard::internal(op);
769✔
1809
    log_info("setting SQL marker: %s", stmt_str.c_str());
769✔
1810
    this->lss_marker_stmt_text = std::move(stmt_str);
769✔
1811
    this->lss_marker_stmt = stmt;
769✔
1812

1813
    if (this->tss_view == nullptr || this->lss_force_rebuild) {
769✔
1814
        log_info("skipping SQL marker update");
130✔
1815
        return Ok();
130✔
1816
    }
1817

1818
    auto& vis_bm = this->tss_view->get_bookmarks();
639✔
1819
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
639✔
1820
    auto& cl_marks_bv = this->lss_user_marks[&textview_curses::BM_USER_EXPR];
639✔
1821

1822
    expr_marks_bv.clear();
639✔
1823
    if (this->lss_index_delegate) {
639✔
1824
        this->lss_index_delegate->index_start(*this);
639✔
1825
    }
1826
    for (auto row = 0_vl; row < vis_line_t(this->lss_filtered_index.size());
1,072✔
1827
         row += 1_vl)
433✔
1828
    {
1829
        auto cl = this->at(row);
433✔
1830
        uint64_t line_number;
1831
        auto ld = this->find_data(cl, line_number);
433✔
1832

1833
        if (!(*ld)->is_visible()) {
433✔
1834
            continue;
1✔
1835
        }
1836
        auto ll = (*ld)->get_file()->begin() + line_number;
433✔
1837
        if (ll->is_continued() || ll->is_ignored()) {
433✔
1838
            continue;
1✔
1839
        }
1840
        auto eval_res
1841
            = this->eval_sql_filter(this->lss_marker_stmt.in(), ld, ll);
432✔
1842

1843
        if (eval_res.isErr()) {
432✔
1844
            ll->set_expr_mark(false);
×
1845
        } else {
1846
            auto matched = eval_res.unwrap();
432✔
1847

1848
            ll->set_expr_mark(matched);
432✔
1849
            if (matched) {
432✔
1850
                expr_marks_bv.insert_once(row);
22✔
1851
                cl_marks_bv.insert_once(cl);
22✔
1852
            }
1853
        }
1854
        if (this->lss_index_delegate) {
432✔
1855
            this->lss_index_delegate->index_line(
432✔
1856
                *this, (*ld)->get_file_ptr(), ll);
432✔
1857
        }
1858
    }
432✔
1859
    if (this->lss_index_delegate) {
639✔
1860
        this->lss_index_delegate->index_complete(*this);
639✔
1861
    }
1862
    log_info("SQL marker update complete");
639✔
1863

1864
    return Ok();
639✔
1865
}
769✔
1866

1867
Result<void, lnav::console::user_message>
1868
logfile_sub_source::set_preview_sql_filter(sqlite3_stmt* stmt)
764✔
1869
{
1870
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
764✔
1871
        auto top_cl = this->at(0_vl);
×
1872
        auto ld = this->find_data(top_cl);
×
1873
        auto eval_res
1874
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
×
1875

1876
        if (eval_res.isErr()) {
×
1877
            sqlite3_finalize(stmt);
×
1878
            return Err(eval_res.unwrapErr());
×
1879
        }
1880
    }
1881

1882
    this->lss_preview_filter_stmt = stmt;
764✔
1883

1884
    return Ok();
764✔
1885
}
1886

1887
Result<bool, lnav::console::user_message>
1888
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
15,358✔
1889
                                    iterator ld,
1890
                                    logfile::const_iterator ll)
1891
{
1892
    if (stmt == nullptr) {
15,358✔
1893
        return Ok(false);
29,486✔
1894
    }
1895

1896
    auto* lf = (*ld)->get_file_ptr();
615✔
1897
    char timestamp_buffer[64];
1898
    shared_buffer_ref raw_sbr;
615✔
1899
    logline_value_vector values;
615✔
1900
    auto& sbr = values.lvv_sbr;
615✔
1901
    lf->read_full_message(ll, sbr);
615✔
1902
    sbr.erase_ansi();
615✔
1903
    auto format = lf->get_format();
615✔
1904
    string_attrs_t sa;
615✔
1905
    auto line_number = std::distance(lf->cbegin(), ll);
615✔
1906
    format->annotate(lf, line_number, sa, values);
615✔
1907
    auto lffs = lf->get_format_file_state();
615✔
1908

1909
    sqlite3_reset(stmt);
615✔
1910
    sqlite3_clear_bindings(stmt);
615✔
1911

1912
    auto count = sqlite3_bind_parameter_count(stmt);
615✔
1913
    for (int lpc = 0; lpc < count; lpc++) {
1,242✔
1914
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
627✔
1915

1916
        if (name[0] == '$') {
627✔
1917
            const char* env_value;
1918

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

1977
                sqlite3_bind_text(stmt,
×
1978
                                  lpc + 1,
1979
                                  anno_str.c_str(),
1980
                                  anno_str.length(),
×
1981
                                  SQLITE_TRANSIENT);
1982
            }
1983
            continue;
×
1984
        }
1985
        if (strcmp(name, ":log_tags") == 0) {
616✔
1986
            const auto& bm = lf->get_bookmark_metadata();
9✔
1987
            auto line_number
1988
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
9✔
1989
            auto bm_iter = bm.find(line_number);
9✔
1990
            if (bm_iter != bm.end() && !bm_iter->second.bm_tags.empty()) {
9✔
1991
                const auto& meta = bm_iter->second;
1✔
1992
                yajlpp_gen gen;
1✔
1993

1994
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
1995

1996
                {
1997
                    yajlpp_array arr(gen);
1✔
1998

1999
                    for (const auto& str : meta.bm_tags) {
2✔
2000
                        arr.gen(str);
1✔
2001
                    }
2002
                }
1✔
2003

2004
                string_fragment sf = gen.to_string_fragment();
1✔
2005

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

2056
                sqlite3_bind_text(stmt,
32✔
2057
                                  lpc + 1,
2058
                                  sbr.get_data_at(sar.lr_start),
16✔
2059
                                  sar.length(),
2060
                                  SQLITE_STATIC);
2061
            } else {
2062
                sqlite3_bind_null(stmt, lpc + 1);
×
2063
            }
2064
            continue;
16✔
2065
        }
16✔
2066
        if (strcmp(name, ":log_opid") == 0) {
581✔
2067
            if (values.lvv_opid_value) {
×
2068
                sqlite3_bind_text(stmt,
×
2069
                                  lpc + 1,
2070
                                  values.lvv_opid_value->c_str(),
2071
                                  values.lvv_opid_value->length(),
×
2072
                                  SQLITE_STATIC);
2073
            } else {
2074
                sqlite3_bind_null(stmt, lpc + 1);
×
2075
            }
2076
            continue;
×
2077
        }
2078
        if (strcmp(name, ":log_raw_text") == 0) {
581✔
2079
            auto res = lf->read_raw_message(ll);
×
2080

2081
            if (res.isOk()) {
×
2082
                raw_sbr = res.unwrap();
×
2083
                sqlite3_bind_text(stmt,
×
2084
                                  lpc + 1,
2085
                                  raw_sbr.get_data(),
2086
                                  raw_sbr.length(),
×
2087
                                  SQLITE_STATIC);
2088
            }
2089
            continue;
×
2090
        }
2091
        for (const auto& lv : values.lvv_values) {
6,782✔
2092
            if (lv.lv_meta.lvm_name != &name[1]) {
6,777✔
2093
                continue;
6,201✔
2094
            }
2095

2096
            switch (lv.lv_meta.lvm_kind) {
576✔
2097
                case value_kind_t::VALUE_BOOLEAN:
×
2098
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
2099
                    break;
×
2100
                case value_kind_t::VALUE_FLOAT:
×
2101
                    sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
2102
                    break;
×
2103
                case value_kind_t::VALUE_INTEGER:
436✔
2104
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
436✔
2105
                    break;
436✔
2106
                case value_kind_t::VALUE_NULL:
×
2107
                    sqlite3_bind_null(stmt, lpc + 1);
×
2108
                    break;
×
2109
                default:
140✔
2110
                    sqlite3_bind_text(stmt,
140✔
2111
                                      lpc + 1,
2112
                                      lv.text_value(),
2113
                                      lv.text_length(),
140✔
2114
                                      SQLITE_TRANSIENT);
2115
                    break;
140✔
2116
            }
2117
            break;
576✔
2118
        }
2119
    }
2120

2121
    auto step_res = sqlite3_step(stmt);
615✔
2122

2123
    sqlite3_reset(stmt);
615✔
2124
    sqlite3_clear_bindings(stmt);
615✔
2125
    switch (step_res) {
615✔
2126
        case SQLITE_OK:
474✔
2127
        case SQLITE_DONE:
2128
            return Ok(false);
948✔
2129
        case SQLITE_ROW:
140✔
2130
            return Ok(true);
280✔
2131
        default:
1✔
2132
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2133
    }
2134
}
615✔
2135

2136
bool
2137
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
14,782✔
2138
{
2139
    if (this->lss_marked_only) {
14,782✔
2140
        auto found_mark = ll->is_marked() || ll->is_expr_marked();
6✔
2141
        auto to_start_ll = ll;
6✔
2142
        while (!found_mark && to_start_ll->is_continued()) {
6✔
2143
            if (to_start_ll->is_marked() || to_start_ll->is_expr_marked()) {
×
2144
                found_mark = true;
×
2145
            }
2146
            --to_start_ll;
×
2147
        }
2148
        auto to_end_ll = std::next(ll);
6✔
2149
        while (!found_mark && to_end_ll != (*ld)->get_file_ptr()->end()
10✔
2150
               && to_end_ll->is_continued())
11✔
2151
        {
2152
            if (to_end_ll->is_marked() || to_end_ll->is_expr_marked()) {
1✔
2153
                found_mark = true;
1✔
2154
            }
2155
            ++to_end_ll;
1✔
2156
        }
2157
        if (!found_mark) {
6✔
2158
            return false;
3✔
2159
        }
2160
    }
2161

2162
    if (ll->get_msg_level() < this->lss_min_log_level) {
14,779✔
2163
        return false;
2✔
2164
    }
2165

2166
    if (*ll < this->ttt_min_row_time) {
14,777✔
2167
        return false;
36✔
2168
    }
2169

2170
    if (!(*ll <= this->ttt_max_row_time)) {
14,741✔
2171
        return false;
4✔
2172
    }
2173

2174
    return true;
14,737✔
2175
}
2176

2177
void
2178
logfile_sub_source::invalidate_sql_filter()
24✔
2179
{
2180
    for (auto& ld : *this) {
48✔
2181
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
24✔
2182
    }
2183
}
24✔
2184

2185
void
2186
logfile_sub_source::text_mark(const bookmark_type_t* bm,
148✔
2187
                              vis_line_t line,
2188
                              bool added)
2189
{
2190
    if (line >= (int) this->lss_index.size()) {
148✔
2191
        return;
×
2192
    }
2193

2194
    auto cl = this->at(line);
148✔
2195

2196
    if (bm == &textview_curses::BM_USER) {
148✔
2197
        auto* ll = this->find_line(cl);
59✔
2198

2199
        ll->set_mark(added);
59✔
2200
    }
2201
    if (added) {
148✔
2202
        this->lss_user_marks[bm].insert_once(cl);
67✔
2203
    } else {
2204
        this->lss_user_marks[bm].erase(cl);
81✔
2205
    }
2206
    if (bm == &textview_curses::BM_META
148✔
2207
        && this->lss_meta_grepper.gps_proc != nullptr)
16✔
2208
    {
2209
        this->tss_view->search_range(line, line + 1_vl);
1✔
2210
        this->tss_view->search_new_data();
1✔
2211
    }
2212
}
2213

2214
void
2215
logfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
39✔
2216
{
2217
    if (bm == &textview_curses::BM_USER) {
39✔
2218
        for (auto iter = this->lss_user_marks[bm].bv_tree.begin();
6✔
2219
             iter != this->lss_user_marks[bm].bv_tree.end();
6✔
2220
             ++iter)
×
2221
        {
2222
            this->find_line(*iter)->set_mark(false);
×
2223
        }
2224
    }
2225
    this->lss_user_marks[bm].clear();
39✔
2226
}
39✔
2227

2228
void
2229
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
522✔
2230
{
2231
    auto iter = std::find_if(
522✔
2232
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
1,044✔
2233
    if (iter != this->lss_files.end()) {
522✔
2234
        int file_index = iter - this->lss_files.begin();
522✔
2235

2236
        (*iter)->clear();
522✔
2237
        for (auto& bv : this->lss_user_marks) {
4,698✔
2238
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
4,176✔
2239
            auto mark_end
2240
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
4,176✔
2241
            auto file_range = bv.equal_range(mark_curr, mark_end);
4,176✔
2242

2243
            if (file_range.first != file_range.second) {
4,176✔
2244
                auto to_del = std::vector<content_line_t>{};
69✔
2245
                for (auto file_iter = file_range.first;
69✔
2246
                     file_iter != file_range.second;
178✔
2247
                     ++file_iter)
109✔
2248
                {
2249
                    to_del.emplace_back(*file_iter);
109✔
2250
                }
2251

2252
                for (auto cl : to_del) {
178✔
2253
                    bv.erase(cl);
109✔
2254
                }
2255
            }
69✔
2256
        }
2257

2258
        this->lss_force_rebuild = true;
522✔
2259
    }
2260
    while (!this->lss_files.empty()) {
1,044✔
2261
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
576✔
2262
            this->lss_files.pop_back();
522✔
2263
        } else {
2264
            break;
54✔
2265
        }
2266
    }
2267
    this->lss_token_file = nullptr;
522✔
2268
}
522✔
2269

2270
std::optional<vis_line_t>
2271
logfile_sub_source::find_from_content(content_line_t cl)
5✔
2272
{
2273
    content_line_t line = cl;
5✔
2274
    std::shared_ptr<logfile> lf = this->find(line);
5✔
2275

2276
    if (lf != nullptr) {
5✔
2277
        auto ll_iter = lf->begin() + line;
5✔
2278
        auto& ll = *ll_iter;
5✔
2279
        auto vis_start_opt = this->find_from_time(ll.get_timeval());
5✔
2280

2281
        if (!vis_start_opt) {
5✔
2282
            return std::nullopt;
×
2283
        }
2284

2285
        auto vis_start = *vis_start_opt;
5✔
2286

2287
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2288
            content_line_t guess_cl = this->at(vis_start);
5✔
2289

2290
            if (cl == guess_cl) {
5✔
2291
                return vis_start;
5✔
2292
            }
2293

2294
            auto guess_line = this->find_line(guess_cl);
×
2295

2296
            if (!guess_line || ll < *guess_line) {
×
2297
                return std::nullopt;
×
2298
            }
2299

2300
            ++vis_start;
×
2301
        }
2302
    }
2303

2304
    return std::nullopt;
×
2305
}
5✔
2306

2307
void
2308
logfile_sub_source::reload_index_delegate()
641✔
2309
{
2310
    if (this->lss_index_delegate == nullptr) {
641✔
2311
        return;
×
2312
    }
2313

2314
    this->lss_index_delegate->index_start(*this);
641✔
2315
    for (const auto index : this->lss_filtered_index) {
669✔
2316
        auto cl = this->lss_index[index].value();
28✔
2317
        uint64_t line_number;
2318
        auto ld = this->find_data(cl, line_number);
28✔
2319
        std::shared_ptr<logfile> lf = (*ld)->get_file();
28✔
2320

2321
        this->lss_index_delegate->index_line(
28✔
2322
            *this, lf.get(), lf->begin() + line_number);
28✔
2323
    }
28✔
2324
    this->lss_index_delegate->index_complete(*this);
641✔
2325
}
2326

2327
std::optional<std::shared_ptr<text_filter>>
2328
logfile_sub_source::get_sql_filter()
2,982✔
2329
{
2330
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
2,982✔
2331
               return filt->get_index() == 0;
359✔
2332
           })
2333
        | lnav::itertools::deref();
5,964✔
2334
}
2335

2336
void
2337
log_location_history::loc_history_append(vis_line_t top)
95✔
2338
{
2339
    if (top < 0_vl || top >= vis_line_t(this->llh_log_source.text_line_count()))
95✔
2340
    {
2341
        return;
1✔
2342
    }
2343

2344
    auto cl = this->llh_log_source.at(top);
94✔
2345

2346
    auto iter = this->llh_history.begin();
94✔
2347
    iter += this->llh_history.size() - this->lh_history_position;
94✔
2348
    this->llh_history.erase_from(iter);
94✔
2349
    this->lh_history_position = 0;
94✔
2350
    this->llh_history.push_back(cl);
94✔
2351
}
2352

2353
std::optional<vis_line_t>
2354
log_location_history::loc_history_back(vis_line_t current_top)
2✔
2355
{
2356
    while (this->lh_history_position < this->llh_history.size()) {
2✔
2357
        auto iter = this->llh_history.rbegin();
2✔
2358

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

2361
        if (this->lh_history_position == 0 && vis_for_pos != current_top) {
2✔
2362
            return vis_for_pos;
2✔
2363
        }
2364

2365
        if ((this->lh_history_position + 1) >= this->llh_history.size()) {
2✔
2366
            break;
×
2367
        }
2368

2369
        this->lh_history_position += 1;
2✔
2370

2371
        iter += this->lh_history_position;
2✔
2372

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

2375
        if (vis_for_pos) {
2✔
2376
            return vis_for_pos;
2✔
2377
        }
2378
    }
2379

2380
    return std::nullopt;
×
2381
}
2382

2383
std::optional<vis_line_t>
2384
log_location_history::loc_history_forward(vis_line_t current_top)
1✔
2385
{
2386
    while (this->lh_history_position > 0) {
1✔
2387
        this->lh_history_position -= 1;
1✔
2388

2389
        auto iter = this->llh_history.rbegin();
1✔
2390

2391
        iter += this->lh_history_position;
1✔
2392

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

2395
        if (vis_for_pos) {
1✔
2396
            return vis_for_pos;
1✔
2397
        }
2398
    }
2399

2400
    return std::nullopt;
×
2401
}
2402

2403
bool
2404
sql_filter::matches(std::optional<line_source> ls_opt,
124✔
2405
                    const shared_buffer_ref& line)
2406
{
2407
    if (!ls_opt) {
124✔
2408
        return false;
×
2409
    }
2410

2411
    auto ls = ls_opt;
124✔
2412

2413
    if (!ls->ls_line->is_message()) {
124✔
2414
        return false;
3✔
2415
    }
2416
    if (this->sf_filter_stmt == nullptr) {
121✔
2417
        return false;
×
2418
    }
2419

2420
    auto lfp = ls->ls_file.shared_from_this();
121✔
2421
    auto ld = this->sf_log_source.find_data_i(lfp);
121✔
2422
    if (ld == this->sf_log_source.end()) {
121✔
2423
        return false;
×
2424
    }
2425

2426
    auto eval_res = this->sf_log_source.eval_sql_filter(
121✔
2427
        this->sf_filter_stmt, ld, ls->ls_line);
121✔
2428
    if (eval_res.unwrapOr(true)) {
121✔
2429
        return false;
74✔
2430
    }
2431

2432
    return true;
47✔
2433
}
121✔
2434

2435
std::string
2436
sql_filter::to_command() const
×
2437
{
2438
    return fmt::format(FMT_STRING("filter-expr {}"), this->lf_id);
×
2439
}
2440

2441
std::optional<line_info>
2442
logfile_sub_source::meta_grepper::grep_value_for_line(vis_line_t line,
×
2443
                                                      std::string& value_out)
2444
{
2445
    auto line_meta_opt = this->lmg_source.find_bookmark_metadata(line);
×
2446
    if (!line_meta_opt) {
×
2447
        value_out.clear();
×
2448
    } else {
2449
        auto& bm = *(line_meta_opt.value());
×
2450

2451
        {
2452
            md2attr_line mdal;
×
2453

2454
            auto parse_res = md4cpp::parse(bm.bm_comment, mdal);
×
2455
            if (parse_res.isOk()) {
×
2456
                value_out.append(parse_res.unwrap().get_string());
×
2457
            } else {
2458
                value_out.append(bm.bm_comment);
×
2459
            }
2460
        }
2461

2462
        value_out.append("\x1c");
×
2463
        for (const auto& tag : bm.bm_tags) {
×
2464
            value_out.append(tag);
×
2465
            value_out.append("\x1c");
×
2466
        }
2467
        value_out.append("\x1c");
×
2468
        for (const auto& pair : bm.bm_annotations.la_pairs) {
×
2469
            value_out.append(pair.first);
×
2470
            value_out.append("\x1c");
×
2471

2472
            md2attr_line mdal;
×
2473

2474
            auto parse_res = md4cpp::parse(pair.second, mdal);
×
2475
            if (parse_res.isOk()) {
×
2476
                value_out.append(parse_res.unwrap().get_string());
×
2477
            } else {
2478
                value_out.append(pair.second);
×
2479
            }
2480
            value_out.append("\x1c");
×
2481
        }
2482
        value_out.append("\x1c");
×
2483
        value_out.append(bm.bm_opid);
×
2484
    }
2485

2486
    if (!this->lmg_done) {
×
2487
        return line_info{};
×
2488
    }
2489

2490
    return std::nullopt;
×
2491
}
2492

2493
vis_line_t
2494
logfile_sub_source::meta_grepper::grep_initial_line(vis_line_t start,
×
2495
                                                    vis_line_t highest)
2496
{
2497
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2498
    auto& bv = bm[&textview_curses::BM_META];
×
2499

2500
    if (bv.empty()) {
×
2501
        return -1_vl;
×
2502
    }
2503
    return *bv.bv_tree.begin();
×
2504
}
2505

2506
void
2507
logfile_sub_source::meta_grepper::grep_next_line(vis_line_t& line)
×
2508
{
2509
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2510
    auto& bv = bm[&textview_curses::BM_META];
×
2511

2512
    auto line_opt = bv.next(vis_line_t(line));
×
2513
    if (!line_opt) {
×
2514
        this->lmg_done = true;
×
2515
    }
2516
    line = line_opt.value_or(-1_vl);
×
2517
}
2518

2519
void
2520
logfile_sub_source::meta_grepper::grep_begin(grep_proc<vis_line_t>& gp,
23✔
2521
                                             vis_line_t start,
2522
                                             vis_line_t stop)
2523
{
2524
    this->lmg_source.quiesce();
23✔
2525

2526
    this->lmg_source.tss_view->grep_begin(gp, start, stop);
23✔
2527
}
23✔
2528

2529
void
2530
logfile_sub_source::meta_grepper::grep_end(grep_proc<vis_line_t>& gp)
23✔
2531
{
2532
    this->lmg_source.tss_view->grep_end(gp);
23✔
2533
}
23✔
2534

2535
void
2536
logfile_sub_source::meta_grepper::grep_match(grep_proc<vis_line_t>& gp,
3✔
2537
                                             vis_line_t line)
2538
{
2539
    this->lmg_source.tss_view->grep_match(gp, line);
3✔
2540
}
3✔
2541

2542
static std::vector<breadcrumb::possibility>
2543
timestamp_poss()
11✔
2544
{
2545
    const static std::vector<breadcrumb::possibility> retval = {
2546
        breadcrumb::possibility{"-1 day"},
2547
        breadcrumb::possibility{"-1h"},
2548
        breadcrumb::possibility{"-30m"},
2549
        breadcrumb::possibility{"-15m"},
2550
        breadcrumb::possibility{"-5m"},
2551
        breadcrumb::possibility{"-1m"},
2552
        breadcrumb::possibility{"+1m"},
2553
        breadcrumb::possibility{"+5m"},
2554
        breadcrumb::possibility{"+15m"},
2555
        breadcrumb::possibility{"+30m"},
2556
        breadcrumb::possibility{"+1h"},
2557
        breadcrumb::possibility{"+1 day"},
2558
    };
95✔
2559

2560
    return retval;
11✔
2561
}
24✔
2562

2563
static attr_line_t
2564
to_display(const std::shared_ptr<logfile>& lf)
29✔
2565
{
2566
    attr_line_t retval;
29✔
2567

2568
    if (lf->get_open_options().loo_piper) {
29✔
2569
        if (!lf->get_open_options().loo_piper->is_finished()) {
×
2570
            retval.append("\u21bb "_list_glyph);
×
2571
        }
2572
    }
2573
    retval.append(lf->get_unique_path());
29✔
2574

2575
    return retval;
29✔
2576
}
×
2577

2578
void
2579
logfile_sub_source::text_crumbs_for_line(int line,
27✔
2580
                                         std::vector<breadcrumb::crumb>& crumbs)
2581
{
2582
    text_sub_source::text_crumbs_for_line(line, crumbs);
27✔
2583

2584
    if (this->lss_filtered_index.empty()) {
27✔
2585
        return;
9✔
2586
    }
2587

2588
    auto vl = vis_line_t(line);
18✔
2589
    auto bmc = this->get_bookmark_metadata_context(
18✔
2590
        vl, bookmark_metadata::categories::partition);
2591
    if (bmc.bmc_current_metadata) {
18✔
2592
        const auto& name = bmc.bmc_current_metadata.value()->bm_name;
1✔
2593
        auto key = text_anchors::to_anchor_string(name);
1✔
2594
        auto display = attr_line_t()
1✔
2595
                           .append("\u2291 "_symbol)
1✔
2596
                           .append(lnav::roles::variable(name))
2✔
2597
                           .move();
1✔
2598
        crumbs.emplace_back(
1✔
2599
            key,
2600
            display,
2601
            [this]() -> std::vector<breadcrumb::possibility> {
×
2602
                auto& vb = this->tss_view->get_bookmarks();
1✔
2603
                const auto& bv = vb[&textview_curses::BM_PARTITION];
1✔
2604
                std::vector<breadcrumb::possibility> retval;
1✔
2605

2606
                for (const auto& vl : bv.bv_tree) {
2✔
2607
                    auto meta_opt = this->find_bookmark_metadata(vl);
1✔
2608
                    if (!meta_opt || meta_opt.value()->bm_name.empty()) {
1✔
2609
                        continue;
×
2610
                    }
2611

2612
                    const auto& name = meta_opt.value()->bm_name;
1✔
2613
                    retval.emplace_back(text_anchors::to_anchor_string(name),
1✔
2614
                                        name);
2615
                }
2616

2617
                return retval;
1✔
2618
            },
×
2619
            [ec = this->lss_exec_context](const auto& part) {
1✔
2620
                auto cmd = fmt::format(FMT_STRING(":goto {}"),
×
2621
                                       part.template get<std::string>());
2622
                ec->execute(INTERNAL_SRC_LOC, cmd);
×
2623
            });
×
2624
    }
1✔
2625

2626
    auto line_pair_opt = this->find_line_with_file(vl);
18✔
2627
    if (!line_pair_opt) {
18✔
2628
        return;
×
2629
    }
2630
    auto line_pair = line_pair_opt.value();
18✔
2631
    auto& lf = line_pair.first;
18✔
2632
    auto format = lf->get_format();
18✔
2633
    char ts[64];
2634

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

2637
    crumbs.emplace_back(std::string(ts),
18✔
2638
                        timestamp_poss,
2639
                        [ec = this->lss_exec_context](const auto& ts) {
18✔
2640
                            auto cmd
×
2641
                                = fmt::format(FMT_STRING(":goto {}"),
×
2642
                                              ts.template get<std::string>());
2643
                            ec->execute(INTERNAL_SRC_LOC, cmd);
×
2644
                        });
×
2645
    crumbs.back().c_expected_input
18✔
2646
        = breadcrumb::crumb::expected_input_t::anything;
18✔
2647
    crumbs.back().c_search_placeholder = "(Enter an absolute or relative time)";
18✔
2648

2649
    auto format_name = format->get_name().to_string();
18✔
2650

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

2680
            ec->execute_with(
×
2681
                INTERNAL_SRC_LOC,
×
2682
                MOVE_STMT,
2683
                std::make_pair("format_name",
2684
                               format_name.template get<std::string>()));
2685
        });
×
2686

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

2715
            ec->execute_with(
×
2716
                INTERNAL_SRC_LOC,
×
2717
                MOVE_STMT,
2718
                std::make_pair("uniq_path",
2719
                               uniq_path.template get<std::string>()));
2720
        });
×
2721

2722
    shared_buffer sb;
18✔
2723
    logline_value_vector values;
18✔
2724
    auto& sbr = values.lvv_sbr;
18✔
2725

2726
    lf->read_full_message(msg_start_iter, sbr);
18✔
2727
    attr_line_t al(to_string(sbr));
18✔
2728
    if (!sbr.get_metadata().m_valid_utf) {
18✔
2729
        scrub_to_utf8(&al.al_string[0], al.al_string.length());
×
2730
    }
2731
    if (sbr.get_metadata().m_has_ansi) {
18✔
2732
        // bleh
2733
        scrub_ansi_string(al.get_string(), &al.al_attrs);
×
2734
        sbr.share(sb, al.al_string.data(), al.al_string.size());
×
2735
    }
2736
    format->annotate(lf.get(), file_line_number, al.get_attrs(), values);
18✔
2737

2738
    {
2739
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
30✔
2740
          SET selection = ifnull(
2741
            (SELECT log_line FROM all_logs WHERE log_thread_id = $tid LIMIT 1),
2742
            (SELECT raise_error('Could not find thread ID: ' || $tid,
2743
                                'The corresponding log messages might have been filtered out')))
2744
          WHERE name = 'log'
2745
        )";
2746
        static const std::string ELLIPSIS = "\u22ef";
30✔
2747

2748
        auto tid_display = values.lvv_thread_id_value.has_value()
18✔
2749
            ? lnav::roles::identifier(values.lvv_thread_id_value.value())
1✔
2750
            : lnav::roles::hidden(ELLIPSIS);
19✔
2751
        crumbs.emplace_back(
18✔
2752
            values.lvv_thread_id_value.has_value()
18✔
2753
                ? values.lvv_thread_id_value.value()
53✔
2754
                : "",
2755
            attr_line_t().append("\U0001f9f5 ").append(tid_display),
36✔
NEW
2756
            [this]() -> std::vector<breadcrumb::possibility> {
×
2757
                std::set<std::string> poss_strs;
11✔
2758

2759
                for (const auto& file_data : this->lss_files) {
22✔
2760
                    if (file_data->get_file_ptr() == nullptr) {
11✔
NEW
2761
                        continue;
×
2762
                    }
2763
                    safe::ReadAccess<logfile::safe_thread_id_state> r_tid_map(
2764
                        file_data->get_file_ptr()->get_thread_ids());
11✔
2765

2766
                    for (const auto& pair : r_tid_map->ltis_tid_ranges) {
24✔
2767
                        poss_strs.emplace(pair.first.to_string());
13✔
2768
                    }
2769
                }
11✔
2770

2771
                std::vector<breadcrumb::possibility> retval;
11✔
2772

2773
                std::transform(poss_strs.begin(),
11✔
2774
                               poss_strs.end(),
2775
                               std::back_inserter(retval),
2776
                               [](const auto& tid_str) {
13✔
2777
                                   return breadcrumb::possibility(tid_str);
13✔
2778
                               });
2779

2780
                return retval;
22✔
2781
            },
11✔
2782
            [ec = this->lss_exec_context](const auto& tid) {
18✔
NEW
2783
                ec->execute_with(
×
NEW
2784
                    INTERNAL_SRC_LOC,
×
2785
                    MOVE_STMT,
2786
                    std::make_pair("tid", tid.template get<std::string>()));
NEW
2787
            });
×
2788
    }
18✔
2789

2790
    {
2791
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
30✔
2792
          SET selection = ifnull(
2793
            (SELECT log_line FROM all_logs WHERE log_opid = $opid LIMIT 1),
2794
            (SELECT raise_error('Could not find opid: ' || $opid,
2795
                                'The corresponding log messages might have been filtered out')))
2796
          WHERE name = 'log'
2797
        )";
2798
        static const std::string ELLIPSIS = "\u22ef";
30✔
2799

2800
        auto opid_display = values.lvv_opid_value.has_value()
18✔
2801
            ? lnav::roles::identifier(values.lvv_opid_value.value())
10✔
2802
            : lnav::roles::hidden(ELLIPSIS);
28✔
2803
        crumbs.emplace_back(
36✔
2804
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
44✔
2805
                                              : "",
2806
            attr_line_t().append(opid_display),
18✔
2807
            [this]() -> std::vector<breadcrumb::possibility> {
×
2808
                std::unordered_set<std::string> poss_strs;
11✔
2809

2810
                for (const auto& file_data : this->lss_files) {
22✔
2811
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2812
                        continue;
×
2813
                    }
2814
                    safe::ReadAccess<logfile::safe_opid_state> r_opid_map(
2815
                        file_data->get_file_ptr()->get_opids());
11✔
2816

2817
                    poss_strs.reserve(poss_strs.size()
22✔
2818
                                      + r_opid_map->los_opid_ranges.size());
11✔
2819
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
786✔
2820
                        poss_strs.insert(pair.first.to_string());
775✔
2821
                    }
2822
                }
11✔
2823

2824
                std::vector<breadcrumb::possibility> retval;
11✔
2825
                retval.reserve(poss_strs.size());
11✔
2826

2827
                std::transform(poss_strs.begin(),
11✔
2828
                               poss_strs.end(),
2829
                               std::back_inserter(retval),
2830
                               [](const auto& opid_str) {
775✔
2831
                                   return breadcrumb::possibility(opid_str);
775✔
2832
                               });
2833

2834
                return retval;
22✔
2835
            },
11✔
2836
            [ec = this->lss_exec_context](const auto& opid) {
18✔
2837
                ec->execute_with(
×
2838
                    INTERNAL_SRC_LOC,
×
2839
                    MOVE_STMT,
2840
                    std::make_pair("opid", opid.template get<std::string>()));
2841
            });
×
2842
    }
18✔
2843

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

2870
        const auto initial_size = crumbs.size();
9✔
2871
        auto sf_body
2872
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
9✔
2873
                           body_opt->saw_string_attr->sa_range.lr_end);
9✔
2874
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
9✔
2875
        file_off_t line_end_offset = sf.length();
9✔
2876
        ssize_t line_number = 0;
9✔
2877

2878
        for (const auto& sf_line : sf_body.split_lines()) {
18✔
2879
            if (line_number >= msg_line_number) {
12✔
2880
                line_end_offset = line_offset + sf_line.length();
3✔
2881
                break;
3✔
2882
            }
2883
            line_number += 1;
9✔
2884
            line_offset += sf_line.length();
9✔
2885
        }
9✔
2886

2887
        this->lss_token_meta.m_sections_tree.visit_overlapping(
9✔
2888
            line_offset,
2889
            line_end_offset,
2890
            [this,
2✔
2891
             initial_size,
2892
             meta = &this->lss_token_meta,
9✔
2893
             &crumbs,
2894
             line_from_top](const auto& iv) {
2895
                auto path = crumbs | lnav::itertools::skip(initial_size)
6✔
2896
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
4✔
2897
                    | lnav::itertools::append(iv.value);
2✔
2898
                auto curr_node = lnav::document::hier_node::lookup_path(
2✔
2899
                    meta->m_sections_root.get(), path);
2✔
2900

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

2929
                    crumbs.back().c_expected_input
×
2930
                        = curr_node.value()
×
2931
                              ->hn_parent->hn_named_children.empty()
×
2932
                        ? breadcrumb::crumb::expected_input_t::index
×
2933
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
2934
                    crumbs.back().with_possible_range(
×
2935
                        node | lnav::itertools::map([](const auto hn) {
×
2936
                            return hn->hn_parent->hn_children.size();
×
2937
                        })
2938
                        | lnav::itertools::unwrap_or(size_t{0}));
×
2939
                }
2940
            });
2✔
2941

2942
        auto path = crumbs | lnav::itertools::skip(initial_size)
18✔
2943
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
18✔
2944
        auto node = lnav::document::hier_node::lookup_path(
9✔
2945
            this->lss_token_meta.m_sections_root.get(), path);
9✔
2946

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

2979
void
2980
logfile_sub_source::quiesce()
42✔
2981
{
2982
    for (auto& ld : this->lss_files) {
84✔
2983
        auto* lf = ld->get_file_ptr();
42✔
2984

2985
        if (lf == nullptr) {
42✔
2986
            continue;
×
2987
        }
2988

2989
        lf->quiesce();
42✔
2990
    }
2991
}
42✔
2992

2993
bookmark_metadata&
2994
logfile_sub_source::get_bookmark_metadata(content_line_t cl)
25✔
2995
{
2996
    auto line_pair = this->find_line_with_file(cl).value();
25✔
2997
    auto line_number = static_cast<uint32_t>(
2998
        std::distance(line_pair.first->begin(), line_pair.second));
25✔
2999

3000
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
3001
}
25✔
3002

3003
logfile_sub_source::bookmark_metadata_context
3004
logfile_sub_source::get_bookmark_metadata_context(
4,107✔
3005
    vis_line_t vl, bookmark_metadata::categories desired) const
3006
{
3007
    const auto& vb = this->tss_view->get_bookmarks();
4,107✔
3008
    const auto& bv = vb[desired == bookmark_metadata::categories::partition
3009
                            ? &textview_curses::BM_PARTITION
3010
                            : &textview_curses::BM_META];
4,107✔
3011
    auto vl_iter = bv.bv_tree.lower_bound(vl + 1_vl);
4,107✔
3012

3013
    std::optional<vis_line_t> next_line;
4,107✔
3014
    for (auto next_vl_iter = vl_iter; next_vl_iter != bv.bv_tree.end();
4,107✔
3015
         ++next_vl_iter)
×
3016
    {
3017
        auto bm_opt = this->find_bookmark_metadata(*next_vl_iter);
2✔
3018
        if (!bm_opt) {
2✔
3019
            continue;
×
3020
        }
3021

3022
        if (bm_opt.value()->has(desired)) {
2✔
3023
            next_line = *next_vl_iter;
2✔
3024
            break;
2✔
3025
        }
3026
    }
3027
    if (vl_iter == bv.bv_tree.begin()) {
4,107✔
3028
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
4,091✔
3029
    }
3030

3031
    --vl_iter;
16✔
3032
    while (true) {
3033
        auto bm_opt = this->find_bookmark_metadata(*vl_iter);
16✔
3034
        if (bm_opt) {
16✔
3035
            if (bm_opt.value()->has(desired)) {
13✔
3036
                return bookmark_metadata_context{
3037
                    *vl_iter, bm_opt.value(), next_line};
16✔
3038
            }
3039
        }
3040

3041
        if (vl_iter == bv.bv_tree.begin()) {
3✔
3042
            return bookmark_metadata_context{
3043
                std::nullopt, std::nullopt, next_line};
3✔
3044
        }
3045
        --vl_iter;
×
3046
    }
3047
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3048
}
3049

3050
std::optional<bookmark_metadata*>
3051
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
23,223✔
3052
{
3053
    auto line_pair = this->find_line_with_file(cl).value();
23,223✔
3054
    auto line_number = static_cast<uint32_t>(
3055
        std::distance(line_pair.first->begin(), line_pair.second));
23,223✔
3056

3057
    auto& bm = line_pair.first->get_bookmark_metadata();
23,223✔
3058
    auto bm_iter = bm.find(line_number);
23,223✔
3059
    if (bm_iter == bm.end()) {
23,223✔
3060
        return std::nullopt;
22,861✔
3061
    }
3062

3063
    return &bm_iter->second;
362✔
3064
}
23,223✔
3065

3066
void
3067
logfile_sub_source::erase_bookmark_metadata(content_line_t cl)
26✔
3068
{
3069
    auto line_pair = this->find_line_with_file(cl).value();
26✔
3070
    auto line_number = static_cast<uint32_t>(
3071
        std::distance(line_pair.first->begin(), line_pair.second));
26✔
3072

3073
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
3074
    auto bm_iter = bm.find(line_number);
26✔
3075
    if (bm_iter != bm.end()) {
26✔
3076
        bm.erase(bm_iter);
6✔
3077
    }
3078
}
26✔
3079

3080
void
3081
logfile_sub_source::clear_bookmark_metadata()
6✔
3082
{
3083
    for (auto& ld : *this) {
14✔
3084
        if (ld->get_file_ptr() == nullptr) {
8✔
3085
            continue;
×
3086
        }
3087

3088
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3089
    }
3090
}
6✔
3091

3092
void
3093
logfile_sub_source::increase_line_context()
×
3094
{
3095
    auto old_context = this->lss_line_context;
×
3096

3097
    switch (this->lss_line_context) {
×
3098
        case line_context_t::filename:
×
3099
            // nothing to do
3100
            break;
×
3101
        case line_context_t::basename:
×
3102
            this->lss_line_context = line_context_t::filename;
×
3103
            break;
×
3104
        case line_context_t::none:
×
3105
            this->lss_line_context = line_context_t::basename;
×
3106
            break;
×
3107
        case line_context_t::time_column:
×
3108
            this->lss_line_context = line_context_t::none;
×
3109
            break;
×
3110
    }
3111
    if (old_context != this->lss_line_context) {
×
3112
        this->clear_line_size_cache();
×
3113
    }
3114
}
3115

3116
bool
3117
logfile_sub_source::decrease_line_context()
×
3118
{
3119
    static const auto& cfg
3120
        = injector::get<const logfile_sub_source_ns::config&>();
×
3121
    auto old_context = this->lss_line_context;
×
3122

3123
    switch (this->lss_line_context) {
×
3124
        case line_context_t::filename:
×
3125
            this->lss_line_context = line_context_t::basename;
×
3126
            break;
×
3127
        case line_context_t::basename:
×
3128
            this->lss_line_context = line_context_t::none;
×
3129
            break;
×
3130
        case line_context_t::none:
×
3131
            if (cfg.c_time_column
×
3132
                != logfile_sub_source_ns::time_column_feature_t::Disabled)
3133
            {
3134
                this->lss_line_context = line_context_t::time_column;
×
3135
            }
3136
            break;
×
3137
        case line_context_t::time_column:
×
3138
            break;
×
3139
    }
3140
    if (old_context != this->lss_line_context) {
×
3141
        this->clear_line_size_cache();
×
3142

3143
        return true;
×
3144
    }
3145

3146
    return false;
×
3147
}
3148

3149
size_t
3150
logfile_sub_source::get_filename_offset() const
222✔
3151
{
3152
    switch (this->lss_line_context) {
222✔
3153
        case line_context_t::filename:
×
3154
            return this->lss_filename_width;
×
3155
        case line_context_t::basename:
×
3156
            return this->lss_basename_width;
×
3157
        default:
222✔
3158
            return 0;
222✔
3159
    }
3160
}
3161

3162
size_t
3163
logfile_sub_source::file_count() const
4,008✔
3164
{
3165
    size_t retval = 0;
4,008✔
3166
    const_iterator iter;
4,008✔
3167

3168
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,616✔
3169
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,608✔
3170
            retval += 1;
3,602✔
3171
        }
3172
    }
3173

3174
    return retval;
4,008✔
3175
}
3176

3177
size_t
3178
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3179
                                       int row,
3180
                                       text_sub_source::line_flags_t flags)
3181
{
3182
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3183

3184
    if (this->lss_line_size_cache[index].first != row) {
×
3185
        std::string value;
×
3186

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

3211
int
3212
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
1✔
3213
{
3214
    int retval = 0;
1✔
3215

3216
    for (const auto& ld : this->lss_files) {
2✔
3217
        retval += ld->ld_filter_state.lfo_filter_state
1✔
3218
                      .tfs_filter_hits[filter_index];
1✔
3219
    }
3220

3221
    return retval;
1✔
3222
}
3223

3224
std::optional<vis_line_t>
3225
logfile_sub_source::row_for(const row_info& ri)
264✔
3226
{
3227
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
528✔
3228
                               this->lss_filtered_index.end(),
3229
                               ri.ri_time,
264✔
3230
                               filtered_logline_cmp(*this));
3231
    if (lb != this->lss_filtered_index.end()) {
264✔
3232
        auto first_lb = lb;
259✔
3233
        while (true) {
3234
            auto cl = this->lss_index[*lb].value();
273✔
3235
            if (content_line_t(ri.ri_id) == cl) {
273✔
3236
                first_lb = lb;
227✔
3237
                break;
227✔
3238
            }
3239
            auto ll = this->find_line(cl);
46✔
3240
            if (ll->get_timeval() != ri.ri_time) {
46✔
3241
                break;
32✔
3242
            }
3243
            auto next_lb = std::next(lb);
14✔
3244
            if (next_lb == this->lss_filtered_index.end()) {
14✔
3245
                break;
×
3246
            }
3247
            lb = next_lb;
14✔
3248
        }
14✔
3249

3250
        const auto dst
3251
            = std::distance(this->lss_filtered_index.begin(), first_lb);
259✔
3252
        return vis_line_t(dst);
259✔
3253
    }
3254

3255
    return std::nullopt;
5✔
3256
}
3257

3258
std::unique_ptr<logline_window>
3259
logfile_sub_source::window_at(vis_line_t start_vl, vis_line_t end_vl)
35✔
3260
{
3261
    return std::make_unique<logline_window>(*this, start_vl, end_vl);
35✔
3262
}
3263

3264
std::unique_ptr<logline_window>
3265
logfile_sub_source::window_at(vis_line_t start_vl)
201✔
3266
{
3267
    return std::make_unique<logline_window>(*this, start_vl, start_vl + 1_vl);
201✔
3268
}
3269

3270
std::unique_ptr<logline_window>
3271
logfile_sub_source::window_to_end(vis_line_t start_vl)
×
3272
{
3273
    return std::make_unique<logline_window>(
3274
        *this, start_vl, vis_line_t(this->text_line_count()));
×
3275
}
3276

3277
std::optional<vis_line_t>
3278
logfile_sub_source::row_for_anchor(const std::string& id)
3✔
3279
{
3280
    if (startswith(id, "#msg")) {
3✔
3281
        static const auto ANCHOR_RE
3282
            = lnav::pcre2pp::code::from_const(R"(#msg([0-9a-fA-F]+)-(.+))");
3✔
3283
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
3✔
3284

3285
        if (ANCHOR_RE.capture_from(id).into(md).found_p()) {
3✔
3286
            auto scan_res = scn::scan<int64_t>(md[1]->to_string_view(), "{:x}");
3✔
3287
            if (scan_res) {
3✔
3288
                auto ts_low = std::chrono::microseconds{scan_res->value()};
3✔
3289
                auto ts_high = ts_low + 1us;
3✔
3290

3291
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3292
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3293
                if (low_vl) {
3✔
3294
                    auto lw = this->window_at(
3295
                        low_vl.value(),
3✔
3296
                        high_vl.value_or(low_vl.value() + 1_vl));
6✔
3297

3298
                    for (const auto& li : *lw) {
3✔
3299
                        auto hash_res = li.get_line_hash();
3✔
3300
                        if (hash_res.isErr()) {
3✔
3301
                            auto errmsg = hash_res.unwrapErr();
×
3302

3303
                            log_error("unable to get line hash: %s",
×
3304
                                      errmsg.c_str());
3305
                            continue;
×
3306
                        }
3307

3308
                        auto hash = hash_res.unwrap();
3✔
3309
                        if (hash == md[2]) {
3✔
3310
                            return li.get_vis_line();
3✔
3311
                        }
3312
                    }
12✔
3313
                }
3✔
3314
            }
3315
        }
3316

3317
        return std::nullopt;
×
3318
    }
3319

3320
    auto& vb = this->tss_view->get_bookmarks();
×
3321
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3322

3323
    for (const auto& vl : bv.bv_tree) {
×
3324
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3325
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3326
            continue;
×
3327
        }
3328

3329
        const auto& name = meta_opt.value()->bm_name;
×
3330
        if (id == text_anchors::to_anchor_string(name)) {
×
3331
            return vl;
×
3332
        }
3333
    }
3334

3335
    return std::nullopt;
×
3336
}
3337

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

3369
    auto bmc = this->get_bookmark_metadata_context(
2✔
3370
        vl, bookmark_metadata::categories::partition);
3371
    switch (dir) {
2✔
3372
        case direction::prev: {
×
3373
            if (bmc.bmc_current && bmc.bmc_current.value() != vl) {
×
3374
                return bmc.bmc_current;
×
3375
            }
3376
            if (!bmc.bmc_current || bmc.bmc_current.value() == 0_vl) {
×
3377
                return 0_vl;
×
3378
            }
3379
            auto prev_bmc = this->get_bookmark_metadata_context(
×
3380
                bmc.bmc_current.value() - 1_vl,
×
3381
                bookmark_metadata::categories::partition);
3382
            if (!prev_bmc.bmc_current) {
×
3383
                return 0_vl;
×
3384
            }
3385
            return prev_bmc.bmc_current;
×
3386
        }
3387
        case direction::next:
2✔
3388
            return bmc.bmc_next_line;
2✔
3389
    }
3390
    return std::nullopt;
×
3391
}
3392

3393
std::optional<std::string>
3394
logfile_sub_source::anchor_for_row(vis_line_t vl)
75✔
3395
{
3396
    auto line_meta = this->get_bookmark_metadata_context(
75✔
3397
        vl, bookmark_metadata::categories::partition);
3398
    if (!line_meta.bmc_current_metadata) {
75✔
3399
        auto lw = window_at(vl);
72✔
3400

3401
        for (const auto& li : *lw) {
72✔
3402
            auto hash_res = li.get_line_hash();
72✔
3403
            if (hash_res.isErr()) {
72✔
3404
                auto errmsg = hash_res.unwrapErr();
×
3405
                log_error("unable to compute line hash: %s", errmsg.c_str());
×
3406
                break;
×
3407
            }
3408
            auto hash = hash_res.unwrap();
72✔
3409
            auto retval = fmt::format(
3410
                FMT_STRING("#msg{:016x}-{}"),
144✔
3411
                li.get_logline().get_time<std::chrono::microseconds>().count(),
72✔
3412
                hash);
×
3413

3414
            return retval;
72✔
3415
        }
288✔
3416

3417
        return std::nullopt;
×
3418
    }
72✔
3419

3420
    return text_anchors::to_anchor_string(
3✔
3421
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3422
}
3423

3424
std::unordered_set<std::string>
3425
logfile_sub_source::get_anchors()
×
3426
{
3427
    auto& vb = this->tss_view->get_bookmarks();
×
3428
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3429
    std::unordered_set<std::string> retval;
×
3430

3431
    for (const auto& vl : bv.bv_tree) {
×
3432
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3433
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3434
            continue;
×
3435
        }
3436

3437
        const auto& name = meta_opt.value()->bm_name;
×
3438
        retval.emplace(text_anchors::to_anchor_string(name));
×
3439
    }
3440

3441
    return retval;
×
3442
}
×
3443

3444
bool
3445
logfile_sub_source::text_handle_mouse(
×
3446
    textview_curses& tc,
3447
    const listview_curses::display_line_content_t& mouse_line,
3448
    mouse_event& me)
3449
{
3450
    if (mouse_line.is<listview_curses::static_overlay_content>()
×
3451
        && this->text_line_count() > 0)
×
3452
    {
3453
        auto top = tc.get_top();
×
3454
        if (top > 0) {
×
3455
            auto win = this->window_at(top - 1_vl);
×
3456
            for (const auto& li : *win) {
×
3457
                tc.set_top(li.get_vis_line());
×
3458
                tc.set_selection(li.get_vis_line());
×
3459
                return true;
×
3460
            }
3461
        }
3462
    }
3463

3464
    if (tc.get_overlay_selection()) {
×
3465
        auto nci = ncinput{};
×
3466
        if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 2, 4)) {
×
3467
            nci.id = ' ';
×
3468
            nci.eff_text[0] = ' ';
×
3469
            this->list_input_handle_key(tc, nci);
×
3470
        } else if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 5, 6)) {
×
3471
            nci.id = '#';
×
3472
            nci.eff_text[0] = '#';
×
3473
            this->list_input_handle_key(tc, nci);
×
3474
        }
3475
    }
3476
    return true;
×
3477
}
3478

3479
void
3480
logfile_sub_source::reload_config(error_reporter& reporter)
662✔
3481
{
3482
    static const auto& cfg
3483
        = injector::get<const logfile_sub_source_ns::config&>();
662✔
3484

3485
    switch (cfg.c_time_column) {
662✔
3486
        case logfile_sub_source_ns::time_column_feature_t::Default:
×
3487
            if (this->lss_line_context == line_context_t::none) {
×
3488
                this->lss_line_context = line_context_t::time_column;
×
3489
            }
3490
            break;
×
3491
        case logfile_sub_source_ns::time_column_feature_t::Disabled:
662✔
3492
            if (this->lss_line_context == line_context_t::time_column) {
662✔
3493
                this->lss_line_context = line_context_t::none;
×
3494
            }
3495
            break;
662✔
3496
        case logfile_sub_source_ns::time_column_feature_t::Enabled:
×
3497
            break;
×
3498
    }
3499
}
662✔
3500

3501
void
3502
logfile_sub_source::add_commands_for_session(
47✔
3503
    const std::function<void(const std::string&)>& receiver)
3504
{
3505
    text_sub_source::add_commands_for_session(receiver);
47✔
3506

3507
    auto mark_expr = this->get_sql_marker_text();
47✔
3508
    if (!mark_expr.empty()) {
47✔
3509
        receiver(fmt::format(FMT_STRING("mark-expr {}"), mark_expr));
4✔
3510
    }
3511
    auto filter_expr = this->get_sql_filter_text();
47✔
3512
    if (!filter_expr.empty()) {
47✔
3513
        receiver(fmt::format(FMT_STRING("filter-expr {}"), filter_expr));
×
3514
    }
3515

3516
    for (const auto& format : log_format::get_root_formats()) {
3,522✔
3517
        auto field_states = format->get_field_states();
3,475✔
3518

3519
        for (const auto& fs_pair : field_states) {
47,294✔
3520
            if (!fs_pair.second.lvm_user_hidden) {
43,819✔
3521
                continue;
43,817✔
3522
            }
3523

3524
            if (fs_pair.second.lvm_user_hidden.value()) {
2✔
3525
                receiver(fmt::format(FMT_STRING("hide-fields {}.{}"),
8✔
3526
                                     format->get_name().to_string(),
4✔
3527
                                     fs_pair.first.to_string()));
4✔
3528
            } else if (fs_pair.second.lvm_hidden) {
×
3529
                receiver(fmt::format(FMT_STRING("show-fields {}.{}"),
×
3530
                                     format->get_name().to_string(),
×
3531
                                     fs_pair.first.to_string()));
×
3532
            }
3533
        }
3534
    }
3,475✔
3535
}
47✔
3536

3537
void
3538
logfile_sub_source::update_filter_hash_state(hasher& h) const
5✔
3539
{
3540
    text_sub_source::update_filter_hash_state(h);
5✔
3541

3542
    for (const auto& ld : this->lss_files) {
5✔
3543
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
×
3544
            h.update(0);
×
3545
        } else {
3546
            h.update(1);
×
3547
        }
3548
    }
3549
    h.update(this->lss_min_log_level);
5✔
3550
    h.update(this->lss_marked_only);
5✔
3551
}
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc