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

tstack / lnav / 22507085525-2793

27 Feb 2026 10:49PM UTC coverage: 68.948% (-0.02%) from 68.966%
22507085525-2793

push

github

tstack
fix mixup of O_CLOEXEC with FD_CLOEXEC

52007 of 75429 relevant lines covered (68.95%)

440500.48 hits per line

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

64.48
/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 "file_collection.hh"
55
#include "hasher.hh"
56
#include "k_merge_tree.h"
57
#include "lnav_util.hh"
58
#include "log_accel.hh"
59
#include "logfile_sub_source.cfg.hh"
60
#include "logline_window.hh"
61
#include "md2attr_line.hh"
62
#include "ptimec.hh"
63
#include "scn/scan.h"
64
#include "shlex.hh"
65
#include "sql_util.hh"
66
#include "tlx/container/btree_set.hpp"
67
#include "vtab_module.hh"
68
#include "yajlpp/yajlpp.hh"
69
#include "yajlpp/yajlpp_def.hh"
70

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

158
        return retval;
×
159
    });
×
160

161
    return retval;
×
162
}
163

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

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

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

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

196
    return retval;
25✔
197
}
×
198

199
struct filtered_logline_cmp {
200
    filtered_logline_cmp(const logfile_sub_source& lc) : llss_controller(lc) {}
355✔
201

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

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

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

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

229
    const logfile_sub_source& llss_controller;
230
};
231

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

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

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

259
    line_info retval;
4,467✔
260
    content_line_t line(0);
4,467✔
261

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

265
    line = this->at(vis_line_t(row));
4,467✔
266

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

289
    require_false(this->lss_in_value_for_line);
4,435✔
290

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

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

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

325
    auto format = this->lss_token_file->get_format();
4,435✔
326

327
    value_out = this->lss_token_al.al_string;
4,435✔
328

329
    auto& sbr = this->lss_token_values.lvv_sbr;
4,435✔
330

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

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

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

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

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

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

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

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

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

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

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

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

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

542
    this->lss_in_value_for_line = false;
4,435✔
543

544
    return retval;
4,435✔
545
}
4,435✔
546

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

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

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

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

575
    const auto& line_values = this->lss_token_values;
4,435✔
576

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

582
    lr.lr_start = time_offset_end;
4,435✔
583
    lr.lr_end = -1;
4,435✔
584

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

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

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

600
    for (const auto& line_value : line_values.lvv_values) {
45,961✔
601
        if ((!(this->lss_token_flags & RF_FULL)
92,921✔
602
             && line_value.lv_sub_offset
82,624✔
603
                 != this->lss_token_line->get_sub_offset())
41,312✔
604
            || !line_value.lv_origin.is_valid())
82,838✔
605
        {
606
            continue;
9,869✔
607
        }
608

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

614
        if (!line_value.lv_meta.lvm_identifier
81,399✔
615
            || !line_value.lv_origin.is_valid())
31,657✔
616
        {
617
            continue;
18,085✔
618
        }
619

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

932
        return (*ll_lhs) < (*ll_rhs);
105,237✔
933
    }
934

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

940
        return *ll_lhs < rhs;
×
941
    }
942

943
    logfile_sub_source& llss_controller;
944
};
945

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

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

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

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

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

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

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

994
                             return left_ld->get_file_ptr()->back()
248✔
995
                                 < right_ld->get_file_ptr()->back();
496✔
996
                         });
997
    }
