abduco

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 3aa08a2d89ef37e3355450b31888adf263e5ce35
parent 6000bc6240a62f057ac2899cab2090e615d9db85
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Fri, 21 Mar 2014 23:00:08 +0100

Clean up debug packet printing code

Diffstat:
debug.c | 45+++++++++++++++++++--------------------------
1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/debug.c b/debug.c @@ -11,39 +11,32 @@ static void debug(const char *errstr, ...) { } static void print_packet(const char *prefix, Packet *pkt) { - // XXX: look up table - char *s = "UNKNOWN"; + static const char *msgtype[] = { + [MSG_CONTENT] = "CONTENT", + [MSG_ATTACH] = "ATTACH", + [MSG_DETACH] = "DETACH", + [MSG_RESIZE] = "RESIZE", + [MSG_REDRAW] = "REDRAW", + [MSG_EXIT] = "EXIT", + }; + const char *type = "UNKNOWN"; + if (pkt->type < countof(msgtype) && msgtype[pkt->type]) + type = msgtype[pkt->type]; + + fprintf(stderr, "%s: %s ", prefix, type); switch (pkt->type) { case MSG_CONTENT: - s = "CONTENT"; - break; - case MSG_ATTACH: - s = "ATTACH"; - break; - case MSG_DETACH: - s = "DETACH"; + for (size_t i = 0; i < pkt->len && i < sizeof(pkt->u.msg); i++) + fprintf(stderr, "%c", pkt->u.msg[i]); break; case MSG_RESIZE: - s = "RESIZE"; + fprintf(stderr, "%dx%d", pkt->u.ws.ws_col, pkt->u.ws.ws_row); break; - case MSG_REDRAW: - s = "REDRAW"; + default: + fprintf(stderr, "len: %d", pkt->len); break; - case MSG_EXIT: - s = "EXIT"; - break; - } - - if (pkt->type == MSG_CONTENT) { - fprintf(stderr, "%s %s len: %d content: ", prefix, s, pkt->len); - for (size_t i = 0; i < pkt->len && i < sizeof(pkt->u.msg); i++) - fprintf(stderr, "%c", pkt->u.msg[i]); - fprintf(stderr, "\n"); - } else if (pkt->type == MSG_RESIZE) { - fprintf(stderr, "%s %s %d x %d\n", prefix, s, pkt->u.ws.ws_col, pkt->u.ws.ws_row); - } else { - fprintf(stderr, "%s %s len: %d\n", prefix, s, pkt->len); } + fprintf(stderr, "\n"); } #endif /* NDEBUG */