diff --git a/lightningd/log.c b/lightningd/log.c index 0d975934c..3b4df6a1c 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -809,22 +809,25 @@ void log_backtrace_exit(void) } } +void fatal_vfmt(const char *fmt, va_list ap) +{ + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + + if (!crashlog) + exit(1); + + logv(crashlog, LOG_BROKEN, NULL, true, fmt, ap); + abort(); +} + void fatal(const char *fmt, ...) { va_list ap; va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); + fatal_vfmt(fmt, ap); va_end(ap); - - if (!crashlog) - exit(1); - - va_start(ap, fmt); - logv(crashlog, LOG_BROKEN, NULL, true, fmt, ap); - va_end(ap); - abort(); } struct log_info { diff --git a/lightningd/log.h b/lightningd/log.h index daf7896e3..95dc2ada2 100644 --- a/lightningd/log.h +++ b/lightningd/log.h @@ -53,6 +53,7 @@ char *arg_log_to_file(const char *arg, struct lightningd *ld); /* Once this is set, we dump fatal with a backtrace to this log */ extern struct log *crashlog; void NORETURN PRINTF_FMT(1,2) fatal(const char *fmt, ...); +void NORETURN fatal_vfmt(const char *fmt, va_list ap); void log_backtrace_print(const char *fmt, ...); void log_backtrace_exit(void); diff --git a/wallet/db_common.h b/wallet/db_common.h index 0ab196c29..233d7b93a 100644 --- a/wallet/db_common.h +++ b/wallet/db_common.h @@ -6,11 +6,6 @@ #include #include -/* For testing, we want to catch fatal messages. */ -#ifndef db_fatal -#define db_fatal fatal -#endif - struct db { char *filename; const char *in_transaction; @@ -163,6 +158,9 @@ struct db_config { /* Provide a way for DB backends to register themselves */ AUTODATA_TYPE(db_backends, struct db_config); +void db_fatal(const char *fmt, ...) + PRINTF_FMT(1, 2); + /** * Report a statement that changes the wallet * diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 176661324..637658526 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -1,9 +1,6 @@ #include "config.h" #include -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 struct node_id *node_id UNUSED, bool call_notifier UNUSED, const char *fmt UNUSED, ...) { } @@ -59,7 +56,7 @@ bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED) /* AUTOGENERATED MOCKS END */ static char *db_err; -static void db_test_fatal(const char *fmt, ...) +void db_fatal(const char *fmt, ...) { va_list ap; diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index d97b72296..fa12dcf62 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -1,15 +1,34 @@ #include "config.h" #include -static void wallet_test_fatal(const char *fmt, ...); -#define db_fatal wallet_test_fatal #include "test_utils.h" +#include +#include static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const struct node_id *node_id UNUSED, bool call_notifier UNUSED, const char *fmt UNUSED, ...) { } #define log_ db_log_ +#ifndef DB_FATAL +#define DB_FATAL +static char *wallet_err; +void db_fatal(const char *fmt, ...) +{ + va_list ap; + + /* Fail hard if we're complaining about not being in transaction */ + assert(!strstarts(fmt, "No longer in transaction")); + + /* Fail hard if we're complaining about not being in transaction */ + assert(!strstarts(fmt, "No longer in transaction")); + + va_start(ap, fmt); + wallet_err = tal_vfmt(NULL, fmt, ap); + va_end(ap); +} +#endif /* DB_FATAL */ + #include "wallet/wallet.c" #include "lightningd/htlc_end.c" #include "lightningd/peer_control.c" @@ -837,22 +856,6 @@ bool fromwire_hsmd_get_channel_basepoints_reply(const void *p UNNEEDED, return true; } -static char *wallet_err; -static void wallet_test_fatal(const char *fmt, ...) -{ - va_list ap; - - /* Fail hard if we're complaining about not being in transaction */ - assert(!strstarts(fmt, "No longer in transaction")); - - /* Fail hard if we're complaining about not being in transaction */ - assert(!strstarts(fmt, "No longer in transaction")); - - va_start(ap, fmt); - wallet_err = tal_vfmt(NULL, fmt, ap); - va_end(ap); -} - #define transaction_wrap(db, ...) \ (db_begin_transaction(db), __VA_ARGS__, db_commit_transaction(db), wallet_err == NULL) diff --git a/wallet/wallet.c b/wallet/wallet.c index 4fe3740c0..3cbe424ec 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -45,6 +45,21 @@ struct channel_state_param { const enum channel_state_bucket state; }; +/* Implement db_fatal, as a wrapper around fatal. + * We use a ifndef block so that it can get be + * implemented in a test file first, if necessary */ +#ifndef DB_FATAL +#define DB_FATAL +void db_fatal(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fatal_vfmt(fmt, ap); + va_end(ap); +} +#endif /* DB_FATAL */ + static void outpointfilters_init(struct wallet *w) { struct db_stmt *stmt;