From 0a39fbefcb55379de1cdf1049de303c5ca0691b3 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 12 Apr 2024 15:16:59 +0200 Subject: logging: Add a truncate keyword to log_printhex. * src/logging.c (_gpgrt_logv_printhex): Add keyword support. -- A keyword in the format string is indicated by a leading pipe symbo, and exlamation mark, an arbitrary string of keywords, and a fina pipe sumbol. The only supprted keyword righty now is "trunc". Thus gpgrt_log_printhex (buf, buflen, "|!trunc|The buffer:") Will output not more that 32 hex bytes after "The buffer:" and instead of wrapping indicate the truncation by an Unicode horizontal ellipsis (#x2026). --- src/logging.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/logging.c') diff --git a/src/logging.c b/src/logging.c index 9008ba4..9b15da3 100644 --- a/src/logging.c +++ b/src/logging.c @@ -1169,10 +1169,23 @@ _gpgrt_logv_printhex (const void *buffer, size_t length, int wrap = 0; int cnt = 0; const unsigned char *p; + int trunc = 0; /* Only print a shortened string. */ /* FIXME: This printing is not yet protected by _gpgrt_flockfile. */ if (fmt && *fmt) { + const char *s; + + if (*fmt == '|' && fmt[1] == '!' && (s=strchr (fmt+2, '|')) && s[1]) + { + /* Skip initial keywords and parse them. */ + fmt += 2; + if (strstr (fmt, "trunc")) + trunc = 1; + + fmt = s+1; + } + _gpgrt_logv_internal (GPGRT_LOGLVL_DEBUG, 0, NULL, NULL, fmt, arg_ptr); wrap = 1; } @@ -1187,6 +1200,12 @@ _gpgrt_logv_printhex (const void *buffer, size_t length, _gpgrt_log_printf ("%02x", *p); if (wrap && ++cnt == 32 && length) { + if (trunc) + { + _gpgrt_log_printf (" …"); + break; + } + cnt = 0; /* (we indicate continuations with a backslash) */ _gpgrt_log_printf (" \\\n"); -- cgit v1.2.3