1
1
mirror of https://github.com/tstack/lnav.git synced 2024-09-11 13:05:51 +03:00

[logfile] refactor visibility

This commit is contained in:
Timothy Stack 2020-11-25 14:47:39 -08:00
parent 2aad7985a6
commit 6d0054d3b6
19 changed files with 167 additions and 112 deletions

View File

@ -385,8 +385,6 @@ Result<string, string> execute_sql(exec_context &ec, const string &sql, string &
ensure_view(&lnav_data.ld_views[LNV_DB]);
}
}
} else {
lnav_data.ld_log_source.text_filters_changed();
}
#endif
}

View File

@ -52,8 +52,7 @@ CREATE TABLE lnav_file (
filepath text, -- The path to the file.
format text, -- The log file format for the file.
lines integer, -- The number of lines in the file.
time_offset integer, -- The millisecond offset for timestamps.
visible integer -- Indicates whether or not this file is being shown.
time_offset integer -- The millisecond offset for timestamps.
);
)";
@ -99,9 +98,6 @@ CREATE TABLE lnav_file (
to_sqlite(ctx, ms);
break;
}
case 6:
to_sqlite(ctx, (int64_t) lf->is_visible());
break;
default:
ensure(0);
break;
@ -129,8 +125,7 @@ CREATE TABLE lnav_file (
std::string path,
const char *format,
int64_t lines,
int64_t time_offset,
bool visible) {
int64_t time_offset) {
auto lf = this->lf_collection.fc_files[rowid];
struct timeval tv = {
(int) (time_offset / 1000LL),
@ -161,10 +156,6 @@ CREATE TABLE lnav_file (
}
}
if (lf->is_visible() != visible) {
lf->set_visibility(visible);
}
return SQLITE_OK;
};

View File

@ -62,12 +62,10 @@ bool files_sub_source::list_input_handle_key(listview_curses &lv, int ch)
auto& lss = lnav_data.ld_log_source;
auto &lf = fc.fc_files[sel];
if (!lf->is_visible()) {
lf->show();
if (lf->get_format() != nullptr) {
lss.text_filters_changed();
}
}
lss.find_data(lf) | [](auto ld) {
ld->set_visibility(true);
lss.text_filters_changed();
};
if (lf->get_format() != nullptr) {
auto& log_view = lnav_data.ld_views[LNV_LOG];
@ -102,8 +100,13 @@ bool files_sub_source::list_input_handle_key(listview_curses &lv, int ch)
return true;
}
auto& lss = lnav_data.ld_log_source;
auto &lf = fc.fc_files[sel];
lf->set_visibility(!lf->is_visible());
lss.find_data(lf) | [](auto ld) {
ld->set_visibility(!ld->ld_visible);
};
auto top_view = *lnav_data.ld_view_stack.top();
auto tss = top_view->get_sub_source();
@ -222,11 +225,16 @@ void files_sub_source::text_attrs_for_line(textview_curses &tc, int line,
line -= fc.fc_other_files.size();
auto& lss = lnav_data.ld_log_source;
auto &lf = fc.fc_files[line];
auto ld_opt = lss.find_data(lf);
chtype visible = lf->is_visible() ? ACS_DIAMOND : ' ';
chtype visible = ACS_DIAMOND;
if (ld_opt && !ld_opt.value()->ld_visible) {
visible = ' ';
}
value_out.emplace_back(line_range{2, 3}, &view_curses::VC_GRAPHIC, visible);
if (lf->is_visible()) {
if (visible == ACS_DIAMOND) {
value_out.emplace_back(line_range{2, 3}, &view_curses::VC_FOREGROUND,
vcolors.ansi_to_theme_color(COLOR_GREEN));
}

View File

@ -280,12 +280,16 @@ size_t filter_help_status_source::statusview_fields()
}
sel -= fc.fc_other_files.size();
auto& lss = lnav_data.ld_log_source;
auto &lf = lnav_data.ld_active_files.fc_files[sel];
auto vis_help = "Hide";
auto ld_opt = lss.find_data(lf);
if (ld_opt && !ld_opt.value()->ld_visible) {
vis_help = "Show";
}
this->fss_help.set_value(" %s%s %s",
ENABLE_HELP,
lf->is_visible() ? "Hide" : "Show",
JUMP_HELP);
ENABLE_HELP, vis_help, JUMP_HELP);
}
};