998

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1225
        for (auto& ld : this->lss_files) {
1,725✔
1226
            auto* lf = ld->get_file_ptr();
563✔
1227

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

1240
        if (full_sort) {
1,162✔
1241
            log_trace("rebuild_index full sort");
1,162✔
1242
            for (auto& ld : this->lss_files) {
1,725✔
1243
                auto* lf = ld->get_file_ptr();
563✔
1244

1245
                if (lf == nullptr) {
563✔
1246
                    continue;
1✔
1247
                }
1248

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1386
            if (lf == nullptr) {
563✔
1387
                continue;
1✔
1388
            }
1389

1390
            (*iter)->ld_lines_indexed = lf->size();
562✔
1391
        }
1392

1393
        this->lss_filtered_index.reserve(this->lss_index.size());
1,162✔
1394

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

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

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

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

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

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

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

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

1449
        this->lss_indexing_in_progress = false;
1,162✔
1450

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

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

1477
    return retval;
4,389✔
1478
}
4,389✔
1479

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

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

1490
    bm_warnings.clear();
2,246✔
1491
    bm_errors.clear();
2,246✔
1492
    bm_files.clear();
2,246✔
1493

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

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

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

1521
        if (lf != last_file) {
17,410✔
1522
            bm_files.insert_once(vl);
978✔
1523
        }
1524

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

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

1534
            default:
13,885✔
1535
                break;
13,885✔
1536
        }
1537

1538
        last_file = lf;
17,410✔
1539
    }
1540
}
2,246✔
1541

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

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

1553
    for (auto& ld : *this) {
285✔
1554
        auto* lf = ld->get_file_ptr();
119✔
1555

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1647
                {
1648
                    yajlpp_map root(gen);
×
1649

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

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

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

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

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

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

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

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

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

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

1787
    require_lt(lf->size(), MAX_LINES_PER_FILE);
529✔
1788

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

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

1805
    return true;
529✔
1806
}
1807

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

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

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

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

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

1841
    return Ok();
783✔
1842
}
1843

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

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

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

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

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

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

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

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

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

1916
    return Ok();
651✔
1917
}
781✔
1918

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

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

1934
    this->lss_preview_filter_stmt = stmt;
776✔
1935

1936
    return Ok();
776✔
1937
}
1938

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2229
    return retval;
14,841✔
2230
}
2231

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2340
        auto vis_start = *vis_start_opt;
5✔
2341

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

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

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

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

2355
            ++vis_start;
×
2356
        }
2357
    }
2358

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2436
    return std::nullopt;
×
2437
}
2438

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

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

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

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

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

2456
    return std::nullopt;
×
2457
}
2458

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

2467
    auto ls = ls_opt;
124✔
2468

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

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

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

2488
    return true;
47✔
2489
}
121✔
2490

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

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

2507
        {
2508
            md2attr_line mdal;
×
2509

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

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

2528
            md2attr_line mdal;
×
2529

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

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

2546
    return std::nullopt;
×
2547
}
2548

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

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

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

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

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

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

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

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

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

2616
    return retval;
11✔
2617
}
24✔
2618

2619
static attr_line_t
2620
to_display(const std::shared_ptr<logfile>& lf)
30✔
2621
{
2622
    const auto& loo = lf->get_open_options();
30✔
2623
    attr_line_t retval;
30✔
2624

2625
    if (loo.loo_piper) {
30✔
2626
        if (!lf->get_open_options().loo_piper->is_finished()) {
×
2627
            retval.append("\u21bb "_list_glyph);
×
2628
        }
2629
    } else if (loo.loo_child_poller && loo.loo_child_poller->is_alive()) {
30✔
2630
        retval.append("\u21bb "_list_glyph);
×
2631
    }
2632
    retval.append(lf->get_unique_path());
30✔
2633

2634
    return retval;
30✔
2635
}
×
2636

2637
void
2638
logfile_sub_source::text_crumbs_for_line(int line,
28✔
2639
                                         std::vector<breadcrumb::crumb>& crumbs)
2640
{
2641
    text_sub_source::text_crumbs_for_line(line, crumbs);
28✔
2642

2643
    if (this->lss_filtered_index.empty()) {
28✔
2644
        return;
9✔
2645
    }
2646

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

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

2671
                    const auto& name = meta_opt.value()->bm_name;
1✔
2672
                    retval.emplace_back(text_anchors::to_anchor_string(name),
1✔
2673
                                        name);
2674
                }
2675

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

