lightningd: move log_prefix and log_entry struct definitions into log.c.
What changed, and why it matters
This commit is a straightforward internal code cleanup. It moves two data structure definitions from a public header file into the single source file where they are actually used, and changes notification functions to accept plain values instead of a pointer to those internal structures. There is no user-visible behavior change and no security fix.
No security action required. Treat as normal refactoring; review for merge conflicts only.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch hides struct log_prefix and struct log_entry from lightningd/log.h by moving them into lightningd/log.c. Callers of notify_warning() and notify_log() now pass individual fields (level, time, prefix->prefix, log) rather than a struct log_entry *. This reduces header coupling and improves encapsulation. No logic, serialization format, memory allocation, or notification semantics are altered.
Changed components
lightningd/log.clightningd/log.hlightningd/notification.clightningd/notification.hlightningd/test/run-log-pruning.clightningd/test/run-log_filter.cInspect captured patch +88 / −42
diff --git a/lightningd/log.c b/lightningd/log.c
index 8e12a6a1..8b7a9f20 100644
--- a/lightningd/log.c
+++ b/lightningd/log.c
@@ -21,6 +21,24 @@
/* Once we're up and running, this is set up. */
struct logger *crashlog;
+/* Reference counted log_prefix. Log entries keep a pointer, and they
+ * can outlast the log entry point which created them. */
+struct log_prefix {
+ size_t refcnt;
+ const char *prefix;
+};
+
+struct log_entry {
+ struct timeabs time;
+ enum log_level level;
+ unsigned int skipped;
+ struct node_id_cache *nc;
+ struct log_prefix *prefix;
+ char *log;
+ /* Iff LOG_IO */
+ const u8 *io;
+};
+
struct print_filter {
/* In list log_book->print_filters / log_file->print_filters */
struct list_node list;
@@ -565,7 +583,11 @@ static void maybe_notify_log(struct logger *log,
const struct log_entry *l)
{
if (l->level >= log->print_level)
- notify_log(log->log_book->ld, l);
+ notify_log(log->log_book->ld,
+ l->level,
+ l->time,
+ l->prefix->prefix,
+ l->log);
}
void logv(struct logger *log, enum log_level level,
@@ -594,7 +616,11 @@ void logv(struct logger *log, enum log_level level,
add_entry(log, &l);
if (call_notifier)
- notify_warning(log->log_book->ld, l);
+ notify_warning(log->log_book->ld,
+ l->level,
+ l->time,
+ l->prefix->prefix,
+ l->log);
errno = save_errno;
}
diff --git a/lightningd/log.h b/lightningd/log.h
index c9b5277a..a05c7762 100644
--- a/lightningd/log.h
+++ b/lightningd/log.h
@@ -75,24 +75,6 @@ struct command_result *param_loglevel(struct command *cmd,
const jsmntok_t *tok,
enum log_level **level);
-/* Reference counted log_prefix. Log entries keep a pointer, and they
- * can outlast the log entry point which created them. */
-struct log_prefix {
- size_t refcnt;
- const char *prefix;
-};
-
-struct log_entry {
- struct timeabs time;
- enum log_level level;
- unsigned int skipped;
- struct node_id_cache *nc;
- struct log_prefix *prefix;
- char *log;
- /* Iff LOG_IO */
- const u8 *io;
-};
-
/* For options.c's listconfig */
char *opt_log_level(const char *arg, struct log_book *log_book);
void json_add_opt_log_levels(struct json_stream *response, struct log_book *log_book);
diff --git a/lightningd/notification.c b/lightningd/notification.c
index b2adb64e..755a606b 100644
--- a/lightningd/notification.c
+++ b/lightningd/notification.c
@@ -100,31 +100,38 @@ void notify_disconnect(struct lightningd *ld, const struct node_id *nodeid)
/*'warning' is based on LOG_UNUSUAL/LOG_BROKEN level log
*(in plugin module, they're 'warn'/'error' level). */
static void warning_notification_serialize(struct json_stream *stream,
- struct log_entry *l)
+ enum log_level level,
+ struct timeabs time,
+ const char *source,
+ const char *logmsg)
{
/* Choose "BROKEN"/"UNUSUAL" to keep consistent with the habit
* of plugin. But this may confuses the users who want to 'getlog'
* with the level indicated by notifications. It is the duty of a
* plugin to eliminate this misunderstanding. */
json_add_string(stream, "level",
- l->level == LOG_BROKEN ? "error"
+ level == LOG_BROKEN ? "error"
: "warn");
/* unsuaul/broken event is rare, plugin pay more attentions on
* the absolute time, like when channels failed. */
- json_add_timestr(stream, "time", l->time.ts);
- json_add_timeiso(stream, "timestamp", l->time);
- json_add_string(stream, "source", l->prefix->prefix);
- json_add_string(stream, "log", l->log);
+ json_add_timestr(stream, "time", time.ts);
+ json_add_timeiso(stream, "timestamp", time);
+ json_add_string(stream, "source", source);
+ json_add_string(stream, "log", logmsg);
}
REGISTER_NOTIFICATION(warning);
-void notify_warning(struct lightningd *ld, struct log_entry *l)
+void notify_warning(struct lightningd *ld,
+ enum log_level level,
+ struct timeabs time,
+ const char *source,
+ const char *logmsg)
{
struct jsonrpc_notification *n = notify_start(ld, "warning");
if (!n)
return;
- warning_notification_serialize(n->stream, l);
+ warning_notification_serialize(n->stream, level, time, source, logmsg);
notify_send(ld, n);
}
@@ -631,26 +638,33 @@ bool notify_deprecated_oneshot(struct lightningd *ld,
REGISTER_NOTIFICATION(deprecated_oneshot);
static void log_notification_serialize(struct json_stream *stream,
- const struct log_entry *l)
+ enum log_level level,
+ struct timeabs time,
+ const char *source,
+ const char *logmsg)
{
- json_add_string(stream, "level", log_level_name(l->level));
- json_add_timestr(stream, "time", l->time.ts);
- json_add_timeiso(stream, "timestamp", l->time);
- json_add_string(stream, "source", l->prefix->prefix);
- json_add_string(stream, "log", l->log);
+ json_add_string(stream, "level", log_level_name(level));
+ json_add_timestr(stream, "time", time.ts);
+ json_add_timeiso(stream, "timestamp", time);
+ json_add_string(stream, "source", source);
+ json_add_string(stream, "log", logmsg);
}
REGISTER_NOTIFICATION(log);
-void notify_log(struct lightningd *ld, const struct log_entry *l)
+void notify_log(struct lightningd *ld,
+ enum log_level level,
+ struct timeabs time,
+ const char *source,
+ const char *logmsg)
{
struct jsonrpc_notification *n;
n = notify_start(ld, "log");
if (!n)
return;
- log_notification_serialize(n->stream, l);
+ log_notification_serialize(n->stream, level, time, source, logmsg);
notify_send(ld, n);
}
diff --git a/lightningd/notification.h b/lightningd/notification.h
index 91454a55..7e4c9411 100644
--- a/lightningd/notification.h
+++ b/lightningd/notification.h
@@ -27,7 +27,11 @@ void notify_connect(struct lightningd *ld,
const struct wireaddr_internal *addr);
void notify_disconnect(struct lightningd *ld, const struct node_id *nodeid);
-void notify_warning(struct lightningd *ld, struct log_entry *l);
+void notify_warning(struct lightningd *ld,
+ enum log_level level,
+ struct timeabs time,
+ const char *source,
+ const char *logmsg);
void notify_custommsg(struct lightningd *ld,
const struct node_id *peer_id,
@@ -120,7 +124,11 @@ bool notify_deprecated_oneshot(struct lightningd *ld,
/* Tell this plugin to shutdown: returns true if it was subscribed. */
bool notify_plugin_shutdown(struct lightningd *ld, struct plugin *p);
/* Inform the plugin when a log line is emitted */
-void notify_log(struct lightningd *ld, const struct log_entry *l);
+void notify_log(struct lightningd *ld,
+ enum log_level level,
+ struct timeabs time,
+ const char *source,
+ const char *logmsg);
void notify_plugin_started(struct lightningd *ld, struct plugin *plugin);
void notify_plugin_stopped(struct lightningd *ld, struct plugin *plugin);
diff --git a/lightningd/test/run-log-pruning.c b/lightningd/test/run-log-pruning.c
index 893c7071..02fcc9a0 100644
--- a/lightningd/test/run-log-pruning.c
+++ b/lightningd/test/run-log-pruning.c
@@ -56,10 +56,18 @@ void json_stream_log_suppress_for_cmd(struct json_stream *js UNNEEDED,
struct json_stream *json_stream_success(struct command *cmd UNNEEDED)
{ fprintf(stderr, "json_stream_success called!\n"); abort(); }
/* Generated stub for notify_log */
-void notify_log(struct lightningd *ld UNNEEDED, const struct log_entry *l UNNEEDED)
+void notify_log(struct lightningd *ld UNNEEDED,
+ enum log_level level UNNEEDED,
+ struct timeabs time UNNEEDED,
+ const char *source UNNEEDED,
+ const char *logmsg UNNEEDED)
{ fprintf(stderr, "notify_log called!\n"); abort(); }
/* Generated stub for notify_warning */
-void notify_warning(struct lightningd *ld UNNEEDED, struct log_entry *l UNNEEDED)
+void notify_warning(struct lightningd *ld UNNEEDED,
+ enum log_level level UNNEEDED,
+ struct timeabs time UNNEEDED,
+ const char *source UNNEEDED,
+ const char *logmsg UNNEEDED)
{ fprintf(stderr, "notify_warning called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
diff --git a/lightningd/test/run-log_filter.c b/lightningd/test/run-log_filter.c
index b1258654..c700eae8 100644
--- a/lightningd/test/run-log_filter.c
+++ b/lightningd/test/run-log_filter.c
@@ -11,7 +11,11 @@ static size_t test_fwrite(const void *ptr, size_t size, size_t nmemb,
#include "../log.c"
-void notify_log(struct lightningd *ld UNNEEDED, const struct log_entry *l UNNEEDED)
+void notify_log(struct lightningd *ld UNNEEDED,
+ enum log_level level UNNEEDED,
+ struct timeabs time UNNEEDED,
+ const char *source UNNEEDED,
+ const char *logmsg UNNEEDED)
{ }
/* AUTOGENERATED MOCKS START */
@@ -67,7 +71,11 @@ void json_stream_log_suppress_for_cmd(struct json_stream *js UNNEEDED,
struct json_stream *json_stream_success(struct command *cmd UNNEEDED)
{ fprintf(stderr, "json_stream_success called!\n"); abort(); }
/* Generated stub for notify_warning */
-void notify_warning(struct lightningd *ld UNNEEDED, struct log_entry *l UNNEEDED)
+void notify_warning(struct lightningd *ld UNNEEDED,
+ enum log_level level UNNEEDED,
+ struct timeabs time UNNEEDED,
+ const char *source UNNEEDED,
+ const char *logmsg UNNEEDED)
{ fprintf(stderr, "notify_warning called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.