From e8eab5b27e599d23157849ab4f949e0650c6f867 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Tue, 10 Jul 2012 19:57:56 +0930 Subject: [PATCH] Improve diagnostics from write_all(), write_nonblock() etc. Add toprint() function and alloca_toprint() macro. --- log.c | 23 +++++++++++++++++++++++ net.c | 18 +++++++++++------- serval.h | 8 +++++--- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/log.c b/log.c index 53881b73..1366e8e7 100644 --- a/log.c +++ b/log.c @@ -214,3 +214,26 @@ unsigned int debugFlagMask(const char *flagname) { return 0; } +char *toprint(char *dstPrint, const unsigned char *srcStr, size_t dstBytes) +{ + strbuf b = strbuf_local(dstPrint, dstBytes); + strbuf_putc(b, '"'); + for (; *srcStr || !strbuf_overrun(b); ++srcStr) { + if (*srcStr == '\n') + strbuf_puts(b, "\\n"); + else if (*srcStr == '\r') + strbuf_puts(b, "\\r"); + else if (*srcStr == '\t') + strbuf_puts(b, "\\t"); + else if (isprint(*srcStr)) + strbuf_putc(b, *srcStr); + else + strbuf_sprintf(b, "\\x%02x", *srcStr); + } + strbuf_putc(b, '"'); + if (strbuf_overrun(b)) { + strbuf_trunc(b, -4); + strbuf_puts(b, "\"..."); + } + return dstPrint; +} diff --git a/net.c b/net.c index 4b2e4904..6c49f6a8 100644 --- a/net.c +++ b/net.c @@ -47,21 +47,23 @@ int _set_block(int fd, const char *file, unsigned int line, const char *function return 0; } -int _write_all(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function) +int _write_all(int fd, const void *buf, size_t len, const char *file, unsigned int line, const char *function) { ssize_t written = write(fd, buf, len); if (written == -1) { - logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "write_all: write(%d,%p,%lu)", fd, buf, (unsigned long)len); + logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "write_all: write(%d,%p %s,%lu)", + fd, buf, alloca_toprint(buf, 30), (unsigned long)len); return -1; } if (written != len) { - logMessage(LOG_LEVEL_ERROR, file, line, function, "write_all: write(%d,%p,%lu) returned %ld", fd, buf, (unsigned long)len, (long)written); + logMessage(LOG_LEVEL_ERROR, file, line, function, "write_all: write(%d,%p %s,%lu) returned %ld", + fd, buf, alloca_toprint(buf, 30), (unsigned long)len, (long)written); return -1; } return written; } -int _write_nonblock(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function) +int _write_nonblock(int fd, const void *buf, size_t len, const char *file, unsigned int line, const char *function) { ssize_t written = write(fd, buf, len); if (written == -1) { @@ -70,17 +72,19 @@ int _write_nonblock(int fd, const char *buf, size_t len, const char *file, unsig case EINTR: return 0; } - logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "write_nonblock: write(%d,%p,%lu)", fd, buf, (unsigned long)len); + logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "write_nonblock: write(%d,%p %s,%lu)", + fd, buf, alloca_toprint(buf, 30), (unsigned long)len); return -1; } return written; } -int _write_all_nonblock(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function) +int _write_all_nonblock(int fd, const void *buf, size_t len, const char *file, unsigned int line, const char *function) { ssize_t written = _write_nonblock(fd, buf, len, file, line, function); if (written != -1 && written != len) { - logMessage(LOG_LEVEL_ERROR, file, line, function, "write_all_nonblock: write(%d,%p,%lu) returned %ld", fd, buf, (unsigned long)len, (long)written); + logMessage(LOG_LEVEL_ERROR, file, line, function, "write_all_nonblock: write(%d,%p %s,%lu) returned %ld", + fd, buf, alloca_toprint(buf, 30), (unsigned long)len, (long)written); return -1; } return written; diff --git a/serval.h b/serval.h index 2c876708..1a0d7717 100755 --- a/serval.h +++ b/serval.h @@ -791,7 +791,9 @@ void vlogMessage(int level, const char *file, unsigned int line, const char *fun unsigned int debugFlagMask(const char *flagname); char *catv(const char *data, char *buf, size_t len); int dump(char *name, unsigned char *addr, size_t len); +char *toprint(char *dstPrint, const unsigned char *srcStr, size_t dstBytes); +#define alloca_toprint(buf,len) toprint((char *)alloca((len) + 1), (buf), (len) + 1) #define alloca_tohex(buf,len) tohex((char *)alloca((len)*2+1), (buf), (len)) #define alloca_tohex_sid(sid) alloca_tohex((sid), SID_SIZE) @@ -1572,9 +1574,9 @@ void sigIoHandler(int signal); int _set_nonblock(int fd, const char *file, unsigned int line, const char *function); int _set_block(int fd, const char *file, unsigned int line, const char *function); -int _write_all(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function); -int _write_nonblock(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function); -int _write_all_nonblock(int fd, const char *buf, size_t len, const char *file, unsigned int line, const char *function); +int _write_all(int fd, const void *buf, size_t len, const char *file, unsigned int line, const char *function); +int _write_nonblock(int fd, const void *buf, size_t len, const char *file, unsigned int line, const char *function); +int _write_all_nonblock(int fd, const void *buf, size_t len, const char *file, unsigned int line, const char *function); int _write_str(int fd, const char *str, const char *file, unsigned int line, const char *function); int _write_str_nonblock(int fd, const char *str, const char *file, unsigned int line, const char *function);