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

tstack / lnav / 21802468722-2778

08 Feb 2026 05:40PM UTC coverage: 68.978% (+0.01%) from 68.966%
21802468722-2778

push

github

tstack
[formats] allow highlights to be limited to a field

Related to #1620

218 of 325 new or added lines in 11 files covered. (67.08%)

2 existing lines in 2 files now uncovered.

51991 of 75373 relevant lines covered (68.98%)

440117.55 hits per line

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

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

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

39
#include "logfile_sub_source.hh"
40

41
#include <sqlite3.h>
42

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

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

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

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

83
    const auto ncols = sqlite3_column_count(stmt);
39✔
84

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

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

95
        ec.ec_accumulator->append(res);
39✔
96
    }
97

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

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

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

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

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

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

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

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

157
        return retval;
×
158
    });
×
159

160
    return retval;
×
161
}
162

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

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

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

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

195
    return retval;
25✔
196
}
×
197

198
struct filtered_logline_cmp {
199
    filtered_logline_cmp(const logfile_sub_source& lc) : llss_controller(lc) {}
354✔
200

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

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

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

222
        if (ll_lhs == nullptr) {
1,166✔
223
            return true;
×
224
        }
225
        return (*ll_lhs) < rhs;
1,166✔
226
    }
227

228
    const logfile_sub_source& llss_controller;
229
};
230

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

243
    return std::nullopt;
20✔
244
}
245

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

258
    line_info retval;
4,427✔
259
    content_line_t line(0);
4,427✔
260

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

264
    line = this->at(vis_line_t(row));
4,427✔
265

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

288
    require_false(this->lss_in_value_for_line);
4,395✔
289

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

296
    this->lss_token_al.clear();
4,395✔
297
    this->lss_token_values.clear();
4,395✔
298
    this->lss_share_manager.invalidate_refs();
4,395✔
299
    if (flags & RF_FULL) {
4,395✔
300
        shared_buffer_ref sbr;
48✔
301

302
        this->lss_token_file->read_full_message(this->lss_token_line, sbr);
48✔
303
        this->lss_token_al.al_string = to_string(sbr);
48✔
304
        if (sbr.get_metadata().m_has_ansi) {
48✔
305
            scrub_ansi_string(this->lss_token_al.al_string,
17✔
306
                              &this->lss_token_al.al_attrs);
307
            sbr.get_metadata().m_has_ansi = false;
17✔
308
        }
309
    } else {
48✔
310
        auto sub_opts = subline_options{};
4,347✔
311
        sub_opts.scrub_invalid_utf8 = false;
4,347✔
312
        this->lss_token_al.al_string
313
            = this->lss_token_file->read_line(this->lss_token_line, sub_opts)
8,694✔
314
                  .map([](auto sbr) { return to_string(sbr); })
8,694✔
315
                  .unwrapOr({});
4,347✔
316
        if (this->lss_token_line->has_ansi()) {
4,347✔
317
            scrub_ansi_string(this->lss_token_al.al_string,
24✔
318
                              &this->lss_token_al.al_attrs);
319
        }
320
    }
321
    this->lss_token_shift_start = 0;
4,395✔
322
    this->lss_token_shift_size = 0;
4,395✔
323

324
    auto format = this->lss_token_file->get_format();
4,395✔
325

326
    value_out = this->lss_token_al.al_string;
4,395✔
327

328
    auto& sbr = this->lss_token_values.lvv_sbr;
4,395✔
329

330
    sbr.share(this->lss_share_manager,
4,395✔
331
              (char*) this->lss_token_al.al_string.c_str(),
332
              this->lss_token_al.al_string.size());
333
    format->annotate(this->lss_token_file.get(),
8,790✔
334
                     line,
335
                     this->lss_token_al.al_attrs,
4,395✔
336
                     this->lss_token_values);
4,395✔
337

338
    for (const auto& hl : format->lf_highlighters) {
5,250✔
339
        auto hl_range = line_range{0, -1};
855✔
340
        auto value_iter = this->lss_token_values.lvv_values.end();
855✔
341
        if (!hl.h_field.empty()) {
855✔
342
            value_iter = std::find_if(this->lss_token_values.lvv_values.begin(),
171✔
343
                                      this->lss_token_values.lvv_values.end(),
344
                                      logline_value_name_cmp(&hl.h_field));
345
            if (value_iter == this->lss_token_values.lvv_values.end()) {
171✔
346
                continue;
160✔
347
            }
348
            hl_range = value_iter->lv_origin;
11✔
349
        }
350
        if (hl.annotate(this->lss_token_al, hl_range)
695✔
351
            && value_iter != this->lss_token_values.lvv_values.end())
695✔
352
        {
353
            value_iter->lv_highlighted = true;
7✔
354
        }
355
    }
356

357
    for (const auto& hl : this->lss_highlighters) {
4,404✔
358
        auto hl_range = line_range{0, -1};
9✔
359
        auto value_iter = this->lss_token_values.lvv_values.end();
9✔
360
        if (!hl.h_field.empty()) {
9✔
361
            value_iter = std::find_if(this->lss_token_values.lvv_values.begin(),
9✔
362
                                      this->lss_token_values.lvv_values.end(),
363
                                      logline_value_name_cmp(&hl.h_field));
364
            if (value_iter == this->lss_token_values.lvv_values.end()) {
9✔
NEW
365
                continue;
×
366
            }
367
            hl_range = value_iter->lv_origin;
9✔
368
        }
369
        if (hl.annotate(this->lss_token_al, hl_range)
9✔
370
            && value_iter != this->lss_token_values.lvv_values.end())
9✔
371
        {
372
            value_iter->lv_highlighted = true;
4✔
373
        }
374
    }
375

376
    if (flags & RF_REWRITE) {
4,395✔
377
        exec_context ec(
378
            &this->lss_token_values, pretty_sql_callback, pretty_pipe_callback);
48✔
379
        std::string rewritten_line;
48✔
380
        db_label_source rewrite_label_source;
48✔
381

382
        ec.with_perms(exec_context::perm_t::READ_ONLY);
48✔
383
        ec.ec_local_vars.push(std::map<std::string, scoped_value_t>());
48✔
384
        ec.ec_top_line = vis_line_t(row);
48✔
385
        ec.ec_label_source_stack.push_back(&rewrite_label_source);
48✔
386
        add_ansi_vars(ec.ec_global_vars);
48✔
387
        add_global_vars(ec);
48✔
388
        format->rewrite(ec, sbr, this->lss_token_al.al_attrs, rewritten_line);
48✔
389
        this->lss_token_al.al_string.assign(rewritten_line);
48✔
390
        value_out = this->lss_token_al.al_string;
48✔
391
    }
48✔
392

393
    {
394
        auto lr = line_range{0, (int) this->lss_token_al.al_string.length()};
4,395✔
395
        this->lss_token_al.al_attrs.emplace_back(lr, SA_ORIGINAL_LINE.value());
4,395✔
396
    }
397

398
    std::optional<exttm> adjusted_tm;
4,395✔
399
    auto time_attr
400
        = find_string_attr(this->lss_token_al.al_attrs, &L_TIMESTAMP);
4,395✔
401
    if (!this->lss_token_line->is_continued() && !format->lf_formatted_lines
6,951✔
402
        && (this->lss_token_file->is_time_adjusted()
2,314✔
403
            || ((format->lf_timestamp_flags & ETF_ZONE_SET
2,299✔
404
                 || format->lf_date_time.dts_default_zone != nullptr)
1,389✔
405
                && format->lf_date_time.dts_zoned_to_local)
994✔
406
            || format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
1,305✔
407
            || !(format->lf_timestamp_flags & ETF_DAY_SET)
1,305✔
408
            || !(format->lf_timestamp_flags & ETF_MONTH_SET))
1,284✔
409
        && format->lf_date_time.dts_fmt_lock != -1)
6,951✔
410
    {
411
        if (time_attr != this->lss_token_al.al_attrs.end()) {
975✔
412
            const auto time_range = time_attr->sa_range;
974✔
413
            const auto time_sf
414
                = string_fragment::from_str_range(this->lss_token_al.al_string,
974✔
415
                                                  time_range.lr_start,
974✔
416
                                                  time_range.lr_end);
974✔
417
            adjusted_tm = format->tm_for_display(this->lss_token_line, time_sf);
974✔
418

419
            char buffer[128];
420
            const char* fmt;
421
            ssize_t len;
422

423
            if (format->lf_timestamp_flags & ETF_MACHINE_ORIENTED
974✔
424
                || !(format->lf_timestamp_flags & ETF_DAY_SET)
340✔
425
                || !(format->lf_timestamp_flags & ETF_MONTH_SET))
1,314✔
426
            {
427
                if (format->lf_timestamp_flags & ETF_NANOS_SET) {
640✔
428
                    fmt = "%Y-%m-%d %H:%M:%S.%N";
×
429
                } else if (format->lf_timestamp_flags & ETF_MICROS_SET) {
640✔
430
                    fmt = "%Y-%m-%d %H:%M:%S.%f";
635✔
431
                } else if (format->lf_timestamp_flags & ETF_MILLIS_SET) {
5✔
432
                    fmt = "%Y-%m-%d %H:%M:%S.%L";
2✔
433
                } else {
434
                    fmt = "%Y-%m-%d %H:%M:%S";
3✔
435
                }
436
                len = ftime_fmt(
640✔
437
                    buffer, sizeof(buffer), fmt, adjusted_tm.value());
640✔
438
            } else {
439
                len = format->lf_date_time.ftime(
668✔
440
                    buffer,
441
                    sizeof(buffer),
442
                    format->get_timestamp_formats(),
443
                    adjusted_tm.value());
334✔
444
            }
445

446
            value_out.replace(
1,948✔
447
                time_range.lr_start, time_range.length(), buffer, len);
974✔
448
            this->lss_token_shift_start = time_range.lr_start;
974✔
449
            this->lss_token_shift_size = len - time_range.length();
974✔
450
        }
451
    }
452

453
    // Insert space for the file/search-hit markers.
454
    value_out.insert(0, 1, ' ');
4,395✔
455
    this->lss_time_column_size = 0;
4,395✔
456
    if (this->lss_line_context == line_context_t::time_column) {
4,395✔
NEW
457
        if (time_attr != this->lss_token_al.al_attrs.end()) {
×
458
            const char* fmt;
459
            if (this->lss_all_timestamp_flags
×
460
                & (ETF_MICROS_SET | ETF_NANOS_SET))
×
461
            {
462
                fmt = "%H:%M:%S.%f";
×
463
            } else if (this->lss_all_timestamp_flags & ETF_MILLIS_SET) {
×
464
                fmt = "%H:%M:%S.%L";
×
465
            } else {
466
                fmt = "%H:%M:%S";
×
467
            }
468
            if (!adjusted_tm) {
×
469
                const auto time_range = time_attr->sa_range;
×
NEW
470
                const auto time_sf = string_fragment::from_str_range(
×
NEW
471
                    this->lss_token_al.al_string,
×
NEW
472
                    time_range.lr_start,
×
NEW
473
                    time_range.lr_end);
×
474
                adjusted_tm
475
                    = format->tm_for_display(this->lss_token_line, time_sf);
×
476
            }
477
            adjusted_tm->et_flags |= this->lss_all_timestamp_flags
×
478
                & (ETF_MILLIS_SET | ETF_MICROS_SET | ETF_NANOS_SET);
×
479
            char buffer[128];
480
            this->lss_time_column_size
481
                = ftime_fmt(buffer, sizeof(buffer), fmt, adjusted_tm.value());
×
482
            if (this->tss_view->is_selectable()
×
483
                && this->tss_view->get_selection() == row)
×
484
            {
485
                buffer[this->lss_time_column_size] = ' ';
×
486
                buffer[this->lss_time_column_size + 1] = ' ';
×
487
                this->lss_time_column_size += 2;
×
488
            } else {
489
                constexpr char block[] = "\u258c ";
×
490

491
                strcpy(&buffer[this->lss_time_column_size], block);
×
492
                this->lss_time_column_size += sizeof(block) - 1;
×
493
            }
494
            if (time_attr->sa_range.lr_start != 0) {
×
495
                buffer[this->lss_time_column_size] = ' ';
×
496
                this->lss_time_column_size += 1;
×
497
                this->lss_time_column_padding = 1;
×
498
            } else {
499
                this->lss_time_column_padding = 0;
×
500
            }
501
            value_out.insert(1, buffer, this->lss_time_column_size);
×
NEW
502
            this->lss_token_al.al_attrs.emplace_back(time_attr->sa_range,
×
NEW
503
                                                     SA_REPLACED.value());
×
504
        }
505
        if (format->lf_level_hideable) {
×
506
            auto level_attr
NEW
507
                = find_string_attr(this->lss_token_al.al_attrs, &L_LEVEL);
×
NEW
508
            if (level_attr != this->lss_token_al.al_attrs.end()) {
×
NEW
509
                this->lss_token_al.al_attrs.emplace_back(level_attr->sa_range,
×
NEW
510
                                                         SA_REPLACED.value());
×
511
            }
512
        }
513
    } else if (this->lss_line_context < line_context_t::none) {
4,395✔
514
        size_t file_offset_end;
515
        std::string name;
×
516
        if (this->lss_line_context == line_context_t::filename) {
×
517
            file_offset_end = this->lss_filename_width;
×
518
            name = fmt::to_string(this->lss_token_file->get_filename());
×
519
            if (file_offset_end < name.size()) {
×
520
                file_offset_end = name.size();
×
521
                this->lss_filename_width = name.size();
×
522
            }
523
        } else {
524
            file_offset_end = this->lss_basename_width;
×
525
            name = fmt::to_string(this->lss_token_file->get_unique_path());
×
526
            if (file_offset_end < name.size()) {
×
527
                file_offset_end = name.size();
×
528
                this->lss_basename_width = name.size();
×
529
            }
530
        }
531
        value_out.insert(0, file_offset_end - name.size(), ' ');
×
532
        value_out.insert(0, name);
×
533
    }
534

535
    if (this->tas_display_time_offset) {
4,395✔
536
        auto row_vl = vis_line_t(row);
210✔
537
        auto relstr = this->get_time_offset_for_line(tc, row_vl);
210✔
538
        value_out = fmt::format(FMT_STRING("{: >12}|{}"), relstr, value_out);
840✔
539
    }
210✔
540

541
    this->lss_in_value_for_line = false;
4,395✔
542

543
    return retval;
4,395✔
544
}
4,395✔
545

546
void
547
logfile_sub_source::text_attrs_for_line(textview_curses& lv,
4,395✔
548
                                        int row,
549
                                        string_attrs_t& value_out)
