| 372 | } |
| 373 | |
| 374 | static void log_to_files(const char *log_prefix, |
| 375 | const char *entry_prefix, |
| 376 | enum log_level level, |
| 377 | /* The node_id to log under. */ |
| 378 | const struct node_id *node_id, |
| 379 | /* Filters to apply, if non-NULL */ |
| 380 | const struct list_head *print_filters, |
| 381 | const struct timeabs *time, |
| 382 | const char *str, size_t str_len, |
| 383 | const u8 *io, size_t io_len, |
| 384 | bool print_timestamps, |
| 385 | const enum log_level *default_print_level, |
| 386 | struct log_file **log_files) |
| 387 | { |
| 388 | char tstamp[sizeof("YYYY-mm-ddTHH:MM:SS.nnnZ ")]; |
| 389 | char *entry, nodestr[hex_str_size(PUBKEY_CMPR_LEN)]; |
| 390 | /* Entries are usually small, so a stack buffer is fine; but a |
| 391 | * peer can make us log arbitrarily large strings (e.g. clnrest |
| 392 | * logging unauthenticated request parameters), which must not |
| 393 | * be allocated on the stack! */ |
| 394 | char sbuf[1024]; |
| 395 | char *buf = sbuf; |
| 396 | size_t buf_len = sizeof("%s%s%s %s-%s: %s\n") |
| 397 | + strlen(log_prefix) |
| 398 | + sizeof(tstamp) |
| 399 | + strlen(level_prefix(level)) |
| 400 | + sizeof(nodestr) |
| 401 | + strlen(entry_prefix) |
| 402 | + str_len; |
| 403 | bool filtered; |
| 404 | |
| 405 | if (buf_len > sizeof(sbuf)) |
| 406 | buf = tal_arr(tmpctx, char, buf_len); |
| 407 | |
| 408 | if (print_timestamps) { |
| 409 | char iso8601_msec_fmt[sizeof("YYYY-mm-ddTHH:MM:SS.%03dZ ")]; |
| 410 | strftime(iso8601_msec_fmt, sizeof(iso8601_msec_fmt), "%FT%T.%%03dZ ", gmtime(&time->ts.tv_sec)); |
| 411 | snprintf(tstamp, sizeof(tstamp), iso8601_msec_fmt, (int) time->ts.tv_nsec / 1000000); |
| 412 | } else |
| 413 | tstamp[0] = '\0'; |
| 414 | |
| 415 | if (node_id) |
| 416 | hex_encode(node_id->k, sizeof(node_id->k), |
| 417 | nodestr, sizeof(nodestr)); |
| 418 | else |
| 419 | nodestr[0] = '\0'; |
| 420 | if (level == LOG_IO_IN || level == LOG_IO_OUT) { |
| 421 | const char *dir = level == LOG_IO_IN ? "[IN]" : "[OUT]"; |
| 422 | char *hex = tal_hexstr(NULL, io, io_len); |
| 423 | if (!node_id) |
| 424 | entry = tal_fmt(tmpctx, "%s%s%s: %.*s%s %s\n", |
| 425 | log_prefix, tstamp, entry_prefix, (int)str_len, str, dir, hex); |
| 426 | else |
| 427 | entry = tal_fmt(tmpctx, "%s%s%s-%s: %.*s%s %s\n", |
| 428 | log_prefix, tstamp, |
| 429 | nodestr, |
| 430 | entry_prefix, (int)str_len, str, dir, hex); |
| 431 | tal_free(hex); |
no test coverage detected