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

tstack / lnav / 19870100498-2728

02 Dec 2025 06:56PM UTC coverage: 68.872% (+0.04%) from 68.835%
19870100498-2728

push

github

tstack
[timeline] add files and threads

118 of 126 new or added lines in 9 files covered. (93.65%)

458 existing lines in 5 files now uncovered.

51405 of 74639 relevant lines covered (68.87%)

435136.79 hits per line

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

64.28
/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) {}
348✔
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,151✔
216
    {
217
        const auto cl_lhs = llss_controller.lss_index[lhs].value();
1,151✔
218
        const auto* ll_lhs = this->llss_controller.find_line(cl_lhs);
1,151✔
219

220
        if (ll_lhs == nullptr) {
1,151✔
221
            return true;
×
222
        }
223
        return (*ll_lhs) < rhs;
1,151✔
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
85✔
231
{
232
    const auto lb = std::lower_bound(this->lss_filtered_index.begin(),
85✔
233
                                     this->lss_filtered_index.end(),
234
                                     start,
235
                                     filtered_logline_cmp(*this));
236
    if (lb != this->lss_filtered_index.end()) {
85✔
237
        auto retval = std::distance(this->lss_filtered_index.begin(), lb);
65✔
238
        return vis_line_t(retval);
65✔
239
    }
240

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

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

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

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

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

264
    if (flags & RF_RAW) {
4,405✔
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,373✔
287

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

294
    this->lss_token_attrs.clear();
4,373✔
295
    this->lss_token_values.clear();
4,373✔
296
    this->lss_share_manager.invalidate_refs();
4,373✔
297
    if (flags & text_sub_source::RF_FULL) {
4,373✔
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)
8,650✔
309
                  .map([](auto sbr) { return to_string(sbr); })
8,650✔
310
                  .unwrapOr({});
4,325✔
311
        if (this->lss_token_line->has_ansi()) {
4,325✔
312
            scrub_ansi_string(this->lss_token_value, &this->lss_token_attrs);
24✔
313
        }
314
    }
315
    this->lss_token_shift_start = 0;
4,373✔
316
    this->lss_token_shift_size = 0;
4,373✔
317

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

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

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

324
    sbr.share(this->lss_share_manager,
4,373✔
325
              (char*) this->lss_token_value.c_str(),
326
              this->lss_token_value.size());
327
    format->annotate(this->lss_token_file.get(),
8,746✔
328
                     line,
329
                     this->lss_token_attrs,
4,373✔
330
                     this->lss_token_values);
4,373✔
331
    if (flags & RF_REWRITE) {
4,373✔
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,373✔
350
        this->lss_token_attrs.emplace_back(lr, SA_ORIGINAL_LINE.value());
4,373✔
351
    }
352

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

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

375
            if (format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
957✔
376
                || !(format->lf_timestamp_flags & ETF_DAY_SET)
323✔
377
                || !(format->lf_timestamp_flags & ETF_MONTH_SET))
1,280✔
378
            {
379
                if (format->lf_timestamp_flags & ETF_NANOS_SET) {
640✔
380
                    fmt = "%Y-%m-%d %H:%M:%S.%N";
×
381
                } else if (format->lf_timestamp_flags & ETF_MICROS_SET) {
640✔
382
                    fmt = "%Y-%m-%d %H:%M:%S.%f";
635✔
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(
640✔
389
                    buffer, sizeof(buffer), fmt, adjusted_tm.value());
640✔
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,914✔
399
                time_range.lr_start, time_range.length(), buffer, len);
957✔
400
            this->lss_token_shift_start = time_range.lr_start;
957✔
401
            this->lss_token_shift_size = len - time_range.length();
957✔
402
        }
403
    }
404

405
    // Insert space for the file/search-hit markers.
406
    value_out.insert(0, 1, ' ');
4,373✔
407
    this->lss_time_column_size = 0;
4,373✔
408
    if (this->lss_line_context == line_context_t::time_column) {
4,373✔
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,373✔
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,373✔
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,373✔
493

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

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

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

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

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

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

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

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

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

536
    if (!attrs.empty()) {
4,373✔
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,373✔
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) {
45,623✔
552
        if ((!(this->lss_token_flags & RF_FULL)
92,329✔
553
             && line_value.lv_sub_offset
82,056✔
554
                 != this->lss_token_line->get_sub_offset())
41,028✔
555
            || !line_value.lv_origin.is_valid())
82,278✔
556
        {
557
            continue;
9,829✔
558
        }
559

560
        if (line_value.lv_meta.is_hidden()) {
31,421✔
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
80,815✔
566
            || !line_value.lv_origin.is_valid())
31,421✔
567
        {
568
            continue;
17,973✔
569
        }
570

571
        value_out.emplace_back(line_value.lv_origin,
13,448✔
572
                               VC_ROLE.value(role_t::VCR_IDENTIFIER));
26,896✔
573
    }
574

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

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

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

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

605
            if (bv_search.bv_tree.exists(vis_line_t(row))) {
4,325✔
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,373✔
615
                           VC_STYLE.value(vc.attrs_for_ident(
8,746✔
616
                               this->lss_token_file->get_filename())));
4,373✔
617

618
    if (this->lss_line_context < line_context_t::none) {
4,373✔
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,373✔
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,373✔
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,373✔
721
    lr.lr_end = -1;
4,373✔
722
    value_out.emplace_back(lr, L_FILE.value(this->lss_token_file));
4,373✔
723
    value_out.emplace_back(
4,373✔
724
        lr, SA_FORMAT.value(this->lss_token_file->get_format()->get_name()));
8,746✔
725

726
    {
727
        auto line_meta_context = this->get_bookmark_metadata_context(
4,373✔
728
            vis_line_t(row + 1), bookmark_metadata::categories::partition);
729
        if (line_meta_context.bmc_current_metadata) {
4,373✔
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,373✔
739

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

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

760
    if (this->lss_time_column_size == 0) {
4,373✔
761
        if (this->lss_token_file->is_time_adjusted()) {
4,373✔
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()) {
4,356✔
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,373✔
779
        && this->lss_token_line->get_timeval() < this->ttt_preview_min_time)
4,373✔
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,373✔
786
        && this->ttt_preview_max_time < this->lss_token_line->get_timeval())
4,373✔
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,373✔
793
        if (this->lss_preview_filter_stmt != nullptr) {
2,531✔
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,531✔
825
        if (sql_filter_opt) {
2,531✔
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,531✔
843
}
844

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

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

854
        return (*ll_lhs) < (*ll_rhs);
105,154✔
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,434✔
898
{
899
    if (this->tss_view == nullptr) {
4,434✔
900
        return rebuild_result::rr_no_change;
124✔
901
    }
902

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

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

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

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

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

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

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

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

954
        if (lf == nullptr) {
2,985✔
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,981✔
964
                all_time_ordered_formats = false;
191✔
965
            }
966
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
2,981✔
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,981✔
972
                |= lf->get_format_ptr()->lf_timestamp_flags;
2,981✔
973

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

977
                if (ld.ld_lines_indexed < lf->size()
2,979✔
978
                    && log_rebuild_res
2,979✔
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;
50✔
986
                }
987
                switch (log_rebuild_res) {
2,979✔
988
                    case logfile::rebuild_result_t::NO_NEW_LINES:
2,437✔
989
                        break;
2,437✔
990
                    case logfile::rebuild_result_t::NEW_LINES:
482✔
991
                        if (retval == rebuild_result::rr_no_change) {
482✔
992
                            retval = rebuild_result::rr_appended_lines;
432✔
993
                        }
994
                        log_debug("new lines for %s:%zu",
482✔
995
                                  lf->get_filename_as_string().c_str(),
996
                                  lf->size());
997
                        if (!this->lss_index.empty()
482✔
998
                            && lf->size() > ld.ld_lines_indexed)
482✔
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;
482✔
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,981✔
1056
            total_lines += lf->size();
2,981✔
1057

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

1062
    if (!all_time_ordered_formats
4,310✔
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,310✔
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,310✔
1080

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

1088
        this->lss_index.clear();
1,127✔
1089
        this->lss_filtered_index.clear();
1,127✔
1090
        this->lss_longest_line = 0;
1,127✔
1091
        this->lss_basename_width = 0;
1,127✔
1092
        this->lss_filename_width = 0;
1,127✔
1093
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,127✔
1094
        if (this->lss_index_delegate) {
1,127✔
1095
            this->lss_index_delegate->index_start(*this);
1,127✔
1096
        }
1097
    } else if (retval == rebuild_result::rr_partial_rebuild) {
3,183✔
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,310✔
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,310✔
1169
        size_t index_size = 0, start_size = this->lss_index.size();
1,141✔
1170
        logline_cmp line_cmper(*this);
1,141✔
1171

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

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

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

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

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

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

1208
                    if (lf_iter->is_meta_marked()) {
13,826✔
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,826✔
1235
                        indexed_content{con_line, lf_iter});
27,652✔
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,141✔
1242
                this->lss_sorting_observer(*this, 0, this->lss_index.size());
3✔
1243
            }
1244
            std::sort(
1,141✔
1245
                this->lss_index.begin(), this->lss_index.end(), line_cmper);
1246
            if (this->lss_sorting_observer) {
1,141✔
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,693✔
1329
             ++iter)
552✔
1330
        {
1331
            auto* lf = (*iter)->get_file_ptr();
552✔
1332

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

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

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

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

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

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

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

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

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

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

1381
                    line_iter->set_expr_mark(matched);
13,796✔
1382
                    if (matched) {
13,796✔
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,796✔
1390
                if (this->lss_index_delegate != nullptr) {
13,796✔
1391
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
13,796✔
1392
                }
1393
            }
13,796✔
1394
        }
1395

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

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

1403
    switch (retval) {
4,310✔
1404
        case rebuild_result::rr_no_change:
3,169✔
1405
            break;
3,169✔
1406
        case rebuild_result::rr_full_rebuild:
1,127✔
1407
            log_debug("redoing search");
1,127✔
1408
            this->lss_index_generation += 1;
1,127✔
1409
            this->tss_view->reload_data();
1,127✔
1410
            this->tss_view->redo_search();
1,127✔
1411
            break;
1,127✔
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:
14✔
1419
            this->tss_view->reload_data();
14✔
1420
            this->tss_view->search_new_data();
14✔
1421
            break;
14✔
1422
    }
1423

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

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

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

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

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

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

1460
        for (const auto& bmt : used_marks) {
19,479✔
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,341✔
1468
            bm_files.insert_once(vl);
965✔
1469
        }
1470

1471
        switch (orig_ic.level()) {
17,341✔
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,420✔
1477
                bm_errors.insert_once(vl);
3,420✔
1478
                break;
3,420✔
1479

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

1484
        last_file = lf;
17,341✔
1485
    }
1486
}
2,210✔
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) {
284✔
1499
        auto* lf = ld->get_file_ptr();
118✔
1500

1501
        if (lf != nullptr) {
118✔
1502
            ld->ld_filter_state.clear_deleted_filter_state();
118✔
1503
            lf->reobserve_from(lf->begin()
118✔
1504
                               + ld->ld_filter_state.get_min_count(lf->size()));
118✔
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,494✔
1524
         index_index++)
1525
    {
1526
        auto cl = this->lss_index[index_index].value();
1,328✔
1527
        uint64_t line_number;
1528
        auto ld = this->find_data(cl, line_number);
1,328✔
1529

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

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

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

1549
                line_iter->set_expr_mark(matched);
937✔
1550
                if (matched) {
937✔
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);
937✔
1558
            if (this->lss_index_delegate != nullptr) {
937✔
1559
                this->lss_index_delegate->index_line(*this, lf, line_iter);
937✔
1560
            }
1561
        }
937✔
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,036✔
1714
        : lde_file(std::move(lf))
1,036✔
1715
    {
1716
    }
1,036✔
1717

1718
    bool operator()(
634✔
1719
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1720
    {
1721
        return this->lde_file == ld->get_file();
634✔
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)
518✔
1729
{
1730
    iterator existing;
518✔
1731

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

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

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

1750
    return true;
518✔
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_iter = this->tss_filters.find(0);
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_iter != this->tss_filters.end()) {
7✔
1778
            *old_filter_iter = new_filter;
×
1779
        } else {
1780
            this->tss_filters.add_filter(new_filter);
7✔
1781
        }
1782
    } else if (old_filter_iter != this->tss_filters.end()) {
771✔
1783
        this->tss_filters.delete_filter((*old_filter_iter)->get_id());
7✔
1784
    }
1785

1786
    return Ok();
771✔
1787
}
1788

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

1799
        if (eval_res.isErr()) {
5✔
1800
            sqlite3_finalize(stmt);
×
1801
            return Err(eval_res.unwrapErr());
×
1802
        }
1803
    }
5✔
1804

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

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

1815
    auto& vis_bm = this->tss_view->get_bookmarks();
639✔
1816
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
639✔
1817
    auto& cl_marks_bv = this->lss_user_marks[&textview_curses::BM_USER_EXPR];
639✔
1818

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

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

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

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

1861
    return Ok();
639✔
1862
}
769✔
1863

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

1873
        if (eval_res.isErr()) {
×
1874
            sqlite3_finalize(stmt);
×
1875
            return Err(eval_res.unwrapErr());
×
1876
        }
1877
    }
1878

1879
    this->lss_preview_filter_stmt = stmt;
764✔
1880

1881
    return Ok();
764✔
1882
}
1883

1884
Result<bool, lnav::console::user_message>
1885
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
15,351✔
1886
                                    iterator ld,
1887
                                    logfile::const_iterator ll)
1888
{
1889
    if (stmt == nullptr) {
15,351✔
1890
        return Ok(false);
29,472✔
1891
    }
1892

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

1906
    sqlite3_reset(stmt);
615✔
1907
    sqlite3_clear_bindings(stmt);
615✔
1908

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

1913
        if (name[0] == '$') {
627✔
1914
            const char* env_value;
1915

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

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

1991
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
1992

1993
                {
1994
                    yajlpp_array arr(gen);
1✔
1995

1996
                    for (const auto& str : meta.bm_tags) {
2✔
1997
                        arr.gen(str);
1✔
1998
                    }
1999
                }
1✔
2000

2001
                string_fragment sf = gen.to_string_fragment();
1✔
2002

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

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

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

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

2118
    auto step_res = sqlite3_step(stmt);
615✔
2119

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

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

2159
    if (ll->get_msg_level() < this->lss_min_log_level) {
14,772✔
2160
        return false;
2✔
2161
    }
2162

2163
    if (*ll < this->ttt_min_row_time) {
14,770✔
2164
        return false;
36✔
2165
    }
2166

2167
    if (!(*ll <= this->ttt_max_row_time)) {
14,734✔
2168
        return false;
4✔
2169
    }
2170

2171
    return true;
14,730✔
2172
}
2173

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

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

2191
    auto cl = this->at(line);
148✔
2192

2193
    if (bm == &textview_curses::BM_USER) {
148✔
2194
        auto* ll = this->find_line(cl);
59✔
2195

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

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

2225
void
2226
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
518✔
2227
{
2228
    auto iter = std::find_if(
518✔
2229
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
1,036✔
2230
    if (iter != this->lss_files.end()) {
518✔
2231
        int file_index = iter - this->lss_files.begin();
518✔
2232

2233
        (*iter)->clear();
518✔
2234
        for (auto& bv : this->lss_user_marks) {
4,662✔
2235
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
4,144✔
2236
            auto mark_end
2237
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
4,144✔
2238
            auto file_range = bv.equal_range(mark_curr, mark_end);
4,144✔
2239

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

2249
                for (auto cl : to_del) {
178✔
2250
                    bv.erase(cl);
109✔
2251
                }
2252
            }
69✔
2253
        }
2254

2255
        this->lss_force_rebuild = true;
518✔
2256
    }
2257
    while (!this->lss_files.empty()) {
1,036✔
2258
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
569✔
2259
            this->lss_files.pop_back();
518✔
2260
        } else {
2261
            break;
51✔
2262
        }
2263
    }
2264
    this->lss_token_file = nullptr;
518✔
2265
}
518✔
2266

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

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

2278
        if (!vis_start_opt) {
5✔
2279
            return std::nullopt;
×
2280
        }
2281

2282
        auto vis_start = *vis_start_opt;
5✔
2283

2284
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2285
            content_line_t guess_cl = this->at(vis_start);
5✔
2286

2287
            if (cl == guess_cl) {
5✔
2288
                return vis_start;
5✔
2289
            }
2290

2291
            auto guess_line = this->find_line(guess_cl);
×
2292

2293
            if (!guess_line || ll < *guess_line) {
×
2294
                return std::nullopt;
×
2295
            }
2296

2297
            ++vis_start;
×
2298
        }
2299
    }