550
{
551
    if (this->lss_indexing_in_progress) {
4,395✔
552
        return;
×
553
    }
554

555
    auto& vc = view_colors::singleton();
4,395✔
556
    logline* next_line = nullptr;
4,395✔
557
    line_range lr;
4,395✔
558
    int time_offset_end = 0;
4,395✔
559
    text_attrs attrs;
4,395✔
560
    auto* format = this->lss_token_file->get_format_ptr();
4,395✔
561

562
    if ((row + 1) < (int) this->lss_filtered_index.size()) {
4,395✔
563
        next_line = this->find_line(this->at(vis_line_t(row + 1)));
4,201✔
564
    }
565

566
    if (next_line != nullptr
4,395✔
567
        && (day_num(next_line->get_time<std::chrono::seconds>().count())
8,596✔
568
            > day_num(this->lss_token_line->get_time<std::chrono::seconds>()
8,596✔
569
                          .count())))
570
    {
571
        attrs |= text_attrs::style::underline;
17✔
572
    }
573

574
    const auto& line_values = this->lss_token_values;
4,395✔
575

576
    lr.lr_start = 0;
4,395✔
577
    lr.lr_end = -1;
4,395✔
578
    this->lss_token_al.al_attrs.emplace_back(
4,395✔
579
        lr, SA_LEVEL.value(this->lss_token_line->get_msg_level()));
8,790✔
580

581
    lr.lr_start = time_offset_end;
4,395✔
582
    lr.lr_end = -1;
4,395✔
583

584
    if (!attrs.empty()) {
4,395✔
585
        this->lss_token_al.al_attrs.emplace_back(lr, VC_STYLE.value(attrs));
17✔
586
    }
587

588
    if (this->lss_token_line->get_msg_level() == log_level_t::LEVEL_INVALID) {
4,395✔
589
        for (auto& token_attr : this->lss_token_al.al_attrs) {
70✔
590
            if (token_attr.sa_type != &SA_INVALID) {
49✔
591
                continue;
42✔
592
            }
593

594
            this->lss_token_al.al_attrs.emplace_back(
14✔
595
                token_attr.sa_range, VC_ROLE.value(role_t::VCR_INVALID_MSG));
7✔
596
        }
597
    }
598

599
    for (const auto& line_value : line_values.lvv_values) {
45,872✔
600
        if ((!(this->lss_token_flags & RF_FULL)
92,832✔
601
             && line_value.lv_sub_offset
82,526✔
602
                 != this->lss_token_line->get_sub_offset())
41,263✔
603
            || !line_value.lv_origin.is_valid())
82,740✔
604
        {
605
            continue;
9,878✔
606
        }
607

608
        if (line_value.lv_meta.is_hidden()) {
31,599✔
609
            this->lss_token_al.al_attrs.emplace_back(
288✔
610
                line_value.lv_origin, SA_HIDDEN.value(ui_icon_t::hidden));
144✔
611
        }
612

613
        if (!line_value.lv_meta.lvm_identifier
81,240✔
614
            || !line_value.lv_origin.is_valid())
31,599✔
615
        {
616
            continue;
18,042✔
617
        }
618

619
        if (line_value.lv_highlighted) {
13,557✔
620
            continue;
7✔
621
        }
622

623
        this->lss_token_al.al_attrs.emplace_back(
27,100✔
624
            line_value.lv_origin, VC_ROLE.value(role_t::VCR_IDENTIFIER));
13,550✔
625
    }
626

627
    if (this->lss_token_shift_size) {
4,395✔
628
        shift_string_attrs(this->lss_token_al.al_attrs,
668✔
629
                           this->lss_token_shift_start + 1,
668✔
630
                           this->lss_token_shift_size);
631
    }
632

633
    shift_string_attrs(this->lss_token_al.al_attrs, 0, 1);
4,395✔
634

635
    lr.lr_start = 0;
4,395✔
636
    lr.lr_end = 1;
4,395✔
637
    {
638
        auto& bm = lv.get_bookmarks();
4,395✔
639
        const auto& bv = bm[&BM_FILES];
4,395✔
640
        bool is_first_for_file = bv.bv_tree.exists(vis_line_t(row));
4,395✔
641
        bool is_last_for_file = bv.bv_tree.exists(vis_line_t(row + 1));
4,395✔
642
        auto graph = NCACS_VLINE;
4,395✔
643
        if (is_first_for_file) {
4,395✔
644
            if (is_last_for_file) {
219✔
645
                graph = NCACS_HLINE;
8✔
646
            } else {
647
                graph = NCACS_ULCORNER;
211✔
648
            }
649
        } else if (is_last_for_file) {
4,176✔
650
            graph = NCACS_LLCORNER;
23✔
651
        }
652
        this->lss_token_al.al_attrs.emplace_back(lr, VC_GRAPHIC.value(graph));
4,395✔
653

654
        if (!(this->lss_token_flags & RF_FULL)) {
4,395✔
655
            const auto& bv_search = bm[&textview_curses::BM_SEARCH];
4,347✔
656

657
            if (bv_search.bv_tree.exists(vis_line_t(row))) {
4,347✔
658
                lr.lr_start = 0;
9✔
659
                lr.lr_end = 1;
9✔
660
                this->lss_token_al.al_attrs.emplace_back(
9✔
661
                    lr, VC_STYLE.value(text_attrs::with_reverse()));
18✔
662
            }
663
        }
664
    }
665

666
    this->lss_token_al.al_attrs.emplace_back(
4,395✔
667
        lr,
668
        VC_STYLE.value(
8,790✔
669
            vc.attrs_for_ident(this->lss_token_file->get_filename())));
8,790✔
670

671
    if (this->lss_line_context < line_context_t::none) {
4,395✔
672
        size_t file_offset_end
×
673
            = (this->lss_line_context == line_context_t::filename)
×
674
            ? this->lss_filename_width
×
675
            : this->lss_basename_width;
676

NEW
677
        shift_string_attrs(this->lss_token_al.al_attrs, 0, file_offset_end);
×
678

679
        lr.lr_start = 0;
×
680
        lr.lr_end = file_offset_end + 1;
×
NEW
681
        this->lss_token_al.al_attrs.emplace_back(
×
682
            lr,
NEW
683
            VC_STYLE.value(
×
NEW
684
                vc.attrs_for_ident(this->lss_token_file->get_filename())));
×
685
    } else if (this->lss_time_column_size > 0) {
4,395✔
NEW
686
        shift_string_attrs(
×
NEW
687
            this->lss_token_al.al_attrs, 1, this->lss_time_column_size);
×
688

689
        ui_icon_t icon;
690
        switch (this->lss_token_line->get_msg_level()) {
×
691
            case LEVEL_TRACE:
×
692
                icon = ui_icon_t::log_level_trace;
×
693
                break;
×
694
            case LEVEL_DEBUG:
×
695
            case LEVEL_DEBUG2:
696
            case LEVEL_DEBUG3:
697
            case LEVEL_DEBUG4:
698
            case LEVEL_DEBUG5:
699
                icon = ui_icon_t::log_level_debug;
×
700
                break;
×
701
            case LEVEL_INFO:
×
702
                icon = ui_icon_t::log_level_info;
×
703
                break;
×
704
            case LEVEL_STATS:
×
705
                icon = ui_icon_t::log_level_stats;
×
706
                break;
×
707
            case LEVEL_NOTICE:
×
708
                icon = ui_icon_t::log_level_notice;
×
709
                break;
×
710
            case LEVEL_WARNING:
×
711
                icon = ui_icon_t::log_level_warning;
×
712
                break;
×
713
            case LEVEL_ERROR:
×
714
                icon = ui_icon_t::log_level_error;
×
715
                break;
×
716
            case LEVEL_CRITICAL:
×
717
                icon = ui_icon_t::log_level_critical;
×
718
                break;
×
719
            case LEVEL_FATAL:
×
720
                icon = ui_icon_t::log_level_fatal;
×
721
                break;
×
722
            default:
×
723
                icon = ui_icon_t::hidden;
×
724
                break;
×
725
        }
726
        auto extra_space_size = this->lss_time_column_padding;
×
727
        lr.lr_start = 1 + this->lss_time_column_size - 1 - extra_space_size;
×
728
        lr.lr_end = 1 + this->lss_time_column_size - extra_space_size;
×
NEW
729
        this->lss_token_al.al_attrs.emplace_back(lr, VC_ICON.value(icon));
×
730
        if (this->tss_view->is_selectable()
×
731
            && this->tss_view->get_selection() != row)
×
732
        {
733
            lr.lr_start = 1;
×
734
            lr.lr_end = 1 + this->lss_time_column_size - 2 - extra_space_size;
×
NEW
735
            this->lss_token_al.al_attrs.emplace_back(
×
NEW
736
                lr, VC_ROLE.value(role_t::VCR_TIME_COLUMN));
×
737
            if (this->lss_token_line->is_time_skewed()) {
×
NEW
738
                this->lss_token_al.al_attrs.emplace_back(
×
NEW
739
                    lr, VC_ROLE.value(role_t::VCR_SKEWED_TIME));
×
740
            }
741
            lr.lr_start = 1 + this->lss_time_column_size - 2 - extra_space_size;
×
742
            lr.lr_end = 1 + this->lss_time_column_size - 1 - extra_space_size;
×
NEW
743
            this->lss_token_al.al_attrs.emplace_back(
×
744
                lr, VC_ROLE.value(role_t::VCR_TIME_COLUMN_TO_TEXT));
×
745
        }
746
    }
747

748
    if (this->tas_display_time_offset) {
4,395✔
749
        time_offset_end = 13;
210✔
750
        lr.lr_start = 0;
210✔
751
        lr.lr_end = time_offset_end;
210✔
752

753
        shift_string_attrs(this->lss_token_al.al_attrs, 0, time_offset_end);
210✔
754

755
        this->lss_token_al.al_attrs.emplace_back(
210✔
756
            lr, VC_ROLE.value(role_t::VCR_OFFSET_TIME));
420✔
757
        this->lss_token_al.al_attrs.emplace_back(line_range(12, 13),
210✔
758
                                                 VC_GRAPHIC.value(NCACS_VLINE));
420✔
759

760
        auto bar_role = role_t::VCR_NONE;
210✔
761

762
        switch (this->get_line_accel_direction(vis_line_t(row))) {
210✔
763
            case log_accel::direction_t::A_STEADY:
126✔
764
                break;
126✔
765
            case log_accel::direction_t::A_DECEL:
42✔
766
                bar_role = role_t::VCR_DIFF_DELETE;
42✔
767
                break;
42✔
768
            case log_accel::direction_t::A_ACCEL:
42✔
769
                bar_role = role_t::VCR_DIFF_ADD;
42✔
770
                break;
42✔
771
        }
772
        if (bar_role != role_t::VCR_NONE) {
210✔
773
            this->lss_token_al.al_attrs.emplace_back(line_range(12, 13),
84✔
774
                                                     VC_ROLE.value(bar_role));
168✔
775
        }
776
    }
777

778
    lr.lr_start = 0;
4,395✔
779
    lr.lr_end = -1;
4,395✔
780
    this->lss_token_al.al_attrs.emplace_back(
4,395✔
781
        lr, L_FILE.value(this->lss_token_file));
8,790✔
782
    this->lss_token_al.al_attrs.emplace_back(
4,395✔
783
        lr, SA_FORMAT.value(format->get_name()));
8,790✔
784

785
    {
786
        auto line_meta_context = this->get_bookmark_metadata_context(
4,395✔
787
            vis_line_t(row + 1), bookmark_metadata::categories::partition);
788
        if (line_meta_context.bmc_current_metadata) {
4,395✔
789
            lr.lr_start = 0;
8✔
790
            lr.lr_end = -1;
8✔
791
            this->lss_token_al.al_attrs.emplace_back(
8✔
792
                lr,
793
                L_PARTITION.value(
16✔
794
                    line_meta_context.bmc_current_metadata.value()));
795
        }
796

797
        auto line_meta_opt = this->find_bookmark_metadata(vis_line_t(row));
4,395✔
798

799
        if (line_meta_opt) {
4,395✔
800
            lr.lr_start = 0;
25✔
801
            lr.lr_end = -1;
25✔
802
            this->lss_token_al.al_attrs.emplace_back(
25✔
803
                lr, L_META.value(line_meta_opt.value()));
50✔
804
        }
805
    }
806

807
    auto src_file_attr
808
        = get_string_attr(this->lss_token_al.al_attrs, SA_SRC_FILE);
4,395✔
809
    if (src_file_attr) {
4,395✔
810
        auto lr = src_file_attr->saw_string_attr->sa_range;
23✔
811
        lr.lr_end = lr.lr_start + 1;
23✔
812
        this->lss_token_al.al_attrs.emplace_back(
23✔
813
            lr, VC_STYLE.value(text_attrs::with_underline()));
46✔
814
        this->lss_token_al.al_attrs.emplace_back(lr,
23✔
815
                                                 VC_COMMAND.value(ui_command{
92✔
816
                                                     source_location{},
817
                                                     ":toggle-breakpoint",
818
                                                 }));
819
    }
820

821
    if (this->lss_time_column_size == 0) {
4,395✔
822
        if (this->lss_token_file->is_time_adjusted()) {
4,395✔
823
            auto time_range = find_string_attr_range(
17✔
824
                this->lss_token_al.al_attrs, &L_TIMESTAMP);
17✔
825

826
            if (time_range.lr_end != -1) {
17✔
827
                this->lss_token_al.al_attrs.emplace_back(
15✔
828
                    time_range, VC_ROLE.value(role_t::VCR_ADJUSTED_TIME));
30✔
829
            }
830
        } else if (this->lss_token_line->is_time_skewed()) {
4,378✔
831
            auto time_range = find_string_attr_range(
8✔
832
                this->lss_token_al.al_attrs, &L_TIMESTAMP);
8✔
833

834
            if (time_range.lr_end != -1) {
8✔
835
                this->lss_token_al.al_attrs.emplace_back(
8✔
836
                    time_range, VC_ROLE.value(role_t::VCR_SKEWED_TIME));
16✔
837
            }
838
        }
839
    }
840

841
    if (this->tss_preview_min_log_level
4,395✔
842
        && this->lss_token_line->get_msg_level()
4,395✔
843
            < this->tss_preview_min_log_level)
4,395✔
844
    {
845
        auto color = styling::color_unit::from_palette(
×
846
            lnav::enums::to_underlying(ansi_color::red));
×
NEW
847
        this->lss_token_al.al_attrs.emplace_back(line_range{0, 1},
×
NEW
848
                                                 VC_BACKGROUND.value(color));
×
849
    }
850
    if (this->ttt_preview_min_time
4,395✔
851
        && this->lss_token_line->get_timeval() < this->ttt_preview_min_time)
4,395✔
852
    {
853
        auto color = styling::color_unit::from_palette(
×
854
            lnav::enums::to_underlying(ansi_color::red));
×
NEW
855
        this->lss_token_al.al_attrs.emplace_back(line_range{0, 1},
×
NEW
856
                                                 VC_BACKGROUND.value(color));
×
857
    }
858
    if (this->ttt_preview_max_time
4,395✔
859
        && this->ttt_preview_max_time < this->lss_token_line->get_timeval())
4,395✔
860
    {
861
        auto color = styling::color_unit::from_palette(
×
862
            lnav::enums::to_underlying(ansi_color::red));
×
NEW
863
        this->lss_token_al.al_attrs.emplace_back(line_range{0, 1},
×
NEW
864
                                                 VC_BACKGROUND.value(color));
×
865
    }
866
    if (!this->lss_token_line->is_continued()) {
4,395✔
867
        if (this->lss_preview_filter_stmt != nullptr) {
2,556✔
868
            auto color = styling::color_unit::EMPTY;
×
869
            auto eval_res
870
                = this->eval_sql_filter(this->lss_preview_filter_stmt.in(),
871
                                        this->lss_token_file_data,
872
                                        this->lss_token_line);
×
873
            if (eval_res.isErr()) {
×
874
                color = palette_color{
×
875
                    lnav::enums::to_underlying(ansi_color::yellow)};
×
NEW
876
                this->lss_token_al.al_attrs.emplace_back(
×
877
                    line_range{0, -1},
×
878
                    SA_ERROR.value(
×
879
                        eval_res.unwrapErr().to_attr_line().get_string()));
×
880
            } else {
881
                auto matched = eval_res.unwrap();
×
882

883
                if (matched) {
×
884
                    color = palette_color{
×
885
                        lnav::enums::to_underlying(ansi_color::green)};
×
886
                } else {
887
                    color = palette_color{
×
888
                        lnav::enums::to_underlying(ansi_color::red)};
×
NEW
889
                    this->lss_token_al.al_attrs.emplace_back(
×
890
                        line_range{0, 1},
×
891
                        VC_STYLE.value(text_attrs::with_blink()));
×
892
                }
893
            }
NEW
894
            this->lss_token_al.al_attrs.emplace_back(
×
NEW
895
                line_range{0, 1}, VC_BACKGROUND.value(color));
×
896
        }
897

898
        auto sql_filter_opt = this->get_sql_filter();
2,556✔
899
        if (sql_filter_opt) {
2,556✔
900
            auto* sf = (sql_filter*) sql_filter_opt.value().get();
36✔
901
            auto eval_res = this->eval_sql_filter(sf->sf_filter_stmt.in(),
902
                                                  this->lss_token_file_data,
903
                                                  this->lss_token_line);
36✔
904
            if (eval_res.isErr()) {
36✔
905
                auto msg = fmt::format(
906
                    FMT_STRING(
×
907
                        "filter expression evaluation failed with -- {}"),
908
                    eval_res.unwrapErr().to_attr_line().get_string());
×
909
                auto cu = styling::color_unit::from_palette(palette_color{
×
910
                    lnav::enums::to_underlying(ansi_color::yellow)});
NEW
911
                this->lss_token_al.al_attrs.emplace_back(line_range{0, -1},
×
NEW
912
                                                         SA_ERROR.value(msg));
×
NEW
913
                this->lss_token_al.al_attrs.emplace_back(
×
NEW
914
                    line_range{0, 1}, VC_BACKGROUND.value(cu));
×
915
            }
916
        }
36✔
917
    }
2,556✔
918

919
    value_out = std::move(this->lss_token_al.al_attrs);
4,395✔
920
}
921

922
struct logline_cmp {
923
    explicit logline_cmp(logfile_sub_source& lc) : llss_controller(lc) {}
1,158✔
924

925
    bool operator()(const logfile_sub_source::indexed_content& lhs,
105,196✔
926
                    const logfile_sub_source::indexed_content& rhs) const
927
    {
928
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
105,196✔
929
        const auto* ll_rhs = this->llss_controller.find_line(rhs.value());
105,196✔
930

931
        return (*ll_lhs) < (*ll_rhs);
105,196✔
932
    }
933

934
    bool operator()(const logfile_sub_source::indexed_content& lhs,
×
935
                    const timeval& rhs) const
936
    {
937
        const auto* ll_lhs = this->llss_controller.find_line(lhs.value());
×
938

939
        return *ll_lhs < rhs;
×
940
    }
941

942
    logfile_sub_source& llss_controller;
943
};
944

