lib/util: Add "debug syslog format = always", which logs to stdout in syslog style
authorAndrew Bartlett <abartlet@samba.org>
Thu, 6 Apr 2023 00:26:11 +0000 (12:26 +1200)
committerStefan Metzmacher <metze@samba.org>
Thu, 6 Apr 2023 12:51:30 +0000 (12:51 +0000)
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
ctdb/common/logging.c
docs-xml/smbdotconf/logging/debugsyslogformat.xml
lib/param/param_table.c
lib/util/debug.c
lib/util/debug.h

index 3aa5ca996ee0272cb0d8cd54e43f84cded681a19..ad6d0c959a46ee0f64c30a51a67bf7d1eb0115c4 100644 (file)
@@ -146,7 +146,7 @@ static int file_log_setup(TALLOC_CTX *mem_ctx,
                          const char *app_name)
 {
        struct debug_settings settings = {
-               .debug_syslog_format = true,
+               .debug_syslog_format = DEBUG_SYSLOG_FORMAT_ALWAYS,
                .debug_hires_timestamp = true,
                .debug_no_stderr_redirect = true,
        };
index f943f3a53233576262c66ba2ec08b48ed1df8d5b..ee1627de534edecadd81056e9e13703c3c6c9b63 100644 (file)
@@ -1,16 +1,28 @@
 <samba:parameter name="debug syslog format"
                  context="G"
-                 type="boolean"
+                 type="enum"
+                 enumlist="enum_debug_syslog_format"
                  xmlns:samba="http://www.samba.org/samba/DTD/samba-doc">
 <description>
     <para>
-    With this option enabled, debug messages are printed in a
+    With this option enabled (<constant>yes</constant> (alias
+    <constant>in_logs</constant>) or
+    <constant>always</constant>), debug messages are printed in a
     single-line format like that traditionally produced by syslog.
     The timestamp consists of an abbreviated month, space-padded date,
     and time including seconds.  This is followed by the hostname and
     the program name, with the process-ID in square brackets.
     </para>
 
+    <para>The value <constant>always</constant> produces this log
+    format even to <constant>STDOUT</constant> or
+    <constant>STDERR</constant>
+    </para>
+
+    <para>The value <constant>no</constant> defers to other parameters
+    and typically produces traditional two-line Samba logs to log files.
+    </para>
+
     <para>
     If <smbconfoption name="debug hires timestamp"/> is also enabled
     then an RFC5424 timestamp is used instead.
index cd6e3403a4c4a86bba9f887157f1c6f19cd2a56f..512de250a2f6f5bf3c5856969ff48c9495a7fe42 100644 (file)
@@ -418,6 +418,18 @@ static const struct enum_list enum_spotlight_backend[] = {
        {-1, NULL}
 };
 
+static const struct enum_list enum_debug_syslog_format[] = {
+       {DEBUG_SYSLOG_FORMAT_NO, "No"},
+       {DEBUG_SYSLOG_FORMAT_NO, "False"},
+       {DEBUG_SYSLOG_FORMAT_NO, "0"},
+       {DEBUG_SYSLOG_FORMAT_IN_LOGS, "in_logs"},
+       {DEBUG_SYSLOG_FORMAT_IN_LOGS, "Yes"},
+       {DEBUG_SYSLOG_FORMAT_IN_LOGS, "True"},
+       {DEBUG_SYSLOG_FORMAT_IN_LOGS, "1"},
+       {DEBUG_SYSLOG_FORMAT_ALWAYS, "always"},
+       {-1, NULL}
+};
+
 /* Note: We do not initialise the defaults union - it is not allowed in ANSI C
  *
  * NOTE: Handling of duplicated (synonym) parameters:
index 43d5151fbab20fe305b7eea3fa6aced12a659119..95de5ce3595612de8275973f900aa0f7544d2a1b 100644 (file)
@@ -1557,10 +1557,25 @@ void check_log_size( void )
 static void Debug1(const char *msg, size_t msg_len)
 {
        int old_errno = errno;
+       enum debug_logtype logtype = state.logtype;
 
        debug_count++;
 
-       switch(state.logtype) {
+       if (state.settings.debug_syslog_format == DEBUG_SYSLOG_FORMAT_ALWAYS) {
+               switch(state.logtype) {
+               case DEBUG_STDOUT:
+               case DEBUG_STDERR:
+               case DEBUG_DEFAULT_STDOUT:
+               case DEBUG_DEFAULT_STDERR:
+                       /* Behave the same as logging to a file */
+                       logtype = DEBUG_FILE;
+                       break;
+               default:
+                       break;
+               }
+       }
+
+       switch(logtype) {
        case DEBUG_CALLBACK:
                debug_callback_log(msg, msg_len, current_msg_level);
                break;
@@ -1749,23 +1764,31 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 
        dbgsetclass(level, cls);
 
-       /* Don't print a header if we're logging to stdout. */
-       if ( state.logtype != DEBUG_FILE ) {
-               return( true );
+       /*
+        * Don't print a header if we're logging to stdout,
+        * unless 'debug syslog format = always'
+        */
+       if (state.logtype != DEBUG_FILE &&
+           state.settings.debug_syslog_format != DEBUG_SYSLOG_FORMAT_ALWAYS)
+       {
+               return true;
        }
 
-       /* Print the header if timestamps are turned on.  If parameters are
-        * not yet loaded, then default to timestamps on.
+       /*
+        * Print the header if timestamps (or debug syslog format) is
+        * turned on.  If parameters are not yet loaded, then default
+        * to timestamps on.
         */
        if (!(state.settings.timestamp_logs ||
              state.settings.debug_prefix_timestamp ||
-             state.settings.debug_syslog_format)) {
+             state.settings.debug_syslog_format != DEBUG_SYSLOG_FORMAT_NO))
+       {
                return true;
        }
 
        GetTimeOfDay(&tv);
 
-       if (state.settings.debug_syslog_format) {
+       if (state.settings.debug_syslog_format != DEBUG_SYSLOG_FORMAT_NO) {
                if (state.settings.debug_hires_timestamp) {
                        timeval_str_buf(&tv, true, true, &tvbuf);
                } else {
index 4bbfa05df65279d1eb31db36c8816f88cb5b2b65..4e5ce3da035f3b1346a5ddbeaa2ad9294df8876e 100644 (file)
@@ -325,12 +325,18 @@ enum debug_logtype {
        DEBUG_CALLBACK = 5
 };
 
+enum debug_syslog_format {
+       DEBUG_SYSLOG_FORMAT_NO = 0,
+       DEBUG_SYSLOG_FORMAT_IN_LOGS = 1,
+       DEBUG_SYSLOG_FORMAT_ALWAYS = 2,
+};
+
 struct debug_settings {
        size_t max_log_size;
        bool timestamp_logs;
        bool debug_prefix_timestamp;
        bool debug_hires_timestamp;
-       bool debug_syslog_format;
+       enum debug_syslog_format debug_syslog_format;
        bool debug_pid;
        bool debug_uid;
        bool debug_class;