]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
ITS#9745 add config keyword for logfile format
authorHoward Chu <hyc@openldap.org>
Mon, 15 Nov 2021 22:21:37 +0000 (22:21 +0000)
committerQuanah Gibson-Mount <quanah@openldap.org>
Tue, 16 Nov 2021 18:57:54 +0000 (18:57 +0000)
doc/man/man5/lloadd.conf.5
doc/man/man5/slapd-config.5
doc/man/man5/slapd.conf.5
servers/lloadd/config.c
servers/slapd/bconfig.c
servers/slapd/logging.c
servers/slapd/main.c
servers/slapd/proto-slap.h
servers/slapd/slap-cfglog.h

index 7a938043f5d3fcb1e1fcfc60ee640944cf69fdc1..569e35db4b03da1237f5c7ae4b7d8ac74f2ebc41 100644 (file)
@@ -176,6 +176,13 @@ effect until the server has been restarted.
 Specify a file for recording lloadd debug messages. By default these messages
 only go to stderr, are not recorded anywhere else, and are unrelated to
 messages exposed by the
+.TP
+.B logfile-format debug | syslog-utc | syslog-localtime
+Specify the prefix format for messages written to the logfile. The debug
+format is the normal format used for slapd debug messages, with a timestamp
+in hexadecimal, followed by a thread ID.  The other options are to
+use syslog(3) style prefixes, with timestamps either in UTC or in the
+local timezone. The default is debug format.
 .B loglevel
 configuration parameter. Specifying a logfile copies messages to both stderr
 and the logfile.
index 3b2efedcd05bdf8f28b777fa6b4247db53c1e96a..cd508544096bd115b140f079568ec78aa634240a 100644 (file)
@@ -571,6 +571,13 @@ messages exposed by the
 configuration parameter. Specifying a logfile copies messages to both stderr
 and the logfile.
 .TP
+.B olcLogFileFormat: debug | syslog-utc | syslog-localtime
+Specify the prefix format for messages written to the logfile. The debug
+format is the normal format used for slapd debug messages, with a timestamp
+in hexadecimal, followed by a thread ID.  The other options are to
+use syslog(3) style prefixes, with timestamps either in UTC or in the
+local timezone. The default is debug format.
+.TP
 .B olcLogFileOnly: TRUE | FALSE
 Specify that debug messages should only go to the configured logfile, and
 not to stderr.
index 1a815b06cffd55cc894235fd32a5e89685bddf61..80811c03ea8f6359acd7fb571e0e0883239d5f79 100644 (file)
@@ -625,6 +625,13 @@ messages exposed by the
 configuration parameter. Specifying a logfile copies messages to both stderr
 and the logfile.
 .TP
+.B logfile-format debug | syslog-utc | syslog-localtime
+Specify the prefix format for messages written to the logfile. The debug
+format is the normal format used for slapd debug messages, with a timestamp
+in hexadecimal, followed by a thread ID.  The other options are to
+use syslog(3) style prefixes, with timestamps either in UTC or in the
+local timezone. The default is debug format.
+.TP
 .B logfile-only on | off
 Specify that debug messages should only go to the configured logfile, and
 not to stderr.
index 299fdff5b71edf9ba466a7f0258a1481b04a86ff..fa4af751203b5cfdc9a38114467c486b4e2d37d2 100644 (file)
@@ -291,6 +291,11 @@ static ConfigTable config_back_cf_table[] = {
         &config_logging,
         NULL, NULL, NULL
     },