945
logfile_sub_source::rebuild_result
946
logfile_sub_source::rebuild_index(std::optional<ui_clock::time_point> deadline)
4,507✔
947
{
948
    if (this->tss_view == nullptr) {
4,507✔
949
        return rebuild_result::rr_no_change;
124✔
950
    }
951

952
    this->lss_indexing_in_progress = true;
4,383✔
953
    auto fin = finally([this]() { this->lss_indexing_in_progress = false; });
4,383✔
954

955
    iterator iter;
4,383✔
956
    size_t total_lines = 0;
4,383✔
957
    size_t est_remaining_lines = 0;
4,383✔
958
    auto all_time_ordered_formats = true;
4,383✔
959
    auto full_sort = this->lss_index.empty();
4,383✔
960
    int file_count = 0;
4,383✔
961
    auto force = std::exchange(this->lss_force_rebuild, false);
4,383✔
962
    auto retval = rebuild_result::rr_no_change;
4,383✔
963
    std::optional<timeval> lowest_tv = std::nullopt;
4,383✔
964
    auto search_start = 0_vl;
4,383✔
965

966
    if (force) {
4,383✔
967
        log_debug("forced to full rebuild");
488✔
968
        retval = rebuild_result::rr_full_rebuild;
488✔
969
        full_sort = true;
488✔
970
        this->tss_level_filtered_count = 0;
488✔
971
        this->lss_index.clear();
488✔
972
    }
973

974
    std::vector<size_t> file_order(this->lss_files.size());
4,383✔
975

976
    for (size_t lpc = 0; lpc < file_order.size(); lpc++) {
7,428✔
977
        file_order[lpc] = lpc;
3,045✔
978
    }
979
    if (!this->lss_index.empty()) {
4,383✔
980
        std::stable_sort(file_order.begin(),
2,276✔
981
                         file_order.end(),
982
                         [this](const auto& left, const auto& right) {
255✔
983
                             const auto& left_ld = this->lss_files[left];
255✔
984
                             const auto& right_ld = this->lss_files[right];
255✔
985

986
                             if (left_ld->get_file_ptr() == nullptr) {
255✔
987
                                 return true;
×
988
                             }
989
                             if (right_ld->get_file_ptr() == nullptr) {
255✔
990
                                 return false;
3✔
991
                             }
992

993
                             return left_ld->get_file_ptr()->back()
252✔
994
                                 < right_ld->get_file_ptr()->back();
504✔
995
                         });
996
    }
997

998
    bool time_left = true;
4,383✔
999
    this->lss_all_timestamp_flags = 0;
4,383✔
1000
    for (const auto file_index : file_order) {
7,428✔
1001
        auto& ld = *(this->lss_files[file_index]);
3,045✔
1002
        auto* lf = ld.get_file_ptr();
3,045✔
1003

1004
        if (lf == nullptr) {
3,045✔
1005
            if (ld.ld_lines_indexed > 0) {
4✔
1006
                log_debug("%zu: file closed, doing full rebuild",
1✔
1007
                          ld.ld_file_index);
1008
                force = true;
1✔
1009
                retval = rebuild_result::rr_full_rebuild;
1✔
1010
                full_sort = true;
1✔
1011
            }
1012
        } else {
1013
            if (!lf->get_format_ptr()->lf_time_ordered) {
3,041✔
1014
                all_time_ordered_formats = false;
183✔
1015
            }
1016
            if (time_left && deadline && ui_clock::now() > deadline.value()) {
3,041✔
1017
                log_debug("no time left, skipping %s",
2✔
1018
                          lf->get_filename_as_string().c_str());
1019
                time_left = false;
2✔
1020
            }
1021
            this->lss_all_timestamp_flags
3,041✔
1022
                |= lf->get_format_ptr()->lf_timestamp_flags;
3,041✔
1023

1024
            if (!this->tss_view->is_paused() && time_left) {
3,041✔
1025
                auto log_rebuild_res = lf->rebuild_index(deadline);
3,039✔
1026

1027
                if (ld.ld_lines_indexed < lf->size()
3,039✔
1028
                    && log_rebuild_res
3,039✔
1029
                        == logfile::rebuild_result_t::NO_NEW_LINES)
1030
                {
1031
                    // This is a bit awkward... if the logfile indexing was
1032
                    // complete before being added to us, we need to adjust
1033
                    // the rebuild result to make it look like new lines
1034
                    // were added.
1035
                    log_rebuild_res = logfile::rebuild_result_t::NEW_LINES;
51✔
1036
                }
1037
                switch (log_rebuild_res) {
3,039✔
1038
                    case logfile::rebuild_result_t::NO_NEW_LINES:
2,487✔
1039
                        break;
2,487✔
1040
                    case logfile::rebuild_result_t::NEW_LINES:
493✔
1041
                        if (retval == rebuild_result::rr_no_change) {
493✔
1042
                            retval = rebuild_result::rr_appended_lines;
442✔
1043
                        }
1044
                        log_debug("new lines for %s:%zu",
493✔
1045
                                  lf->get_filename_as_string().c_str(),
1046
                                  lf->size());
1047
                        if (!this->lss_index.empty()
493✔
1048
                            && lf->size() > ld.ld_lines_indexed)
493✔
1049
                        {
1050
                            auto& new_file_line = (*lf)[ld.ld_lines_indexed];
×
1051
                            auto cl = this->lss_index.back().value();
×
1052
                            auto* last_indexed_line = this->find_line(cl);
×
1053

1054
                            // If there are new lines that are older than what
1055
                            // we have in the index, we need to resort.
1056
                            if (last_indexed_line == nullptr
×
1057
                                || new_file_line
×
1058
                                    < last_indexed_line->get_timeval())
×
1059
                            {
1060
                                log_debug(
×
1061
                                    "%s:%ld: found older lines, full "
1062
                                    "rebuild: %p  %lld < %lld",
1063
                                    lf->get_filename().c_str(),
1064
                                    ld.ld_lines_indexed,
1065
                                    last_indexed_line,
1066
                                    new_file_line
1067
                                        .get_time<std::chrono::microseconds>()
1068
                                        .count(),
1069
                                    last_indexed_line == nullptr
1070
                                        ? (uint64_t) -1
1071
                                        : last_indexed_line
1072
                                              ->get_time<
1073
                                                  std::chrono::microseconds>()
1074
                                              .count());
1075
                                if (retval <= rebuild_result::rr_partial_rebuild
×
1076
                                    && all_time_ordered_formats)
×
1077
                                {
1078
                                    retval = rebuild_result::rr_partial_rebuild;
×
1079
                                    if (!lowest_tv
×
1080
                                        || new_file_line.get_timeval()
×
1081
                                            < lowest_tv.value())
×
1082
                                    {
1083
                                        lowest_tv = new_file_line.get_timeval();
×
1084
                                    }
1085
                                } else {
1086
                                    log_debug(
×
1087
                                        "already doing full rebuild, doing "
1088
                                        "full_sort as well");
1089
                                    force = true;
×
1090
                                    full_sort = true;
×
1091
                                }
1092
                            }
1093
                        }
1094
                        break;
493✔
1095
                    case logfile::rebuild_result_t::INVALID:
59✔
1096
                    case logfile::rebuild_result_t::NEW_ORDER:
1097
                        log_debug("%s: log file has a new order, full rebuild",
59✔
1098
                                  lf->get_filename().c_str());
1099
                        retval = rebuild_result::rr_full_rebuild;
59✔
1100
                        force = true;
59✔
1101
                        full_sort = true;
59✔
1102
                        break;
59✔
1103
                }
1104
            }
1105
            file_count += 1;
3,041✔
1106
            total_lines += lf->size();
3,041✔
1107

1108
            est_remaining_lines += lf->estimated_remaining_lines();
3,041✔
1109
        }
1110
    }
1111

1112
    if (!all_time_ordered_formats
4,383✔
1113
        && retval == rebuild_result::rr_partial_rebuild)
177✔
1114
    {
1115
        force = true;
×
1116
        full_sort = true;
×
1117
        retval = rebuild_result::rr_full_rebuild;
×
1118
    }
1119

1120
    if (this->lss_index.reserve(total_lines + est_remaining_lines)) {
4,383✔
1121
        // The index array was reallocated, just do a full sort/rebuild since
1122
        // it's been cleared out.
1123
        log_debug("expanding index capacity %zu", this->lss_index.ba_capacity);
644✔
1124
        force = true;
644✔
1125
        retval = rebuild_result::rr_full_rebuild;
644✔
1126
        full_sort = true;
644✔
1127
        this->tss_level_filtered_count = 0;
644✔
1128
    }
1129

1130
    auto& vis_bm = this->tss_view->get_bookmarks();
4,383✔
1131

1132
    if (force) {
4,383✔
1133
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,695✔
1134
             iter++)
548✔
1135
        {
1136
            (*iter)->ld_lines_indexed = 0;
548✔
1137
        }
1138

1139
        this->lss_index.clear();
1,147✔
1140
        this->lss_filtered_index.clear();
1,147✔
1141
        this->tss_level_filtered_count = 0;
1,147✔
1142
        this->lss_longest_line = 0;
1,147✔
1143
        this->lss_basename_width = 0;
1,147✔
1144
        this->lss_filename_width = 0;
1,147✔
1145
        vis_bm[&textview_curses::BM_USER_EXPR].clear();
1,147✔
1146
        if (this->lss_index_delegate) {
1,147✔
1147
            this->lss_index_delegate->index_start(*this);
1,147✔
1148
        }
1149
    } else if (retval == rebuild_result::rr_partial_rebuild) {
3,236✔
1150
        size_t remaining = 0;
×
1151

1152
        log_debug("partial rebuild with lowest time: %ld",
×
1153
                  lowest_tv.value().tv_sec);
1154
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1155
             iter++)
×
1156
        {
1157
            logfile_data& ld = *(*iter);
×
1158
            auto* lf = ld.get_file_ptr();
×
1159

1160
            if (lf == nullptr) {
×
1161
                continue;
×
1162
            }
1163

1164
            require(lf->get_format_ptr()->lf_time_ordered);
×
1165

1166
            auto line_iter = lf->find_from_time(lowest_tv.value());
×
1167

1168
            if (line_iter) {
×
1169
                log_debug("lowest line time %ld; line %ld; size %ld; path=%s",
×
1170
                          line_iter.value()->get_timeval().tv_sec,
1171
                          std::distance(lf->cbegin(), line_iter.value()),
1172
                          lf->size(),
1173
                          lf->get_filename_as_string().c_str());
1174
            }
1175
            ld.ld_lines_indexed
1176
                = std::distance(lf->cbegin(), line_iter.value_or(lf->cend()));
×
1177
            remaining += lf->size() - ld.ld_lines_indexed;
×
1178
        }
1179

1180
        auto* row_iter = std::lower_bound(this->lss_index.begin(),
×
1181
                                          this->lss_index.end(),
1182
                                          lowest_tv.value(),
×
1183
                                          logline_cmp(*this));
1184
        this->lss_index.shrink_to(
×
1185
            std::distance(this->lss_index.begin(), row_iter));
×
1186
        log_debug("new index size %ld/%ld; remain %ld",
×
1187
                  this->lss_index.ba_size,
1188
                  this->lss_index.ba_capacity,
1189
                  remaining);
1190
        auto filt_row_iter = std::lower_bound(this->lss_filtered_index.begin(),
×
1191
                                              this->lss_filtered_index.end(),
1192
                                              lowest_tv.value(),
×
1193
                                              filtered_logline_cmp(*this));
1194
        this->lss_filtered_index.resize(
×
1195
            std::distance(this->lss_filtered_index.begin(), filt_row_iter));
×
1196
        search_start = vis_line_t(this->lss_filtered_index.size());
×
1197

1198
        if (this->lss_index_delegate) {
×
1199
            this->lss_index_delegate->index_start(*this);
×
1200
            for (const auto row_in_full_index : this->lss_filtered_index) {
×
1201
                auto cl = this->lss_index[row_in_full_index].value();
×
1202
                uint64_t line_number;
1203
                auto ld_iter = this->find_data(cl, line_number);
×
1204
                auto& ld = *ld_iter;
×
1205
                auto line_iter = ld->get_file_ptr()->begin() + line_number;
×
1206

1207
                this->lss_index_delegate->index_line(
×
1208
                    *this, ld->get_file_ptr(), line_iter);
1209
            }
1210
        }
1211
    }
1212

1213
    if (this->lss_index.empty() && !time_left) {
4,383✔
1214
        log_info("ran out of time, skipping rebuild");
×
1215
        // need to make sure we rebuild in case no new data comes in
1216
        this->lss_force_rebuild = true;
×
1217
        return rebuild_result::rr_appended_lines;
×
1218
    }
1219

1220
    if (retval != rebuild_result::rr_no_change || force) {
4,383✔
1221
        size_t index_size = 0, start_size = this->lss_index.size();
1,158✔
1222
        logline_cmp line_cmper(*this);
1,158✔
1223

1224
        for (auto& ld : this->lss_files) {
1,720✔
1225
            auto* lf = ld->get_file_ptr();
562✔
1226

1227
            if (lf == nullptr) {
562✔
1228
                continue;
1✔
1229
            }
1230
            this->lss_longest_line = std::max(
1,122✔
1231
                this->lss_longest_line, lf->get_longest_line_length() + 1);
561✔
1232
            this->lss_basename_width
1233
                = std::max(this->lss_basename_width,
1,122✔
1234
                           lf->get_unique_path().native().size());
561✔
1235
            this->lss_filename_width = std::max(
1,122✔
1236
                this->lss_filename_width, lf->get_filename().native().size());
561✔
1237
        }
1238

1239
        if (full_sort) {
1,158✔
1240
            log_trace("rebuild_index full sort");
1,158✔
1241
            for (auto& ld : this->lss_files) {
1,720✔
1242
                auto* lf = ld->get_file_ptr();
562✔
1243

1244
                if (lf == nullptr) {
562✔
1245
                    continue;
1✔
1246
                }
1247

1248
                for (size_t line_index = 0; line_index < lf->size();
14,859✔
1249
                     line_index++)
1250
                {
1251
                    const auto lf_iter
1252
                        = ld->get_file_ptr()->begin() + line_index;
14,298✔
1253
                    if (lf_iter->is_ignored()) {
14,298✔
1254
                        continue;
414✔
1255
                    }
1256

1257
                    content_line_t con_line(
1258
                        ld->ld_file_index * MAX_LINES_PER_FILE + line_index);
13,884✔
1259

1260
                    if (lf_iter->is_meta_marked()) {
13,884✔
1261
                        auto start_iter = lf_iter;
12✔
1262
                        while (start_iter->is_continued()) {
12✔
1263
                            --start_iter;
×
1264
                        }
1265
                        int start_index
1266
                            = start_iter - ld->get_file_ptr()->begin();
12✔
1267
                        content_line_t start_con_line(ld->ld_file_index
12✔
1268
                                                          * MAX_LINES_PER_FILE
12✔
1269
                                                      + start_index);
12✔
1270

1271
                        auto& line_meta
1272
                            = ld->get_file_ptr()
1273
                                  ->get_bookmark_metadata()[start_index];
12✔
1274
                        if (line_meta.has(bookmark_metadata::categories::notes))
12✔
1275
                        {
1276
                            this->lss_user_marks[&textview_curses::BM_META]
4✔
1277
                                .insert_once(start_con_line);
4✔
1278
                        }
1279
                        if (line_meta.has(
12✔
1280
                                bookmark_metadata::categories::partition))
1281
                        {
1282
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
8✔
1283
                                .insert_once(start_con_line);
8✔
1284
                        }
1285
                    }
1286
                    this->lss_index.push_back(
13,884✔
1287
                        indexed_content{con_line, lf_iter});
27,768✔
1288
                }
1289
            }
1290

1291
            // XXX get rid of this full sort on the initial run, it's not
1292
            // needed unless the file is not in time-order
1293
            if (this->lss_sorting_observer) {
1,158✔
1294
                this->lss_sorting_observer(*this, 0, this->lss_index.size());
3✔
1295
            }
1296
            std::sort(
1,158✔
1297
                this->lss_index.begin(), this->lss_index.end(), line_cmper);
1298
            if (this->lss_sorting_observer) {
1,158✔
1299
                this->lss_sorting_observer(
6✔
1300
                    *this, this->lss_index.size(), this->lss_index.size());
3✔
1301
            }
1302
        } else {
1303
            kmerge_tree_c<logline, logfile_data, logfile::iterator> merge(
1304
                file_count);
×
1305

1306
            for (iter = this->lss_files.begin(); iter != this->lss_files.end();
×
1307
                 iter++)
×
1308
            {
1309
                auto* ld = iter->get();
×
1310
                auto* lf = ld->get_file_ptr();
×
1311
                if (lf == nullptr) {
×
1312
                    continue;
×
1313
                }
1314

1315
                merge.add(ld, lf->begin() + ld->ld_lines_indexed, lf->end());
×
1316
                index_size += lf->size();
×
1317
            }
1318

1319
            file_off_t index_off = 0;
×
1320
            merge.execute();
×
1321
            if (this->lss_sorting_observer) {
×
1322
                this->lss_sorting_observer(*this, index_off, index_size);
×
1323
            }
1324
            log_trace("k-way merge");
×
1325
            for (;;) {
1326
                logfile::iterator lf_iter;
×
1327
                logfile_data* ld;
1328

1329
                if (!merge.get_top(ld, lf_iter)) {
×
1330
                    break;
×
1331
                }
1332

1333
                if (!lf_iter->is_ignored()) {
×
1334
                    int file_index = ld->ld_file_index;
×
1335
                    int line_index = lf_iter - ld->get_file_ptr()->begin();
×
1336

1337
                    content_line_t con_line(file_index * MAX_LINES_PER_FILE
×
1338
                                            + line_index);
×
1339

1340
                    if (lf_iter->is_meta_marked()) {
×
1341
                        auto start_iter = lf_iter;
×
1342
                        while (start_iter->is_continued()) {
×
1343
                            --start_iter;
×
1344
                        }
1345
                        int start_index
1346
                            = start_iter - ld->get_file_ptr()->begin();
×
1347
                        content_line_t start_con_line(
1348
                            file_index * MAX_LINES_PER_FILE + start_index);
×
1349

1350
                        auto& line_meta
1351
                            = ld->get_file_ptr()
1352
                                  ->get_bookmark_metadata()[start_index];
×
1353
                        if (line_meta.has(bookmark_metadata::categories::notes))
×
1354
                        {
1355
                            this->lss_user_marks[&textview_curses::BM_META]
×
1356
                                .insert_once(start_con_line);
×
1357
                        }
1358
                        if (line_meta.has(
×
1359
                                bookmark_metadata::categories::partition))
1360
                        {
1361
                            this->lss_user_marks[&textview_curses::BM_PARTITION]
×
1362
                                .insert_once(start_con_line);
×
1363
                        }
1364
                    }
1365
                    this->lss_index.push_back(
×
1366
                        indexed_content{con_line, lf_iter});
×
1367
                }
1368

1369
                merge.next();
×
1370
                index_off += 1;
×
1371
                if (index_off % 100000 == 0 && this->lss_sorting_observer) {
×
1372
                    this->lss_sorting_observer(*this, index_off, index_size);
×
1373
                }
1374
            }
1375
            if (this->lss_sorting_observer) {
×
1376
                this->lss_sorting_observer(*this, index_size, index_size);
×
1377
            }
1378
        }
1379

1380
        for (iter = this->lss_files.begin(); iter != this->lss_files.end();
1,720✔
1381
             ++iter)
562✔
1382
        {
1383
            auto* lf = (*iter)->get_file_ptr();
562✔
1384

1385
            if (lf == nullptr) {
562✔
1386
                continue;
1✔
1387
            }
1388

1389
            (*iter)->ld_lines_indexed = lf->size();
561✔
1390
        }
1391

1392
        this->lss_filtered_index.reserve(this->lss_index.size());
1,158✔
1393

1394
        uint32_t filter_in_mask, filter_out_mask;
1395
        this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
1,158✔
1396

1397
        if (start_size == 0 && this->lss_index_delegate != nullptr) {
1,158✔
1398
            this->lss_index_delegate->index_start(*this);
1,158✔
1399
        }
1400

1401
        log_trace("filtered index");
1,158✔
1402
        for (size_t index_index = start_size;
15,042✔
1403
             index_index < this->lss_index.size();
15,042✔
1404
             index_index++)
1405
        {
1406
            const auto cl = this->lss_index[index_index].value();
13,884✔
1407
            uint64_t line_number;
1408
            auto ld = this->find_data(cl, line_number);
13,884✔
1409

1410
            if (!(*ld)->is_visible()) {
13,884✔
1411
                continue;
×
1412
            }
1413

1414
            auto* lf = (*ld)->get_file_ptr();
13,884✔
1415
            auto line_iter = lf->begin() + line_number;
13,884✔
1416

1417
            if (line_iter->is_ignored()) {
13,884✔
1418
                continue;
×
1419
            }
1420

1421
            if (!this->tss_apply_filters
27,768✔
1422
                || (!(*ld)->ld_filter_state.excluded(
27,738✔
1423
                        filter_in_mask, filter_out_mask, line_number)
1424
                    && this->check_extra_filters(ld, line_iter)))
13,854✔
1425
            {
1426
                auto eval_res = this->eval_sql_filter(
1427
                    this->lss_marker_stmt.in(), ld, line_iter);
13,854✔
1428
                if (eval_res.isErr()) {
13,854✔
1429
                    line_iter->set_expr_mark(false);
×
1430
                } else {
1431
                    auto matched = eval_res.unwrap();
13,854✔
1432

1433
                    line_iter->set_expr_mark(matched);
13,854✔
1434
                    if (matched) {
13,854✔
1435
                        vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1436
                            vis_line_t(this->lss_filtered_index.size()));
×
1437
                        this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1438
                            .insert_once(cl);
×
1439
                    }
1440
                }
1441
                this->lss_filtered_index.push_back(index_index);
13,854✔
1442
                if (this->lss_index_delegate != nullptr) {
13,854✔
1443
                    this->lss_index_delegate->index_line(*this, lf, line_iter);
13,854✔
1444
                }
1445
            }
13,854✔
1446
        }
