Skip to content

Commit

Permalink
[log] allow parts of log messages to be hidden
Browse files Browse the repository at this point in the history
Fixes #412
  • Loading branch information
tstack committed Feb 23, 2017
1 parent 34d1422 commit 1a87184
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 111 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ lnav v0.8.2:
command used to rewrite the field in the pretty-printed version of a
log message. For example, the HTTP access log format will rewrite the
status code field to include the textual version (e.g. 200 (OK)).
* Log message fields can now be hidden using the 'hide-fields' command or
by setting the 'hidden' property in the log format. When hidden, the
fields will be replaced with a yellow ellipsis when displayed. Hiding
large fields that contain extra details can make the log easier to read.
The 'x' hotkey can be used to quickly toggle whether these fields are
displayed or not.

Interface Changes:
* The color used for text colored via ":highlight" is now based on the
Expand Down
9 changes: 8 additions & 1 deletion docs/source/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Display
* disable-word-wrap - Disable word wrapping in the log and text file views.
* enable-word-wrap - Enable word wrapping in the log and text file views.

* hide-fields <field-name> [<field-name2> ... <field-nameN>] - Hide large log
message fields by replacing them with an ellipsis. You can quickly switching
between showing and hiding hidden fields using the 'x' hotkey.

* show-fields <field-name> [<field-name2> ... <field-nameN>] - Show previously
hidden log message fields.

* highlight <regex> - Colorize text that matches the given regex.
* clear-highlight <regex> - Clear a previous highlight.

Expand Down Expand Up @@ -147,7 +154,7 @@ The following options are available:
- open
- pipe-to
- pipe-line-to
- write-*-to
- write-\*-to

This makes it easier to run lnav in restricted environments without the risk
of privilege escalation.
8 changes: 6 additions & 2 deletions docs/source/formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ fields:
an identifier and should be syntax colored.
:foreign-key: A boolean that indicates that this field is a key and should
not be graphed. This should only need to be set for integer fields.
:hidden: A boolean for JSON log fields that indicates whether they should
be displayed if they are not present in the line-format.
:hidden: A boolean for log fields that indicates whether they should
be displayed. The behavior is slightly different for JSON logs and text
logs. For a JSON log, this property determines whether an extra line
will be added with the key/value pair. For text logs, this property
controls whether the value should be displayed by default or replaced
with an ellipsis.
:rewriter: A command to rewrite this field when pretty-printing log
messages containing this value. The command must start with ':', ';',
or '|' to signify whether it is a regular command, SQL query, or a script
Expand Down
4 changes: 3 additions & 1 deletion docs/source/hotkeys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ Display
terminal without any decorations so they can be copied easily.
* - |ks| Ctrl |ke| + |ks| w |ke|
- Toggle word-wrap.

* - |ks| x |ke|
- Toggle the hiding of log message fields. The hidden fields will be
replaced with three bullets and highlighted in yellow.

Session
-------
Expand Down
13 changes: 13 additions & 0 deletions src/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ Display options

CTRL-W Toggle word-wrapping.

x Toggle the hiding of log message fields. The hidden fields
will be replaced with three bullets and highlighted in
yellow.

F2 Toggle mouse support.

Query
Expand Down Expand Up @@ -481,6 +485,15 @@ COMMANDS
show-lines-before-and-after
Show lines that were hidden by the 'hide-lines' commands.

hide-fields <field-name> [<field-name2> ... <field-nameN>]
Hide large log message fields by replacing them with an
ellipsis. You can quickly switching between showing and
hiding hidden fields using the 'x' hotkey.

show-fields <field-name> [<field-name2> ... <field-nameN>]
Show log messages fields that were previously hidden with
the 'hide-fields' command.

highlight <regex> Highlight strings that match the given regular
expression.

Expand Down
9 changes: 9 additions & 0 deletions src/hotkeys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,15 @@ void handle_paging_key(int ch)
}
break;

case 'x':
if (tc->toggle_hide_fields()) {
lnav_data.ld_rl_view->set_value("Showing hidden fields");
} else {
lnav_data.ld_rl_view->set_value("Hiding hidden fields");
}
tc->set_needs_update();
break;