+    { "logfile-format", "debug|syslog-utc|syslog-localtime", 2, 2, 0,
+        ARG_MAGIC|CFG_LOGFILE_FORMAT,
+        &config_logging,
+        NULL, NULL, NULL
+    },
     { "logfile-only", "on|off", 2, 2, 0,
         ARG_ON_OFF|ARG_MAGIC|CFG_LOGFILE_ONLY,
         &config_logging,
index a01be7a188e114d70935e2158a315c62a191037c..ad08090cae715b9b1658fd3a01ea31d290428aca 100644 (file)
@@ -483,6 +483,10 @@ static ConfigTable config_back_cf_table[] = {
                &config_logging, "( OLcfgGlAt:27 NAME 'olcLogFile' "
                        "EQUALITY caseExactMatch "
                        "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
+       { "logfile-format", "debug|syslog-utc|syslog-localtime", 2, 2, 0, ARG_MAGIC|CFG_LOGFILE_FORMAT,
+               &config_logging, "( OLcfgGlAt:104 NAME 'olcLogFileFormat' "
+                       "EQUALITY caseIgnoreMatch "
+                       "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
        { "logfile-only", "on|off", 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|CFG_LOGFILE_ONLY,
                &config_logging, "( OLcfgGlAt:102 NAME 'olcLogFileOnly' "
                        "EQUALITY booleanMatch "
@@ -984,7 +988,7 @@ static ConfigOCs cf_ocs[] = {
                 "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
                 "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcIndexHash64 $ "
                 "olcIndexIntLen $ "
-                "olcListenerThreads $ olcLocalSSF $ olcLogFile $ olcLogLevel $ "
+                "olcListenerThreads $ olcLocalSSF $ olcLogFile $ olcLogFileFormat $ olcLogLevel $ "
                 "olcLogFileOnly $ olcLogFileRotate $ olcMaxFilterDepth $ "
                 "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
                 "olcPluginLogFile $ olcReadOnly $ olcReferral $ "
index 43b52d796bc35ccc3406d1aaa13af701ec71019a..392a3282f3672c27713decf2e77287e382452755 100644 (file)
@@ -40,7 +40,21 @@ static char logfile_suffix[sizeof(".xx.gz")];
 static char logfile_path[MAXPATHLEN - sizeof(logfile_suffix) -1];
 static long logfile_fslimit;
 static int logfile_age, logfile_only, logfile_max;
+static char *syslog_prefix;
+static int splen;
 
+typedef enum { LFMT_DEFAULT, LFMT_DEBUG, LFMT_SYSLOG_UTC, LFMT_SYSLOG_LOCAL } LogFormat;
+static LogFormat logfile_format;
+
+static slap_verbmasks logformat_key[] = {
+       { BER_BVC("default"),           LFMT_DEFAULT },
+       { BER_BVC("debug"),                     LFMT_DEBUG },
+       { BER_BVC("syslog-utc"),        LFMT_SYSLOG_UTC },
+       { BER_BVC("syslog-localtime"),          LFMT_SYSLOG_LOCAL },
+       { BER_BVNULL, 0 }
+};
+
+char *serverName;
 int slap_debug_orig;
 
 ldap_pvt_thread_mutex_t logfile_mutex;
@@ -51,6 +65,8 @@ static int logfile_fd = -1;
 static char logpaths[2][MAXPATHLEN];
 static int logpathlen;
 
+#define SYSLOG_STAMP   "Mmm dd hh:mm:ss"
+
 void
 slap_debug_print( const char *data )
 {
@@ -69,6 +85,7 @@ slap_debug_print( const char *data )
 #define        gettime(tv)     gettimeofday( tv, NULL )
 #endif
 
+
        gettime( &tv );
        iov[0].iov_base = prefix;
        iov[0].iov_len = sprintf( prefix, "%lx." TS " %p ",
@@ -93,6 +110,20 @@ slap_debug_print( const char *data )
                                logfile_open( logfile_path );
                        }
                }
+
+               if ( logfile_format > LFMT_DEBUG ) {
+                       struct tm tm;
+                       if ( logfile_format == LFMT_SYSLOG_UTC )
+                               ldap_pvt_gmtime( &tv.tv_sec, &tm );
+                       else
+                               ldap_pvt_localtime( &tv.tv_sec, &tm );
+                       strftime( syslog_prefix, sizeof( SYSLOG_STAMP ),
+                               "%b %d %T", &tm );
+                       syslog_prefix[ sizeof( SYSLOG_STAMP )-1 ] = ' ';
+                       iov[0].iov_base = syslog_prefix;
+                       iov[0].iov_len = splen;
+               }
+
                len = writev( logfile_fd, iov, 2 );
                if ( len > 0 )
                        logfile_fsize += len;
@@ -575,6 +606,13 @@ config_logging(ConfigArgs *c) {
                                rc = 1;
                        }
                        break;
+               case CFG_LOGFILE_FORMAT:
+                       if ( logfile_format ) {
+                               value_add_one( &c->rvalue_vals, &logformat_key[logfile_format].word );
+                       } else {
+                               rc = 1;
+                       }
+                       break;
                case CFG_LOGFILE_ONLY:
                        c->value_int = logfile_only;
                        break;
@@ -611,6 +649,12 @@ config_logging(ConfigArgs *c) {
                        logfile_close();
                        break;
 
+               case CFG_LOGFILE_FORMAT:
+                       logfile_format = 0;
+                       ch_free( syslog_prefix );
+                       syslog_prefix = NULL;
+                       break;
+
                case CFG_LOGFILE_ONLY:
                        /* remove loglevel from debuglevel */
                        slap_debug = slap_debug_orig;
@@ -672,6 +716,26 @@ reset:
                        ch_free( c->value_string );
                        break;
 
+               case CFG_LOGFILE_FORMAT: {
+                       int len;
+                       i = verb_to_mask( c->argv[1], logformat_key );
+
+                       if ( BER_BVISNULL( &logformat_key[ i ].word ) ) {
+                               snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown format", c->argv[0] );
+                               Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
+                                       c->log, c->cr_msg, c->argv[1]);
+                               return( 1 );
+                       }
+                       if ( syslog_prefix )
+                               ch_free( syslog_prefix );
+                       len = strlen( global_host ) + 1 + strlen( serverName ) + 1 + sizeof("[123456789]:") +
+                               sizeof( SYSLOG_STAMP );
+                       syslog_prefix = ch_malloc( len );
+                       splen = sprintf( syslog_prefix, SYSLOG_STAMP " %s %s[%d]: ", global_host, serverName, getpid() );
+                       logfile_format = logformat_key[i].mask;
+                       }
+                       break;
+
                case CFG_LOGFILE_ONLY:
                        logfile_only = c->value_int;
                        goto reset;
index 2ef75245432068fec757bc05683f0d66eaeb6d34..ec61e1e9f6b2708fd904093b1f528b59d63efa58 100644 (file)
@@ -238,7 +238,6 @@ int main( int argc, char **argv )
 
        char *configfile = NULL;
        char *configdir = NULL;
-       char *serverName;
        int serverMode = SLAP_SERVER_MODE;
 
        struct sync_cookie *scp = NULL;
index 34b7ec25a22a35a3b71011575cd0cf53a34d9734..18dae88e1277ac64f82cbef318a6b026bd597ec3 100644 (file)
@@ -1246,6 +1246,7 @@ LDAP_SLAPD_F (void)
 slap_check_unknown_level LDAP_P(( char *levelstr, int level ));
 LDAP_SLAPD_V(ldap_pvt_thread_mutex_t) logfile_mutex;
 LDAP_SLAPD_V(int) slap_debug_orig;
+LDAP_SLAPD_V (char *)  serverName;
 
 /*
  * main.c
index 8fc27c740059216622c901821887c66114f2c52e..70aad77cc127d0f3fa44697dd9124181064e13d5 100644 (file)
@@ -21,7 +21,8 @@ enum {
        CFG_LOGLEVEL = 1,
        CFG_LOGFILE,
        CFG_LOGFILE_ROTATE,
-       CFG_LOGFILE_ONLY
+       CFG_LOGFILE_ONLY,
+       CFG_LOGFILE_FORMAT
 };
 
 extern ConfigDriver config_logging;