]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib:util:debug Logging macros for JSON output
authorGary Lockyer <gary@catalyst.net.nz>
Thu, 22 Jan 2026 20:25:13 +0000 (09:25 +1300)
committerDouglas Bagnall <dbagnall@samba.org>
Wed, 28 Jan 2026 23:29:39 +0000 (23:29 +0000)
Add new debug macros for outputting JSON log lines

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15898

Signed-off-by: Gary Lockyer <gary@catalyst.net.nz>
Reviewed-by: Volker Lendecke <vl@samba.org>
lib/util/debug.c
lib/util/debug.h

index f79b8811a4b591a267555d60a3783cce705f0b0c..2a1620898fb839378596dc0bfeb55dec38693525 100644 (file)
 /*
  * 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.
 
index 2a56c7d48fe15ff34e1986318604888bb5757801..6f59c7f194e212c7128141e3f61a517d0eb67861 100644 (file)
@@ -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"))