Skip to content

Commit

Permalink
logv: unescape "\n" and "\\" in the log
Browse files Browse the repository at this point in the history
Double escape node alias in the log, to print weird node alias
in escaped form.

Fix #4912
  • Loading branch information
starius committed Jan 25, 2024
1 parent d9a747e commit 376e4b7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1266,11 +1266,12 @@ int main(int argc, char *argv[])
* formatting. json_escape() avoids printing weird characters in our
* log. And tal_hex() is a helper from utils which returns a hex string;
* it's assumed that the argument was allocated with tal or tal_arr
* so it can use tal_bytelen() to get the length. */
* so it can use tal_bytelen() to get the length.
* Double json_escape is needed, because logv unescapes \\ and \n. */
log_info(ld->log, "--------------------------------------------------");
log_info(ld->log, "Server started with public key %s, alias %s (color #%s) and lightningd %s",
type_to_string(tmpctx, struct node_id, &ld->id),
json_escape(tmpctx, (const char *)ld->alias)->s,
json_escape(tmpctx, json_escape(tmpctx, (const char *)ld->alias)->s)->s,
tal_hex(tmpctx, ld->rgb), version());
ld->state = LD_STATE_RUNNING;

Expand Down
18 changes: 18 additions & 0 deletions lightningd/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,24 @@ void logv(struct logger *log, enum log_level level,
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
l->log[i] = '?';

/* Replace "\n" with line breaks and "\\" with "\" in the log, if they exist. */
char *write_ptr = l->log;
for (size_t i=0; i<log_len; i++) {
if (l->log[i] == '\\' && i < log_len-1 && l->log[i+1] == 'n') {
// Replace \n with line break.
*write_ptr = '\n';
i++;
} else if (l->log[i] == '\\' && i < log_len-1 && l->log[i+1] == '\\') {
// Replace \\ with \.
*write_ptr = '\\';
i++;
} else {
*write_ptr = l->log[i];
}
write_ptr++;
}
*write_ptr = 0x00;

maybe_print(log, l);

add_entry(log, &l);
Expand Down
3 changes: 2 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,8 @@ def mock_fail(*args):
l1.daemon.start(wait_for_initialized=False, stderr_redir=True)
l1.daemon.wait_for_logs([r'getblockhash [a-z0-9]* exited with status 1',
r'Unable to estimate any fees',
r'BROKEN.*we have been retrying command for --bitcoin-retry-timeout={} seconds'.format(timeout)])
r'BROKEN',
r'we have been retrying command for --bitcoin-retry-timeout={} seconds'.format(timeout)])
# Will exit with failure code.
assert l1.daemon.wait() == 1

Expand Down
6 changes: 5 additions & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,11 @@ def test_libplugin(node_factory):
# Test commands
assert l1.rpc.call("helloworld") == {"hello": "NOT FOUND"}
l1.daemon.wait_for_log("get_ds_bin_done: 00010203")
l1.daemon.wait_for_log("BROKEN.* Datastore gave nonstring result.*00010203")
l1.daemon.wait_for_logs([
r'BROKEN',
r'Datastore gave nonstring result',
r'00010203',
])
assert l1.rpc.call("helloworld", {"name": "test"}) == {"hello": "test"}
l1.stop()
l1.daemon.opts["plugin"] = plugin
Expand Down

0 comments on commit 376e4b7

Please sign in to comment.