Skip to content

Commit 60fba42

Browse files
committed
lightningd: move log_prefix and log_entry struct definitions into log.c.
They're only exposed because of the notifications, but they are better off with explicit parameters anyway. Signed-off-by: Rusty Russell <[email protected]>
1 parent 396a189 commit 60fba42

File tree

6 files changed

+88
-42
lines changed

6 files changed

+88
-42
lines changed

lightningd/log.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
/* Once we're up and running, this is set up. */
2222
struct logger *crashlog;
2323

24+
/* Reference counted log_prefix. Log entries keep a pointer, and they
25+
* can outlast the log entry point which created them. */
26+
struct log_prefix {
27+
size_t refcnt;
28+
const char *prefix;
29+
};
30+
31+
struct log_entry {
32+
struct timeabs time;
33+
enum log_level level;
34+
unsigned int skipped;
35+
struct node_id_cache *nc;
36+
struct log_prefix *prefix;
37+
char *log;
38+
/* Iff LOG_IO */
39+
const u8 *io;
40+
};
41+
2442
struct print_filter {
2543
/* In list log_book->print_filters / log_file->print_filters */
2644
struct list_node list;
@@ -565,7 +583,11 @@ static void maybe_notify_log(struct logger *log,
565583
const struct log_entry *l)
566584
{
567585
if (l->level >= log->print_level)
568-
notify_log(log->log_book->ld, l);
586+
notify_log(log->log_book->ld,
587+
l->level,
588+
l->time,
589+
l->prefix->prefix,
590+
l->log);
569591
}
570592

571593
void logv(struct logger *log, enum log_level level,
@@ -594,7 +616,11 @@ void logv(struct logger *log, enum log_level level,
594616
add_entry(log, &l);
595617

596618
if (call_notifier)
597-
notify_warning(log->log_book->ld, l);
619+
notify_warning(log->log_book->ld,
620+
l->level,
621+
l->time,
622+
l->prefix->prefix,
623+
l->log);
598624

599625
errno = save_errno;
600626
}

lightningd/log.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,6 @@ struct command_result *param_loglevel(struct command *cmd,
7575
const jsmntok_t *tok,
7676
enum log_level **level);
7777

78-
/* Reference counted log_prefix. Log entries keep a pointer, and they
79-
* can outlast the log entry point which created them. */
80-
struct log_prefix {
81-
size_t refcnt;
82-
const char *prefix;
83-
};
84-
85-
struct log_entry {
86-
struct timeabs time;
87-
enum log_level level;
88-
unsigned int skipped;
89-
struct node_id_cache *nc;
90-
struct log_prefix *prefix;
91-
char *log;
92-
/* Iff LOG_IO */
93-
const u8 *io;
94-
};
95-
9678
/* For options.c's listconfig */
9779
char *opt_log_level(const char *arg, struct log_book *log_book);
9880
void json_add_opt_log_levels(struct json_stream *response, struct log_book *log_book);

lightningd/notification.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,38 @@ void notify_disconnect(struct lightningd *ld, const struct node_id *nodeid)
100100
/*'warning' is based on LOG_UNUSUAL/LOG_BROKEN level log
101101
*(in plugin module, they're 'warn'/'error' level). */
102102
static void warning_notification_serialize(struct json_stream *stream,
103-
struct log_entry *l)
103+
enum log_level level,
104+
struct timeabs time,
105+
const char *source,
106+
const char *logmsg)
104107
{
105108
/* Choose "BROKEN"/"UNUSUAL" to keep consistent with the habit
106109
* of plugin. But this may confuses the users who want to 'getlog'
107110
* with the level indicated by notifications. It is the duty of a
108111
* plugin to eliminate this misunderstanding. */
109112
json_add_string(stream, "level",
110-
l->level == LOG_BROKEN ? "error"
113+
level == LOG_BROKEN ? "error"
111114
: "warn");
112115
/* unsuaul/broken event is rare, plugin pay more attentions on
113116
* the absolute time, like when channels failed. */
114-
json_add_timestr(stream, "time", l->time.ts);
115-
json_add_timeiso(stream, "timestamp", l->time);
116-
json_add_string(stream, "source", l->prefix->prefix);
117-
json_add_string(stream, "log", l->log);
117+
json_add_timestr(stream, "time", time.ts);
118+
json_add_timeiso(stream, "timestamp", time);
119+
json_add_string(stream, "source", source);
120+
json_add_string(stream, "log", logmsg);
118121
}
119122

120123
REGISTER_NOTIFICATION(warning);
121124

122-
void notify_warning(struct lightningd *ld, struct log_entry *l)
125+
void notify_warning(struct lightningd *ld,
126+
enum log_level level,
127+
struct timeabs time,
128+
const char *source,
129+
const char *logmsg)
123130
{
124131
struct jsonrpc_notification *n = notify_start(ld, "warning");
125132
if (!n)
126133
return;
127-
warning_notification_serialize(n->stream, l);
134+
warning_notification_serialize(n->stream, level, time, source, logmsg);
128135
notify_send(ld, n);
129136
}
130137

@@ -631,26 +638,33 @@ bool notify_deprecated_oneshot(struct lightningd *ld,
631638
REGISTER_NOTIFICATION(deprecated_oneshot);
632639

633640
static void log_notification_serialize(struct json_stream *stream,
634-
const struct log_entry *l)
641+
enum log_level level,
642+
struct timeabs time,
643+
const char *source,
644+
const char *logmsg)
635645
{
636-
json_add_string(stream, "level", log_level_name(l->level));
637-
json_add_timestr(stream, "time", l->time.ts);
638-
json_add_timeiso(stream, "timestamp", l->time);
639-
json_add_string(stream, "source", l->prefix->prefix);
640-
json_add_string(stream, "log", l->log);
646+
json_add_string(stream, "level", log_level_name(level));
647+
json_add_timestr(stream, "time", time.ts);
648+
json_add_timeiso(stream, "timestamp", time);
649+
json_add_string(stream, "source", source);
650+
json_add_string(stream, "log", logmsg);
641651
}
642652

643653

644654
REGISTER_NOTIFICATION(log);
645655

646-
void notify_log(struct lightningd *ld, const struct log_entry *l)
656+
void notify_log(struct lightningd *ld,
657+
enum log_level level,
658+
struct timeabs time,
659+
const char *source,
660+
const char *logmsg)
647661
{
648662
struct jsonrpc_notification *n;
649663

650664
n = notify_start(ld, "log");
651665
if (!n)
652666
return;
653-
log_notification_serialize(n->stream, l);
667+
log_notification_serialize(n->stream, level, time, source, logmsg);
654668
notify_send(ld, n);
655669
}
656670

lightningd/notification.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ void notify_connect(struct lightningd *ld,
2727
const struct wireaddr_internal *addr);
2828
void notify_disconnect(struct lightningd *ld, const struct node_id *nodeid);
2929

30-
void notify_warning(struct lightningd *ld, struct log_entry *l);
30+
void notify_warning(struct lightningd *ld,
31+
enum log_level level,
32+
struct timeabs time,
33+
const char *source,
34+
const char *logmsg);
3135

3236
void notify_custommsg(struct lightningd *ld,
3337
const struct node_id *peer_id,
@@ -120,7 +124,11 @@ bool notify_deprecated_oneshot(struct lightningd *ld,
120124
/* Tell this plugin to shutdown: returns true if it was subscribed. */
121125
bool notify_plugin_shutdown(struct lightningd *ld, struct plugin *p);
122126
/* Inform the plugin when a log line is emitted */
123-
void notify_log(struct lightningd *ld, const struct log_entry *l);
127+
void notify_log(struct lightningd *ld,
128+
enum log_level level,
129+
struct timeabs time,
130+
const char *source,
131+
const char *logmsg);
124132

125133
void notify_plugin_started(struct lightningd *ld, struct plugin *plugin);
126134
void notify_plugin_stopped(struct lightningd *ld, struct plugin *plugin);

lightningd/test/run-log-pruning.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,18 @@ void json_stream_log_suppress_for_cmd(struct json_stream *js UNNEEDED,
5656
struct json_stream *json_stream_success(struct command *cmd UNNEEDED)
5757
{ fprintf(stderr, "json_stream_success called!\n"); abort(); }
5858
/* Generated stub for notify_log */
59-
void notify_log(struct lightningd *ld UNNEEDED, const struct log_entry *l UNNEEDED)
59+
void notify_log(struct lightningd *ld UNNEEDED,
60+
enum log_level level UNNEEDED,
61+
struct timeabs time UNNEEDED,
62+
const char *source UNNEEDED,
63+
const char *logmsg UNNEEDED)
6064
{ fprintf(stderr, "notify_log called!\n"); abort(); }
6165
/* Generated stub for notify_warning */
62-
void notify_warning(struct lightningd *ld UNNEEDED, struct log_entry *l UNNEEDED)
66+
void notify_warning(struct lightningd *ld UNNEEDED,
67+
enum log_level level UNNEEDED,
68+
struct timeabs time UNNEEDED,
69+
const char *source UNNEEDED,
70+
const char *logmsg UNNEEDED)
6371
{ fprintf(stderr, "notify_warning called!\n"); abort(); }
6472
/* AUTOGENERATED MOCKS END */
6573

lightningd/test/run-log_filter.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ static size_t test_fwrite(const void *ptr, size_t size, size_t nmemb,
1111

1212
#include "../log.c"
1313

14-
void notify_log(struct lightningd *ld UNNEEDED, const struct log_entry *l UNNEEDED)
14+
void notify_log(struct lightningd *ld UNNEEDED,
15+
enum log_level level UNNEEDED,
16+
struct timeabs time UNNEEDED,
17+
const char *source UNNEEDED,
18+
const char *logmsg UNNEEDED)
1519
{ }
1620

1721
/* AUTOGENERATED MOCKS START */
@@ -67,7 +71,11 @@ void json_stream_log_suppress_for_cmd(struct json_stream *js UNNEEDED,
6771
struct json_stream *json_stream_success(struct command *cmd UNNEEDED)
6872
{ fprintf(stderr, "json_stream_success called!\n"); abort(); }
6973
/* Generated stub for notify_warning */
70-
void notify_warning(struct lightningd *ld UNNEEDED, struct log_entry *l UNNEEDED)
74+
void notify_warning(struct lightningd *ld UNNEEDED,
75+
enum log_level level UNNEEDED,
76+
struct timeabs time UNNEEDED,
77+
const char *source UNNEEDED,
78+
const char *logmsg UNNEEDED)
7179
{ fprintf(stderr, "notify_warning called!\n"); abort(); }
7280
/* AUTOGENERATED MOCKS END */
7381

0 commit comments

Comments
 (0)