1447

1448
        this->lss_indexing_in_progress = false;
1,158✔
1449

1450
        if (this->lss_index_delegate != nullptr) {
1,158✔
1451
            this->lss_index_delegate->index_complete(*this);
1,158✔
1452
        }
1453
    }
1454

1455
    switch (retval) {
4,383✔
1456
        case rebuild_result::rr_no_change:
3,225✔
1457
            break;
3,225✔
1458
        case rebuild_result::rr_full_rebuild:
1,147✔
1459
            log_debug("redoing search");
1,147✔
1460
            this->lss_index_generation += 1;
1,147✔
1461
            this->tss_view->reload_data();
1,147✔
1462
            this->tss_view->redo_search();
1,147✔
1463
            break;
1,147✔
1464
        case rebuild_result::rr_partial_rebuild:
×
1465
            log_debug("redoing search from: %d", (int) search_start);
×
1466
            this->lss_index_generation += 1;
×
1467
            this->tss_view->reload_data();
×
1468
            this->tss_view->search_new_data(search_start);
×
1469
            break;
×
1470
        case rebuild_result::rr_appended_lines:
11✔
1471
            this->tss_view->reload_data();
11✔
1472
            this->tss_view->search_new_data();
11✔
1473
            break;
11✔
1474
    }
1475

1476
    return retval;
4,383✔
1477
}
4,383✔
1478

1479
void
1480
logfile_sub_source::text_update_marks(vis_bookmarks& bm)
2,241✔
1481
{
1482
    logfile* last_file = nullptr;
2,241✔
1483
    vis_line_t vl;
2,241✔
1484

1485
    auto& bm_warnings = bm[&textview_curses::BM_WARNINGS];
2,241✔
1486
    auto& bm_errors = bm[&textview_curses::BM_ERRORS];
2,241✔
1487
    auto& bm_files = bm[&BM_FILES];
2,241✔
1488

1489
    bm_warnings.clear();
2,241✔
1490
    bm_errors.clear();
2,241✔
1491
    bm_files.clear();
2,241✔
1492

1493
    std::vector<const bookmark_type_t*> used_marks;
2,241✔
1494
    for (const auto* bmt :
11,205✔
1495
         {
1496
             &textview_curses::BM_USER,
1497
             &textview_curses::BM_USER_EXPR,
1498
             &textview_curses::BM_PARTITION,
1499
             &textview_curses::BM_META,
1500
         })
13,446✔
1501
    {
1502
        bm[bmt].clear();
8,964✔
1503
        if (!this->lss_user_marks[bmt].empty()) {
8,964✔
1504
            used_marks.emplace_back(bmt);
118✔
1505
        }
1506
    }
1507

1508
    for (; vl < (int) this->lss_filtered_index.size(); ++vl) {
19,643✔
1509
        const auto& orig_ic = this->lss_index[this->lss_filtered_index[vl]];
17,402✔
1510
        auto cl = orig_ic.value();
17,402✔
1511
        auto* lf = this->find_file_ptr(cl);
17,402✔
1512

1513
        for (const auto& bmt : used_marks) {
19,540✔
1514
            auto& user_mark = this->lss_user_marks[bmt];
2,138✔
1515
            if (user_mark.bv_tree.exists(orig_ic.value())) {
2,138✔
1516
                bm[bmt].insert_once(vl);
156✔
1517
            }
1518
        }
1519

1520
        if (lf != last_file) {
17,402✔
1521
            bm_files.insert_once(vl);
976✔
1522
        }
1523

1524
        switch (orig_ic.level()) {
17,402✔
1525
            case indexed_content::level_t::warning:
94✔
1526
                bm_warnings.insert_once(vl);
94✔
1527
                break;
94✔
1528

1529
            case indexed_content::level_t::error:
3,430✔
1530
                bm_errors.insert_once(vl);
3,430✔
1531
                break;
3,430✔
1532

1533
            default:
13,878✔
1534
                break;
13,878✔
1535
        }
1536

1537
        last_file = lf;
17,402✔
1538
    }
1539
}
2,241✔
1540

1541
void
1542
logfile_sub_source::text_filters_changed()
166✔
1543
{
1544
    this->lss_index_generation += 1;
166✔
1545
    this->tss_level_filtered_count = 0;
166✔
1546

1547
    if (this->lss_line_meta_changed) {
166✔
1548
        this->invalidate_sql_filter();
24✔
1549
        this->lss_line_meta_changed = false;
24✔
1550
    }
1551

1552
    for (auto& ld : *this) {
284✔
1553
        auto* lf = ld->get_file_ptr();
118✔
1554

1555
        if (lf != nullptr) {
118✔
1556
            ld->ld_filter_state.clear_deleted_filter_state();
118✔
1557
            lf->reobserve_from(lf->begin()
118✔
1558
                               + ld->ld_filter_state.get_min_count(lf->size()));
118✔
1559
        }
1560
    }
1561

1562
    if (this->lss_force_rebuild) {
166✔
1563
        return;
×
1564
    }
1565

1566
    auto& vis_bm = this->tss_view->get_bookmarks();
166✔
1567
    uint32_t filtered_in_mask, filtered_out_mask;
1568

1569
    this->get_filters().get_enabled_mask(filtered_in_mask, filtered_out_mask);
166✔
1570

1571
    if (this->lss_index_delegate != nullptr) {
166✔
1572
        this->lss_index_delegate->index_start(*this);
166✔
1573
    }
1574
    vis_bm[&textview_curses::BM_USER_EXPR].clear();
166✔
1575

1576
    this->lss_filtered_index.clear();
166✔
1577
    for (size_t index_index = 0; index_index < this->lss_index.size();
1,495✔
1578
         index_index++)
1579
    {
1580
        auto cl = this->lss_index[index_index].value();
1,329✔
1581
        uint64_t line_number;
1582
        auto ld = this->find_data(cl, line_number);
1,329✔
1583

1584
        if (!(*ld)->is_visible()) {
1,329✔
1585
            continue;
213✔
1586
        }
1587

1588
        auto lf = (*ld)->get_file_ptr();
1,116✔
1589
        auto line_iter = lf->begin() + line_number;
1,116✔
1590

1591
        if (!this->tss_apply_filters
2,232✔
1592
            || (!(*ld)->ld_filter_state.excluded(
2,095✔
1593
                    filtered_in_mask, filtered_out_mask, line_number)
1594
                && this->check_extra_filters(ld, line_iter)))
979✔
1595
        {
1596
            auto eval_res = this->eval_sql_filter(
1597
                this->lss_marker_stmt.in(), ld, line_iter);
937✔
1598
            if (eval_res.isErr()) {
937✔
1599
                line_iter->set_expr_mark(false);
×
1600
            } else {
1601
                auto matched = eval_res.unwrap();
937✔
1602

1603
                line_iter->set_expr_mark(matched);
937✔
1604
                if (matched) {
937✔
1605
                    vis_bm[&textview_curses::BM_USER_EXPR].insert_once(
×
1606
                        vis_line_t(this->lss_filtered_index.size()));
×
1607
                    this->lss_user_marks[&textview_curses::BM_USER_EXPR]
×
1608
                        .insert_once(cl);
×
1609
                }
1610
            }
1611
            this->lss_filtered_index.push_back(index_index);
937✔
1612
            if (this->lss_index_delegate != nullptr) {
937✔
1613
                this->lss_index_delegate->index_line(*this, lf, line_iter);
937✔
1614
            }
1615
        }
937✔
1616
    }
1617

1618
    if (this->lss_index_delegate != nullptr) {
166✔
1619
        this->lss_index_delegate->index_complete(*this);
166✔
1620
    }
1621

1622
    if (this->tss_view != nullptr) {
166✔
1623
        this->tss_view->reload_data();
166✔
1624
        this->tss_view->redo_search();
166✔
1625
    }
1626
}
1627

1628
std::optional<json_string>
1629
logfile_sub_source::text_row_details(const textview_curses& tc)
29✔
1630
{
1631
    if (this->lss_index.empty()) {
29✔
1632
        log_trace("logfile_sub_source::text_row_details empty");
1✔
1633
        return std::nullopt;
1✔
1634
    }
1635

1636
    auto ov_sel = tc.get_overlay_selection();
28✔
1637
    if (ov_sel.has_value()) {
28✔
1638
        auto* fos
1639
            = dynamic_cast<field_overlay_source*>(tc.get_overlay_source());
×
1640
        auto iter = fos->fos_row_to_field_meta.find(ov_sel.value());
×
1641
        if (iter != fos->fos_row_to_field_meta.end()) {
×
1642
            auto find_res = this->find_line_with_file(tc.get_top());
×
1643
            if (find_res) {
×
1644
                yajlpp_gen gen;
×
1645

1646
                {
1647
                    yajlpp_map root(gen);
×
1648

1649
                    root.gen("value");
×
1650
                    root.gen(iter->second.ri_value);
×
1651
                }
1652

1653
                return json_string(gen);
×
1654
            }
1655
        }
1656
    }
1657

1658
    return std::nullopt;
28✔
1659
}
1660

1661
bool
1662
logfile_sub_source::list_input_handle_key(listview_curses& lv,
×
1663
                                          const ncinput& ch)
1664
{
1665
    switch (ch.eff_text[0]) {
×
1666
        case ' ': {
×
1667
            auto ov_vl = lv.get_overlay_selection();
×
1668
            if (ov_vl) {
×
1669
                auto* fos = dynamic_cast<field_overlay_source*>(
×
1670
                    lv.get_overlay_source());
×
1671
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
1672
                if (iter != fos->fos_row_to_field_meta.end()
×
1673
                    && iter->second.ri_meta)
×
1674
                {
1675
                    auto find_res = this->find_line_with_file(lv.get_top());
×
1676
                    if (find_res) {
×
1677
                        auto file_and_line = find_res.value();
×
1678
                        auto* format = file_and_line.first->get_format_ptr();
×
1679
                        auto fstates = format->get_field_states();
×
1680
                        auto state_iter
1681
                            = fstates.find(iter->second.ri_meta->lvm_name);
×
1682
                        if (state_iter != fstates.end()) {
×
1683
                            format->hide_field(iter->second.ri_meta->lvm_name,
×
1684
                                               !state_iter->second.is_hidden());
×
1685
                            lv.set_needs_update();
×
1686
                        }
1687
                    }
1688
                }
1689
                return true;
×
1690
            }
1691
            return false;
×
1692
        }
1693
        case '#': {
×
1694
            auto ov_vl = lv.get_overlay_selection();
×
1695
            if (ov_vl) {
×
1696
                auto* fos = dynamic_cast<field_overlay_source*>(
×
1697
                    lv.get_overlay_source());
×
1698
                auto iter = fos->fos_row_to_field_meta.find(ov_vl.value());
×
1699
                if (iter != fos->fos_row_to_field_meta.end()
×
1700
                    && iter->second.ri_meta)
×
1701
                {
1702
                    const auto& meta = iter->second.ri_meta.value();
×
1703
                    std::string cmd;
×
1704

1705
                    switch (meta.to_chart_type()) {
×
1706
                        case chart_type_t::none:
×
1707
                            break;
×
1708
                        case chart_type_t::hist: {
×
1709
                            auto prql = fmt::format(
1710
                                FMT_STRING(
×
1711
                                    "from {} | stats.hist {} slice:'1h'"),
1712
                                meta.lvm_format.value()->get_name(),
×
1713
                                meta.lvm_name);
×
1714
                            cmd = fmt::format(FMT_STRING(":prompt sql ; '{}'"),
×
1715
                                              shlex::escape(prql));
×
1716
                            break;
×
1717
                        }
1718
                        case chart_type_t::spectro:
×
1719
                            cmd = fmt::format(FMT_STRING(":spectrogram {}"),
×
1720
                                              meta.lvm_name);
×
1721
                            break;
×
1722
                    }
1723
                    if (!cmd.empty()) {
×
1724
                        this->lss_exec_context
×
1725
                            ->with_provenance(exec_context::mouse_input{})
×
1726
                            ->execute(INTERNAL_SRC_LOC, cmd);
×
1727
                    }
1728
                }
1729
                return true;
×
1730
            }
1731
            return false;
×
1732
        }
1733
        case 'h':
×
1734
        case 'H':
1735
        case NCKEY_LEFT:
1736
            if (lv.get_left() == 0) {
×
1737
                this->increase_line_context();
×
1738
                lv.set_needs_update();
×
1739
                return true;
×
1740
            }
1741
            break;
×
1742
        case 'l':
×
1743
        case 'L':
1744
        case NCKEY_RIGHT:
1745
            if (this->decrease_line_context()) {
×
1746
                lv.set_needs_update();
×
1747
                return true;
×
1748
            }
1749
            break;
×
1750
    }
1751
    return false;
×
1752
}
1753

1754
std::optional<
1755
    std::pair<grep_proc_source<vis_line_t>*, grep_proc_sink<vis_line_t>*>>
1756
logfile_sub_source::get_grepper()
9✔
1757
{
1758
    return std::make_pair(
18✔
1759
        (grep_proc_source<vis_line_t>*) &this->lss_meta_grepper,
×
1760
        (grep_proc_sink<vis_line_t>*) &this->lss_meta_grepper);
9✔
1761
}
1762

1763
/**
1764
 * Functor for comparing the ld_file field of the logfile_data struct.
1765
 */
1766
struct logfile_data_eq {
1767
    explicit logfile_data_eq(std::shared_ptr<logfile> lf)
1,056✔
1768
        : lde_file(std::move(lf))
1,056✔
1769
    {
1770
    }
1,056✔
1771

1772
    bool operator()(
646✔
1773
        const std::unique_ptr<logfile_sub_source::logfile_data>& ld) const
1774
    {
1775
        return this->lde_file == ld->get_file();
646✔
1776
    }
1777

1778
    std::shared_ptr<logfile> lde_file;
1779
};
1780

1781
bool
1782
logfile_sub_source::insert_file(const std::shared_ptr<logfile>& lf)
528✔
1783
{
1784
    iterator existing;
528✔
1785

1786
    require_lt(lf->size(), MAX_LINES_PER_FILE);
528✔
1787

1788
    existing = std::find_if(this->lss_files.begin(),
528✔
1789
                            this->lss_files.end(),
1790
                            logfile_data_eq(nullptr));
1,056✔
1791
    if (existing == this->lss_files.end()) {
528✔
1792
        if (this->lss_files.size() >= MAX_FILES) {
528✔
1793
            return false;
×
1794
        }
1795

1796
        auto ld = std::make_unique<logfile_data>(
1797
            this->lss_files.size(), this->get_filters(), lf);
528✔
1798
        ld->set_visibility(lf->get_open_options().loo_is_visible);
528✔
1799
        this->lss_files.push_back(std::move(ld));
528✔
1800
    } else {
528✔
1801
        (*existing)->set_file(lf);
×
1802
    }
1803

1804
    return true;
528✔
1805
}
1806

1807
Result<void, lnav::console::user_message>
1808
logfile_sub_source::set_sql_filter(std::string stmt_str, sqlite3_stmt* stmt)
783✔
1809
{
1810
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
783✔
1811
        auto top_cl = this->at(0_vl);
8✔
1812
        auto ld = this->find_data(top_cl);
8✔
1813
        auto eval_res
1814
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
8✔
1815

1816
        if (eval_res.isErr()) {
8✔
1817
            sqlite3_finalize(stmt);
1✔
1818
            return Err(eval_res.unwrapErr());
1✔
1819
        }
1820
    }
8✔
1821

1822
    for (auto& ld : *this) {
799✔
1823
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
17✔
1824
    }
1825

1826
    auto old_filter_iter = this->tss_filters.find(0);
782✔
1827
    if (stmt != nullptr) {
782✔
1828
        auto new_filter
1829
            = std::make_shared<sql_filter>(*this, std::move(stmt_str), stmt);
7✔
1830

1831
        if (old_filter_iter != this->tss_filters.end()) {
7✔
1832
            *old_filter_iter = new_filter;
×
1833
        } else {
1834
            this->tss_filters.add_filter(new_filter);
7✔
1835
        }
1836
    } else if (old_filter_iter != this->tss_filters.end()) {
782✔
1837
        this->tss_filters.delete_filter((*old_filter_iter)->get_id());
7✔
1838
    }
1839

1840
    return Ok();
782✔
1841
}
1842

