From 96135dab5e8cfc31065a49d7edd1215050ab33dc Mon Sep 17 00:00:00 2001 From: trueptolemy <823220586@qq.com> Date: Thu, 6 Jun 2019 16:26:42 +0800 Subject: [PATCH] log: add 'warning' notification when log - Related Changes for `warning` notification Add a `bool` type parameter in `log_()` and `lov()`, this `bool` flag indicates if we should call `warning` notifier. 1) The process of copying `log_book` of every peer to the `log_book` of `ld` is usually included in `log_()` and `lov()`, and it may lead to repeated `warning` notification. So a `bool`, which explicitly indicates if the `warning` notification is disabled during this call, is necessary . 2) The `LOG_INFO` and `LOG_DEBUG` level don't need to call warning, so set that `bool` paramater as `FALSE` for these log level and only set it as `TRUE` for `LOG_UNUAUSL`/`LOG_BROKEN`. As for `LOG_IO`, it use `log_io()` to log, so we needn't think about notifier for it. --- lightningd/lightningd.c | 2 +- lightningd/log.c | 20 ++++++++++++----- lightningd/log.h | 25 ++++++++++++--------- lightningd/log_status.c | 5 ++++- lightningd/peer_control.c | 4 ++-- lightningd/plugin.c | 4 +++- lightningd/test/run-find_my_abspath.c | 4 ++-- lightningd/test/run-invoice-select-inchan.c | 4 ++-- lightningd/test/run-jsonrpc.c | 2 +- wallet/test/run-db.c | 2 +- wallet/test/run-wallet.c | 4 ++-- 11 files changed, 47 insertions(+), 29 deletions(-) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 357212223..bc8e31ac2 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -155,7 +155,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx) * book to hold all the entries (and trims as necessary), and multiple * log objects which each can write into it, each with a unique * prefix. */ - ld->log_book = new_log_book(20*1024*1024, LOG_INFORM); + ld->log_book = new_log_book(ld, 20*1024*1024, LOG_INFORM); /*~ Note the tal context arg (by convention, the first argument to any * allocation function): ld->log will be implicitly freed when ld * is. */ diff --git a/lightningd/log.c b/lightningd/log.c index 28f7e4e95..3d4205d0e 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -102,7 +103,7 @@ static size_t prune_log(struct log_book *log) return deleted; } -struct log_book *new_log_book(size_t max_mem, +struct log_book *new_log_book(struct lightningd *ld, size_t max_mem, enum log_level printlevel) { struct log_book *lr = tal_linkable(tal(NULL, struct log_book)); @@ -114,6 +115,7 @@ struct log_book *new_log_book(size_t max_mem, lr->print = log_to_stdout; lr->print_level = printlevel; lr->init_time = time_now(); + lr->ld = ld; list_head_init(&lr->log); return lr; @@ -226,7 +228,8 @@ static void maybe_print(const struct log *log, const struct log_entry *l, l->io, tal_bytelen(l->io), log->lr->print_arg); } -void logv(struct log *log, enum log_level level, const char *fmt, va_list ap) +void logv(struct log *log, enum log_level level, bool call_notifier, + const char *fmt, va_list ap) { int save_errno = errno; struct log_entry *l = new_log_entry(log, level); @@ -243,6 +246,10 @@ void logv(struct log *log, enum log_level level, const char *fmt, va_list ap) maybe_print(log, l, 0); add_entry(log, l); + + if (call_notifier) + notify_warning(log->lr->ld, l); + errno = save_errno; } @@ -295,12 +302,13 @@ void logv_add(struct log *log, const char *fmt, va_list ap) maybe_print(log, l, oldlen); } -void log_(struct log *log, enum log_level level, const char *fmt, ...) +void log_(struct log *log, enum log_level level, bool call_notifier, + const char *fmt, ...) { va_list ap; va_start(ap, fmt); - logv(log, level, fmt, ap); + logv(log, level, call_notifier, fmt, ap); va_end(ap); } @@ -536,7 +544,7 @@ void log_backtrace_print(const char *fmt, ...) return; va_start(ap, fmt); - logv(crashlog, LOG_BROKEN, fmt, ap); + logv(crashlog, LOG_BROKEN, false, fmt, ap); va_end(ap); } @@ -609,7 +617,7 @@ void fatal(const char *fmt, ...) exit(1); va_start(ap, fmt); - logv(crashlog, LOG_BROKEN, fmt, ap); + logv(crashlog, LOG_BROKEN, true, fmt, ap); va_end(ap); abort(); } diff --git a/lightningd/log.h b/lightningd/log.h index de8a4932e..8b501d8df 100644 --- a/lightningd/log.h +++ b/lightningd/log.h @@ -41,6 +41,11 @@ struct log_book { struct timeabs init_time; struct list_head log; + /* Although log_book will copy log entries to parent log_book + * (the log_book belongs to lightningd), a pointer to lightningd + * is more directly because the notification needs ld->plugins. + */ + struct lightningd *ld; }; struct log { @@ -48,26 +53,26 @@ struct log { const char *prefix; }; -/* We can have a single log book, with multiple logs in it: it's freed by - * the last struct log itself. */ -struct log_book *new_log_book(size_t max_mem, +/* We can have a single log book, with multiple logs in it: it's freed + * by the last struct log itself. */ +struct log_book *new_log_book(struct lightningd *ld, size_t max_mem, enum log_level printlevel); /* With different entry points */ struct log *new_log(const tal_t *ctx, struct log_book *record, const char *fmt, ...) PRINTF_FMT(3,4); -#define log_debug(log, ...) log_((log), LOG_DBG, __VA_ARGS__) -#define log_info(log, ...) log_((log), LOG_INFORM, __VA_ARGS__) -#define log_unusual(log, ...) log_((log), LOG_UNUSUAL, __VA_ARGS__) -#define log_broken(log, ...) log_((log), LOG_BROKEN, __VA_ARGS__) +#define log_debug(log, ...) log_((log), LOG_DBG, false, __VA_ARGS__) +#define log_info(log, ...) log_((log), LOG_INFORM, false, __VA_ARGS__) +#define log_unusual(log, ...) log_((log), LOG_UNUSUAL, true, __VA_ARGS__) +#define log_broken(log, ...) log_((log), LOG_BROKEN, true, __VA_ARGS__) void log_io(struct log *log, enum log_level dir, const char *comment, const void *data, size_t len); -void log_(struct log *log, enum log_level level, const char *fmt, ...) - PRINTF_FMT(3,4); +void log_(struct log *log, enum log_level level, bool call_notifier, const char *fmt, ...) + PRINTF_FMT(4,5); void log_add(struct log *log, const char *fmt, ...) PRINTF_FMT(2,3); -void logv(struct log *log, enum log_level level, const char *fmt, va_list ap); +void logv(struct log *log, enum log_level level, bool call_notifier, const char *fmt, va_list ap); void logv_add(struct log *log, const char *fmt, va_list ap); enum log_level get_log_level(struct log_book *lr); diff --git a/lightningd/log_status.c b/lightningd/log_status.c index 88d6befc1..2af7d46f6 100644 --- a/lightningd/log_status.c +++ b/lightningd/log_status.c @@ -6,10 +6,13 @@ bool log_status_msg(struct log *log, const u8 *msg) char *entry, *who; u8 *data; enum log_level level; + bool call_notifier; if (fromwire_status_log(msg, msg, &level, &entry)) { if (level != LOG_IO_IN && level != LOG_IO_OUT) { - log_(log, level, "%s", entry); + call_notifier = (level == LOG_BROKEN || + level == LOG_UNUSUAL)? true : false; + log_(log, level, call_notifier, "%s", entry); return true; } } else if (fromwire_status_io(msg, msg, &level, &who, &data)) { diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 29a143c16..284ce259d 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -81,7 +81,7 @@ static void copy_to_parent_log(const char *prefix, else if (continued) log_add(parent_log, "%s ... %s", prefix, str); else - log_(parent_log, level, "%s %s", prefix, str); + log_(parent_log, level, false, "%s %s", prefix, str); } static void peer_update_features(struct peer *peer, @@ -119,7 +119,7 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid, #endif /* Max 128k per peer. */ - peer->log_book = new_log_book(128*1024, get_log_level(ld->log_book)); + peer->log_book = new_log_book(peer->ld, 128*1024, get_log_level(ld->log_book)); set_log_outfn(peer->log_book, copy_to_parent_log, ld->log); list_add_tail(&ld->peers, &peer->list); tal_add_destructor(peer, destroy_peer); diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 8dbafb6ad..9628f9bc7 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -196,6 +196,7 @@ static void plugin_log_handle(struct plugin *plugin, const jsmntok_t *paramstok) { const jsmntok_t *msgtok, *leveltok; enum log_level level; + bool call_notifier; msgtok = json_get_member(plugin->buffer, paramstok, "message"); leveltok = json_get_member(plugin->buffer, paramstok, "level"); @@ -222,7 +223,8 @@ static void plugin_log_handle(struct plugin *plugin, const jsmntok_t *paramstok) return; } - log_(plugin->log, level, "%.*s", msgtok->end - msgtok->start, + call_notifier = (level == LOG_BROKEN || level == LOG_UNUSUAL)? true : false; + log_(plugin->log, level, call_notifier, "%.*s", msgtok->end - msgtok->start, plugin->buffer + msgtok->start); } diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index cdc58a8e1..2a3b219a1 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -106,7 +106,7 @@ void jsonrpc_setup(struct lightningd *ld UNNEEDED) void load_channels_from_wallet(struct lightningd *ld UNNEEDED) { fprintf(stderr, "load_channels_from_wallet called!\n"); abort(); } /* Generated stub for log_ */ -void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...) +void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, bool call_notifier UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "log_ called!\n"); abort(); } /* Generated stub for log_backtrace_exit */ @@ -125,7 +125,7 @@ bool log_status_msg(struct log *log UNNEEDED, const u8 *msg UNNEEDED) struct log *new_log(const tal_t *ctx UNNEEDED, struct log_book *record UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "new_log called!\n"); abort(); } /* Generated stub for new_log_book */ -struct log_book *new_log_book(size_t max_mem UNNEEDED, +struct log_book *new_log_book(struct lightningd *ld UNNEEDED, size_t max_mem UNNEEDED, enum log_level printlevel UNNEEDED) { fprintf(stderr, "new_log_book called!\n"); abort(); } /* Generated stub for new_topology */ diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 43443fb07..77ea44e10 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -244,7 +244,7 @@ void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED, const char *why UNNEEDED) { fprintf(stderr, "kill_uncommitted_channel called!\n"); abort(); } /* Generated stub for log_ */ -void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...) +void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, bool call_notifier UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "log_ called!\n"); abort(); } /* Generated stub for log_add */ @@ -262,7 +262,7 @@ struct bolt11 *new_bolt11(const tal_t *ctx UNNEEDED, struct log *new_log(const tal_t *ctx UNNEEDED, struct log_book *record UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "new_log called!\n"); abort(); } /* Generated stub for new_log_book */ -struct log_book *new_log_book(size_t max_mem UNNEEDED, +struct log_book *new_log_book(struct lightningd *ld UNNEEDED, size_t max_mem UNNEEDED, enum log_level printlevel UNNEEDED) { fprintf(stderr, "new_log_book called!\n"); abort(); } /* Generated stub for new_reltimer_ */ diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 1808f6d3c..7d7b51c56 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -39,7 +39,7 @@ bool json_to_txid(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct bitcoin_txid *txid UNNEEDED) { fprintf(stderr, "json_to_txid called!\n"); abort(); } /* Generated stub for log_ */ -void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...) +void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, bool call_notifier UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "log_ called!\n"); abort(); } /* Generated stub for log_io */ diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 25cfbf393..63b37fe25 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -3,7 +3,7 @@ static void db_test_fatal(const char *fmt, ...); #define db_fatal db_test_fatal -static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const char *fmt UNUSED, ...) +static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, bool call_notifier UNUSED, const char *fmt UNUSED, ...) { } #define log_ db_log_ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 95647ff7f..da7d2e056 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -4,7 +4,7 @@ static void wallet_test_fatal(const char *fmt, ...); #define db_fatal wallet_test_fatal #include "test_utils.h" -static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const char *fmt UNUSED, ...) +static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, bool call_notifier UNUSED, const char *fmt UNUSED, ...) { } #define log_ db_log_ @@ -647,7 +647,7 @@ struct log *new_log(const tal_t *ctx UNNEEDED, struct log_book *record UNNEEDED, return NULL; } -struct log_book *new_log_book(size_t max_mem UNNEEDED, +struct log_book *new_log_book(struct lightningd *ld UNNEEDED, size_t max_mem UNNEEDED, enum log_level printlevel UNNEEDED) { return NULL;