Skip to content

Commit

Permalink
[session] add (commented-out) hide-fields/show-fields commands to export
Browse files Browse the repository at this point in the history
Related to #1352
  • Loading branch information
tstack committed Jan 30, 2025
1 parent 4d624f5 commit 2fb77b8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
7 changes: 5 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ Features:
adding a column with the name `__lnav_style__`.
* Added "format <format-name> test" management command to make it
easier to test a format against a file.
Can be helpful for determining why a file is not being recognized
by particular format.
This can be helpful for determining why a file is not being
recognized by particular format.
* Added a "performance" section to the documentation.
* Session exports now include `:hide-fields` and `:show-fields`
commands from the session.
They are currently commented out by default.

Interface changes:
* DB query results that start with a number are right justified
Expand Down
69 changes: 54 additions & 15 deletions src/session.export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "session.export.hh"

#include "base/injector.hh"
#include "base/itertools.hh"
#include "bound_tags.hh"
#include "lnav.hh"
#include "sqlitepp.client.hh"
Expand All @@ -49,9 +50,9 @@ struct log_message_session_state {

template<>
struct from_sqlite<log_message_session_state> {
inline log_message_session_state operator()(int argc,
sqlite3_value** argv,
int argi)
log_message_session_state operator()(int argc,
sqlite3_value** argv,
int argi)
{
return {
from_sqlite<int64_t>()(argc, argv, argi + 0),
Expand All @@ -76,9 +77,9 @@ struct log_filter_session_state {

template<>
struct from_sqlite<log_filter_session_state> {
inline log_filter_session_state operator()(int argc,
sqlite3_value** argv,
int argi)
log_filter_session_state operator()(int argc,
sqlite3_value** argv,
int argi)
{
return {
from_sqlite<std::string>()(argc, argv, argi + 0),
Expand All @@ -98,9 +99,7 @@ struct log_file_session_state {

template<>
struct from_sqlite<log_file_session_state> {
inline log_file_session_state operator()(int argc,
sqlite3_value** argv,
int argi)
log_file_session_state operator()(int argc, sqlite3_value** argv, int argi)
{
return {
from_sqlite<std::string>()(argc, argv, argi + 0),
Expand Down Expand Up @@ -199,7 +198,7 @@ SELECT content_id, format, time_offset FROM lnav_file
WHERE format IS NOT NULL AND time_offset != 0
)";

static constexpr const char HEADER[] = R"(#!lnav -Nf
static constexpr char HEADER[] = R"(#!lnav -Nf
# This file is an export of an lnav session. You can type
# '|/path/to/this/file' in lnav to execute this file and
# restore the state of the session.
Expand All @@ -211,20 +210,20 @@ SELECT content_id, format, time_offset FROM lnav_file
)";

static constexpr const char LOG_DIR_INSERT[] = R"(
static constexpr char LOG_DIR_INSERT[] = R"(
# Set this environment variable to override this value or edit this script.
;INSERT OR IGNORE INTO environ (name, value) VALUES ('LOG_DIR_{}', {})
)";

static constexpr const char MARK_HEADER[] = R"(
static constexpr char MARK_HEADER[] = R"(
# The following SQL statements will restore the bookmarks,
# comments, and tags that were added in the session.
;SELECT total_changes() AS before_mark_changes
)";

static constexpr const char MARK_FOOTER[] = R"(
static constexpr char MARK_FOOTER[] = R"(
;SELECT {} - (total_changes() - $before_mark_changes) AS failed_mark_changes
;SELECT echoln(printf('%sERROR%s: failed to restore %d bookmarks',
$ansi_red, $ansi_norm, $failed_mark_changes))
Expand All @@ -246,14 +245,20 @@ SELECT content_id, format, time_offset FROM lnav_file
;SELECT total_changes() AS before_file_changes
)";

static constexpr const char FILE_FOOTER[] = R"(
static const char* FIELD_HEADER = R"(
# The following field visibility commands were run by the
# original user during this session. Uncomment them if
# desired.
)";

static constexpr char FILE_FOOTER[] = R"(
;SELECT {} - (total_changes() - $before_file_changes) AS failed_file_changes
;SELECT echoln(printf('%sERROR%s: failed to restore the state of %d files',
$ansi_red, $ansi_norm, $failed_file_changes))
WHERE $failed_file_changes != 0
)";

static constexpr const char VIEW_HEADER[] = R"(
static constexpr char VIEW_HEADER[] = R"(
# The following commands will restore the state of the {} view.
Expand Down Expand Up @@ -430,6 +435,40 @@ SELECT content_id, format, time_offset FROM lnav_file
FMT_STRING(":switch-to-view {}\n"),
lnav_view_strings[view_index]);

if (view_index == LNV_LOG) {
std::vector<std::string> field_cmds;
for (const auto& format : log_format::get_root_formats()) {
auto field_states = format->get_field_states();

for (const auto& fs_pair : field_states) {
if (!fs_pair.second.lvm_user_hidden) {
continue;
}

if (fs_pair.second.lvm_user_hidden.value()) {
field_cmds.emplace_back(
fmt::format(FMT_STRING("# :hide-fields {}.{}\n"),

format->get_name(),
fs_pair.first));
} else if (fs_pair.second.lvm_hidden) {
field_cmds.emplace_back(
fmt::format(FMT_STRING("# :show-fields {}.{}\n"),
format->get_name().to_string(),
fs_pair.first));
}
}
}

if (!field_cmds.empty()) {
fmt::print(file, FMT_STRING("{}"), FIELD_HEADER);
field_cmds
| lnav::itertools::for_each([&file](const auto& cmd) {
fmt::print(file, FMT_STRING("{}"), cmd);
});
}
}

auto* tss = tc.get_sub_source();
auto* lss = dynamic_cast<logfile_sub_source*>(tss);
if (lss != nullptr) {
Expand Down
4 changes: 2 additions & 2 deletions test/expected/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ dist_noinst_DATA = \
test_regex101.sh_fc41b6ee90cbf038620151f16d164b361acf82dd.out \
test_sessions.sh_0300a1391c33b1c45ddfa90198a6bd0a5404a77f.err \
test_sessions.sh_0300a1391c33b1c45ddfa90198a6bd0a5404a77f.out \
test_sessions.sh_14aae534ade7470dee4e812297dd719f0725fee4.err \
test_sessions.sh_14aae534ade7470dee4e812297dd719f0725fee4.out \
test_sessions.sh_17b85654b929b2a8fc1705a170ced544783292fa.err \
test_sessions.sh_17b85654b929b2a8fc1705a170ced544783292fa.out \
test_sessions.sh_2ee066bea01006950407a34421e763020d36acd5.err \
Expand Down Expand Up @@ -598,8 +600,6 @@ dist_noinst_DATA = \
test_sessions.sh_a2b217a4102aa5cbe4952f935c5d416f02fa4cb7.out \
test_sessions.sh_a92822d121a836140a401fd71535dc4a7a8d5b48.err \
test_sessions.sh_a92822d121a836140a401fd71535dc4a7a8d5b48.out \
test_sessions.sh_b3d71a87fcb4e3487f71ccad8c6ce681db220572.err \
test_sessions.sh_b3d71a87fcb4e3487f71ccad8c6ce681db220572.out \
test_sessions.sh_b932b33dd087b94d4306dd179c5d4f9ddd394960.err \
test_sessions.sh_b932b33dd087b94d4306dd179c5d4f9ddd394960.out \
test_sessions.sh_ba1ded92531350668301431db64df2d2f4a2e9ee.err \
Expand Down
1 change: 1 addition & 0 deletions test/test_sessions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ run_cap_test ${lnav_test} -nq \

run_cap_test ${lnav_test} -nq \
-c ";update access_log set log_mark = 1 where sc_bytes > 60000" \
-c ":hide-fields cs_user_agent" \
-c ":set-min-log-level debug" \
-c ":hide-lines-before 2005" \
-c ":hide-lines-after 2030" \
Expand Down

0 comments on commit 2fb77b8

Please sign in to comment.