1843
Result<void, lnav::console::user_message>
1844
logfile_sub_source::set_sql_marker(std::string stmt_str, sqlite3_stmt* stmt)
780✔
1845
{
1846
    static auto op = lnav_operation{"set_sql_marker"};
780✔
1847
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
780✔
1848
        auto top_cl = this->at(0_vl);
5✔
1849
        auto ld = this->find_data(top_cl);
5✔
1850
        auto eval_res
1851
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
5✔
1852

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

1859
    auto op_guard = lnav_opid_guard::internal(op);
780✔
1860
    log_info("setting SQL marker: %s", stmt_str.c_str());
780✔
1861
    this->lss_marker_stmt_text = std::move(stmt_str);
780✔
1862
    this->lss_marker_stmt = stmt;
780✔
1863

1864
    if (this->tss_view == nullptr || this->lss_force_rebuild) {
780✔
1865
        log_info("skipping SQL marker update");
130✔
1866
        return Ok();
130✔
1867
    }
1868

1869
    auto& vis_bm = this->tss_view->get_bookmarks();
650✔
1870
    auto& expr_marks_bv = vis_bm[&textview_curses::BM_USER_EXPR];
650✔
1871
    auto& cl_marks_bv = this->lss_user_marks[&textview_curses::BM_USER_EXPR];
650✔
1872

1873
    expr_marks_bv.clear();
650✔
1874
    if (this->lss_index_delegate) {
650✔
1875
        this->lss_index_delegate->index_start(*this);
650✔
1876
    }
1877
    for (auto row = 0_vl; row < vis_line_t(this->lss_filtered_index.size());
1,083✔
1878
         row += 1_vl)
433✔
1879
    {
1880
        auto cl = this->at(row);
433✔
1881
        uint64_t line_number;
1882
        auto ld = this->find_data(cl, line_number);
433✔
1883

1884
        if (!(*ld)->is_visible()) {
433✔
1885
            continue;
1✔
1886
        }
1887
        auto ll = (*ld)->get_file()->begin() + line_number;
433✔
1888
        if (ll->is_continued() || ll->is_ignored()) {
433✔
1889
            continue;
1✔
1890
        }
1891
        auto eval_res
1892
            = this->eval_sql_filter(this->lss_marker_stmt.in(), ld, ll);
432✔
1893

1894
        if (eval_res.isErr()) {
432✔
1895
            ll->set_expr_mark(false);
×
1896
        } else {
1897
            auto matched = eval_res.unwrap();
432✔
1898

1899
            ll->set_expr_mark(matched);
432✔
1900
            if (matched) {
432✔
1901
                expr_marks_bv.insert_once(row);
22✔
1902
                cl_marks_bv.insert_once(cl);
22✔
1903
            }
1904
        }
1905
        if (this->lss_index_delegate) {
432✔
1906
            this->lss_index_delegate->index_line(
432✔
1907
                *this, (*ld)->get_file_ptr(), ll);
432✔
1908
        }
1909
    }
432✔
1910
    if (this->lss_index_delegate) {
650✔
1911
        this->lss_index_delegate->index_complete(*this);
650✔
1912
    }
1913
    log_info("SQL marker update complete");
650✔
1914

1915
    return Ok();
650✔
1916
}
780✔
1917

1918
Result<void, lnav::console::user_message>
1919
logfile_sub_source::set_preview_sql_filter(sqlite3_stmt* stmt)
775✔
1920
{
1921
    if (stmt != nullptr && !this->lss_filtered_index.empty()) {
775✔
1922
        auto top_cl = this->at(0_vl);
×
1923
        auto ld = this->find_data(top_cl);
×
1924
        auto eval_res
1925
            = this->eval_sql_filter(stmt, ld, (*ld)->get_file_ptr()->begin());
×
1926

1927
        if (eval_res.isErr()) {
×
1928
            sqlite3_finalize(stmt);
×
1929
            return Err(eval_res.unwrapErr());
×
1930
        }
1931
    }
1932

1933
    this->lss_preview_filter_stmt = stmt;
775✔
1934

1935
    return Ok();
775✔
1936
}
1937

1938
Result<bool, lnav::console::user_message>
1939
logfile_sub_source::eval_sql_filter(sqlite3_stmt* stmt,
15,409✔
1940
                                    iterator ld,
1941
                                    logfile::const_iterator ll)
1942
{
1943
    if (stmt == nullptr) {
15,409✔
1944
        return Ok(false);
29,588✔
1945
    }
1946

1947
    auto* lf = (*ld)->get_file_ptr();
615✔
1948
    char timestamp_buffer[64];
1949
    shared_buffer_ref raw_sbr;
615✔
1950
    logline_value_vector values;
615✔
1951
    auto& sbr = values.lvv_sbr;
615✔
1952
    lf->read_full_message(ll, sbr);
615✔
1953
    sbr.erase_ansi();
615✔
1954
    auto format = lf->get_format();
615✔
1955
    string_attrs_t sa;
615✔
1956
    auto line_number = std::distance(lf->cbegin(), ll);
615✔
1957
    format->annotate(lf, line_number, sa, values);
615✔
1958
    auto lffs = lf->get_format_file_state();
615✔
1959

1960
    sqlite3_reset(stmt);
615✔
1961
    sqlite3_clear_bindings(stmt);
615✔
1962

1963
    auto count = sqlite3_bind_parameter_count(stmt);
615✔
1964
    for (int lpc = 0; lpc < count; lpc++) {
1,242✔
1965
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
627✔
1966

1967
        if (name[0] == '$') {
627✔
1968
            const char* env_value;
1969

1970
            if ((env_value = getenv(&name[1])) != nullptr) {
4✔
1971
                sqlite3_bind_text(stmt, lpc + 1, env_value, -1, SQLITE_STATIC);
1✔
1972
            }
1973
            continue;
4✔
1974
        }
4✔
1975
        if (strcmp(name, ":log_level") == 0) {
623✔
1976
            auto lvl = ll->get_level_name();
6✔
1977
            sqlite3_bind_text(
6✔
1978
                stmt, lpc + 1, lvl.data(), lvl.length(), SQLITE_STATIC);
1979
            continue;
6✔
1980
        }
6✔
1981
        if (strcmp(name, ":log_time") == 0) {
617✔
1982
            auto len = sql_strftime(timestamp_buffer,
×
1983
                                    sizeof(timestamp_buffer),
1984
                                    ll->get_timeval(),
×
1985
                                    'T');
1986
            sqlite3_bind_text(
×
1987
                stmt, lpc + 1, timestamp_buffer, len, SQLITE_STATIC);
1988
            continue;
×
1989
        }
1990
        if (strcmp(name, ":log_time_msecs") == 0) {
617✔
1991
            sqlite3_bind_int64(
1✔
1992
                stmt,
1993
                lpc + 1,
1994
                ll->get_time<std::chrono::milliseconds>().count());
1✔
1995
            continue;
1✔
1996
        }
1997
        if (strcmp(name, ":log_mark") == 0) {
616✔
1998
            sqlite3_bind_int(stmt, lpc + 1, ll->is_marked());
×
1999
            continue;
×
2000
        }
2001
        if (strcmp(name, ":log_comment") == 0) {
616✔
2002
            const auto& bm = lf->get_bookmark_metadata();
×
2003
            auto line_number
2004
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
2005
            auto bm_iter = bm.find(line_number);
×
2006
            if (bm_iter != bm.end() && !bm_iter->second.bm_comment.empty()) {
×
2007
                const auto& meta = bm_iter->second;
×
2008
                sqlite3_bind_text(stmt,
×
2009
                                  lpc + 1,
2010
                                  meta.bm_comment.c_str(),
2011
                                  meta.bm_comment.length(),
×
2012
                                  SQLITE_STATIC);
2013
            }
2014
            continue;
×
2015
        }
2016
        if (strcmp(name, ":log_annotations") == 0) {
616✔
2017
            const auto& bm = lf->get_bookmark_metadata();
×
2018
            auto line_number
2019
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
×
2020
            auto bm_iter = bm.find(line_number);
×
2021
            if (bm_iter != bm.end()
×
2022
                && !bm_iter->second.bm_annotations.la_pairs.empty())
×
2023
            {
2024
                const auto& meta = bm_iter->second;
×
2025
                auto anno_str = logmsg_annotations_handlers.to_string(
2026
                    meta.bm_annotations);
×
2027

2028
                sqlite3_bind_text(stmt,
×
2029
                                  lpc + 1,
2030
                                  anno_str.c_str(),
2031
                                  anno_str.length(),
×
2032
                                  SQLITE_TRANSIENT);
2033
            }
2034
            continue;
×
2035
        }
2036
        if (strcmp(name, ":log_tags") == 0) {
616✔
2037
            const auto& bm = lf->get_bookmark_metadata();
9✔
2038
            auto line_number
2039
                = static_cast<uint32_t>(std::distance(lf->cbegin(), ll));
9✔
2040
            auto bm_iter = bm.find(line_number);
9✔
2041
            if (bm_iter != bm.end() && !bm_iter->second.bm_tags.empty()) {
9✔
2042
                const auto& meta = bm_iter->second;
1✔
2043
                yajlpp_gen gen;
1✔
2044

2045
                yajl_gen_config(gen, yajl_gen_beautify, false);
1✔
2046

2047
                {
2048
                    yajlpp_array arr(gen);
1✔
2049

2050
                    for (const auto& str : meta.bm_tags) {
2✔
2051
                        arr.gen(str);
1✔
2052
                    }
2053
                }
1✔
2054

2055
                string_fragment sf = gen.to_string_fragment();
1✔
2056

2057
                sqlite3_bind_text(
1✔
2058
                    stmt, lpc + 1, sf.data(), sf.length(), SQLITE_TRANSIENT);
2059
            }
1✔
2060
            continue;
9✔
2061
        }
9✔
2062
        if (strcmp(name, ":log_format") == 0) {
607✔
2063
            const auto format_name = format->get_name();
6✔
2064
            sqlite3_bind_text(stmt,
6✔
2065
                              lpc + 1,
2066
                              format_name.get(),
2067
                              format_name.size(),
6✔
2068
                              SQLITE_STATIC);
2069
            continue;
6✔
2070
        }
6✔
2071
        if (strcmp(name, ":log_format_regex") == 0) {
601✔
2072
            const auto pat_name = format->get_pattern_name(
×
2073
                lffs.lffs_pattern_locks, line_number);
2074
            sqlite3_bind_text(
×
2075
                stmt, lpc + 1, pat_name.get(), pat_name.size(), SQLITE_STATIC);
×
2076
            continue;
×
2077
        }
2078
        if (strcmp(name, ":log_path") == 0) {
601✔
2079
            const auto& filename = lf->get_filename();
×
2080
            sqlite3_bind_text(stmt,
×
2081
                              lpc + 1,
2082
                              filename.c_str(),
2083
                              filename.native().length(),
×
2084
                              SQLITE_STATIC);
2085
            continue;
×
2086
        }
2087
        if (strcmp(name, ":log_unique_path") == 0) {
601✔
2088
            const auto& filename = lf->get_unique_path();
×
2089
            sqlite3_bind_text(stmt,
×
2090
                              lpc + 1,
2091
                              filename.c_str(),
2092
                              filename.native().length(),
×
2093
                              SQLITE_STATIC);
2094
            continue;
×
2095
        }
2096
        if (strcmp(name, ":log_text") == 0) {
601✔
2097
            sqlite3_bind_text(
4✔
2098
                stmt, lpc + 1, sbr.get_data(), sbr.length(), SQLITE_STATIC);
4✔
2099
            continue;
4✔
2100
        }
2101
        if (strcmp(name, ":log_body") == 0) {
597✔
2102
            auto body_attr_opt = get_string_attr(sa, SA_BODY);
16✔
2103
            if (body_attr_opt) {
16✔
2104
                const auto& sar
2105
                    = body_attr_opt.value().saw_string_attr->sa_range;
16✔
2106

2107
                sqlite3_bind_text(stmt,
32✔
2108
                                  lpc + 1,
2109
                                  sbr.get_data_at(sar.lr_start),
16✔
2110
                                  sar.length(),
2111
                                  SQLITE_STATIC);
2112
            } else {
2113
                sqlite3_bind_null(stmt, lpc + 1);
×
2114
            }
2115
            continue;
16✔
2116
        }
16✔
2117
        if (strcmp(name, ":log_opid") == 0) {
581✔
2118
            if (values.lvv_opid_value) {
×
2119
                sqlite3_bind_text(stmt,
×
2120
                                  lpc + 1,
2121
                                  values.lvv_opid_value->c_str(),
2122
                                  values.lvv_opid_value->length(),
×
2123
                                  SQLITE_STATIC);
2124
            } else {
2125
                sqlite3_bind_null(stmt, lpc + 1);
×
2126
            }
2127
            continue;
×
2128
        }
2129
        if (strcmp(name, ":log_raw_text") == 0) {
581✔
2130
            auto res = lf->read_raw_message(ll);
×
2131

2132
            if (res.isOk()) {
×
2133
                raw_sbr = res.unwrap();
×
2134
                sqlite3_bind_text(stmt,
×
2135
                                  lpc + 1,
2136
                                  raw_sbr.get_data(),
2137
                                  raw_sbr.length(),
×
2138
                                  SQLITE_STATIC);
2139
            }
2140
            continue;
×
2141
        }
2142
        for (const auto& lv : values.lvv_values) {
6,782✔
2143
            if (lv.lv_meta.lvm_name != &name[1]) {
6,777✔
2144
                continue;
6,201✔
2145
            }
2146

2147
            switch (lv.lv_meta.lvm_kind) {
576✔
2148
                case value_kind_t::VALUE_BOOLEAN:
×
2149
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
2150
                    break;
×
2151
                case value_kind_t::VALUE_FLOAT:
×
2152
                    sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
2153
                    break;
×
2154
                case value_kind_t::VALUE_INTEGER:
436✔
2155
                    sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
436✔
2156
                    break;
436✔
2157
                case value_kind_t::VALUE_NULL:
×
2158
                    sqlite3_bind_null(stmt, lpc + 1);
×
2159
                    break;
×
2160
                default:
140✔
2161
                    sqlite3_bind_text(stmt,
140✔
2162
                                      lpc + 1,
2163
                                      lv.text_value(),
2164
                                      lv.text_length(),
140✔
2165
                                      SQLITE_TRANSIENT);
2166
                    break;
140✔
2167
            }
2168
            break;
576✔
2169
        }
2170
    }
2171

2172
    auto step_res = sqlite3_step(stmt);
615✔
2173

2174
    sqlite3_reset(stmt);
615✔
2175
    sqlite3_clear_bindings(stmt);
615✔
2176
    switch (step_res) {
615✔
2177
        case SQLITE_OK:
474✔
2178
        case SQLITE_DONE:
2179
            return Ok(false);
948✔
2180
        case SQLITE_ROW:
140✔
2181
            return Ok(true);
280✔
2182
        default:
1✔
2183
            return Err(sqlite3_error_to_user_message(sqlite3_db_handle(stmt)));
1✔
2184
    }
2185
}
615✔
2186

2187
bool
2188
logfile_sub_source::check_extra_filters(iterator ld, logfile::iterator ll)
14,833✔
2189
{
2190
    auto retval = true;
14,833✔
2191

2192
    if (this->lss_marked_only) {
14,833✔
2193
        auto found_mark = ll->is_marked() || ll->is_expr_marked();
6✔
2194
        auto to_start_ll = ll;
6✔
2195
        while (!found_mark && to_start_ll->is_continued()) {
6✔
2196
            if (to_start_ll->is_marked() || to_start_ll->is_expr_marked()) {
×
2197
                found_mark = true;
×
2198
            }
2199
            --to_start_ll;
×
2200
        }
2201
        auto to_end_ll = std::next(ll);
6✔
2202
        while (!found_mark && to_end_ll != (*ld)->get_file_ptr()->end()
10✔
2203
               && to_end_ll->is_continued())
11✔
2204
        {
2205
            if (to_end_ll->is_marked() || to_end_ll->is_expr_marked()) {
1✔
2206
                found_mark = true;
1✔
2207
            }
2208
            ++to_end_ll;
1✔
2209
        }
2210
        if (!found_mark) {
6✔
2211
            retval = false;
3✔
2212
        }
2213
    }
2214

2215
    if (ll->get_msg_level() < this->tss_min_log_level) {
14,833✔
2216
        this->tss_level_filtered_count += 1;
2✔
2217
        retval = false;
2✔
2218
    }
2219

2220
    if (*ll < this->ttt_min_row_time) {
14,833✔
2221
        retval = false;
36✔
2222
    }
2223

2224
    if (!(*ll <= this->ttt_max_row_time)) {
14,833✔
2225
        retval = false;
4✔
2226
    }
2227

2228
    return retval;
14,833✔
2229
}
2230

2231
void
2232
logfile_sub_source::invalidate_sql_filter()
24✔
2233
{
2234
    for (auto& ld : *this) {
48✔
2235
        ld->ld_filter_state.lfo_filter_state.clear_filter_state(0);
24✔
2236
    }
2237
}
24✔
2238

2239
void
2240
logfile_sub_source::text_mark(const bookmark_type_t* bm,
148✔
2241
                              vis_line_t line,
2242
                              bool added)
2243
{
2244
    if (line >= (int) this->lss_index.size()) {
148✔
2245
        return;
×
2246
    }
2247

2248
    auto cl = this->at(line);
148✔
2249

2250
    if (bm == &textview_curses::BM_USER) {
148✔
2251
        auto* ll = this->find_line(cl);
59✔
2252

2253
        ll->set_mark(added);
59✔
2254
    }
2255
    if (added) {
148✔
2256
        this->lss_user_marks[bm].insert_once(cl);
67✔
2257
    } else {
2258
        this->lss_user_marks[bm].erase(cl);
81✔
2259
    }
2260
    if (bm == &textview_curses::BM_META
148✔
2261
        && this->lss_meta_grepper.gps_proc != nullptr)
16✔
2262
    {
2263
        this->tss_view->search_range(line, line + 1_vl);
1✔
2264
        this->tss_view->search_new_data();
1✔
2265
    }
2266
}
2267

2268
void
2269
logfile_sub_source::text_clear_marks(const bookmark_type_t* bm)
40✔
2270
{
2271
    if (bm == &textview_curses::BM_USER) {
40✔
2272
        for (auto iter = this->lss_user_marks[bm].bv_tree.begin();
6✔
2273
             iter != this->lss_user_marks[bm].bv_tree.end();
6✔
2274
             ++iter)
×
2275
        {
2276
            this->find_line(*iter)->set_mark(false);
×
2277
        }
2278
    }
2279
    this->lss_user_marks[bm].clear();
40✔
2280
}
40✔
2281

