Skip to content

Commit

Permalink
log: add 'warning' notification when log
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
trueptolemy authored and rustyrussell committed Jun 7, 2019
1 parent 231703c commit 96135da
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
20 changes: 14 additions & 6 deletions lightningd/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/notification.h>
#include <lightningd/options.h>
#include <signal.h>
#include <stdio.h>
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
Expand Down
25 changes: 15 additions & 10 deletions lightningd/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,38 @@ 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 {
struct log_book *lr;
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);
Expand Down
5 changes: 4 additions & 1 deletion lightningd/log_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions lightningd/test/run-find_my_abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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_ */
Expand Down
2 changes: 1 addition & 1 deletion lightningd/test/run-jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion wallet/test/run-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
4 changes: 2 additions & 2 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 96135da

Please sign in to comment.