lightningd/log: Improve logging to handle multi-line messages

Changelog-Added: Improved logging format in `lightningd/log` to handle multi-line messages by unescaping '\n' and logging each line separately.
Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>

lightningd/log: Improve log handling
Changelog-Added: Improved log handling to save memory.

Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>

lightningd/log: Refactor code for logging
Changelog-Added: Refactored code for log handling.

Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>
This commit is contained in:
Nishant Bansal 2024-12-16 21:12:34 +05:30
parent 7be96ae8bc
commit abf30f5f30
No known key found for this signature in database
GPG key ID: EAE292198F6C5B98

View file

@ -1,6 +1,7 @@
#include "config.h"
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/json_escape/json_escape.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/link/link.h>
@ -557,23 +558,17 @@ static void maybe_notify_log(struct logger *log,
notify_log(log->log_book->ld, l);
}
void logv(struct logger *log, enum log_level level,
const struct node_id *node_id,
bool call_notifier,
const char *fmt, va_list ap)
static void add_one_log_entry(struct logger *log, enum log_level level,
const struct node_id *node_id, bool call_notifier,
const char *log_msg)
{
int save_errno = errno;
struct log_entry *l = new_log_entry(log, level, node_id);
/* This is WARN_UNUSED_RESULT, because everyone should somehow deal
* with OOM, even though nobody does. */
if (vasprintf(&l->log, fmt, ap) == -1)
abort();
l->log = strdup(log_msg);
size_t log_len = strlen(l->log);
/* Sanitize any non-printable characters, and replace with '?' */
for (size_t i=0; i<log_len; i++)
/* Sanitize any non-printable characters, and replace with '?'
*/
for (size_t i = 0; log_msg[i] != '\0'; i++)
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
l->log[i] = '?';
@ -584,6 +579,39 @@ void logv(struct logger *log, enum log_level level,
if (call_notifier)
notify_warning(log->log_book->ld, l);
}
void logv(struct logger *log, enum log_level level,
const struct node_id *node_id,
bool call_notifier,
const char *fmt, va_list ap)
{
int save_errno = errno;
char *log_msg = NULL;
const char *unescaped_log;
/* This is WARN_UNUSED_RESULT, because everyone should somehow deal
* with OOM, even though nobody does. */
if (vasprintf(&log_msg, fmt, ap) == -1)
abort();
/* Nothing to escape: simple copy */
if (!strchr(log_msg, '\\'))
add_one_log_entry(log, level, node_id, call_notifier, log_msg);
/* If it's weird, unescaping can fail */
else if ((unescaped_log = json_escape_unescape(
tmpctx, (struct json_escape *)log_msg)) == NULL)
add_one_log_entry(log, level, node_id, call_notifier, log_msg);
else {
char **lines = tal_strsplit(unescaped_log, unescaped_log, "\n",
STR_EMPTY_OK);
/* Split to lines and log them separately. */
for (size_t i = 0; lines[i]; i++)
add_one_log_entry(log, level, node_id, call_notifier,
lines[i]);
}
free(log_msg);
errno = save_errno;
}