Skip to content

Commit 1445f87

Browse files
committed
[log] show invalid utf hex dump in log view too
1 parent ba882bb commit 1445f87

8 files changed

+51
-1
lines changed

src/field_overlay_source.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <curl/curl.h>
3333

34+
#include "base/attr_line.builder.hh"
3435
#include "base/auto_mem.hh"
3536
#include "base/humanize.time.hh"
3637
#include "base/injector.hh"
@@ -86,6 +87,33 @@ field_overlay_source::build_field_lines(const listview_curses& lv,
8687
display = display || this->fos_contexts.top().c_show;
8788
}
8889

90+
if (!ll->is_valid_utf()) {
91+
auto sub_opts = subline_options{};
92+
sub_opts.scrub_invalid_utf8 = false;
93+
auto read_res = file->read_line(ll, sub_opts);
94+
if (read_res.isOk()) {
95+
auto sbr = read_res.unwrap();
96+
attr_line_t al;
97+
attr_line_builder alb(al);
98+
99+
alb.append_as_hexdump(sbr.to_string_fragment());
100+
this->fos_lines.emplace_back(
101+
attr_line_t(" ")
102+
.append(
103+
"Hex dump of line with invalid UTF-8 content"_table_header)
104+
.move());
105+
al.split_lines(this->fos_lines);
106+
auto first_line = true;
107+
for (auto& al_line : this->fos_lines) {
108+
if (first_line) {
109+
first_line = false;
110+
continue;
111+
}
112+
al_line.insert(0, 4, ' ');
113+
}
114+
}
115+
}
116+
89117
if (!display) {
90118
return;
91119
}

src/logfile_sub_source.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ logfile_sub_source::text_value_for_line(textview_curses& tc,
305305
sbr.get_metadata().m_has_ansi = false;
306306
}
307307
} else {
308+
auto sub_opts = subline_options{};
309+
sub_opts.scrub_invalid_utf8 = false;
308310
this->lss_token_value
309-
= this->lss_token_file->read_line(this->lss_token_line)
311+
= this->lss_token_file->read_line(this->lss_token_line, sub_opts)
310312
.map([](auto sbr) { return to_string(sbr); })
311313
.unwrapOr({});
312314
if (this->lss_token_line->has_ansi()) {

test/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ dist_noinst_DATA = \
372372
logfile_invalid_json2.json \
373373
logfile_mixed_json2.json \
374374
logfile_mux_zookeeper.0 \
375+
logfile_invalid_utf8.0 \
375376
logfile_java.0 \
376377
logfile_java.1 \
377378
logfile_journald.json \

test/expected/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ dist_noinst_DATA = \
494494
test_logfile.sh_7c2e11488bccc59458b5775db4b90de964858259.out \
495495
test_logfile.sh_82d65f4dac07b1623f8202444d5b9144096e8243.err \
496496
test_logfile.sh_82d65f4dac07b1623f8202444d5b9144096e8243.out \
497+
test_logfile.sh_83b6f3269785adf92e60154e959ab14fba7cfb7f.err \
498+
test_logfile.sh_83b6f3269785adf92e60154e959ab14fba7cfb7f.out \
497499
test_logfile.sh_8a5e754cd471e5fdcdaede49c9290903acd7aad6.err \
498500
test_logfile.sh_8a5e754cd471e5fdcdaede49c9290903acd7aad6.out \
499501
test_logfile.sh_977cd36c516d568070253202a0ba81f545cb216f.err \

test/expected/test_logfile.sh_83b6f3269785adf92e60154e959ab14fba7cfb7f.err

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<node>???????????���??</node>
2+
▌ Line 3 metadata
3+
Hex dump of line with invalid UTF-8 content
4+
09 09 3c 6e 6f 64 65 3e 3f 3f 3f 3f 3f 3f 3f 3f __<node> ????????
5+
3f 3f 3f ed b9 a4 3f 3f 3c 2f 6e 6f 64 65 3e ???×××?? </node>
6+
</sample>
7+
</tiptop>

test/logfile_invalid_utf8.0

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2015-03-12T23:16:52.071:INFO:com.root:Response :
2+
<tiptop>
3+
<sample>
4+
<node>???????????���??</node>
5+
</sample>
6+
</tiptop>

test/test_logfile.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export TZ=UTC
44
echo ${top_srcdir}
55
echo ${top_builddir}
66

7+
run_cap_test ${lnav_test} -n \
8+
-c ':goto 3' \
9+
${test_dir}/logfile_invalid_utf8.0
10+
711
printf '#Date:\t20\x800-2-02\n0\n' | run_cap_test \
812
env TEST_COMMENT="short timestamp" ${lnav_test} -n
913

0 commit comments

Comments
 (0)