2282
void
2283
logfile_sub_source::remove_file(std::shared_ptr<logfile> lf)
528✔
2284
{
2285
    auto iter = std::find_if(
528✔
2286
        this->lss_files.begin(), this->lss_files.end(), logfile_data_eq(lf));
1,056✔
2287
    if (iter != this->lss_files.end()) {
528✔
2288
        int file_index = iter - this->lss_files.begin();
528✔
2289

2290
        (*iter)->clear();
528✔
2291
        for (auto& bv : this->lss_user_marks) {
4,752✔
2292
            auto mark_curr = content_line_t(file_index * MAX_LINES_PER_FILE);
4,224✔
2293
            auto mark_end
2294
                = content_line_t((file_index + 1) * MAX_LINES_PER_FILE);
4,224✔
2295
            auto file_range = bv.equal_range(mark_curr, mark_end);
4,224✔
2296

2297
            if (file_range.first != file_range.second) {
4,224✔
2298
                auto to_del = std::vector<content_line_t>{};
69✔
2299
                for (auto file_iter = file_range.first;
69✔
2300
                     file_iter != file_range.second;
178✔
2301
                     ++file_iter)
109✔
2302
                {
2303
                    to_del.emplace_back(*file_iter);
109✔
2304
                }
2305

2306
                for (auto cl : to_del) {
178✔
2307
                    bv.erase(cl);
109✔
2308
                }
2309
            }
69✔
2310
        }
2311

2312
        this->lss_force_rebuild = true;
528✔
2313
    }
2314
    while (!this->lss_files.empty()) {
1,056✔
2315
        if (this->lss_files.back()->get_file_ptr() == nullptr) {
580✔
2316
            this->lss_files.pop_back();
528✔
2317
        } else {
2318
            break;
52✔
2319
        }
2320
    }
2321
    this->lss_token_file = nullptr;
528✔
2322
}
528✔
2323

2324
std::optional<vis_line_t>
2325
logfile_sub_source::find_from_content(content_line_t cl)
5✔
2326
{
2327
    content_line_t line = cl;
5✔
2328
    std::shared_ptr<logfile> lf = this->find(line);
5✔
2329

2330
    if (lf != nullptr) {
5✔
2331
        auto ll_iter = lf->begin() + line;
5✔
2332
        auto& ll = *ll_iter;
5✔
2333
        auto vis_start_opt = this->find_from_time(ll.get_timeval());
5✔
2334

2335
        if (!vis_start_opt) {
5✔
2336
            return std::nullopt;
×
2337
        }
2338

2339
        auto vis_start = *vis_start_opt;
5✔
2340

2341
        while (vis_start < vis_line_t(this->text_line_count())) {
5✔
2342
            content_line_t guess_cl = this->at(vis_start);
5✔
2343

2344
            if (cl == guess_cl) {
5✔
2345
                return vis_start;
5✔
2346
            }
2347

2348
            auto guess_line = this->find_line(guess_cl);
×
2349

2350
            if (!guess_line || ll < *guess_line) {
×
2351
                return std::nullopt;
×
2352
            }
2353

2354
            ++vis_start;
×
2355
        }
2356
    }
2357

2358
    return std::nullopt;
×
2359
}
5✔
2360

2361
void
2362
logfile_sub_source::reload_index_delegate()
652✔
2363
{
2364
    if (this->lss_index_delegate == nullptr) {
652✔
2365
        return;
×
2366
    }
2367

2368
    this->lss_index_delegate->index_start(*this);
652✔
2369
    for (const auto index : this->lss_filtered_index) {
680✔
2370
        auto cl = this->lss_index[index].value();
28✔
2371
        uint64_t line_number;
2372
        auto ld = this->find_data(cl, line_number);
28✔
2373
        std::shared_ptr<logfile> lf = (*ld)->get_file();
28✔
2374

2375
        this->lss_index_delegate->index_line(
28✔
2376
            *this, lf.get(), lf->begin() + line_number);
28✔
2377
    }
28✔
2378
    this->lss_index_delegate->index_complete(*this);
652✔
2379
}
2380

2381
std::optional<std::shared_ptr<text_filter>>
2382
logfile_sub_source::get_sql_filter()
2,609✔
2383
{
2384
    return this->tss_filters | lnav::itertools::find_if([](const auto& filt) {
2,609✔
2385
               return filt->get_index() == 0
270✔
2386
                   && dynamic_cast<sql_filter*>(filt.get()) != nullptr;
270✔
2387
           })
2388
        | lnav::itertools::deref();
5,218✔
2389
}
2390

2391
void
2392
log_location_history::loc_history_append(vis_line_t top)
99✔
2393
{
2394
    if (top < 0_vl || top >= vis_line_t(this->llh_log_source.text_line_count()))
99✔
2395
    {
2396
        return;
1✔
2397
    }
2398

2399
    auto cl = this->llh_log_source.at(top);
98✔
2400

2401
    auto iter = this->llh_history.begin();
98✔
2402
    iter += this->llh_history.size() - this->lh_history_position;
98✔
2403
    this->llh_history.erase_from(iter);
98✔
2404
    this->lh_history_position = 0;
98✔
2405
    this->llh_history.push_back(cl);
98✔
2406
}
2407

2408
std::optional<vis_line_t>
2409
log_location_history::loc_history_back(vis_line_t current_top)
2✔
2410
{
2411
    while (this->lh_history_position < this->llh_history.size()) {
2✔
2412
        auto iter = this->llh_history.rbegin();
2✔
2413

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

2416
        if (this->lh_history_position == 0 && vis_for_pos != current_top) {
2✔
2417
            return vis_for_pos;
2✔
2418
        }
2419

2420
        if ((this->lh_history_position + 1) >= this->llh_history.size()) {
2✔
2421
            break;
×
2422
        }
2423

2424
        this->lh_history_position += 1;
2✔
2425

2426
        iter += this->lh_history_position;
2✔
2427

2428
        vis_for_pos = this->llh_log_source.find_from_content(*iter);
2✔
2429

2430
        if (vis_for_pos) {
2✔
2431
            return vis_for_pos;
2✔
2432
        }
2433
    }
2434

2435
    return std::nullopt;
×
2436
}
2437

2438
std::optional<vis_line_t>
2439
log_location_history::loc_history_forward(vis_line_t current_top)
1✔
2440
{
2441
    while (this->lh_history_position > 0) {
1✔
2442
        this->lh_history_position -= 1;
1✔
2443

2444
        auto iter = this->llh_history.rbegin();
1✔
2445

2446
        iter += this->lh_history_position;
1✔
2447

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

2450
        if (vis_for_pos) {
1✔
2451
            return vis_for_pos;
1✔
2452
        }
2453
    }
2454

2455
    return std::nullopt;
×
2456
}
2457

2458
bool
2459
sql_filter::matches(std::optional<line_source> ls_opt,
124✔
2460
                    const shared_buffer_ref& line)
2461
{
2462
    if (!ls_opt) {
124✔
2463
        return false;
×
2464
    }
2465

2466
    auto ls = ls_opt;
124✔
2467

2468
    if (!ls->ls_line->is_message()) {
124✔
2469
        return false;
3✔
2470
    }
2471
    if (this->sf_filter_stmt == nullptr) {
121✔
2472
        return false;
×
2473
    }
2474

2475
    auto lfp = ls->ls_file.shared_from_this();
121✔
2476
    auto ld = this->sf_log_source.find_data_i(lfp);
121✔
2477
    if (ld == this->sf_log_source.end()) {
121✔
2478
        return false;
×
2479
    }
2480

2481
    auto eval_res = this->sf_log_source.eval_sql_filter(
121✔
2482
        this->sf_filter_stmt, ld, ls->ls_line);
121✔
2483
    if (eval_res.unwrapOr(true)) {
121✔
2484
        return false;
74✔
2485
    }
2486

2487
    return true;
47✔
2488
}
121✔
2489

2490
std::string
2491
sql_filter::to_command() const
×
2492
{
2493
    return fmt::format(FMT_STRING("filter-expr {}"), this->lf_id);
×
2494
}
2495

2496
std::optional<line_info>
2497
logfile_sub_source::meta_grepper::grep_value_for_line(vis_line_t line,
×
2498
                                                      std::string& value_out)
2499
{
2500
    auto line_meta_opt = this->lmg_source.find_bookmark_metadata(line);
×
2501
    if (!line_meta_opt) {
×
2502
        value_out.clear();
×
2503
    } else {
2504
        auto& bm = *(line_meta_opt.value());
×
2505

2506
        {
2507
            md2attr_line mdal;
×
2508

2509
            auto parse_res = md4cpp::parse(bm.bm_comment, mdal);
×
2510
            if (parse_res.isOk()) {
×
2511
                value_out.append(parse_res.unwrap().get_string());
×
2512
            } else {
2513
                value_out.append(bm.bm_comment);
×
2514
            }
2515
        }
2516

2517
        value_out.append("\x1c");
×
2518
        for (const auto& tag : bm.bm_tags) {
×
2519
            value_out.append(tag);
×
2520
            value_out.append("\x1c");
×
2521
        }
2522
        value_out.append("\x1c");
×
2523
        for (const auto& pair : bm.bm_annotations.la_pairs) {
×
2524
            value_out.append(pair.first);
×
2525
            value_out.append("\x1c");
×
2526

2527
            md2attr_line mdal;
×
2528

2529
            auto parse_res = md4cpp::parse(pair.second, mdal);
×
2530
            if (parse_res.isOk()) {
×
2531
                value_out.append(parse_res.unwrap().get_string());
×
2532
            } else {
2533
                value_out.append(pair.second);
×
2534
            }
2535
            value_out.append("\x1c");
×
2536
        }
2537
        value_out.append("\x1c");
×
2538
        value_out.append(bm.bm_opid);
×
2539
    }
2540

2541
    if (!this->lmg_done) {
×
2542
        return line_info{};
×
2543
    }
2544

2545
    return std::nullopt;
×
2546
}
2547

2548
vis_line_t
2549
logfile_sub_source::meta_grepper::grep_initial_line(vis_line_t start,
×
2550
                                                    vis_line_t highest)
2551
{
2552
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2553
    auto& bv = bm[&textview_curses::BM_META];
×
2554

2555
    if (bv.empty()) {
×
2556
        return -1_vl;
×
2557
    }
2558
    return *bv.bv_tree.begin();
×
2559
}
2560

2561
void
2562
logfile_sub_source::meta_grepper::grep_next_line(vis_line_t& line)
×
2563
{
2564
    auto& bm = this->lmg_source.tss_view->get_bookmarks();
×
2565
    auto& bv = bm[&textview_curses::BM_META];
×
2566

2567
    auto line_opt = bv.next(vis_line_t(line));
×
2568
    if (!line_opt) {
×
2569
        this->lmg_done = true;
×
2570
    }
2571
    line = line_opt.value_or(-1_vl);
×
2572
}
2573

2574
void
2575
logfile_sub_source::meta_grepper::grep_begin(grep_proc<vis_line_t>& gp,
23✔
2576
                                             vis_line_t start,
2577
                                             vis_line_t stop)
2578
{
2579
    this->lmg_source.quiesce();
23✔
2580

2581
    this->lmg_source.tss_view->grep_begin(gp, start, stop);
23✔
2582
}
23✔
2583

2584
void
2585
logfile_sub_source::meta_grepper::grep_end(grep_proc<vis_line_t>& gp)
23✔
2586
{
2587
    this->lmg_source.tss_view->grep_end(gp);
23✔
2588
}
23✔
2589

2590
void
2591
logfile_sub_source::meta_grepper::grep_match(grep_proc<vis_line_t>& gp,
3✔
2592
                                             vis_line_t line)
2593
{
2594
    this->lmg_source.tss_view->grep_match(gp, line);
3✔
2595
}
3✔
2596

2597
static std::vector<breadcrumb::possibility>
2598
timestamp_poss()
11✔
2599
{
2600
    const static std::vector<breadcrumb::possibility> retval = {
2601
        breadcrumb::possibility{"-1 day"},
2602
        breadcrumb::possibility{"-1h"},
2603
        breadcrumb::possibility{"-30m"},
2604
        breadcrumb::possibility{"-15m"},
2605
        breadcrumb::possibility{"-5m"},
2606
        breadcrumb::possibility{"-1m"},
2607
        breadcrumb::possibility{"+1m"},
2608
        breadcrumb::possibility{"+5m"},
2609
        breadcrumb::possibility{"+15m"},
2610
        breadcrumb::possibility{"+30m"},
2611
        breadcrumb::possibility{"+1h"},
2612
        breadcrumb::possibility{"+1 day"},
2613
    };
95✔
2614

2615
    return retval;
11✔
2616
}
24✔
2617

2618
static attr_line_t
2619
to_display(const std::shared_ptr<logfile>& lf)
28✔
2620
{
2621
    attr_line_t retval;
28✔
2622

2623
    if (lf->get_open_options().loo_piper) {
28✔
2624
        if (!lf->get_open_options().loo_piper->is_finished()) {
×
2625
            retval.append("\u21bb "_list_glyph);
×
2626
        }
2627
    }
2628
    retval.append(lf->get_unique_path());
28✔
2629

2630
    return retval;
28✔
2631
}
×
2632

2633
void
2634
logfile_sub_source::text_crumbs_for_line(int line,
26✔
2635
                                         std::vector<breadcrumb::crumb>& crumbs)
2636
{
2637
    text_sub_source::text_crumbs_for_line(line, crumbs);
26✔
2638

2639
    if (this->lss_filtered_index.empty()) {
26✔
2640
        return;
9✔
2641
    }
2642

2643
    auto vl = vis_line_t(line);
17✔
2644
    auto bmc = this->get_bookmark_metadata_context(
17✔
2645
        vl, bookmark_metadata::categories::partition);
2646
    if (bmc.bmc_current_metadata) {
17✔
2647
        const auto& name = bmc.bmc_current_metadata.value()->bm_name;
1✔
2648
        auto key = text_anchors::to_anchor_string(name);
1✔
2649
        auto display = attr_line_t()
1✔
2650
                           .append("\u2291 "_symbol)
1✔
2651
                           .append(lnav::roles::variable(name))
2✔
2652
                           .move();
1✔
2653
        crumbs.emplace_back(
1✔
2654
            key,
2655
            display,
2656
            [this]() -> std::vector<breadcrumb::possibility> {
×
2657
                auto& vb = this->tss_view->get_bookmarks();
1✔
2658
                const auto& bv = vb[&textview_curses::BM_PARTITION];
1✔
2659
                std::vector<breadcrumb::possibility> retval;
1✔
2660

2661
                for (const auto& vl : bv.bv_tree) {
2✔
2662
                    auto meta_opt = this->find_bookmark_metadata(vl);
1✔
2663
                    if (!meta_opt || meta_opt.value()->bm_name.empty()) {
1✔
2664
                        continue;
×
2665
                    }
2666

2667
                    const auto& name = meta_opt.value()->bm_name;
1✔
2668
                    retval.emplace_back(text_anchors::to_anchor_string(name),
1✔
2669
                                        name);
2670
                }
2671

2672
                return retval;
1✔
2673
            },
×
2674
            [ec = this->lss_exec_context](const auto& part) {
1✔
2675
                auto cmd = fmt::format(FMT_STRING(":goto {}"),
×
2676
                                       part.template get<std::string>());
2677
                ec->execute(INTERNAL_SRC_LOC, cmd);
×
2678
            });
×
2679
    }
1✔
2680

2681
    auto line_pair_opt = this->find_line_with_file(vl);
17✔
2682
    if (!line_pair_opt) {
17✔
2683
        return;
×
2684
    }
2685
    auto line_pair = line_pair_opt.value();
17✔
2686
    auto& lf = line_pair.first;
17✔
2687
    auto format = lf->get_format();
17✔
2688
    char ts[64];
2689

2690
    sql_strftime(ts, sizeof(ts), line_pair.second->get_timeval(), 'T');
17✔
2691

2692
    crumbs.emplace_back(std::string(ts),
17✔
2693
                        timestamp_poss,
2694
                        [ec = this->lss_exec_context](const auto& ts) {
17✔
2695
                            auto cmd
×
2696
                                = fmt::format(FMT_STRING(":goto {}"),
×
2697
                                              ts.template get<std::string>());
2698
                            ec->execute(INTERNAL_SRC_LOC, cmd);
×
2699
                        });
×
2700
    crumbs.back().c_expected_input
17✔
2701
        = breadcrumb::crumb::expected_input_t::anything;
17✔
2702
    crumbs.back().c_search_placeholder = "(Enter an absolute or relative time)";
17✔
2703

2704
    auto format_name = format->get_name().to_string();
17✔
2705

2706
    crumbs.emplace_back(
17✔
2707
        format_name,
2708
        attr_line_t().append(format_name),
17✔
2709
        [this]() -> std::vector<breadcrumb::possibility> {
×
2710
            return this->lss_files
11✔
2711
                | lnav::itertools::filter_in([](const auto& file_data) {
22✔
2712
                       return file_data->is_visible();
11✔
2713
                   })
2714
                | lnav::itertools::map(&logfile_data::get_file_ptr)
33✔
2715
                | lnav::itertools::map(&logfile::get_format_name)
33✔
2716
                | lnav::itertools::unique()
33✔
2717
                | lnav::itertools::map([](const auto& elem) {
44✔
2718
                       return breadcrumb::possibility{
2719
                           elem.to_string(),
2720
                       };
11✔
2721
                   })
2722
                | lnav::itertools::to_vector();
33✔
2723
        },
2724
        [ec = this->lss_exec_context](const auto& format_name) {
17✔
2725
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2726
     SET selection = ifnull(
2727
         (SELECT log_line FROM all_logs WHERE log_format = $format_name LIMIT 1),
2728
         (SELECT raise_error(
2729
            'Could not find format: ' || $format_name,
2730
            'The corresponding log messages might have been filtered out'))
2731
       )
2732
     WHERE name = 'log'
2733
)";
2734

2735
            ec->execute_with(
×
2736
                INTERNAL_SRC_LOC,
×
2737
                MOVE_STMT,
2738
                std::make_pair("format_name",
2739
                               format_name.template get<std::string>()));
2740
        });
×
2741