View File

@ -36,6 +36,7 @@
#include <list>
#include <string>
#include <utility>
#include <vector>
#include "base/func_util.hh"
@ -166,7 +167,7 @@ public:
* @param va The action to invoke when the view is scrolled.
* @todo Allow multiple observers.
*/
void set_scroll_action(action va) { this->lv_scroll = va; };
void set_scroll_action(action va) { this->lv_scroll = std::move(va); };
void set_show_scrollbar(bool ss) { this->lv_show_scrollbar = ss; };
bool get_show_scrollbar() const { return this->lv_show_scrollbar; };

View File

@ -495,7 +495,10 @@ public:
if (iter != session_data.sd_file_states.end()) {
log_debug("found state for log file %d",
iter->second.fs_is_visible);
lf->set_visibility(iter->second.fs_is_visible);
lnav_data.ld_log_source.find_data(lf) | [&iter](auto ld) {
ld->set_visibility(iter->second.fs_is_visible);
};
}
}
else {
@ -593,10 +596,6 @@ void rebuild_indexes()
bool reload = false;
for (const auto &lf : lnav_data.ld_active_files.fc_files) {
if (!lf->is_visible()) {
continue;
}
id_to_files[lf->get_content_id()].push_back(lf);
}

View File

@ -2098,7 +2098,9 @@ static Result<string, string> com_file_visibility(exec_context &ec, string cmdli
}
if (!ec.ec_dry_run) {
lf->set_visibility(make_visible);
lnav_data.ld_log_source.find_data(lf) | [make_visible](auto ld) {
ld->set_visibility(make_visible);
};
tc->get_sub_source()->text_filters_changed();
}
retval = fmt::format("{} file -- {}",
@ -2116,7 +2118,9 @@ static Result<string, string> com_file_visibility(exec_context &ec, string cmdli
continue;
}
if (lf->is_visible() == make_visible) {
auto ld_opt = lnav_data.ld_log_source.find_data(lf);
if (!ld_opt || ld_opt.value()->ld_visible == make_visible) {
continue;
}
@ -2132,7 +2136,9 @@ static Result<string, string> com_file_visibility(exec_context &ec, string cmdli
}
if (!ec.ec_dry_run) {
lf->set_visibility(make_visible);
ld_opt | [make_visible](auto ld) {
ld->set_visibility(make_visible);
};
}
if (lf->get_format() != nullptr) {
log_file_count += 1;

View File

@ -103,7 +103,7 @@ logfile::logfile(const string &filename, logfile_open_options &loo)
this->lf_line_buffer.set_fd(this->lf_options.loo_fd);
this->lf_index.reserve(INDEX_RESERVE_INCREMENT);
this->lf_is_visible = loo.loo_is_visible;
this->lf_indexing = loo.loo_is_visible;
ensure(this->invariant());
}
@ -133,7 +133,7 @@ bool logfile::exists() const
void logfile::reset_state()
{
this->clear_time_offset();
this->lf_is_visible = this->lf_options.loo_is_visible;
this->lf_indexing = this->lf_options.loo_is_visible;
}
void logfile::set_format_base_time(log_format *lf)
@ -285,7 +285,7 @@ bool logfile::process_prefix(shared_buffer_ref &sbr, const line_info &li)
logfile::rebuild_result_t logfile::rebuild_index()
{
if (!this->lf_is_visible) {
if (!this->lf_indexing) {
return logfile::rebuild_result_t::RR_NO_NEW_LINES;
}
@ -397,7 +397,7 @@ logfile::rebuild_result_t logfile::rebuild_index()
if (!this->lf_options.loo_non_utf_is_visible && !li.li_valid_utf) {
log_info("file is not utf, hiding: %s",
this->lf_filename.c_str());
this->lf_is_visible = false;
this->lf_indexing = false;
this->lf_options.loo_non_utf_is_visible = true;
break;
}
@ -459,7 +459,7 @@ logfile::rebuild_result_t logfile::rebuild_index()
st.st_size >= this->lf_options.loo_visible_size_limit) {
log_info("file has unknown format and is too large: %s",
this->lf_filename.c_str());
this->lf_is_visible = false;
this->lf_indexing = false;
this->lf_options.loo_visible_size_limit = -1;
}

View File

@ -212,7 +212,7 @@ public:
};
void mark_as_duplicate() {
this->hide();
this->lf_indexing = false;
this->lf_options.loo_is_visible = false;
}
@ -247,22 +247,6 @@ public:
/** @return True if this log file still exists. */
bool exists() const;
void hide() {
this->lf_is_visible = false;
}
void show() {
this->lf_is_visible = true;
}
void set_visibility(bool value) {
this->lf_is_visible = value;
}
bool is_visible() const {
return this->lf_is_visible;
}
void close() {
this->lf_is_closed = true;
};
@ -360,6 +344,10 @@ public:
return retval;
};
bool is_indexing() const {
return this->lf_indexing;
}
/** Check the invariants for this object. */
bool invariant()
{
@ -399,7 +387,7 @@ protected:
int lf_time_offset_line{0};
struct timeval lf_time_offset{0, 0};
bool lf_is_closed{false};
bool lf_is_visible{true};
bool lf_indexing{true};
bool lf_partial_line{false};
logline_observer *lf_logline_observer{nullptr};
logfile_observer *lf_logfile_observer{nullptr};

View File

@ -738,7 +738,7 @@ logfile_sub_source::rebuild_result logfile_sub_source::rebuild_index()
uint64_t line_number;
logfile_data *ld = this->find_data(cl, line_number);
if (!ld->get_file()->is_visible()) {
if (!ld->is_visible()) {
continue;
}
@ -884,7 +884,7 @@ void logfile_sub_source::text_filters_changed()
uint64_t line_number;
logfile_data *ld = this->find_data(cl, line_number);
if (!ld->get_file()->is_visible()) {
if (!ld->is_visible()) {
continue;
}

View File

@ -76,7 +76,7 @@ public:
class pcre_filter
: public text_filter {
public:
pcre_filter(type_t type, const std::string id, size_t index, pcre *code)
pcre_filter(type_t type, const std::string& id, size_t index, pcre *code)
: text_filter(type, id, index),
pf_pcre(code) { };
@ -556,8 +556,7 @@ public:
logfile_data(size_t index, filter_stack &fs, const std::shared_ptr<logfile> &lf)
: ld_file_index(index),
ld_filter_state(fs, lf),
ld_lines_indexed(0),
ld_enabled(true) {
ld_visible(lf->is_indexing()) {
lf->set_logline_observer(&this->ld_filter_state);
};
@ -565,11 +564,7 @@ public:
{
this->ld_filter_state.lfo_filter_state.clear();
};
void set_enabled(bool enabled) {
this->ld_enabled = enabled;
}
\
void set_file(const std::shared_ptr<logfile> &lf) {
this->ld_filter_state.lfo_filter_state.tfs_logfile = lf;
lf->set_logline_observer(&this->ld_filter_state);
@ -579,10 +574,18 @@ public:
return this->ld_filter_state.lfo_filter_state.tfs_logfile;
};
bool is_visible() const {
return this->ld_visible;
}
void set_visibility(bool vis) {
this->ld_visible = vis;
}
size_t ld_file_index;
line_filter_observer ld_filter_state;
size_t ld_lines_indexed;
bool ld_enabled;
size_t ld_lines_indexed{0};
bool ld_visible;
};
typedef std::vector<logfile_data *>::iterator iterator;
@ -618,6 +621,15 @@ public:
return retval;
};
nonstd::optional<logfile_data *> find_data(const std::shared_ptr<logfile>& lf) {
for (auto ld : *this) {
if (ld->ld_filter_state.lfo_filter_state.tfs_logfile == lf) {
return ld;
}
}
return nonstd::nullopt;
}
content_line_t get_file_base_content_line(iterator iter) {
ssize_t index = std::distance(this->begin(), iter);

View File

@ -279,12 +279,14 @@ void add_file_possibilities()
continue;
}
auto escaped_fn = lf->get_filename();
sh_escape.GlobalReplace(R"(\\\1)", &escaped_fn);
lnav_data.ld_log_source.find_data(lf) | [&lf, rc](auto ld) {
auto escaped_fn = lf->get_filename();
sh_escape.GlobalReplace(R"(\\\1)", &escaped_fn);
rc->add_possibility(LNM_COMMAND,
lf->is_visible() ? "visible-files" : "hidden-files",
escaped_fn);
rc->add_possibility(LNM_COMMAND,
ld->is_visible() ? "visible-files" : "hidden-files",
escaped_fn);
};
}
}

View File

@ -901,7 +901,9 @@ void load_session()
log_debug("found state for file: %s %d",
lf->get_content_id().c_str(),
iter->second.fs_is_visible);
lf->set_visibility(iter->second.fs_is_visible);
lnav_data.ld_log_source.find_data(lf) | [iter](auto ld) {
ld->set_visibility(iter->second.fs_is_visible);
};
if (!iter->second.fs_is_visible) {
if (lf->get_format() != nullptr) {
log_changes = true;
@ -1323,13 +1325,15 @@ static void save_session_with_id(const std::string session_id)
yajlpp_map file_states(handle);
for (auto &lf : lnav_data.ld_active_files.fc_files) {
auto ld_opt = lnav_data.ld_log_source.find_data(lf);
file_states.gen(lf->get_filename());
{
yajlpp_map file_state(handle);
file_state.gen("visible");
file_state.gen(lf->is_visible());
file_state.gen(!ld_opt || ld_opt.value()->ld_visible);
}
}
}

View File

@ -103,7 +103,6 @@ void textfile_sub_source::to_front(const std::shared_ptr<logfile>& lf)
if (iter != this->tss_hidden_files.end()) {
this->tss_hidden_files.erase(iter);
lf->show();
}
}
this->tss_files.push_front(lf);
@ -152,32 +151,18 @@ void textfile_sub_source::push_back(const std::shared_ptr<logfile>& lf)
{
auto *lfo = new line_filter_observer(this->get_filters(), lf);
lf->set_logline_observer(lfo);
if (lf->is_visible()) {
this->tss_files.push_back(lf);
} else {
this->tss_hidden_files.push_back(lf);
}
this->tss_files.push_back(lf);
}
void textfile_sub_source::text_filters_changed()
{
for (auto iter = this->tss_files.begin();
iter != this->tss_files.end();) {
if ((*iter)->is_visible()) {
++iter;
} else {
this->tss_hidden_files.push_back(*iter);
iter = this->tss_files.erase(iter);
}
++iter;
}
for (auto iter = this->tss_hidden_files.begin();
iter != this->tss_hidden_files.end();) {
if (!(*iter)->is_visible()) {
++iter;
} else {
this->tss_files.push_back(*iter);
iter = this->tss_hidden_files.erase(iter);
}
++iter;
}
std::shared_ptr<logfile> lf = this->current_file();

View File

@ -129,18 +129,18 @@ public:
LFT__MASK = (MAYBE|INCLUDE|EXCLUDE)
} type_t;
text_filter(type_t type, const std::string id, size_t index)
text_filter(type_t type, const std::string& id, size_t index)
: lf_type(type),
lf_id(id),
lf_index(index) { };
virtual ~text_filter() { };
virtual ~text_filter() = default;
type_t get_type() const { return this->lf_type; };
void set_type(type_t t) { this->lf_type = t; };
std::string get_id() const { return this->lf_id; };
size_t get_index() const { return this->lf_index; };
bool is_enabled() { return this->lf_enabled; };
bool is_enabled() const { return this->lf_enabled; };
void enable() { this->lf_enabled = true; };
void disable() { this->lf_enabled = false; };
void set_enabled(bool value) {
@ -690,7 +690,7 @@ public:
int off_start,
std::pair<int, int> &range_out);
void set_search_action(action sa) { this->tc_search_action = sa; };
void set_search_action(action sa) { this->tc_search_action = std::move(sa); };
void grep_end_batch(grep_proc<vis_line_t> &gp);
void grep_end(grep_proc<vis_line_t> &gp);

View File

@ -576,6 +576,67 @@ CREATE TABLE lnav_view_filter_stats (
}
};
struct lnav_view_files : public tvt_iterator_cursor<lnav_view_files> {
static constexpr const char *CREATE_STMT = R"(
--
CREATE TABLE lnav_view_files (
view_name TEXT, -- The name of the view.
filepath TEXT, -- The path to the file.
visible INTEGER -- Indicates whether or not the file is shown.
);
)";
using iterator = logfile_sub_source::iterator;
iterator begin() {
return lnav_data.ld_log_source.begin();
}
iterator end() {
return lnav_data.ld_log_source.end();
}
int get_column(cursor &vc, sqlite3_context *ctx, int col) {
auto ld = *vc.iter;
switch (col) {
case 0:
sqlite3_result_text(ctx,
lnav_view_strings[LNV_LOG], -1,
SQLITE_STATIC);
break;
case 1:
to_sqlite(ctx, ld->ld_filter_state.lfo_filter_state
.tfs_logfile->get_filename());
break;
case 2:
to_sqlite(ctx, ld->ld_visible);
break;
}
return SQLITE_OK;
}
int update_row(sqlite3_vtab *tab,
sqlite3_int64 &rowid,
const char *view_name,
const char *file_path,
bool visible) {
auto &lss = lnav_data.ld_log_source;
auto iter = this->begin();
std::advance(iter, rowid);
auto ld = *iter;
if (ld->ld_visible != visible) {
ld->set_visibility(visible);
lss.text_filters_changed();
}
return SQLITE_OK;
}
};
static const char *CREATE_FILTER_VIEW = R"(
CREATE VIEW lnav_view_filters_and_stats AS
SELECT * FROM lnav_view_filters LEFT NATURAL JOIN lnav_view_filter_stats
@ -587,6 +648,7 @@ int register_views_vtab(sqlite3 *db)
static vtab_module<lnav_view_stack> LNAV_VIEW_STACK_MODULE;
static vtab_module<lnav_view_filters> LNAV_VIEW_FILTERS_MODULE;
static vtab_module<tvt_no_update<lnav_view_filter_stats>> LNAV_VIEW_FILTER_STATS_MODULE;
static vtab_module<lnav_view_files> LNAV_VIEW_FILES_MODULE;
int rc;
@ -602,6 +664,9 @@ int register_views_vtab(sqlite3 *db)
rc = LNAV_VIEW_FILTER_STATS_MODULE.create(db, "lnav_view_filter_stats");
assert(rc == SQLITE_OK);
rc = LNAV_VIEW_FILES_MODULE.create(db, "lnav_view_files");
assert(rc == SQLITE_OK);
char *errmsg;
if (sqlite3_exec(db, CREATE_FILTER_VIEW, nullptr, nullptr, &errmsg) != SQLITE_OK) {
log_error("Unable to create filter view: %s", errmsg);

View File

@ -22,14 +22,6 @@ check_output "hide-file with log file does not work" <<EOF
EOF
run_test ${lnav_test} -n -d /tmp/lnav.err \
-c ":hide-file" \
${test_dir}/textfile_json_indented.0
check_output "hide-file with text file does not work" <<EOF
EOF
run_test ${lnav_test} -n -d /tmp/lnav.err \
-c ":goto 0" \
-c ":next-mark error" \

View File

@ -40,14 +40,13 @@ EOF
fi
run_test env TMPDIR=tmp ${lnav_test} -n \
-c ';SELECT basename(filepath), visible FROM lnav_file' \
-c ';SELECT view_name, basename(filepath), visible FROM lnav_view_files' \
test-logs.tgz
check_output "archive files not loaded correctly" <<EOF
basename(filepath) visible
logfile_access_log.0 1
logfile_access_log.1 1
logfile_empty.0 0
view_name basename(filepath) visible
log logfile_access_log.0 1
log logfile_access_log.1 1
EOF
run_test env TMPDIR=tmp ${lnav_test} -n \

View File

@ -740,7 +740,7 @@ EOF
schema_dump() {
${lnav_test} -n -c ';.schema' ${test_dir}/logfile_access_log.0 | head -n17
${lnav_test} -n -c ';.schema' ${test_dir}/logfile_access_log.0 | head -n18
}
run_test schema_dump
@ -752,6 +752,7 @@ CREATE VIRTUAL TABLE lnav_views USING lnav_views_impl();
CREATE VIRTUAL TABLE lnav_view_stack USING lnav_view_stack_impl();
CREATE VIRTUAL TABLE lnav_view_filters USING lnav_view_filters_impl();
CREATE VIRTUAL TABLE lnav_view_filter_stats USING lnav_view_filter_stats_impl();
CREATE VIRTUAL TABLE lnav_view_files USING lnav_view_files_impl();
CREATE VIEW lnav_view_filters_and_stats AS
SELECT * FROM lnav_view_filters LEFT NATURAL JOIN lnav_view_filter_stats;
CREATE VIRTUAL TABLE lnav_file USING lnav_file_impl();