From: Gary Lockyer Date: Thu, 22 Jan 2026 20:25:13 +0000 (+1300) Subject: lib:util:debug Logging macros for JSON output X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3c30c4740f40f633b5a71c772374e6c1283ec13c;p=thirdparty%2Fsamba.git lib:util:debug Logging macros for JSON output Add new debug macros for outputting JSON log lines BUG: https://bugzilla.samba.org/show_bug.cgi?id=15898 Signed-off-by: Gary Lockyer Reviewed-by: Volker Lendecke --- diff --git a/lib/util/debug.c b/lib/util/debug.c index f79b8811a4b..2a1620898fb 100644 --- a/lib/util/debug.c +++ b/lib/util/debug.c @@ -46,6 +46,10 @@ /* * format_bufr[FORMAT_BUFR_SIZE - 1] should always be reserved * for a terminating null byte. + * + * Note: The json logging unit tests lib/util/tests/test_json_logging.c + * assume this value is 4096, they'll need to be updated if + * this is changed */ #define FORMAT_BUFR_SIZE 4096 @@ -1687,6 +1691,48 @@ static void format_debug_text( const char *msg ) format_bufr[format_pos] = '\0'; } +/*************************************************************************** + Output a single line of JSON to the logs + + Input: msg - text to be output + + Output: none. + + Notes: - msg is output without any added leading white space + - Any embedded "\n" characters are replaced with spaces + - A terminating "\n" is output. +**************************************************************************/ + +bool dbgjson( const char *msg ) +{ + size_t i; + const char eol[] = "\n"; + + debug_init(); + + for( i = 0; msg[i]; i++ ) { + /* If the buffer is full output it */ + if (format_pos >= FORMAT_BUFR_SIZE - 1) { + bufr_print(); + } + /* replace any new lines with spaces*/ + if( '\n' == msg[i] ) { + format_bufr[format_pos++] = ' '; + } else { + format_bufr[format_pos++] = msg[i]; + } + + } + if (format_pos > 0) { + bufr_print(); + } + (void)Debug1(eol , sizeof(eol) - 1); + + /* Just to be safe... */ + format_bufr[format_pos] = '\0'; + return true; +} + /*************************************************************************** Flush debug output, including the format buffer content. diff --git a/lib/util/debug.h b/lib/util/debug.h index 2a56c7d48fe..6f59c7f194e 100644 --- a/lib/util/debug.h +++ b/lib/util/debug.h @@ -237,6 +237,19 @@ void debuglevel_set_class(size_t idx, int level); && (dbgsetclass(level, dbgc_class)) \ && (dbgtext body) ) +bool dbgjson( const char *msg ); +#define DEBUGJSON( level, line) \ + (void) ( ((level) <= MAX_DEBUG_LEVEL) && \ + unlikely(debuglevel_get_class(DBGC_CLASS) >= (level)) \ + && (dbgsetclass(level, DBGC_CLASS)) \ + && (dbgjson line) ) + +#define DEBUGJSONC( dbgc_class, level, line) \ + (void)( ((level) <= MAX_DEBUG_LEVEL) && \ + unlikely((debuglevel_get_class(dbgc_class) >= (level))) \ + && (dbgsetclass(level, dbgc_class)) \ + && (dbgjson line) ) + /* Print a separator to the debug log. */ #define DEBUGSEP(level)\ DEBUG((level),("===============================================================\n"))