Skip to content

Commit

Permalink
status: add multiple levels of logging.
Browse files Browse the repository at this point in the history
status_trace maps to status_debug.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Feb 7, 2018
1 parent f66c830 commit 84bf60f
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 85 deletions.
16 changes: 11 additions & 5 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#include <common/status.h>
#include <stdio.h>
#define status_trace(fmt , ...) \
printf(fmt "\n" , ## __VA_ARGS__)

#include "../../common/key_derive.c"
#include "../../common/keyset.c"
#include "../../common/initial_channel.c"
Expand All @@ -17,6 +12,17 @@
#include <ccan/str/hex/hex.h>
#include <common/sphinx.h>
#include <common/type_to_string.h>
#include <stdio.h>

void status_fmt(enum log_level level, const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}

const void *trc;

Expand Down
2 changes: 1 addition & 1 deletion common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ COMMON_SRC := \
common/wire_error.c \
common/withdraw_tx.c

COMMON_HEADERS_NOGEN := $(COMMON_SRC:.c=.h) common/overflows.h common/htlc.h
COMMON_HEADERS_NOGEN := $(COMMON_SRC:.c=.h) common/overflows.h common/htlc.h common/status_levels.h
COMMON_HEADERS_GEN := common/gen_htlc_state_names.h

COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN)
Expand Down
28 changes: 24 additions & 4 deletions common/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,32 @@ static void status_send_with_hdr(u16 type, const void *p, size_t len)
}
}

void status_tracev(const char *fmt, va_list ap)
void status_io(enum side sender, const u8 *p)
{
u16 type = STATUS_LOG_MIN + LOG_IO;
u8 *msg = tal_arr(NULL, u8, 0);

towire_u16(&msg, type);
towire_bool(&msg, sender == REMOTE);
towire(&msg, p, tal_len(p));
if (too_large(tal_len(msg), type))
tal_resize(&msg, 65535);

if (status_fd >= 0) {
if (!wire_sync_write(status_fd, take(msg)))
err(1, "Writing out status %u len %zu",
type, tal_len(p));
} else {
daemon_conn_send(status_conn, take(msg));
}
}

void status_vfmt(enum log_level level, const char *fmt, va_list ap)
{
char *str;

str = tal_vfmt(NULL, fmt, ap);
status_send_with_hdr(STATUS_TRACE, str, strlen(str));
status_send_with_hdr(STATUS_LOG_MIN + level, str, strlen(str));
tal_free(str);

/* Free up any temporary children. */
Expand All @@ -90,12 +110,12 @@ void status_tracev(const char *fmt, va_list ap)
}
}

void status_trace(const char *fmt, ...)
void status_fmt(enum log_level level, const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
status_tracev(fmt, ap);
status_vfmt(level, fmt, ap);
va_end(ap);
}

Expand Down
55 changes: 22 additions & 33 deletions common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#include "config.h"
#include <ccan/compiler/compiler.h>
#include <ccan/short_types/short_types.h>
#include <common/htlc.h> /* For enum side */
#include <common/status_levels.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>

struct daemon_conn;
Expand All @@ -15,44 +18,30 @@ void status_setup_async(struct daemon_conn *master);
/* Convenient context, frees up after every status_update/failed */
extern const void *trc;

/* Special status code for tracing messages. */
#define STATUS_TRACE 0x7FFF

/* Failure codes always have high bit set. */
#define STATUS_FAIL 0x8000

/* These are always followed by an ASCII string. */
enum status_fail {
/*
* These errors shouldn't happen:
*/
/* Master daemon sent unknown/malformed command, or fd failed */
STATUS_FAIL_MASTER_IO = STATUS_FAIL,
/* Send a message (frees the message). */
void status_send_sync(const u8 *msg);
/* Send a printf-style debugging trace. */
void status_fmt(enum log_level level, const char *fmt, ...)
PRINTF_FMT(2,3);

/* Hsmd sent unknown/malformed command, or fd failed */
STATUS_FAIL_HSM_IO,
/* vprintf-style */
void status_vfmt(enum log_level level, const char *fmt, va_list ap);

/* Gossipd sent unknown/malformed command, or fd failed */
STATUS_FAIL_GOSSIP_IO,
void status_io(enum side sender, const u8 *msg);

/* Other internal error. */
STATUS_FAIL_INTERNAL_ERROR,
/* Helpers */
#define status_debug(...) \
status_fmt(LOG_DBG, __VA_ARGS__)
#define status_info(...) \
status_fmt(LOG_INFORM, __VA_ARGS__)
#define status_unusual(...) \
status_fmt(LOG_UNUSUAL, __VA_ARGS__)
#define status_broken( ...) \
status_fmt(LOG_BROKEN, __VA_ARGS__)

/*
* These errors happen when the other peer misbehaves:
*/
/* I/O failure (probably they closed the socket) */
STATUS_FAIL_PEER_IO,
/* Peer did something else wrong */
STATUS_FAIL_PEER_BAD
};
/* FIXME: Transition */
#define status_trace(...) status_debug(__VA_ARGS__)

