From 5bd8063ddb59c479d493db6156559e166cb9c490 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 4 Jan 2017 14:06:15 +1030 Subject: [PATCH] type_to_string: make type printing dynamic. The union still contains all the types, but we can only print the ones which are linked in. This makes it much easier to use type_to_string in different binaries without pulling in the world. Signed-off-by: Rusty Russell --- type_to_string.c | 25 ++++++++++++++++++++++--- type_to_string.h | 30 +++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/type_to_string.c b/type_to_string.c index 076590c29..35c2ea4d5 100644 --- a/type_to_string.c +++ b/type_to_string.c @@ -11,6 +11,8 @@ #include #include +REGISTER_TYPE_TO_STRING(netaddr, netaddr_name); + char *type_to_string_(const tal_t *ctx, const char *typename, union printable_types u) { @@ -78,9 +80,26 @@ char *type_to_string_(const tal_t *ctx, const char *typename, &u.cstate->side[LOCAL]), type_to_string(ctx, struct channel_oneside, &u.cstate->side[REMOTE])); - } else if (streq(typename, "struct netaddr")) { - s = netaddr_name(ctx, u.netaddr); - } + } else { + size_t i; + static size_t num_p; + static struct type_to_string **t = NULL; + + if (!t) + t = autodata_get(type_to_string, &num_p); + /* Typenames in registrations don't include "struct " */ + if (strstarts(typename, "struct ")) + typename += strlen("struct "); + + for (i = 0; i < num_p; i++) { + if (streq(t[i]->typename, typename)) { + s = t[i]->fmt(ctx, u); + break; + } + } + if (!s) + s = tal_fmt(ctx, "UNKNOWN TYPE %s", typename); + } return s; } diff --git a/type_to_string.h b/type_to_string.h index dbed93515..0b1c718f3 100644 --- a/type_to_string.h +++ b/type_to_string.h @@ -1,7 +1,8 @@ #ifndef LIGHTNING_TYPE_TO_STRING_H #define LIGHTNING_TYPE_TO_STRING_H #include "config.h" -#include +#include "utils.h" +#include /* This must match the type_to_string_ cases. */ union printable_types { @@ -27,4 +28,31 @@ union printable_types { char *type_to_string_(const tal_t *ctx, const char *typename, union printable_types u); +#define REGISTER_TYPE_TO_STRING(typename, fmtfn) \ + static char *fmt_##typename##_(const tal_t *ctx, \ + union printable_types u) \ + { \ + return fmtfn(ctx, u.typename); \ + } \ + static struct type_to_string ttos_##typename = { \ + #typename, fmt_##typename##_ \ + }; \ + AUTODATA(type_to_string, &ttos_##typename) + +#define REGISTER_TYPE_TO_HEXSTR(typename) \ + static char *fmt_##typename##_(const tal_t *ctx, \ + union printable_types u) \ + { \ + return tal_hexstr(ctx, u.typename, sizeof(*u.typename)); \ + } \ + static struct type_to_string ttos_##typename = { \ + #typename, fmt_##typename##_ \ + }; \ + AUTODATA(type_to_string, &ttos_##typename) + +struct type_to_string { + const char *typename; + char *(*fmt)(const tal_t *ctx, union printable_types u); +}; +AUTODATA_TYPE(type_to_string, struct type_to_string); #endif /* LIGHTNING_UTILS_H */