mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-19 05:07:56 +00:00
Improve alloca_toprint() functions
Replace "..." quoting with `...` quoting in output, to avoid slosh-escaping the common double-quote character (") in log output. Introduce alloca_str_toprint() function that produces an entire null-terminated string in printable form. Change various toprint strbuf helper functions to take two optional quote chars instead of one mandatory.
This commit is contained in:
parent
d8f06d0582
commit
62f8d223ea
31
log.c
31
log.c
@ -225,7 +225,7 @@ void logArgv(int level, struct __sourceloc where, const char *label, int argc, c
|
||||
if (i)
|
||||
strbuf_putc(&logbuf, ' ');
|
||||
if (argv[i])
|
||||
strbuf_toprint_quoted(&logbuf, '"', argv[i]);
|
||||
strbuf_toprint_quoted(&logbuf, "\"\"", argv[i]);
|
||||
else
|
||||
strbuf_puts(&logbuf, "NULL");
|
||||
}
|
||||
@ -326,10 +326,10 @@ unsigned int debugFlagMask(const char *flagname) {
|
||||
in log messages.
|
||||
@author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcBytes)
|
||||
char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcBytes, const char quotes[2])
|
||||
{
|
||||
strbuf b = strbuf_local(dstStr, dstBufSiz);
|
||||
strbuf_toprint_quoted_len(b, '"', srcBuf, srcBytes);
|
||||
strbuf_toprint_quoted_len(b, quotes, srcBuf, srcBytes);
|
||||
return dstStr;
|
||||
}
|
||||
|
||||
@ -338,9 +338,30 @@ char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcByt
|
||||
otherwise returns dstStrLen.
|
||||
@author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
size_t toprint_strlen(const char *srcBuf, size_t srcBytes)
|
||||
size_t toprint_len(const char *srcBuf, size_t srcBytes, const char quotes[2])
|
||||
{
|
||||
return strbuf_count(strbuf_toprint_quoted_len(strbuf_local(NULL, 0), '"', srcBuf, srcBytes));
|
||||
return strbuf_count(strbuf_toprint_quoted_len(strbuf_local(NULL, 0), quotes, srcBuf, srcBytes));
|
||||
}
|
||||
|
||||
/* Format a null-terminated string as a printable representation, eg: "Abc\x0b\n", for display
|
||||
in log messages.
|
||||
@author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
char *toprint_str(char *dstStr, ssize_t dstBufSiz, const char *srcStr, const char quotes[2])
|
||||
{
|
||||
strbuf b = strbuf_local(dstStr, dstBufSiz);
|
||||
strbuf_toprint_quoted(b, quotes, srcStr);
|
||||
return dstStr;
|
||||
}
|
||||
|
||||
/* Compute the length of the string produced by toprint_str(). If dstStrLen == -1 then returns the
|
||||
exact number of characters in the printable representation (excluding the terminating nul),
|
||||
otherwise returns dstStrLen.
|
||||
@author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
size_t toprint_str_len(const char *srcStr, const char quotes[2])
|
||||
{
|
||||
return strbuf_count(strbuf_toprint_quoted(strbuf_local(NULL, 0), quotes, srcStr));
|
||||
}
|
||||
|
||||
/* Read the symbolic link into the supplied buffer and add a terminating nul. Return -1 if the
|
||||
|
9
log.h
9
log.h
@ -78,13 +78,16 @@ void logMessage(int level, struct __sourceloc where, const char *fmt, ...);
|
||||
void vlogMessage(int level, struct __sourceloc where, const char *fmt, va_list);
|
||||
unsigned int debugFlagMask(const char *flagname);
|
||||
int logDump(int level, struct __sourceloc where, char *name, unsigned char *addr, size_t len);
|
||||
char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcBytes);
|
||||
size_t toprint_strlen(const char *srcBuf, size_t srcBytes);
|
||||
char *toprint(char *dstStr, ssize_t dstBufSiz, const char *srcBuf, size_t srcBytes, const char quotes[2]);
|
||||
char *toprint_str(char *dstStr, ssize_t dstBufSiz, const char *srcStr, const char quotes[2]);
|
||||
size_t toprint_len(const char *srcBuf, size_t srcBytes, const char quotes[2]);
|
||||
size_t toprint_str_len(const char *srcStr, const char quotes[2]);
|
||||
ssize_t get_self_executable_path(char *buf, size_t len);
|
||||
int log_backtrace(struct __sourceloc where);
|
||||
void set_log_implementation(void (*log_function)(int level, struct strbuf *buf));
|
||||
|
||||
#define alloca_toprint(dstlen,buf,len) toprint((char *)alloca((dstlen) == -1 ? toprint_strlen((buf),(len)) + 1 : (dstlen)), (dstlen), (buf), (len))
|
||||
#define alloca_toprint(dstlen,buf,len) toprint((char *)alloca((dstlen) == -1 ? toprint_len((buf),(len), "``") + 1 : (dstlen)), (dstlen), (buf), (len), "``")
|
||||
#define alloca_str_toprint(str) toprint_str((char *)alloca(toprint_str_len(str, "``") + 1), -1, (str), "``")
|
||||
|
||||
#define __HERE__ ((struct __sourceloc){ .file = __FILE__, .line = __LINE__, .function = __FUNCTION__ })
|
||||
#define __NOWHERE__ ((struct __sourceloc){ .file = NULL, .line = 0, .function = NULL })
|
||||
|
@ -54,8 +54,9 @@ static strbuf inline _overrun(strbuf sb, const char *suffix)
|
||||
static strbuf inline _overrun_quote(strbuf sb, char quote, const char *suffix)
|
||||
{
|
||||
if (strbuf_overrun(sb)) {
|
||||
strbuf_trunc(sb, -strlen(suffix) - 1);
|
||||
strbuf_putc(sb, quote);
|
||||
strbuf_trunc(sb, -strlen(suffix) - (quote ? 1 : 0));
|
||||
if (quote)
|
||||
strbuf_putc(sb, quote);
|
||||
strbuf_puts(sb, suffix);
|
||||
}
|
||||
return sb;
|
||||
@ -75,30 +76,34 @@ strbuf strbuf_toprint(strbuf sb, const char *str)
|
||||
return _overrun(sb, "...");
|
||||
}
|
||||
|
||||
strbuf strbuf_toprint_quoted_len(strbuf sb, char quote, const char *buf, size_t len)
|
||||
strbuf strbuf_toprint_quoted_len(strbuf sb, const char quotes[2], const char *buf, size_t len)
|
||||
{
|
||||
strbuf_putc(sb, quote);
|
||||
if (quotes && quotes[0])
|
||||
strbuf_putc(sb, quotes[0]);
|
||||
for (; len && !strbuf_overrun(sb); ++buf, --len)
|
||||
if (*buf == quote) {
|
||||
if (quotes && *buf == quotes[1]) {
|
||||
strbuf_putc(sb, '\\');
|
||||
strbuf_putc(sb, quote);
|
||||
strbuf_putc(sb, *buf);
|
||||
} else
|
||||
_toprint(sb, *buf);
|
||||
strbuf_putc(sb, quote);
|
||||
return _overrun_quote(sb, quote, "...");
|
||||
if (quotes && quotes[1])
|
||||
strbuf_putc(sb, quotes[1]);
|
||||
return _overrun_quote(sb, quotes ? quotes[1] : '\0', "...");
|
||||
}
|
||||
|
||||
strbuf strbuf_toprint_quoted(strbuf sb, char quote, const char *str)
|
||||
strbuf strbuf_toprint_quoted(strbuf sb, const char quotes[2], const char *str)
|
||||
{
|
||||
strbuf_putc(sb, quote);
|
||||
if (quotes && quotes[0])
|
||||
strbuf_putc(sb, quotes[0]);
|
||||
for (; *str && !strbuf_overrun(sb); ++str)
|
||||
if (*str == quote) {
|
||||
if (quotes && *str == quotes[1]) {
|
||||
strbuf_putc(sb, '\\');
|
||||
strbuf_putc(sb, quote);
|
||||
strbuf_putc(sb, *str);
|
||||
} else
|
||||
_toprint(sb, *str);
|
||||
strbuf_putc(sb, quote);
|
||||
return _overrun_quote(sb, quote, "...");
|
||||
if (quotes && quotes[1])
|
||||
strbuf_putc(sb, quotes[1]);
|
||||
return _overrun_quote(sb, quotes ? quotes[1] : '\0', "...");
|
||||
}
|
||||
|
||||
strbuf strbuf_append_poll_events(strbuf sb, short events)
|
||||
|
@ -39,12 +39,12 @@ strbuf strbuf_toprint(strbuf sb, const char *str);
|
||||
* backslash within the text.
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
strbuf strbuf_toprint_quoted_len(strbuf sb, char quote, const char *buf, size_t len);
|
||||
strbuf strbuf_toprint_quoted_len(strbuf sb, const char quotes[2], const char *buf, size_t len);
|
||||
|
||||
/* Equivalent to strbuf_toprint_quoted_len(sb, str, strlen(str)).
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
strbuf strbuf_toprint_quoted(strbuf sb, char quote, const char *str);
|
||||
strbuf strbuf_toprint_quoted(strbuf sb, const char quotes[2], const char *str);
|
||||
|
||||
/* Append a symbolic representation of the poll(2) event flags.
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
|
Loading…
Reference in New Issue
Block a user