2300

2301
    return std::nullopt;
×
2302
}
5✔
2303

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

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

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

2324
std::optional<std::shared_ptr<text_filter>>
2325
logfile_sub_source::get_sql_filter()
2,584✔
2326
{
2327
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
2,584✔
2328
               return filt->get_index() == 0
268✔
2329
                   && dynamic_cast<sql_filter*>(filt.get()) != nullptr;
268✔
2330
           })
2331
        | lnav::itertools::deref();
5,168✔
2332
}
2333

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

2342
    auto cl = this->llh_log_source.at(top);
94✔
2343

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

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

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

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

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

2367
        this->lh_history_position += 1;
2✔
2368

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

2371
        vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2372

2373
        if (vis_for_pos) {
2✔
2374
            return vis_for_pos;
2✔
2375
        }
2376
    }
2377

2378
    return std::nullopt;
×
2379
}
2380

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

2387
        auto iter = this->llh_history.rbegin();
1✔
2388

2389
        iter += this->lh_history_position;
1✔
2390

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

2393
        if (vis_for_pos) {
1✔
2394
            return vis_for_pos;
1✔
2395
        }
2396
    }
2397

2398
    return std::nullopt;
×
2399
}
2400

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

2409
    auto ls = ls_opt;
124✔
2410

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

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

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

