Skip to content

Commit 499365f

Browse files
lightningd/log: Refactor code for logging
Changelog-Added: Refactored code for log handling. Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>
1 parent df4f617 commit 499365f

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

lightningd/log.c

+37-47
Original file line numberDiff line numberDiff line change
@@ -558,70 +558,60 @@ static void maybe_notify_log(struct logger *log,
558558
notify_log(log->log_book->ld, l);
559559
}
560560

561+
static void add_one_log_entry(struct logger *log, enum log_level level,
562+
const struct node_id *node_id, bool call_notifier,
563+
char *log_msg)
564+
{
565+
struct log_entry *l = new_log_entry(log, level, node_id);
566+
567+
l->log = strdup(log_msg);
568+
size_t log_len = strlen(l->log);
569+
570+
/* Sanitize any non-printable characters, and replace with '?'
571+
*/
572+
for (size_t i = 0; i < log_len; i++)
573+
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
574+
l->log[i] = '?';
575+
576+
maybe_print(log, l);
577+
maybe_notify_log(log, l);
578+
579+
add_entry(log, &l);
580+
581+
if (call_notifier)
582+
notify_warning(log->log_book->ld, l);
583+
}
584+
561585
void logv(struct logger *log, enum log_level level,
562586
const struct node_id *node_id,
563587
bool call_notifier,
564588
const char *fmt, va_list ap)
565589
{
566590
int save_errno = errno;
567591
char *log_msg = NULL;
592+
const char *unescaped_log;
568593

569594
/* This is WARN_UNUSED_RESULT, because everyone should somehow deal
570595
* with OOM, even though nobody does. */
571596
if (vasprintf(&log_msg, fmt, ap) == -1)
572597
abort();
573598

574-
const char *unescaped_log =
575-
json_escape_unescape(NULL, (struct json_escape *)log_msg);
576-
if (!strchr(log_msg, '\\') || !unescaped_log) {
577-
struct log_entry *l = new_log_entry(log, level, node_id);
578-
579-
l->log = strdup(log_msg);
580-
size_t log_len = strlen(l->log);
581-
582-
/* Sanitize any non-printable characters, and replace with '?'
583-
*/
584-
for (size_t i = 0; i < log_len; i++)
585-
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
586-
l->log[i] = '?';
587-
588-
maybe_print(log, l);
589-
maybe_notify_log(log, l);
590-
591-
add_entry(log, &l);
592-
593-
if (call_notifier)
594-
notify_warning(log->log_book->ld, l);
595-
} else {
599+
/* Nothing to escape: simple copy */
600+
if (!strchr(log_msg, '\\'))
601+
add_one_log_entry(log, level, node_id, call_notifier, log_msg);
602+
/* If it's weird, unescaping can fail */
603+
else if ((unescaped_log = json_escape_unescape(
604+
tmpctx, (struct json_escape *)log_msg)) == NULL)
605+
add_one_log_entry(log, level, node_id, call_notifier, log_msg);
606+
else {
596607
char **lines = tal_strsplit(unescaped_log, unescaped_log, "\n",
597-
STR_NO_EMPTY);
598-
608+
STR_EMPTY_OK);
599609
/* Split to lines and log them separately. */
600-
for (size_t j = 0; lines[j]; j++) {
601-
struct log_entry *l =
602-
new_log_entry(log, level, node_id);
603-
604-
l->log = strdup(lines[j]);
605-
606-
/* Sanitize any non-printable characters, and replace
607-
* with '?'
608-
*/
609-
size_t line_len = strlen(l->log);
610-
for (size_t i = 0; i < line_len; i++)
611-
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
612-
l->log[i] = '?';
613-
614-
maybe_print(log, l);
615-
maybe_notify_log(log, l);
616-
617-
add_entry(log, &l);
618-
619-
if (call_notifier)
620-
notify_warning(log->log_book->ld, l);
621-
}
610+
for (size_t i = 0; lines[i]; i++)
611+
add_one_log_entry(log, level, node_id, call_notifier,
612+
lines[i]);
622613
}
623614

624-
tal_free(unescaped_log);
625615
free(log_msg);
626616

627617
errno = save_errno;

0 commit comments

Comments
 (0)