Skip to content

Commit

Permalink
log: Append the current time to the crash log filename
Browse files Browse the repository at this point in the history
This should make it easier to identify the latest crash file and correlate
crashes with external monitoring tools.
  • Loading branch information
cdecker committed Aug 22, 2018
1 parent 3ee85c2 commit 734b5f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lightningd/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -569,18 +569,22 @@ static void log_dump_to_file(int fd, const struct log_book *lr)
void log_backtrace_exit(void)
{
int fd;
char logfile[sizeof("/tmp/lightning-crash.log.%u") + STR_MAX_CHARS(int)];
char timebuf[sizeof("YYYYmmddHHMMSS")];
char logfile[sizeof("/tmp/lightning-crash.log.") + sizeof(timebuf)];
struct timeabs time = time_now();

strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%S", gmtime(&time.ts.tv_sec));

if (!crashlog)
return;

/* We expect to be in config dir. */
snprintf(logfile, sizeof(logfile), "crash.log.%u", getpid());
snprintf(logfile, sizeof(logfile), "crash.log.%s", timebuf);

fd = open(logfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
if (fd < 0) {
snprintf(logfile, sizeof(logfile),
"/tmp/lightning-crash.log.%u", getpid());
"/tmp/lightning-crash.log.%s", timebuf);
fd = open(logfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
}

Expand Down
11 changes: 7 additions & 4 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,11 @@ def test_logging(node_factory):
def test_crashlog(node_factory):
l1 = node_factory.get_node(may_fail=True)

crashpath = os.path.join(l1.daemon.lightning_dir,
'crash.log.{}'.format(l1.daemon.proc.pid))
assert not os.path.exists(crashpath)
def has_crash_log(n):
files = os.listdir(n.daemon.lightning_dir)
crashfiles = [f for f in files if 'crash.log' in f]
return len(crashfiles) > 0

assert not has_crash_log(l1)
l1.daemon.proc.send_signal(signal.SIGSEGV)
wait_for(lambda: os.path.exists(crashpath))
wait_for(lambda: has_crash_log(l1))

0 comments on commit 734b5f8

Please sign in to comment.