2742
    auto msg_start_iter = lf->message_start(line_pair.second);
17✔
2743
    auto file_line_number = std::distance(lf->begin(), msg_start_iter);
17✔
2744
    crumbs.emplace_back(
34✔
2745
        lf->get_unique_path(),
17✔
2746
        to_display(lf).appendf(FMT_STRING("[{:L}]"), file_line_number),
68✔
2747
        [this]() -> std::vector<breadcrumb::possibility> {
×
2748
            return this->lss_files
11✔
2749
                | lnav::itertools::filter_in([](const auto& file_data) {
22✔
2750
                       return file_data->is_visible();
11✔
2751
                   })
2752
                | lnav::itertools::map([](const auto& file_data) {
22✔
2753
                       return breadcrumb::possibility{
2754
                           file_data->get_file_ptr()->get_unique_path(),
11✔
2755
                           to_display(file_data->get_file()),
2756
                       };
11✔
2757
                   });
22✔
2758
        },
2759
        [ec = this->lss_exec_context](const auto& uniq_path) {
17✔
2760
            static const std::string MOVE_STMT = R"(;UPDATE lnav_views
2761
     SET selection = ifnull(
2762
          (SELECT log_line FROM all_logs WHERE log_unique_path = $uniq_path LIMIT 1),
2763
          (SELECT raise_error(
2764
            'Could not find file: ' || $uniq_path,
2765
            'The corresponding log messages might have been filtered out'))
2766
         )
2767
     WHERE name = 'log'
2768
)";
2769

2770
            ec->execute_with(
×
2771
                INTERNAL_SRC_LOC,
×
2772
                MOVE_STMT,
2773
                std::make_pair("uniq_path",
2774
                               uniq_path.template get<std::string>()));
2775
        });
×
2776

2777
    shared_buffer sb;
17✔
2778
    logline_value_vector values;
17✔
2779
    auto& sbr = values.lvv_sbr;
17✔
2780

2781
    lf->read_full_message(msg_start_iter, sbr);
17✔
2782
    attr_line_t al(to_string(sbr));
17✔
2783
    if (!sbr.get_metadata().m_valid_utf) {
17✔
2784
        scrub_to_utf8(al.al_string.data(), al.al_string.length());
×
2785
    }
2786
    if (sbr.get_metadata().m_has_ansi) {
17✔
2787
        // bleh
2788
        scrub_ansi_string(al.get_string(), &al.al_attrs);
×
2789
        sbr.share(sb, al.al_string.data(), al.al_string.size());
×
2790
    }
2791
    format->annotate(lf.get(), file_line_number, al.get_attrs(), values);
17✔
2792

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

2803
        auto tid_display = values.lvv_thread_id_value.has_value()
17✔
2804
            ? lnav::roles::identifier(values.lvv_thread_id_value.value())
1✔
2805
            : lnav::roles::hidden(ELLIPSIS);
18✔
2806
        crumbs.emplace_back(
17✔
2807
            values.lvv_thread_id_value.has_value()
17✔
2808
                ? values.lvv_thread_id_value.value()
50✔
2809
                : "",
2810
            attr_line_t()
17✔
2811
                .append(ui_icon_t::thread)
17✔
2812
                .append(" ")
17✔
2813
                .append(tid_display),
2814
            [this]() -> std::vector<breadcrumb::possibility> {
×
2815
                std::set<std::string> poss_strs;
11✔
2816

2817
                for (const auto& file_data : this->lss_files) {
22✔
2818
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2819
                        continue;
×
2820
                    }
2821
                    safe::ReadAccess<logfile::safe_thread_id_state> r_tid_map(
2822
                        file_data->get_file_ptr()->get_thread_ids());
11✔
2823

2824
                    for (const auto& pair : r_tid_map->ltis_tid_ranges) {
24✔
2825
                        poss_strs.emplace(pair.first.to_string());
13✔
2826
                    }
2827
                }
11✔
2828

2829
                std::vector<breadcrumb::possibility> retval;
11✔
2830

2831
                std::transform(poss_strs.begin(),
11✔
2832
                               poss_strs.end(),
2833
                               std::back_inserter(retval),
2834
                               [](const auto& tid_str) {
13✔
2835
                                   return breadcrumb::possibility(tid_str);
13✔
2836
                               });
2837

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

2848
    {
2849
        static const std::string MOVE_STMT = R"(;UPDATE lnav_views
29✔
2850
          SET selection = ifnull(
2851
            (SELECT log_line FROM all_logs WHERE log_opid = $opid LIMIT 1),
2852
            (SELECT raise_error('Could not find opid: ' || $opid,
2853
                                'The corresponding log messages might have been filtered out')))
2854
          WHERE name = 'log'
2855
        )";
2856
        static const std::string ELLIPSIS = "\u22ef";
29✔
2857

2858
        auto opid_display = values.lvv_opid_value.has_value()
17✔
2859
            ? lnav::roles::identifier(values.lvv_opid_value.value())
9✔
2860
            : lnav::roles::hidden(ELLIPSIS);
26✔
2861
        crumbs.emplace_back(
34✔
2862
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
42✔
2863
                                              : "",
2864
            attr_line_t().append(opid_display),
17✔
2865
            [this]() -> std::vector<breadcrumb::possibility> {
×
2866
                std::unordered_set<std::string> poss_strs;
11✔
2867

2868
                for (const auto& file_data : this->lss_files) {
22✔
2869
                    if (file_data->get_file_ptr() == nullptr) {
11✔
2870
                        continue;
×
2871
                    }
2872
                    safe::ReadAccess<logfile::safe_opid_state> r_opid_map(
2873
                        file_data->get_file_ptr()->get_opids());
11✔
2874

2875
                    poss_strs.reserve(poss_strs.size()
22✔
2876
                                      + r_opid_map->los_opid_ranges.size());
11✔
2877
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
783✔
2878
                        poss_strs.insert(pair.first.to_string());
772✔
2879
                    }
2880
                }
11✔
2881

2882
                std::vector<breadcrumb::possibility> retval;
11✔
2883
                retval.reserve(poss_strs.size());
11✔
2884

2885
                std::transform(poss_strs.begin(),
11✔
2886
                               poss_strs.end(),
2887
                               std::back_inserter(retval),
2888
                               [](const auto& opid_str) {
772✔
2889
                                   return breadcrumb::possibility(opid_str);
772✔
2890
                               });
2891

2892
                return retval;
22✔
2893
            },
11✔
2894
            [ec = this->lss_exec_context](const auto& opid) {
17✔
2895
                ec->execute_with(
×
2896
                    INTERNAL_SRC_LOC,
×
2897
                    MOVE_STMT,
2898
                    std::make_pair("opid", opid.template get<std::string>()));
2899
            });
×
2900
    }
17✔
2901

2902
    auto sf = string_fragment::from_str(al.get_string());
17✔
2903
    auto body_opt = get_string_attr(al.get_attrs(), SA_BODY);
17✔
2904
    auto nl_pos_opt = sf.find('\n');
17✔
2905
    auto msg_line_number = std::distance(msg_start_iter, line_pair.second);
17✔
2906
    auto line_from_top = line - msg_line_number;
17✔
2907
    if (body_opt && nl_pos_opt) {
17✔
2908
        if (this->lss_token_meta_line != file_line_number
16✔
2909
            || this->lss_token_meta_size != sf.length())
8✔
2910
        {
2911
            if (body_opt->saw_string_attr->sa_range.length() < 128 * 1024) {
3✔
2912
                this->lss_token_meta
2913
                    = lnav::document::discover(al)
3✔
2914
                          .over_range(
3✔
2915
                              body_opt.value().saw_string_attr->sa_range)
3✔
2916
                          .perform();
3✔
2917
                // XXX discover_structure() changes `al`, have to recompute
2918
                // stuff
2919
                sf = al.to_string_fragment();
3✔
2920
                body_opt = get_string_attr(al.get_attrs(), SA_BODY);
3✔
2921
            } else {
2922
                this->lss_token_meta = lnav::document::metadata{};
×
2923
            }
2924
            this->lss_token_meta_line = file_line_number;
3✔
2925
            this->lss_token_meta_size = sf.length();
3✔
2926
        }
2927

2928
        const auto initial_size = crumbs.size();
8✔
2929
        auto sf_body
2930
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
8✔
2931
                           body_opt->saw_string_attr->sa_range.lr_end);
8✔
2932
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
8✔
2933
        file_off_t line_end_offset = sf.length();
8✔
2934
        ssize_t line_number = 0;
8✔
2935

2936
        for (const auto& sf_line : sf_body.split_lines()) {
16✔
2937
            if (line_number >= msg_line_number) {
11✔
2938
                line_end_offset = line_offset + sf_line.length();
3✔
2939
                break;
3✔
2940
            }
2941
            line_number += 1;
8✔
2942
            line_offset += sf_line.length();
8✔
2943
        }
8✔
2944

2945
        this->lss_token_meta.m_sections_tree.visit_overlapping(
8✔
2946
            line_offset,
2947
            line_end_offset,
2948
            [this,
2✔
2949
             initial_size,
2950
             meta = &this->lss_token_meta,
8✔
2951
             &crumbs,
2952
             line_from_top](const auto& iv) {
2953
                auto path = crumbs | lnav::itertools::skip(initial_size)
6✔
2954
                    | lnav::itertools::map(&breadcrumb::crumb::c_key)
4✔
2955
                    | lnav::itertools::append(iv.value);
2✔
2956
                auto curr_node = lnav::document::hier_node::lookup_path(
2✔
2957
                    meta->m_sections_root.get(), path);
2✔
2958

2959
                crumbs.emplace_back(
4✔
2960
                    iv.value,
2✔
2961
                    [meta, path]() { return meta->possibility_provider(path); },
4✔
2962
                    [this, curr_node, path, line_from_top](const auto& key) {
4✔
2963
                        if (!curr_node) {
×
2964
                            return;
×
2965
                        }
2966
                        auto* parent_node = curr_node.value()->hn_parent;
×
2967
                        if (parent_node == nullptr) {
×
2968
                            return;
×
2969
                        }
2970
                        key.match(
2971
                            [parent_node](const std::string& str) {
×
2972
                                return parent_node->find_line_number(str);
×
2973
                            },
2974
                            [parent_node](size_t index) {
×
2975
                                return parent_node->find_line_number(index);
×
2976
                            })
2977
                            | [this, line_from_top](auto line_number) {
×
2978
                                  this->tss_view->set_selection(
×
2979
                                      vis_line_t(line_from_top + line_number));
×
2980
                              };
2981
                    });
2982
                if (curr_node
2✔
2983
                    && !curr_node.value()->hn_parent->is_named_only()) {
2✔
2984
                    auto node = lnav::document::hier_node::lookup_path(
×
2985
                        meta->m_sections_root.get(), path);
×
2986

2987
                    crumbs.back().c_expected_input
×
2988
                        = curr_node.value()
×
2989
                              ->hn_parent->hn_named_children.empty()
×
2990
                        ? breadcrumb::crumb::expected_input_t::index
×
2991
                        : breadcrumb::crumb::expected_input_t::index_or_exact;
2992
                    crumbs.back().with_possible_range(
×
2993
                        node | lnav::itertools::map([](const auto hn) {
×
2994
                            return hn->hn_parent->hn_children.size();
×
2995
                        })
2996
                        | lnav::itertools::unwrap_or(size_t{0}));
×
2997
                }
2998
            });
2✔
2999

3000
        auto path = crumbs | lnav::itertools::skip(initial_size)
16✔
3001
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
16✔
3002
        auto node = lnav::document::hier_node::lookup_path(
8✔
3003
            this->lss_token_meta.m_sections_root.get(), path);
8✔
3004

3005
        if (node && !node.value()->hn_children.empty()) {
8✔
3006
            auto poss_provider = [curr_node = node.value()]() {
1✔
3007
                std::vector<breadcrumb::possibility> retval;
1✔
3008
                for (const auto& child : curr_node->hn_named_children) {
1✔
3009
                    retval.emplace_back(child.first);
×
3010
                }
3011
                return retval;
1✔
3012
            };
3013
            auto path_performer
3014
                = [this, curr_node = node.value(), line_from_top](
2✔
3015
                      const breadcrumb::crumb::key_t& value) {
3016
                      value.match(
×
3017
                          [curr_node](const std::string& str) {
×
3018
                              return curr_node->find_line_number(str);
×
3019
                          },
3020
                          [curr_node](size_t index) {
×
3021
                              return curr_node->find_line_number(index);
×
3022
                          })
3023
                          | [this, line_from_top](size_t line_number) {
×
3024
                                this->tss_view->set_selection(
×
3025
                                    vis_line_t(line_from_top + line_number));
×
3026
                            };
3027
                  };
1✔
3028
            crumbs.emplace_back("", "\u22ef", poss_provider, path_performer);
1✔
3029
            crumbs.back().c_expected_input
1✔
3030
                = node.value()->hn_named_children.empty()
2✔
3031
                ? breadcrumb::crumb::expected_input_t::index
1✔
3032
                : breadcrumb::crumb::expected_input_t::index_or_exact;
3033
        }
3034
    }
8✔
3035
}
17✔
3036

3037
void
3038
logfile_sub_source::quiesce()
42✔
3039
{
3040
    for (auto& ld : this->lss_files) {
84✔
3041
        auto* lf = ld->get_file_ptr();
42✔
3042

3043
        if (lf == nullptr) {
42✔
3044
            continue;
×
3045
        }
3046

3047
        lf->quiesce();
42✔
3048
    }
3049
}
42✔
3050

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

3058
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
3059
}
25✔
3060

3061
logfile_sub_source::bookmark_metadata_context
3062
logfile_sub_source::get_bookmark_metadata_context(
4,493✔
3063
    vis_line_t vl, bookmark_metadata::categories desired) const
3064
{
3065
    const auto& vb = this->tss_view->get_bookmarks();
4,493✔
3066
    const auto& bv = vb[desired == bookmark_metadata::categories::partition
3067
                            ? &textview_curses::BM_PARTITION
3068
                            : &textview_curses::BM_META];
4,493✔
3069
    auto vl_iter = bv.bv_tree.lower_bound(vl + 1_vl);
4,493✔
3070

3071
    std::optional<vis_line_t> next_line;
4,493✔
3072
    for (auto next_vl_iter = vl_iter; next_vl_iter != bv.bv_tree.end();
4,493✔
3073
         ++next_vl_iter)
×
3074
    {
3075
        auto bm_opt = this->find_bookmark_metadata(*next_vl_iter);
2✔
3076
        if (!bm_opt) {
2✔
3077
            continue;
×
3078
        }
3079

3080
        if (bm_opt.value()->has(desired)) {
2✔
3081
            next_line = *next_vl_iter;
2✔
3082
            break;
2✔
3083
        }
3084
    }
3085
    if (vl_iter == bv.bv_tree.begin()) {
4,493✔
3086
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
4,477✔
3087
    }
3088

3089
    --vl_iter;
16✔
3090
    while (true) {
3091
        auto bm_opt = this->find_bookmark_metadata(*vl_iter);
16✔
3092
        if (bm_opt) {
16✔
3093
            if (bm_opt.value()->has(desired)) {
13✔
3094
                return bookmark_metadata_context{
3095
                    *vl_iter, bm_opt.value(), next_line};
16✔
3096
            }
3097
        }
3098

3099
        if (vl_iter == bv.bv_tree.begin()) {
3✔
3100
            return bookmark_metadata_context{
3101
                std::nullopt, std::nullopt, next_line};
3✔
3102
        }
3103
        --vl_iter;
×
3104
    }
3105
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3106
}
3107

3108
std::optional<bookmark_metadata*>
3109
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
23,802✔
3110
{
3111
    auto line_pair = this->find_line_with_file(cl).value();
23,802✔
3112
    auto line_number = static_cast<uint32_t>(
3113
        std::distance(line_pair.first->begin(), line_pair.second));
23,802✔
3114

3115
    auto& bm = line_pair.first->get_bookmark_metadata();
23,802✔
3116
    auto bm_iter = bm.find(line_number);
23,802✔
3117
    if (bm_iter == bm.end()) {
23,802✔
3118
        return std::nullopt;
23,446✔
3119
    }
3120

3121
    return &bm_iter->second;
356✔
3122
}
23,802✔
3123

3124
void
3125
logfile_sub_source::erase_bookmark_metadata(content_line_t cl)
26✔
3126
{
3127
    auto line_pair = this->find_line_with_file(cl).value();
26✔
3128
    auto line_number = static_cast<uint32_t>(
3129
        std::distance(line_pair.first->begin(), line_pair.second));
26✔
3130

3131
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
3132
    auto bm_iter = bm.find(line_number);
26✔
3133
    if (bm_iter != bm.end()) {
26✔
3134
        bm.erase(bm_iter);
6✔
3135
    }
3136
}
26✔
3137

3138
void
3139
logfile_sub_source::clear_bookmark_metadata()
6✔
3140
{
3141
    for (auto& ld : *this) {
14✔
3142
        if (ld->get_file_ptr() == nullptr) {
8✔
3143
            continue;
×
3144
        }
3145

3146
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3147
    }
3148
}
6✔
3149

3150
void
3151
logfile_sub_source::increase_line_context()
×
3152
{
3153
    auto old_context = this->lss_line_context;
×
3154

3155
    switch (this->lss_line_context) {
×
3156
        case line_context_t::filename:
×
3157
            // nothing to do
3158
            break;
×
3159
        case line_context_t::basename:
×
3160
            this->lss_line_context = line_context_t::filename;
×
3161
            break;
×
3162
        case line_context_t::none:
×
3163
            this->lss_line_context = line_context_t::basename;
×
3164
            break;
×
3165
        case line_context_t::time_column:
×
3166
            this->lss_line_context = line_context_t::none;
×
3167
            break;
×
3168
    }
3169
    if (old_context != this->lss_line_context) {
×
3170
        this->clear_line_size_cache();
×
3171
    }
3172
}
3173