2430
    return true;
47✔
2431
}
121✔
2432

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

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

2449
        {
2450
            md2attr_line mdal;
×
2451

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

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

2470
            md2attr_line mdal;
×
2471

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

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

2488
    return std::nullopt;
×
2489
}
2490

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

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

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

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

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

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

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

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

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

2558
    return retval;
11✔
2559
}
24✔
2560

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

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

2573
    return retval;
29✔
2574
}
×
2575

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

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

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

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

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

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

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

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

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

2647
    auto format_name = format->get_name().to_string();
18✔
2648

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

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

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

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

2720
    shared_buffer sb;
18✔
2721
    logline_value_vector values;
18✔
2722
    auto& sbr = values.lvv_sbr;
18✔
2723

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3064
    return &bm_iter->second;
356✔
3065
}
23,579✔
3066

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

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

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

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

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

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

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

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

3144
        return true;
×
3145
    }
3146

3147
    return false;
×
3148
}
3149

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

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

3169
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,612✔
3170
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,605✔
3171
            retval += 1;
3,599✔
3172
        }
3173
    }
3174

3175
    return retval;
4,007✔
3176
}
3177

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

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

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

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

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

3222
    return retval;
1✔
3223
}
3224

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

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

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

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

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

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

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

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

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

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

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

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

3318
        return std::nullopt;
×
3319
    }
3320

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

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

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

3336
    return std::nullopt;
×
3337
}
3338

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

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

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

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

3415
            return retval;
72✔
3416
        }
288✔
3417

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

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

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

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

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

3442
    return retval;
×
3443
}
×
3444

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

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

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

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

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

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

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

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

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

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

3543
    for (const auto& ld : this->lss_files) {
5✔
3544
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
×
3545
            h.update(0);
×
3546
        } else {
3547
            h.update(1);
×
3548
        }
3549
    }
3550
    h.update(this->lss_min_log_level);
5✔
3551
    h.update(this->lss_marked_only);
5✔
3552
}
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