2685
    auto line_pair_opt = this->find_line_with_file(vl);
19✔
2686
    if (!line_pair_opt) {
19✔
2687
        return;
×
2688
    }
2689
    auto line_pair = line_pair_opt.value();
19✔
2690
    auto& lf = line_pair.first;
19✔
2691
    auto format = lf->get_format();
19✔
2692
    char ts[64];
2693

2694
    sql_strftime(ts, sizeof(ts), line_pair.second->get_timeval(), 'T');
19✔
2695

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

2708
    auto format_name = format->get_name().to_string();
19✔
2709

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

2739
            ec->execute_with(
×
2740
                INTERNAL_SRC_LOC,
×
2741
                MOVE_STMT,
2742
                std::make_pair("format_name",
2743
                               format_name.template get<std::string>()));
2744
        });
×
2745

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

2774
            ec->execute_with(
×
2775
                INTERNAL_SRC_LOC,
×
2776
                MOVE_STMT,
2777
                std::make_pair("uniq_path",
2778
                               uniq_path.template get<std::string>()));
2779
        });
×
2780

2781
    shared_buffer sb;
19✔
2782
    logline_value_vector values;
19✔
2783
    auto& sbr = values.lvv_sbr;
19✔
2784

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

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

2807
        auto tid_display = values.lvv_thread_id_value.has_value()
19✔
2808
            ? lnav::roles::identifier(values.lvv_thread_id_value.value())
1✔
2809
            : lnav::roles::hidden(ELLIPSIS);
20✔
2810
        crumbs.emplace_back(
19✔
2811
            values.lvv_thread_id_value.has_value()
19✔
2812
                ? values.lvv_thread_id_value.value()
56✔
2813
                : "",
2814
            attr_line_t()
19✔
2815
                .append(ui_icon_t::thread)
19✔
2816
                .append(" ")
19✔
2817
                .append(tid_display),
2818
            [this]() -> std::vector<breadcrumb::possibility> {
×
2819
                std::set<std::string> poss_strs;
11✔
2820

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

2828
                    for (const auto& pair : r_tid_map->ltis_tid_ranges) {
24✔
2829
                        poss_strs.emplace(pair.first.to_string());
13✔
2830
                    }
2831
                }
11✔
2832

2833
                std::vector<breadcrumb::possibility> retval;
11✔
2834

2835
                std::transform(poss_strs.begin(),
11✔
2836
                               poss_strs.end(),
2837
                               std::back_inserter(retval),
2838
                               [](const auto& tid_str) {
13✔
2839
                                   return breadcrumb::possibility(tid_str);
13✔
2840
                               });
2841

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

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

2862
        auto opid_display = values.lvv_opid_value.has_value()
19✔
2863
            ? lnav::roles::identifier(values.lvv_opid_value.value())
9✔
2864
            : lnav::roles::hidden(ELLIPSIS);
28✔
2865
        crumbs.emplace_back(
38✔
2866
            values.lvv_opid_value.has_value() ? values.lvv_opid_value.value()
48✔
2867
                                              : "",
2868
            attr_line_t().append(opid_display),
19✔
2869
            [this]() -> std::vector<breadcrumb::possibility> {
×
2870
                std::unordered_set<std::string> poss_strs;
11✔
2871

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

2879
                    poss_strs.reserve(poss_strs.size()
22✔
2880
                                      + r_opid_map->los_opid_ranges.size());
11✔
2881
                    for (const auto& pair : r_opid_map->los_opid_ranges) {
783✔
2882
                        poss_strs.insert(pair.first.to_string());
772✔
2883
                    }
2884
                }
11✔
2885

2886
                std::vector<breadcrumb::possibility> retval;
11✔
2887
                retval.reserve(poss_strs.size());
11✔
2888

2889
                std::transform(poss_strs.begin(),
11✔
2890
                               poss_strs.end(),
2891
                               std::back_inserter(retval),
2892
                               [](const auto& opid_str) {
772✔
2893
                                   return breadcrumb::possibility(opid_str);
772✔
2894
                               });
2895

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

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

2932
        const auto initial_size = crumbs.size();
10✔
2933
        auto sf_body
2934
            = sf.sub_range(body_opt->saw_string_attr->sa_range.lr_start,
10✔
2935
                           body_opt->saw_string_attr->sa_range.lr_end);
10✔
2936
        file_off_t line_offset = body_opt->saw_string_attr->sa_range.lr_start;
10✔
2937
        file_off_t line_end_offset = sf.length();
10✔
2938
        ssize_t line_number = 0;
10✔
2939

2940
        for (const auto& sf_line : sf_body.split_lines()) {
20✔
2941
            if (line_number >= msg_line_number) {
13✔
2942
                line_end_offset = line_offset + sf_line.length();
3✔
2943
                break;
3✔
2944
            }
2945
            line_number += 1;
10✔
2946
            line_offset += sf_line.length();
10✔
2947
        }
10✔
2948

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

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

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

3004
        auto path = crumbs | lnav::itertools::skip(initial_size)
20✔
3005
            | lnav::itertools::map(&breadcrumb::crumb::c_key);
20✔
3006
        auto node = lnav::document::hier_node::lookup_path(
10✔
3007
            this->lss_token_meta.m_sections_root.get(), path);
10✔
3008

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

3041
void
3042
logfile_sub_source::quiesce()
42✔
3043
{
3044
    for (auto& ld : this->lss_files) {
84✔
3045
        auto* lf = ld->get_file_ptr();
42✔
3046

3047
        if (lf == nullptr) {
42✔
3048
            continue;
×
3049
        }
3050

3051
        lf->quiesce();
42✔
3052
    }
3053
}
42✔
3054

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

3062
    return line_pair.first->get_bookmark_metadata()[line_number];
50✔
3063
}
25✔
3064

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

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

3084
        if (bm_opt.value()->has(desired)) {
2✔
3085
            next_line = *next_vl_iter;
2✔
3086
            break;
2✔
3087
        }
3088
    }
