@ -52,6 +52,7 @@ static const char *level_prefix(enum log_level level)
static void log_to_file ( const char * prefix ,
static void log_to_file ( const char * prefix ,
enum log_level level ,
enum log_level level ,
const struct node_id * node_id ,
bool continued ,
bool continued ,
const struct timeabs * time ,
const struct timeabs * time ,
const char * str ,
const char * str ,
@ -67,11 +68,24 @@ static void log_to_file(const char *prefix,
if ( level = = LOG_IO_IN | | level = = LOG_IO_OUT ) {
if ( level = = LOG_IO_IN | | level = = LOG_IO_OUT ) {
const char * dir = level = = LOG_IO_IN ? " [IN] " : " [OUT] " ;
const char * dir = level = = LOG_IO_IN ? " [IN] " : " [OUT] " ;
char * hex = tal_hexstr ( NULL , io , io_len ) ;
char * hex = tal_hexstr ( NULL , io , io_len ) ;
fprintf ( logf , " %s %s%s%s %s \n " ,
if ( ! node_id )
iso8601_s , prefix , str , dir , hex ) ;
fprintf ( logf , " %s %s%s%s %s \n " ,
iso8601_s , prefix , str , dir , hex ) ;
else
fprintf ( logf , " %s %s-%s%s%s %s \n " ,
iso8601_s ,
node_id_to_hexstr ( tmpctx , node_id ) ,
prefix , str , dir , hex ) ;
tal_free ( hex ) ;
tal_free ( hex ) ;
} else if ( ! continued ) {
} else if ( ! continued ) {
fprintf ( logf , " %s %s %s %s \n " , iso8601_s , level_prefix ( level ) , prefix , str ) ;
if ( ! node_id )
fprintf ( logf , " %s %s %s %s \n " ,
iso8601_s , level_prefix ( level ) , prefix , str ) ;
else
fprintf ( logf , " %s %s %s-%s %s \n " ,
iso8601_s , level_prefix ( level ) ,
node_id_to_hexstr ( tmpctx , node_id ) ,
prefix , str ) ;
} else {
} else {
fprintf ( logf , " %s %s \t %s \n " , iso8601_s , prefix , str ) ;
fprintf ( logf , " %s %s \t %s \n " , iso8601_s , prefix , str ) ;
}
}
@ -80,13 +94,14 @@ static void log_to_file(const char *prefix,
static void log_to_stdout ( const char * prefix ,
static void log_to_stdout ( const char * prefix ,
enum log_level level ,
enum log_level level ,
const struct node_id * node_id ,
bool continued ,
bool continued ,
const struct timeabs * time ,
const struct timeabs * time ,
const char * str ,
const char * str ,
const u8 * io , size_t io_len ,
const u8 * io , size_t io_len ,
void * unused UNUSED )
void * unused UNUSED )
{
{
log_to_file ( prefix , level , continued , time , str , io , io_len , stdout ) ;
log_to_file ( prefix , level , node_id , continued , time , str , io , io_len , stdout ) ;
}
}
static size_t mem_used ( const struct log_entry * e )
static size_t mem_used ( const struct log_entry * e )
@ -141,7 +156,9 @@ struct log_book *new_log_book(struct lightningd *ld, size_t max_mem,
/* With different entry points */
/* With different entry points */
struct log *
struct log *
new_log ( const tal_t * ctx , struct log_book * record , const char * fmt , . . . )
new_log ( const tal_t * ctx , struct log_book * record ,
const struct node_id * default_node_id ,
const char * fmt , . . . )
{
{
struct log * log = tal ( ctx , struct log ) ;
struct log * log = tal ( ctx , struct log ) ;
va_list ap ;
va_list ap ;
@ -152,6 +169,11 @@ new_log(const tal_t *ctx, struct log_book *record, const char *fmt, ...)
/* FIXME: Refcount this! */
/* FIXME: Refcount this! */
log - > prefix = notleak ( tal_vfmt ( log - > lr , fmt , ap ) ) ;
log - > prefix = notleak ( tal_vfmt ( log - > lr , fmt , ap ) ) ;
va_end ( ap ) ;
va_end ( ap ) ;
if ( default_node_id )
log - > default_node_id = tal_dup ( log , struct node_id ,
default_node_id ) ;
else
log - > default_node_id = NULL ;
return log ;
return log ;
}
}
@ -180,6 +202,7 @@ void set_log_prefix(struct log *log, const char *prefix)
void set_log_outfn_ ( struct log_book * lr ,
void set_log_outfn_ ( struct log_book * lr ,
void ( * print ) ( const char * prefix ,
void ( * print ) ( const char * prefix ,
enum log_level level ,
enum log_level level ,
const struct node_id * node ,
bool continued ,
bool continued ,
const struct timeabs * time ,
const struct timeabs * time ,
const char * str ,
const char * str ,
@ -224,7 +247,8 @@ static void add_entry(struct log *log, struct log_entry *l)
}
}
}
}
static struct log_entry * new_log_entry ( struct log * log , enum log_level level )
static struct log_entry * new_log_entry ( struct log * log , enum log_level level ,
const struct node_id * node_id )
{
{
struct log_entry * l = tal ( log - > lr , struct log_entry ) ;
struct log_entry * l = tal ( log - > lr , struct log_entry ) ;
@ -233,6 +257,12 @@ static struct log_entry *new_log_entry(struct log *log, enum log_level level)
l - > skipped = 0 ;
l - > skipped = 0 ;
l - > prefix = log - > prefix ;
l - > prefix = log - > prefix ;
l - > io = NULL ;
l - > io = NULL ;
if ( ! node_id )
node_id = log - > default_node_id ;
if ( node_id )
l - > node_id = tal_dup ( l , struct node_id , node_id ) ;
else
l - > node_id = NULL ;
return l ;
return l ;
}
}
@ -241,16 +271,18 @@ static void maybe_print(const struct log *log, const struct log_entry *l,
size_t offset )
size_t offset )
{
{
if ( l - > level > = log - > lr - > print_level )
if ( l - > level > = log - > lr - > print_level )
log - > lr - > print ( log - > prefix , l - > level , offset ! = 0 ,
log - > lr - > print ( log - > prefix , l - > level , l - > node_id , offset ! = 0 ,
& l - > time , l - > log + offset ,
& l - > time , l - > log + offset ,
l - > io , tal_bytelen ( l - > io ) , log - > lr - > print_arg ) ;
l - > io , tal_bytelen ( l - > io ) , log - > lr - > print_arg ) ;
}
}
void logv ( struct log * log , enum log_level level , bool call_notifier ,
void logv ( struct log * log , enum log_level level ,
const char * fmt , va_list ap )
const struct node_id * node_id ,
bool call_notifier ,
const char * fmt , va_list ap )
{
{
int save_errno = errno ;
int save_errno = errno ;
struct log_entry * l = new_log_entry ( log , level ) ;
struct log_entry * l = new_log_entry ( log , level , node_id ) ;
l - > log = tal_vfmt ( l , fmt , ap ) ;
l - > log = tal_vfmt ( l , fmt , ap ) ;
@ -272,17 +304,18 @@ void logv(struct log *log, enum log_level level, bool call_notifier,
}
}
void log_io ( struct log * log , enum log_level dir ,
void log_io ( struct log * log , enum log_level dir ,
const struct node_id * node_id ,
const char * str TAKES ,
const char * str TAKES ,
const void * data TAKES , size_t len )
const void * data TAKES , size_t len )
{
{
int save_errno = errno ;
int save_errno = errno ;
struct log_entry * l = new_log_entry ( log , dir ) ;
struct log_entry * l = new_log_entry ( log , dir , node_id ) ;
assert ( dir = = LOG_IO_IN | | dir = = LOG_IO_OUT ) ;
assert ( dir = = LOG_IO_IN | | dir = = LOG_IO_OUT ) ;
/* Print first, in case we need to truncate. */
/* Print first, in case we need to truncate. */
if ( l - > level > = log - > lr - > print_level )
if ( l - > level > = log - > lr - > print_level )
log - > lr - > print ( log - > prefix , l - > level , false ,
log - > lr - > print ( log - > prefix , l - > level , l - > node_id , false ,
& l - > time , str ,
& l - > time , str ,
data , len , log - > lr - > print_arg ) ;
data , len , log - > lr - > print_arg ) ;
@ -320,13 +353,15 @@ void logv_add(struct log *log, const char *fmt, va_list ap)
maybe_print ( log , l , oldlen ) ;
maybe_print ( log , l , oldlen ) ;
}
}
void log_ ( struct log * log , enum log_level level , bool call_notifier ,
void log_ ( struct log * log , enum log_level level ,
const char * fmt , . . . )
const struct node_id * node_id ,
bool call_notifier ,
const char * fmt , . . . )
{
{
va_list ap ;
va_list ap ;
va_start ( ap , fmt ) ;
va_start ( ap , fmt ) ;
logv ( log , level , call_notifier , fmt , ap ) ;
logv ( log , level , node_id , call_notifier , fmt , ap ) ;
va_end ( ap ) ;
va_end ( ap ) ;
}
}
@ -343,6 +378,7 @@ void log_each_line_(const struct log_book *lr,
void ( * func ) ( unsigned int skipped ,
void ( * func ) ( unsigned int skipped ,
struct timerel time ,
struct timerel time ,
enum log_level level ,
enum log_level level ,
const struct node_id * node_id ,
const char * prefix ,
const char * prefix ,
const char * log ,
const char * log ,
const u8 * io ,
const u8 * io ,
@ -353,7 +389,7 @@ void log_each_line_(const struct log_book *lr,
list_for_each ( & lr - > log , i , list ) {
list_for_each ( & lr - > log , i , list ) {
func ( i - > skipped , time_between ( i - > time , lr - > init_time ) ,
func ( i - > skipped , time_between ( i - > time , lr - > init_time ) ,
i - > level , i - > prefix , i - > log , i - > io , arg ) ;
i - > level , i - > node_id , i - > prefix , i - > log , i - > io , arg ) ;
}
}
}
}
@ -365,6 +401,7 @@ struct log_data {
static void log_one_line ( unsigned int skipped ,
static void log_one_line ( unsigned int skipped ,
struct timerel diff ,
struct timerel diff ,
enum log_level level ,
enum log_level level ,
const struct node_id * node_id ,
const char * prefix ,
const char * prefix ,
const char * log ,
const char * log ,
const u8 * io ,
const u8 * io ,
@ -561,7 +598,7 @@ void log_backtrace_print(const char *fmt, ...)
return ;
return ;
va_start ( ap , fmt ) ;
va_start ( ap , fmt ) ;
logv ( crashlog , LOG_BROKEN , false , fmt , ap ) ;
logv ( crashlog , LOG_BROKEN , NULL , false , fmt , ap ) ;
va_end ( ap ) ;
va_end ( ap ) ;
}
}
@ -634,7 +671,7 @@ void fatal(const char *fmt, ...)
exit ( 1 ) ;
exit ( 1 ) ;
va_start ( ap , fmt ) ;
va_start ( ap , fmt ) ;
logv ( crashlog , LOG_BROKEN , true , fmt , ap ) ;
logv ( crashlog , LOG_BROKEN , NULL , true , fmt , ap ) ;
va_end ( ap ) ;
va_end ( ap ) ;
abort ( ) ;
abort ( ) ;
}
}
@ -659,6 +696,7 @@ static void add_skipped(struct log_info *info)
static void log_to_json ( unsigned int skipped ,
static void log_to_json ( unsigned int skipped ,
struct timerel diff ,
struct timerel diff ,
enum log_level level ,
enum log_level level ,
const struct node_id * node_id ,
const char * prefix ,
const char * prefix ,
const char * log ,
const char * log ,
const u8 * io ,
const u8 * io ,
@ -683,6 +721,8 @@ static void log_to_json(unsigned int skipped,
: level = = LOG_IO_OUT ? " IO_OUT "
: level = = LOG_IO_OUT ? " IO_OUT "
: " UNKNOWN " ) ;
: " UNKNOWN " ) ;
json_add_time ( info - > response , " time " , diff . ts ) ;
json_add_time ( info - > response , " time " , diff . ts ) ;
if ( node_id )
json_add_node_id ( info - > response , " node_id " , node_id ) ;
json_add_string ( info - > response , " source " , prefix ) ;
json_add_string ( info - > response , " source " , prefix ) ;
json_add_string ( info - > response , " log " , log ) ;
json_add_string ( info - > response , " log " , log ) ;
if ( io )
if ( io )