case 'X':
lnav_data.ld_rl_view->set_value(execute_command(ec, "close"));
break;
Expand Down
75 changes: 75 additions & 0 deletions src/lnav_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,69 @@ static string com_set_min_log_level(exec_context &ec, string cmdline, vector<str
return retval;
}

static string com_toggle_field(exec_context &ec, string cmdline, vector<string> &args)
{
string retval;

if (args.empty()) {
args.push_back("colname");
} else if (args.size() < 2) {
retval = "error: Expecting a log message field name";
} else {
textview_curses *tc = lnav_data.ld_view_stack.top();

if (tc != &lnav_data.ld_views[LNV_LOG]) {
retval = "error: hiding fields only works in the log view";
} else if (tc->get_inner_height() == 0) {
retval = "error: no log messages to hide";
} else {
logfile_sub_source &lss = lnav_data.ld_log_source;
content_line_t cl = lss.at(tc->get_top());
logfile *lf = lss.find(cl);
log_format *format = lf->get_format();
bool hide = args[0] == "hide-fields";
vector<string> found_fields, missing_fields;

for (int lpc = 1; lpc < args.size(); lpc++) {
const intern_string_t name = intern_string::lookup(args[lpc]);

if (format->hide_field(name, hide)) {
found_fields.push_back(args[lpc]);
if (hide) {
if (lnav_data.ld_rl_view != NULL) {
lnav_data.ld_rl_view->set_alt_value(
HELP_MSG_1(x, "to quickly show hidden fields"));
}
}
tc->set_needs_update();
} else {
missing_fields.push_back(args[lpc]);
}
}

if (missing_fields.empty()) {
string all_fields = join(found_fields.begin(),
found_fields.end(),
", ");

if (hide) {
retval = "info: hiding field(s) -- " + all_fields;
} else {
retval = "info: showing field(s) -- " + all_fields;
}
} else {
string all_fields = join(missing_fields.begin(),
missing_fields.end(),
", ");

retval = "error: unknown field(s) -- " + all_fields;
}
}
}

return retval;
}

static string com_hide_line(exec_context &ec, string cmdline, vector<string> &args)
{
string retval;
Expand Down Expand Up @@ -2776,6 +2839,18 @@ readline_context::command_t STD_COMMANDS[] = {
"Open the help text view",
com_help,
},
{
"hide-fields",
"<field-name1> [<field-name2> ... <field-nameN>]",
"Hide log message fields by replacing them with an ellipsis",
com_toggle_field,
},
{
"show-fields",
"<field-name> [<field-name2> ... <field-nameN>]",
"Show log message fields that were previously hidden",
com_toggle_field,
},
{
"hide-lines-before",
"<line#|date>",
Expand Down
17 changes: 17 additions & 0 deletions src/lnav_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <string>
#include <vector>
#include <sstream>
#include <numeric>

#include "ptimec.hh"
#include "byte_array.hh"
Expand Down Expand Up @@ -190,6 +191,22 @@ file_format_t detect_file_format(const std::string &filename);

bool next_format(const char * const fmt[], int &index, int &locked_index);

namespace std {
inline string to_string(const string &s) { return s; }
}

template<class InputIt>
inline std::string join(InputIt first, InputIt last, const std::string &delim)
{
std::string retval;
return std::accumulate(first, last, retval, [&] (
typename InputIt::value_type l, typename InputIt::value_type r) {
std::string lstr = std::to_string(l);

return lstr + (lstr.empty() ? "" : delim) + std::to_string(r);
});
}

inline bool is_glob(const char *fn)
{
return (strchr(fn, '*') != NULL ||
Expand Down
4 changes: 4 additions & 0 deletions src/log_data_helper.hh
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public:
this->ldh_namer.reset(new column_namer());
this->ldh_json_pairs.clear();

for (auto lv : this->ldh_line_values) {
this->ldh_namer->add_column(lv.lv_name.get());
}

for (std::vector<logline_value>::iterator iter =
this->ldh_line_values.begin();
iter != this->ldh_line_values.end();
Expand Down
Loading

0 comments on commit 1a87184

Please sign in to comment.