lightningd: remember --log-file arg.
And fclose old ones if we get handed it multiple times. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
7fd5808803
commit
bd3480dc9c
|
@ -56,6 +56,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx,
|
|||
htlc_out_map_init(&ld->htlcs_out);
|
||||
ld->log_book = log_book;
|
||||
ld->log = new_log(log_book, log_book, "lightningd(%u):", (int)getpid());
|
||||
ld->logfile = NULL;
|
||||
ld->alias = NULL;
|
||||
ld->rgb = NULL;
|
||||
list_head_init(&ld->connects);
|
||||
|
|
|
@ -90,6 +90,7 @@ struct lightningd {
|
|||
/* Log for general stuff. */
|
||||
struct log_book *log_book;
|
||||
struct log *log;
|
||||
const char *logfile;
|
||||
|
||||
/* This is us. */
|
||||
struct pubkey id;
|
||||
|
|
|
@ -376,12 +376,30 @@ static char *arg_log_level(const char *arg, struct log *log)
|
|||
return tal_fmt(NULL, "unknown log level");
|
||||
}
|
||||
|
||||
static void show_log_level(char buf[OPT_SHOW_LEN], const struct log *log)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(log_levels); i++) {
|
||||
if (log->lr->print_level == log_levels[i].level) {
|
||||
strncpy(buf, log_levels[i].name, OPT_SHOW_LEN-1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
abort();
|
||||
}
|
||||
|
||||
static char *arg_log_prefix(const char *arg, struct log *log)
|
||||
{
|
||||
set_log_prefix(log, arg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void show_log_prefix(char buf[OPT_SHOW_LEN], const struct log *log)
|
||||
{
|
||||
strncpy(buf, log->prefix, OPT_SHOW_LEN);
|
||||
}
|
||||
|
||||
static void log_to_file(const char *prefix,
|
||||
enum log_level level,
|
||||
bool continued,
|
||||
|
@ -396,22 +414,30 @@ static void log_to_file(const char *prefix,
|
|||
fflush(logf);
|
||||
}
|
||||
|
||||
static char *arg_log_to_file(const char *arg, struct log *log)
|
||||
static char *arg_log_to_file(const char *arg, struct lightningd *ld)
|
||||
{
|
||||
FILE *logf = fopen(arg, "a");
|
||||
FILE *logf;
|
||||
|
||||
if (ld->logfile) {
|
||||
fclose(ld->log->lr->print_arg);
|
||||
ld->logfile = tal_free(ld->logfile);
|
||||
}
|
||||
ld->logfile = tal_strdup(ld, arg);
|
||||
logf = fopen(arg, "a");
|
||||
if (!logf)
|
||||
return tal_fmt(NULL, "Failed to open: %s", strerror(errno));
|
||||
set_log_outfn(log->lr, log_to_file, logf);
|
||||
set_log_outfn(ld->log->lr, log_to_file, logf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void opt_register_logging(struct log *log)
|
||||
void opt_register_logging(struct lightningd *ld)
|
||||
{
|
||||
opt_register_arg("--log-level", arg_log_level, NULL, log,
|
||||
opt_register_arg("--log-level", arg_log_level, show_log_level, ld->log,
|
||||
"log level (debug, info, unusual, broken)");
|
||||
opt_register_arg("--log-prefix", arg_log_prefix, NULL, log,
|
||||
opt_register_arg("--log-prefix", arg_log_prefix, show_log_prefix,
|
||||
ld->log,
|
||||
"log prefix");
|
||||
opt_register_arg("--log-file=<file>", arg_log_to_file, NULL, log,
|
||||
opt_register_arg("--log-file=<file>", arg_log_to_file, NULL, ld,
|
||||
"log to file instead of stdout");
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <common/type_to_string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
struct lightningd;
|
||||
struct timerel;
|
||||
|
||||
enum log_level {
|
||||
|
@ -85,7 +86,7 @@ void log_each_line_(const struct log_book *lr,
|
|||
|
||||
|
||||
void log_dump_to_file(int fd, const struct log_book *lr);
|
||||
void opt_register_logging(struct log *log);
|
||||
void opt_register_logging(struct lightningd *ld);
|
||||
void crashlog_activate(const char *argv0, struct log *log);
|
||||
|
||||
/* Convenience parent for temporary allocations (eg. type_to_string)
|
||||
|
|
|
@ -563,7 +563,7 @@ void register_opts(struct lightningd *ld)
|
|||
&ld->config.channel_update_interval,
|
||||
"Time in seconds between channel updates for our own channels.");
|
||||
|
||||
opt_register_logging(ld->log);
|
||||
opt_register_logging(ld);
|
||||
opt_register_version();
|
||||
|
||||
configdir_register_opts(ld, &ld->config_dir, &ld->rpc_filename);
|
||||
|
|
Loading…
Reference in New Issue