/* Send a message (frees the message). */
void status_send_sync(const u8 *msg);
/* Send a printf-style debugging trace. */
void status_trace(const char *fmt, ...) PRINTF_FMT(1,2);
/* vprintf-style */
void status_tracev(const char *fmt, va_list ap);
/* Send a failure status code with printf-style msg, and exit. */
void status_failed(enum status_fail code, const char *fmt, ...) PRINTF_FMT(2,3) NORETURN;

Expand Down
52 changes: 52 additions & 0 deletions common/status_levels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef LIGHTNING_COMMON_STATUS_LEVELS_H
#define LIGHTNING_COMMON_STATUS_LEVELS_H
#include "config.h"

enum log_level {
/* Logging all IO. */
LOG_IO,
/* Gory details which are mainly good for debugging. */
LOG_DBG,
/* Information about what's going in. */
LOG_INFORM,
/* That's strange... */
LOG_UNUSUAL,
/* That's really bad, we're broken. */
LOG_BROKEN
};

/* Special status code for tracing messages (subtract log_level). */
#define STATUS_LOG_MIN (STATUS_LOG_MAX - LOG_BROKEN)
#define STATUS_LOG_MAX (0x7FFF)

/* Failure codes always have high bit set. */
#define STATUS_FAIL 0x8000

/* These are always followed by an ASCII string. */
enum status_fail {
/*
* These errors shouldn't happen:
*/
/* Master daemon sent unknown/malformed command, or fd failed */
STATUS_FAIL_MASTER_IO = STATUS_FAIL,

/* Hsmd sent unknown/malformed command, or fd failed */
STATUS_FAIL_HSM_IO,

/* Gossipd sent unknown/malformed command, or fd failed */
STATUS_FAIL_GOSSIP_IO,

/* Other internal error. */
STATUS_FAIL_INTERNAL_ERROR,

/*
* These errors happen when the other peer misbehaves:
*/
/* I/O failure (probably they closed the socket) */
STATUS_FAIL_PEER_IO,

/* Peer did something else wrong */
STATUS_FAIL_PEER_BAD
};