3174
bool
3175
logfile_sub_source::decrease_line_context()
×
3176
{
3177
    static const auto& cfg
3178
        = injector::get<const logfile_sub_source_ns::config&>();
×
3179
    auto old_context = this->lss_line_context;
×
3180

3181
    switch (this->lss_line_context) {
×
3182
        case line_context_t::filename:
×
3183
            this->lss_line_context = line_context_t::basename;
×
3184
            break;
×
3185
        case line_context_t::basename:
×
3186
            this->lss_line_context = line_context_t::none;
×
3187
            break;
×
3188
        case line_context_t::none:
×
3189
            if (cfg.c_time_column
×
3190
                != logfile_sub_source_ns::time_column_feature_t::Disabled)
3191
            {
3192
                this->lss_line_context = line_context_t::time_column;
×
3193
            }
3194
            break;
×
3195
        case line_context_t::time_column:
×
3196
            break;
×
3197
    }
3198
    if (old_context != this->lss_line_context) {
×
3199
        this->clear_line_size_cache();
×
3200

3201
        return true;
×
3202
    }
3203

3204
    return false;
×
3205
}
3206

3207
size_t
3208
logfile_sub_source::get_filename_offset() const
243✔
3209
{
3210
    switch (this->lss_line_context) {
243✔
3211
        case line_context_t::filename:
×
3212
            return this->lss_filename_width;
×
3213
        case line_context_t::basename:
×
3214
            return this->lss_basename_width;
×
3215
        default:
243✔
3216
            return 0;
243✔
3217
    }
3218
}
3219

3220
size_t
3221
logfile_sub_source::file_count() const
4,091✔
3222
{
3223
    size_t retval = 0;
4,091✔
3224
    const_iterator iter;
4,091✔
3225

3226
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,780✔
3227
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,689✔
3228
            retval += 1;
3,683✔
3229
        }
3230
    }
3231

3232
    return retval;
4,091✔
3233
}
3234

3235
size_t
3236
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3237
                                       int row,
3238
                                       text_sub_source::line_flags_t flags)
3239
{
3240
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3241

3242
    if (this->lss_line_size_cache[index].first != row) {
×
3243
        std::string value;
×
3244

3245
        this->text_value_for_line(tc, row, value, flags);
×
3246
        scrub_ansi_string(value, nullptr);
×
3247
        auto line_width = string_fragment::from_str(value).column_width();
×
3248
        if (this->lss_line_context == line_context_t::time_column) {
×
3249
            auto time_attr
NEW
3250
                = find_string_attr(this->lss_token_al.al_attrs, &L_TIMESTAMP);
×
NEW
3251
            if (time_attr != this->lss_token_al.al_attrs.end()) {
×
3252
                line_width -= time_attr->sa_range.length();
×
3253
                auto format = this->lss_token_file->get_format();
×
3254
                if (format->lf_level_hideable) {
×
NEW
3255
                    auto level_attr = find_string_attr(
×
NEW
3256
                        this->lss_token_al.al_attrs, &L_LEVEL);
×
NEW
3257
                    if (level_attr != this->lss_token_al.al_attrs.end()) {
×
UNCOV
3258
                        line_width -= level_attr->sa_range.length();
×
3259
                    }
3260
                }
3261
            }
3262
        }
3263
        this->lss_line_size_cache[index].second = line_width;
×
3264
        this->lss_line_size_cache[index].first = row;
×
3265
    }
3266
    return this->lss_line_size_cache[index].second;
×
3267
}
3268

3269
int
3270
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
1✔
3271
{
3272
    int retval = 0;
1✔
3273

3274
    for (const auto& ld : this->lss_files) {
2✔
3275
        retval += ld->ld_filter_state.lfo_filter_state
1✔
3276
                      .tfs_filter_hits[filter_index];
1✔
3277
    }
3278

3279
    return retval;
1✔
3280
}
3281

3282
std::optional<vis_line_t>
3283
logfile_sub_source::row_for(const row_info& ri)
265✔
3284
{
3285
    auto lb = std::lower_bound(this->lss_filtered_index.begin(),
530✔
3286
                               this->lss_filtered_index.end(),
3287
                               ri.ri_time,
265✔
3288
                               filtered_logline_cmp(*this));
3289
    if (lb != this->lss_filtered_index.end()) {
265✔
3290
        auto first_lb = lb;
260✔
3291
        while (true) {
3292
            auto cl = this->lss_index[*lb].value();
274✔
3293
            if (content_line_t(ri.ri_id) == cl) {
274✔
3294
                first_lb = lb;
228✔
3295
                break;
228✔
3296
            }
3297
            auto ll = this->find_line(cl);
46✔
3298
            if (ll->get_timeval() != ri.ri_time) {
46✔
3299
                break;
32✔
3300
            }
3301
            auto next_lb = std::next(lb);
14✔
3302
            if (next_lb == this->lss_filtered_index.end()) {
14✔
3303
                break;
×
3304
            }
3305
            lb = next_lb;
14✔
3306
        }
14✔
3307

3308
        const auto dst
3309
            = std::distance(this->lss_filtered_index.begin(), first_lb);
260✔
3310
        return vis_line_t(dst);
260✔
3311
    }
3312

3313
    return std::nullopt;
5✔
3314
}
3315

3316
std::unique_ptr<logline_window>
3317
logfile_sub_source::window_at(vis_line_t start_vl, vis_line_t end_vl)
38✔
3318
{
3319
    return std::make_unique<logline_window>(*this, start_vl, end_vl);
38✔
3320
}
3321

3322
std::unique_ptr<logline_window>
3323
logfile_sub_source::window_at(vis_line_t start_vl)
207✔
3324
{
3325
    return std::make_unique<logline_window>(*this, start_vl, start_vl + 1_vl);
207✔
3326
}
3327

3328
std::unique_ptr<logline_window>
3329
logfile_sub_source::window_to_end(vis_line_t start_vl)
×
3330
{
3331
    return std::make_unique<logline_window>(
3332
        *this, start_vl, vis_line_t(this->text_line_count()));
×
3333
}
3334

3335
std::optional<vis_line_t>
3336
logfile_sub_source::row_for_anchor(const std::string& id)
3✔
3337
{
3338
    if (startswith(id, "#msg")) {
3✔
3339
        static const auto ANCHOR_RE
3340
            = lnav::pcre2pp::code::from_const(R"(#msg([0-9a-fA-F]+)-(.+))");
3✔
3341
        thread_local auto md = lnav::pcre2pp::match_data::unitialized();
3✔
3342

3343
        if (ANCHOR_RE.capture_from(id).into(md).found_p()) {
3✔
3344
            auto scan_res = scn::scan<int64_t>(md[1]->to_string_view(), "{:x}");
3✔
3345
            if (scan_res) {
3✔
3346
                auto ts_low = std::chrono::microseconds{scan_res->value()};
3✔
3347
                auto ts_high = ts_low + 1us;
3✔
3348

3349
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3350
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3351
                if (low_vl) {
3✔
3352
                    auto lw = this->window_at(
3353
                        low_vl.value(),
3✔
3354
                        high_vl.value_or(low_vl.value() + 1_vl));
6✔
3355

3356
                    for (const auto& li : *lw) {
3✔
3357
                        auto hash_res = li.get_line_hash();
3✔
3358
                        if (hash_res.isErr()) {
3✔
3359
                            auto errmsg = hash_res.unwrapErr();
×
3360

3361
                            log_error("unable to get line hash: %s",
×
3362
                                      errmsg.c_str());
3363
                            continue;
×
3364
                        }
3365

3366
                        auto hash = hash_res.unwrap();
3✔
3367
                        if (hash == md[2]) {
3✔
3368
                            return li.get_vis_line();
3✔
3369
                        }
3370
                    }
12✔
3371
                }
3✔
3372
            }
3373
        }
3374

3375
        return std::nullopt;
×
3376
    }
3377

3378
    auto& vb = this->tss_view->get_bookmarks();
×
3379
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3380

3381
    for (const auto& vl : bv.bv_tree) {
×
3382
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3383
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3384
            continue;
×
3385
        }
3386

3387
        const auto& name = meta_opt.value()->bm_name;
×
3388
        if (id == text_anchors::to_anchor_string(name)) {
×
3389
            return vl;
×
3390
        }
3391
    }
3392

3393
    return std::nullopt;
×
3394
}
3395

3396
std::optional<vis_line_t>
3397
logfile_sub_source::adjacent_anchor(vis_line_t vl, text_anchors::direction dir)
2✔
3398
{
3399
    if (vl < this->lss_filtered_index.size()) {
2✔
3400
        auto file_and_line_pair = this->find_line_with_file(vl);
2✔
3401
        if (file_and_line_pair) {
2✔
3402
            const auto& [lf, line] = file_and_line_pair.value();
2✔
3403
            if (line->is_continued()) {
2✔
3404
                auto retval = vl;
×
3405
                switch (dir) {
×
3406
                    case direction::prev: {
×
3407
                        auto first_line = line;
×
3408
                        while (first_line->is_continued()) {
×
3409
                            --first_line;
×
3410
                            retval -= 1_vl;
×
3411
                        }
3412
                        return retval;
×
3413
                    }
3414
                    case direction::next: {
×
3415
                        auto first_line = line;
×
3416
                        while (first_line->is_continued()) {
×
3417
                            ++first_line;
×
3418
                            retval += 1_vl;
×
3419
                        }
3420
                        return retval;
×
3421
                    }
3422
                }
3423
            }
3424
        }
3425
    }
2✔
3426

3427
    auto bmc = this->get_bookmark_metadata_context(
2✔
3428
        vl, bookmark_metadata::categories::partition);
3429
    switch (dir) {
2✔
3430
        case direction::prev: {
×
3431
            if (bmc.bmc_current && bmc.bmc_current.value() != vl) {
×
3432
                return bmc.bmc_current;
×
3433
            }
3434
            if (!bmc.bmc_current || bmc.bmc_current.value() == 0_vl) {
×
3435
                return 0_vl;
×
3436
            }
3437
            auto prev_bmc = this->get_bookmark_metadata_context(
×
3438
                bmc.bmc_current.value() - 1_vl,
×
3439
                bookmark_metadata::categories::partition);
3440
            if (!prev_bmc.bmc_current) {
×
3441
                return 0_vl;
×
3442
            }
3443
            return prev_bmc.bmc_current;
×
3444
        }
3445
        case direction::next:
2✔
3446
            return bmc.bmc_next_line;
2✔
3447
    }
3448
    return std::nullopt;
×
3449
}
3450

3451
std::optional<std::string>
3452
logfile_sub_source::anchor_for_row(vis_line_t vl)
79✔
3453
{
3454
    auto line_meta = this->get_bookmark_metadata_context(
79✔
3455
        vl, bookmark_metadata::categories::partition);
3456
    if (!line_meta.bmc_current_metadata) {
79✔
3457
        auto lw = window_at(vl);
76✔
3458

3459
        for (const auto& li : *lw) {
76✔
3460
            auto hash_res = li.get_line_hash();
76✔
3461
            if (hash_res.isErr()) {
76✔
3462
                auto errmsg = hash_res.unwrapErr();
×
3463
                log_error("unable to compute line hash: %s", errmsg.c_str());
×
3464
                break;
×
3465
            }
3466
            auto hash = hash_res.unwrap();
76✔
3467
            auto retval = fmt::format(
3468
                FMT_STRING("#msg{:016x}-{}"),
152✔
3469
                li.get_logline().get_time<std::chrono::microseconds>().count(),
76✔
3470
                hash);
×
3471

3472
            return retval;
76✔
3473
        }
304✔
3474

3475
        return std::nullopt;
×
3476
    }
76✔
3477

3478
    return text_anchors::to_anchor_string(
3✔
3479
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3480
}
3481

3482
std::unordered_set<std::string>
3483
logfile_sub_source::get_anchors()
×
3484
{
3485
    auto& vb = this->tss_view->get_bookmarks();
×
3486
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3487
    std::unordered_set<std::string> retval;
×
3488

3489
    for (const auto& vl : bv.bv_tree) {
×
3490
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3491
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3492
            continue;
×
3493
        }
3494

3495
        const auto& name = meta_opt.value()->bm_name;
×
3496
        retval.emplace(text_anchors::to_anchor_string(name));
×
3497
    }
3498

3499
    return retval;
×
3500
}
×
3501

3502
bool
3503
logfile_sub_source::text_handle_mouse(
×
3504
    textview_curses& tc,
3505
    const listview_curses::display_line_content_t& mouse_line,
3506
    mouse_event& me)
3507
{
3508
    if (mouse_line.is<listview_curses::static_overlay_content>()
×
3509
        && this->text_line_count() > 0)
×
3510
    {
3511
        auto top = tc.get_top();
×
3512
        if (top > 0) {
×
3513
            auto win = this->window_at(top - 1_vl);
×
3514
            for (const auto& li : *win) {
×
3515
                tc.set_top(li.get_vis_line());
×
3516
                tc.set_selection(li.get_vis_line());
×
3517
                return true;
×
3518
            }
3519
        }
3520
    }
3521

3522
    if (tc.get_overlay_selection()) {
×
3523
        auto nci = ncinput{};
×
3524
        if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 2, 4)) {
×
3525
            nci.id = ' ';
×
3526
            nci.eff_text[0] = ' ';
×
3527
            this->list_input_handle_key(tc, nci);
×
3528
        } else if (me.is_click_in(mouse_button_t::BUTTON_LEFT, 5, 6)) {
×
3529
            nci.id = '#';
×
3530
            nci.eff_text[0] = '#';
×
3531
            this->list_input_handle_key(tc, nci);
×
3532
        }
3533
    }
3534
    return true;
×
3535
}
3536

3537
void
3538
logfile_sub_source::reload_config(error_reporter& reporter)
673✔
3539
{
3540
    static const auto& cfg
3541
        = injector::get<const logfile_sub_source_ns::config&>();
673✔
3542

3543
    switch (cfg.c_time_column) {
673✔
3544
        case logfile_sub_source_ns::time_column_feature_t::Default:
×
3545
            if (this->lss_line_context == line_context_t::none) {
×
3546
                this->lss_line_context = line_context_t::time_column;
×
3547
            }
3548
            break;
×
3549
        case logfile_sub_source_ns::time_column_feature_t::Disabled:
673✔
3550
            if (this->lss_line_context == line_context_t::time_column) {
673✔
3551
                this->lss_line_context = line_context_t::none;
×
3552
            }
3553
            break;
673✔
3554
        case logfile_sub_source_ns::time_column_feature_t::Enabled:
×
3555
            break;
×
3556
    }
3557
}
673✔
3558

3559
void
3560
logfile_sub_source::clear_preview()
1✔
3561
{
3562
    text_sub_source::clear_preview();
1✔
3563

3564
    this->set_preview_sql_filter(nullptr);
1✔
3565
    auto last = std::remove_if(this->lss_highlighters.begin(),
1✔
3566
                               this->lss_highlighters.end(),
NEW
3567
                               [](const auto& hl) { return hl.h_preview; });
×
3568
    this->lss_highlighters.erase(last, this->lss_highlighters.end());
1✔
3569
}
1✔
3570

3571
void
3572
logfile_sub_source::add_commands_for_session(
47✔
3573
    const std::function<void(const std::string&)>& receiver)
3574
{
3575
    text_sub_source::add_commands_for_session(receiver);
47✔
3576

3577
    auto mark_expr = this->get_sql_marker_text();
47✔
3578
    if (!mark_expr.empty()) {
47✔
3579
        receiver(fmt::format(FMT_STRING("mark-expr {}"), mark_expr));
4✔
3580
    }
3581
    auto filter_expr = this->get_sql_filter_text();
47✔
3582
    if (!filter_expr.empty()) {
47✔
3583
        receiver(fmt::format(FMT_STRING("filter-expr {}"), filter_expr));
×
3584
    }
3585

3586
    for (const auto& hl : this->lss_highlighters) {
47✔
NEW
3587
        auto cmd = "highlight-field "s;
×
NEW
3588
        if (hl.h_attrs.has_style(text_attrs::style::bold)) {
×
NEW
3589
            cmd += "--bold ";
×
3590
        }
NEW
3591
        if (hl.h_attrs.has_style(text_attrs::style::underline)) {
×
NEW
3592
            cmd += "--underline ";
×
3593
        }
NEW
3594
        if (hl.h_attrs.has_style(text_attrs::style::blink)) {
×
NEW
3595
            cmd += "--blink ";
×
3596
        }
NEW
3597
        if (hl.h_attrs.has_style(text_attrs::style::struck)) {
×
NEW
3598
            cmd += "--strike ";
×
3599
        }
NEW
3600
        if (hl.h_attrs.has_style(text_attrs::style::italic)) {
×
NEW
3601
            cmd += "--italic ";
×
3602
        }
NEW
3603
        cmd += hl.h_field.to_string() + " " + hl.h_regex->get_pattern();
×
NEW
3604
        receiver(cmd);
×
3605
    }
3606

3607
    for (const auto& format : log_format::get_root_formats()) {
3,569✔
3608
        auto field_states = format->get_field_states();
3,522✔
3609

3610
        for (const auto& fs_pair : field_states) {
48,046✔
3611
            if (!fs_pair.second.lvm_user_hidden) {
44,524✔
3612
                continue;
44,522✔
3613
            }
3614

3615
            if (fs_pair.second.lvm_user_hidden.value()) {
2✔
3616
                receiver(fmt::format(FMT_STRING("hide-fields {}.{}"),
8✔
3617
                                     format->get_name().to_string(),
4✔
3618
                                     fs_pair.first.to_string()));
4✔
3619
            } else if (fs_pair.second.lvm_hidden) {
×
3620
                receiver(fmt::format(FMT_STRING("show-fields {}.{}"),
×
3621
                                     format->get_name().to_string(),
×
3622
                                     fs_pair.first.to_string()));
×
3623
            }
3624
        }
3625
    }
3,522✔
3626
}
47✔
3627

3628
void
3629
logfile_sub_source::update_filter_hash_state(hasher& h) const
8✔
3630
{
3631
    text_sub_source::update_filter_hash_state(h);
8✔
3632

3633
    for (const auto& ld : this->lss_files) {
10✔
3634
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
2✔
3635
            h.update(0);
×
3636
        } else {
3637
            h.update(1);
2✔
3638
        }
3639
    }
3640
    h.update(this->tss_min_log_level);
8✔
3641
    h.update(this->lss_marked_only);
8✔
3642
}
8✔
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