Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

last-word shortening algorithm added #1249

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ object with the following fields:
:truncate: Truncates any text past the maximum width.
:dot-dot: Cuts out the middle of the text and replaces it with two
dots (i.e. '..').
:last-word: Removes all but the last word in dotted text. For example,
"com.example.foo" would be shortened to "foo".

(v0.8.2+)
:timestamp-format: The timestamp format to use when displaying the time
Expand Down
28 changes: 28 additions & 0 deletions src/base/string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ is_url(const std::string& fn)
return std::regex_match(fn, url_re);
}

size_t
last_word_str(char* str, size_t len, size_t max_len)
{
if (len < max_len) {
return len;
}

size_t last_start = 0;

for (size_t index = 0; index < len; index++) {
switch (str[index]) {
case '.':
case '-':
case '/':
case ':':
last_start = index + 1;
break;
}
}

if (last_start == 0) {
return len;
}

memmove(&str[0], &str[last_start], len - last_start);
return len - last_start;
}

size_t
abbreviate_str(char* str, size_t len, size_t max_len)
{
Expand Down
2 changes: 2 additions & 0 deletions src/base/string_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ bool is_blank(const std::string& str);

size_t abbreviate_str(char* str, size_t len, size_t max_len);

size_t last_word_str(char* str, size_t len, size_t max_len);

void split_ws(const std::string& str, std::vector<std::string>& toks_out);

std::string repeat(const std::string& input, size_t num);
Expand Down
7 changes: 7 additions & 0 deletions src/log_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,13 @@ external_log_format::get_subline(const logline& ll,
rest);
break;
}
case json_format_element::overflow_t::
LASTWORD: {
size_t new_size = last_word_str(&str[0], str.size(), jfe.jfe_max_width);
str.resize(new_size);
this->json_append(jfe, vd, str.data(), str.size());
break;
}
}
} else {
sub_offset
Expand Down
1 change: 1 addition & 0 deletions src/log_format_ext.hh
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public:
ABBREV,
TRUNCATE,
DOTDOT,
LASTWORD,
};

enum class transform_t {
Expand Down
1 change: 1 addition & 0 deletions src/log_format_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ static const json_path_handler_base::enum_value_t OVERFLOW_ENUM[] = {
{"truncate",
external_log_format::json_format_element::overflow_t::TRUNCATE},
{"dot-dot", external_log_format::json_format_element::overflow_t::DOTDOT},
{"last-word", external_log_format::json_format_element::overflow_t::LASTWORD},

json_path_handler_base::ENUM_TERMINATOR,
};
Expand Down