Improve diagnostics from write_all(), write_nonblock() etc.

Add toprint() function and alloca_toprint() macro.
This commit is contained in:
Andrew Bettison 2012-07-10 19:57:56 +09:30
parent 2c1c44c4fe
commit e8eab5b27e
3 changed files with 39 additions and 10 deletions

23
log.c
View File

@ -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;
}

18
net.c
View File

@ -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;

View File

@ -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);