mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-19 05:07:56 +00:00
Improve toprint() and alloca_toprint()
This commit is contained in:
parent
9eeaeb9dab
commit
a15a9146c3
26
log.c
26
log.c
@ -214,26 +214,32 @@ unsigned int debugFlagMask(const char *flagname) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *toprint(char *dstPrint, const unsigned char *srcStr, size_t dstBytes)
|
||||
/* Format a buffer of data as a printable representation, eg: "Abc\x0b\n\0", for display
|
||||
in log messages.
|
||||
@author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
char *toprint(char *dstStr, size_t dstChars, const unsigned char *srcBuf, size_t srcBytes)
|
||||
{
|
||||
strbuf b = strbuf_local(dstPrint, dstBytes);
|
||||
strbuf b = strbuf_local(dstStr, dstChars);
|
||||
strbuf_putc(b, '"');
|
||||
for (; *srcStr || !strbuf_overrun(b); ++srcStr) {
|
||||
if (*srcStr == '\n')
|
||||
for (; srcBytes && !strbuf_overrun(b); ++srcBuf, --srcBytes) {
|
||||
if (*srcBuf == '\0')
|
||||
strbuf_puts(b, "\\0");
|
||||
else if (*srcBuf == '\n')
|
||||
strbuf_puts(b, "\\n");
|
||||
else if (*srcStr == '\r')
|
||||
else if (*srcBuf == '\r')
|
||||
strbuf_puts(b, "\\r");
|
||||
else if (*srcStr == '\t')
|
||||
else if (*srcBuf == '\t')
|
||||
strbuf_puts(b, "\\t");
|
||||
else if (isprint(*srcStr))
|
||||
strbuf_putc(b, *srcStr);
|
||||
else if (isprint(*srcBuf))
|
||||
strbuf_putc(b, *srcBuf);
|
||||
else
|
||||
strbuf_sprintf(b, "\\x%02x", *srcStr);
|
||||
strbuf_sprintf(b, "\\x%02x", *srcBuf);
|
||||
}
|
||||
strbuf_putc(b, '"');
|
||||
if (strbuf_overrun(b)) {
|
||||
strbuf_trunc(b, -4);
|
||||
strbuf_puts(b, "\"...");
|
||||
}
|
||||
return dstPrint;
|
||||
return dstStr;
|
||||
}
|
||||
|
8
net.c
8
net.c
@ -52,12 +52,12 @@ int _write_all(int fd, const void *buf, size_t len, const char *file, unsigned i
|
||||
ssize_t written = write(fd, buf, len);
|
||||
if (written == -1) {
|
||||
logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "write_all: write(%d,%p %s,%lu)",
|
||||
fd, buf, alloca_toprint(buf, 30), (unsigned long)len);
|
||||
fd, buf, alloca_toprint(30, buf, len), (unsigned long)len);
|
||||
return -1;
|
||||
}
|
||||
if (written != len) {
|
||||
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);
|
||||
fd, buf, alloca_toprint(30, buf, len), (unsigned long)len, (long)written);
|
||||
return -1;
|
||||
}
|
||||
return written;
|
||||
@ -73,7 +73,7 @@ int _write_nonblock(int fd, const void *buf, size_t len, const char *file, unsig
|
||||
return 0;
|
||||
}
|
||||
logMessage_perror(LOG_LEVEL_ERROR, file, line, function, "write_nonblock: write(%d,%p %s,%lu)",
|
||||
fd, buf, alloca_toprint(buf, 30), (unsigned long)len);
|
||||
fd, buf, alloca_toprint(30, buf, len), (unsigned long)len);
|
||||
return -1;
|
||||
}
|
||||
return written;
|
||||
@ -84,7 +84,7 @@ int _write_all_nonblock(int fd, const void *buf, size_t len, const char *file, u
|
||||
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 %s,%lu) returned %ld",
|
||||
fd, buf, alloca_toprint(buf, 30), (unsigned long)len, (long)written);
|
||||
fd, buf, alloca_toprint(30, buf, len), (unsigned long)len, (long)written);
|
||||
return -1;
|
||||
}
|
||||
return written;
|
||||
|
9
serval.h
9
serval.h
@ -791,11 +791,12 @@ 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);
|
||||
char *toprint(char *dstStr, size_t dstChars, const unsigned char *srcBuf, size_t srcBytes);
|
||||
|
||||
#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)
|
||||
#define alloca_toprint(dstlen,buf,len) toprint((char *)alloca((dstlen) + 1), (dstlen) + 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)
|
||||
|
||||
const char *trimbuildpath(const char *s);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user