Skip to content

Commit

Permalink
lightningd/log: Improve log handling
Browse files Browse the repository at this point in the history
Changelog-Added: Improved log handling to save memory.

Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Dec 17, 2024
1 parent 62ed812 commit c83501c
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions lightningd/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,32 +572,48 @@ void logv(struct logger *log, enum log_level level,
if (vasprintf(&log_msg, fmt, ap) == -1)
abort();

char **lines = tal_strsplit(
log, json_escape_unescape(log, (struct json_escape *)log_msg), "\n",
STR_NO_EMPTY);

/* Split to lines and log them separately. */
for (size_t j = 0; lines[j]; j++) {
l->log = tal_strdup(log, lines[j]);
const char *unescaped_log =
json_escape_unescape(log, (struct json_escape *)log_msg);
if (!strchr(log_msg, '\\') || !unescaped_log) {
l->log = log_msg;
size_t log_len = strlen(l->log);

/* Sanitize any non-printable characters, and replace with '?'
*/
size_t line_len = strlen(l->log);
for (size_t i = 0; i < line_len; i++)
for (size_t i = 0; i < log_len; i++)
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
l->log[i] = '?';

maybe_print(log, l);
maybe_notify_log(log, l);

add_entry(log, &l);
} else {
char **lines =
tal_strsplit(log, take(unescaped_log), "\n", STR_NO_EMPTY);

/* Split to lines and log them separately. */
for (size_t j = 0; lines[j]; j++) {
l->log = strdup(lines[j]);

/* Sanitize any non-printable characters, and replace
* with '?'
*/
size_t line_len = strlen(l->log);
for (size_t i = 0; i < line_len; i++)
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
l->log[i] = '?';

maybe_print(log, l);
maybe_notify_log(log, l);

add_entry(log, &l);
}
}

if (call_notifier)
notify_warning(log->log_book->ld, l);

tal_free(lines);
free(log_msg);
errno = save_errno;
}

Expand Down

0 comments on commit c83501c

Please sign in to comment.