3089
    if (vl_iter == bv.bv_tree.begin()) {
4,535✔
3090
        return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
4,519✔
3091
    }
3092

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

3103
        if (vl_iter == bv.bv_tree.begin()) {
3✔
3104
            return bookmark_metadata_context{
3105
                std::nullopt, std::nullopt, next_line};
3✔
3106
        }
3107
        --vl_iter;
×
3108
    }
3109
    return bookmark_metadata_context{std::nullopt, std::nullopt, next_line};
3110
}
3111

3112
std::optional<bookmark_metadata*>
3113
logfile_sub_source::find_bookmark_metadata(content_line_t cl) const
23,949✔
3114
{
3115
    auto line_pair = this->find_line_with_file(cl).value();
23,949✔
3116
    auto line_number = static_cast<uint32_t>(
3117
        std::distance(line_pair.first->begin(), line_pair.second));
23,949✔
3118

3119
    auto& bm = line_pair.first->get_bookmark_metadata();
23,949✔
3120
    auto bm_iter = bm.find(line_number);
23,949✔
3121
    if (bm_iter == bm.end()) {
23,949✔
3122
        return std::nullopt;
23,593✔
3123
    }
3124

3125
    return &bm_iter->second;
356✔
3126
}
23,949✔
3127

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

3135
    auto& bm = line_pair.first->get_bookmark_metadata();
26✔
3136
    auto bm_iter = bm.find(line_number);
26✔
3137
    if (bm_iter != bm.end()) {
26✔
3138
        bm.erase(bm_iter);
6✔
3139
    }
3140
}
26✔
3141

3142
void
3143
logfile_sub_source::clear_bookmark_metadata()
6✔
3144
{
3145
    for (auto& ld : *this) {
14✔
3146
        if (ld->get_file_ptr() == nullptr) {
8✔
3147
            continue;
×
3148
        }
3149

3150
        ld->get_file_ptr()->get_bookmark_metadata().clear();
8✔
3151
    }
3152
}
6✔
3153