#endif /* LIGHTNING_COMMON_STATUS_LEVELS_H */
13 changes: 10 additions & 3 deletions gossipd/test/run-bench-find_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
#include <common/pseudorand.h>
#include <common/status.h>
#include <common/type_to_string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <stdio.h>
#define status_trace(fmt, ...) \
do { printf((fmt) ,##__VA_ARGS__); printf("\n"); } while(0)
void status_fmt(enum log_level level, const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}

static bool in_bench = 0;

Expand Down
2 changes: 1 addition & 1 deletion gossipd/test/run-find_route-specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <common/status.h>

#include <stdio.h>
#define status_trace(fmt, ...) \
#define status_fmt(level, fmt, ...) \
do { printf((fmt) ,##__VA_ARGS__); printf("\n"); } while(0)

#include "../routing.c"
Expand Down
17 changes: 11 additions & 6 deletions gossipd/test/run-find_route.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#include <common/status.h>

#include <stdio.h>
#define status_trace(fmt, ...) \
do { printf((fmt) ,##__VA_ARGS__); printf("\n"); } while(0)

#include "../routing.c"
#include <stdio.h>

struct broadcast_state *new_broadcast_state(tal_t *ctx UNNEEDED)
{
return NULL;
}

void status_fmt(enum log_level level, const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}

/* AUTOGENERATED MOCKS START */
/* Generated stub for fromwire_channel_announcement */
bool fromwire_channel_announcement(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, size_t *plen UNNEEDED, secp256k1_ecdsa_signature *node_signature_1 UNNEEDED, secp256k1_ecdsa_signature *node_signature_2 UNNEEDED, secp256k1_ecdsa_signature *bitcoin_signature_1 UNNEEDED, secp256k1_ecdsa_signature *bitcoin_signature_2 UNNEEDED, u8 **features UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, struct pubkey *node_id_1 UNNEEDED, struct pubkey *node_id_2 UNNEEDED, struct pubkey *bitcoin_key_1 UNNEEDED, struct pubkey *bitcoin_key_2 UNNEEDED)
Expand Down
2 changes: 1 addition & 1 deletion gossipd/test/run-initiator-success.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static struct io_plan *test_read(struct io_conn *conn,
struct handshake *h);

#define SUPERVERBOSE status_trace
void status_trace(const char *fmt, ...)
void status_fmt(enum log_level level, const char *fmt, ...)
{
va_list ap;

Expand Down
4 changes: 2 additions & 2 deletions gossipd/test/run-responder-success.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static struct io_plan *test_read(struct io_conn *conn,
struct handshake *),
struct handshake *h);

#define SUPERVERBOSE status_trace
void status_trace(const char *fmt, ...)
#define SUPERVERBOSE status_debug
void status_fmt(enum log_level level, const char *fmt, ...)
{
va_list ap;

Expand Down
1 change: 1 addition & 0 deletions lightningd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ LIGHTNINGD_SRC := \
lightningd/jsonrpc.c \
lightningd/lightningd.c \
lightningd/log.c \
lightningd/log_status.c \
lightningd/netaddress.c \
lightningd/opt_time.c \
lightningd/options.c \
Expand Down
11 changes: 5 additions & 6 deletions lightningd/hsm_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <inttypes.h>
#include <lightningd/hsm_control.h>
#include <lightningd/log.h>
#include <lightningd/log_status.h>
#include <string.h>
#include <wally_bip32.h>
#include <wire/wire_sync.h>
Expand All @@ -19,15 +20,13 @@ u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld)
{
for (;;) {
u8 *msg = wire_sync_read(ctx, ld->hsm_fd);

if (!msg)
fatal("Could not read from HSM: %s", strerror(errno));
if (fromwire_peektype(msg) != STATUS_TRACE)
if (log_status_msg(ld->log, msg))
tal_free(msg);
else
return msg;

log_debug(ld->log, "HSM TRACE: %.*s",
(int)(tal_len(msg) - sizeof(be16)),
(char *)msg + sizeof(be16));
tal_free(msg);
}
}

Expand Down
14 changes: 1 addition & 13 deletions lightningd/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,14 @@
#include <ccan/tal/tal.h>
#include <ccan/time/time.h>
#include <ccan/typesafe_cb/typesafe_cb.h>
#include <common/status.h>
#include <common/type_to_string.h>
#include <stdarg.h>

struct lightningd;
struct timerel;
struct lightningd;

enum log_level {
/* Logging all IO. */
LOG_IO,
/* Gory details which are mainly good for debugging. */
LOG_DBG,
/* Information about what's going in. */
LOG_INFORM,
/* That's strange... */
LOG_UNUSUAL,
/* That's really bad, we're broken. */
LOG_BROKEN
};

/* We can have a single log book, with multiple logs in it. */
struct log_book *new_log_book(const tal_t *ctx,
size_t max_mem,
Expand Down
28 changes: 28 additions & 0 deletions lightningd/log_status.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <lightningd/log_status.h>
#include <wire/wire.h>

bool log_status_msg(struct log *log, const u8 *msg)
{
size_t max = tal_len(msg);
int type = fromwire_u16(&msg, &max);
enum log_level level;

if (type < STATUS_LOG_MIN || type > STATUS_LOG_MAX)
return false;

level = type - STATUS_LOG_MIN;
if (level == LOG_IO) {
/* First byte is direction */
bool dir = fromwire_bool(&msg, &max);
log_io(log, dir, msg, max);
} else {
int i;
/* Truncate if unprintable */
for (i = 0; i < max; i++) {
if (!cisprint((char)msg[i]))
break;
}
log_(log, level, "%.*s%s", i, msg, i == max ? "" : "...");
}
return true;
}
10 changes: 10 additions & 0 deletions lightningd/log_status.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef LIGHTNING_LIGHTNINGD_LOG_STATUS_H
#define LIGHTNING_LIGHTNINGD_LOG_STATUS_H
#include "config.h"
#include <common/status_levels.h>
#include <lightningd/log.h>

/* Returns true (and writes it to log) if it's a status_log message. */
bool log_status_msg(struct log *log, const u8 *msg);

#endif /* LIGHTNING_LIGHTNINGD_LOG_STATUS_H */
Loading

0 comments on commit 84bf60f

Please sign in to comment.