3154
void
3155
logfile_sub_source::increase_line_context()
×
3156
{
3157
    auto old_context = this->lss_line_context;
×
3158

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

3178
bool
3179
logfile_sub_source::decrease_line_context()
×
3180
{
3181
    static const auto& cfg
3182
        = injector::get<const logfile_sub_source_ns::config&>();
×
3183
    auto old_context = this->lss_line_context;
×
3184

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

3205
        return true;
×
3206
    }
3207

3208
    return false;
×
3209
}
3210

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

3224
size_t
3225
logfile_sub_source::file_count() const
4,098✔
3226
{
3227
    size_t retval = 0;
4,098✔
3228
    const_iterator iter;
4,098✔
3229

3230
    for (iter = this->cbegin(); iter != this->cend(); ++iter) {
7,796✔
3231
        if (*iter != nullptr && (*iter)->get_file() != nullptr) {
3,698✔
3232
            retval += 1;
3,692✔
3233
        }
3234
    }
3235

3236
    return retval;
4,098✔
3237
}
3238

3239
size_t
3240
logfile_sub_source::text_size_for_line(textview_curses& tc,
×
3241
                                       int row,
3242
                                       text_sub_source::line_flags_t flags)
3243
{
3244
    size_t index = row % LINE_SIZE_CACHE_SIZE;
×
3245

3246
    if (this->lss_line_size_cache[index].first != row) {
×
3247
        std::string value;
×
3248

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

3273
int
3274
logfile_sub_source::get_filtered_count_for(size_t filter_index) const
1✔
3275
{
3276
    int retval = 0;
1✔
3277

3278
    for (const auto& ld : this->lss_files) {
2✔
3279
        retval += ld->ld_filter_state.lfo_filter_state
1✔
3280
                      .tfs_filter_hits[filter_index];
1✔
3281
    }
3282

3283
    return retval;
1✔
3284
}
3285

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

3312
        const auto dst
3313
            = std::distance(this->lss_filtered_index.begin(), first_lb);
261✔
3314
        return vis_line_t(dst);
261✔
3315
    }
3316

3317
    return std::nullopt;
5✔
3318
}
3319

3320
std::unique_ptr<logline_window>
3321
logfile_sub_source::window_at(vis_line_t start_vl, vis_line_t end_vl)
38✔
3322
{
3323
    return std::make_unique<logline_window>(*this, start_vl, end_vl);
38✔
3324
}
3325

3326
std::unique_ptr<logline_window>
3327
logfile_sub_source::window_at(vis_line_t start_vl)
208✔
3328
{
3329
    return std::make_unique<logline_window>(*this, start_vl, start_vl + 1_vl);
208✔
3330
}
3331

3332
std::unique_ptr<logline_window>
3333
logfile_sub_source::window_to_end(vis_line_t start_vl)
×
3334
{
3335
    return std::make_unique<logline_window>(
3336
        *this, start_vl, vis_line_t(this->text_line_count()));
×
3337
}
3338

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

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

3353
                auto low_vl = this->row_for_time(to_timeval(ts_low));
3✔
3354
                auto high_vl = this->row_for_time(to_timeval(ts_high));
3✔
3355
                if (low_vl) {
3✔
3356
                    auto lw = this->window_at(
3357
                        low_vl.value(),
3✔
3358
                        high_vl.value_or(low_vl.value() + 1_vl));
6✔
3359

3360
                    for (const auto& li : *lw) {
3✔
3361
                        auto hash_res = li.get_line_hash();
3✔
3362
                        if (hash_res.isErr()) {
3✔
3363
                            auto errmsg = hash_res.unwrapErr();
×
3364

3365
                            log_error("unable to get line hash: %s",
×
3366
                                      errmsg.c_str());
3367
                            continue;
×
3368
                        }
3369

3370
                        auto hash = hash_res.unwrap();
3✔
3371
                        if (hash == md[2]) {
3✔
3372
                            return li.get_vis_line();
3✔
3373
                        }
3374
                    }
12✔
3375
                }
3✔
3376
            }
3377
        }
3378

3379
        return std::nullopt;
×
3380
    }
3381

3382
    auto& vb = this->tss_view->get_bookmarks();
×
3383
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3384

3385
    for (const auto& vl : bv.bv_tree) {
×
3386
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3387
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3388
            continue;
×
3389
        }
3390

3391
        const auto& name = meta_opt.value()->bm_name;
×
3392
        if (id == text_anchors::to_anchor_string(name)) {
×
3393
            return vl;
×
3394
        }
3395
    }
3396

3397
    return std::nullopt;
×
3398
}
3399

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

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

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

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

3476
            return retval;
76✔
3477
        }
304✔
3478

3479
        return std::nullopt;
×
3480
    }
76✔
3481

3482
    return text_anchors::to_anchor_string(
3✔
3483
        line_meta.bmc_current_metadata.value()->bm_name);
3✔
3484
}
3485

3486
std::unordered_set<std::string>
3487
logfile_sub_source::get_anchors()
×
3488
{
3489
    auto& vb = this->tss_view->get_bookmarks();
×
3490
    const auto& bv = vb[&textview_curses::BM_PARTITION];
×
3491
    std::unordered_set<std::string> retval;
×
3492

3493
    for (const auto& vl : bv.bv_tree) {
×
3494
        auto meta_opt = this->find_bookmark_metadata(vl);
×
3495
        if (!meta_opt || meta_opt.value()->bm_name.empty()) {
×
3496
            continue;
×
3497
        }
3498

3499
        const auto& name = meta_opt.value()->bm_name;
×
3500
        retval.emplace(text_anchors::to_anchor_string(name));
×
3501
    }
3502

3503
    return retval;
×
3504
}
×
3505

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

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

3541
void
3542
logfile_sub_source::reload_config(error_reporter& reporter)
674✔
3543
{
3544
    static const auto& cfg
3545
        = injector::get<const logfile_sub_source_ns::config&>();
674✔
3546

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

3563
void
3564
logfile_sub_source::clear_preview()
1✔
3565
{
3566
    text_sub_source::clear_preview();
1✔
3567

3568
    this->set_preview_sql_filter(nullptr);
1✔
3569
    auto last = std::remove_if(this->lss_highlighters.begin(),
1✔
3570
                               this->lss_highlighters.end(),
3571
                               [](const auto& hl) { return hl.h_preview; });
×
3572
    this->lss_highlighters.erase(last, this->lss_highlighters.end());
1✔
3573
}
1✔
3574

3575
void
3576
logfile_sub_source::add_commands_for_session(
47✔
3577
    const std::function<void(const std::string&)>& receiver)
3578
{
3579
    text_sub_source::add_commands_for_session(receiver);
47✔
3580

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

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

3611
    for (const auto& format : log_format::get_root_formats()) {
3,569✔
3612
        auto field_states = format->get_field_states();
3,522✔
3613

3614
        for (const auto& fs_pair : field_states) {
48,046✔
3615
            if (!fs_pair.second.lvm_user_hidden) {
44,524✔
3616
                continue;
44,522✔
3617
            }
3618

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

3632
void
3633
logfile_sub_source::update_filter_hash_state(hasher& h) const
9✔
3634
{
3635
    text_sub_source::update_filter_hash_state(h);
9✔
3636

3637
    for (const auto& ld : this->lss_files) {
12✔
3638
        if (ld->get_file_ptr() == nullptr || !ld->is_visible()) {
3✔
3639
            h.update(0);
×
3640
        } else {
3641
            h.update(1);
3✔
3642
        }
3643
    }
3644
    h.update(this->tss_min_log_level);
9✔
3645
    h.update(this->lss_marked_only);
9✔
3646
}
9✔
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