]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
ITS#10140 Add microsecond timestamp format for local file logging
authorGreg Noe <gnoe@symas.com>
Fri, 17 Jan 2025 21:52:12 +0000 (13:52 -0800)
committerGreg Noe <gnoe@symas.com>
Wed, 30 Apr 2025 14:46:42 +0000 (07:46 -0700)
doc/man/man5/slapd-config.5
doc/man/man5/slapd.conf.5
servers/slapd/logging.c

index cafd49b9cd1bd4d939cfa01e17c8a7a85edaec0e..4e9117a4bf1e452011c0e0ff2cde0f5fa0ab4a18 100644 (file)
@@ -572,7 +572,7 @@ no effect on the command line tools. By default these messages
 only go to stderr and are not recorded anywhere else.
 Specifying a logfile copies messages to both stderr and the logfile.
 .TP
-.B olcLogFileFormat: debug | syslog-utc | syslog-localtime
+.B olcLogFileFormat: debug|syslog-utc|syslog-localtime|rfc3339-utc
 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
index 6468ced05f16cad7f145be8a510f3c541b138896..3582040493594a0cde52dee6b3aa48f12e4a6017 100644 (file)
@@ -626,7 +626,7 @@ no effect on the command line tools. By default these messages
 only go to stderr and are not recorded anywhere else.
 Specifying a logfile copies messages to both stderr and the logfile.
 .TP
-.B logfile-format debug | syslog-utc | syslog-localtime
+.B logfile-format debug|syslog-utc|syslog-localtime|rfc3339-utc
 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
index b766138ce726744c753b9148f34aa8bfe6556321..b72e9b7782554c98af0b09d01fbb3a160a975d77 100644 (file)
@@ -46,14 +46,21 @@ static char *syslog_prefix;
 static int splen;
 static int logfile_rotfail, logfile_openfail;
 
-typedef enum { LFMT_DEFAULT, LFMT_DEBUG, LFMT_SYSLOG_UTC, LFMT_SYSLOG_LOCAL } LogFormat;
+typedef enum { LFMT_DEBUG, LFMT_SYSLOG, LFMT_RFC3339 } LogFormat;
 static LogFormat logfile_format;
 
+#define LFMT_LOCALTIME 0x80
+#define LFMT_DEFAULT   LFMT_DEBUG
+#define LFMT_SYSLOG_LOCAL      (LFMT_SYSLOG|LFMT_LOCALTIME)
+#define LFMT_SYSLOG_UTC        (LFMT_SYSLOG)
+#define LFMT_RFC3339_UTC       (LFMT_RFC3339)
+
 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_BVC("rfc3339-utc"),               LFMT_RFC3339_UTC },
        { BER_BVNULL, 0 }
 };
 
@@ -69,6 +76,13 @@ static char logpaths[2][MAXPATHLEN];
 static int logpathlen;
 
 #define SYSLOG_STAMP   "Mmm dd hh:mm:ss"
+#ifdef HAVE_CLOCK_GETTIME
+#define RFC3339_FRAC   ".fffffffffZ"
+#else
+#define RFC3339_FRAC   ".ffffffZ"
+#endif
+#define RFC3339_BASE   "YYYY-mm-ddTHH:MM:SS"
+#define RFC3339_STAMP   RFC3339_BASE RFC3339_FRAC
 
 void
 slap_debug_print( const char *data )
@@ -84,11 +98,13 @@ slap_debug_print( const char *data )
 #ifdef HAVE_CLOCK_GETTIME
        struct timespec tv;
 #define        TS      "%08x"
+#define        TSf     ".%09ldZ"
 #define        Tfrac   tv.tv_nsec
 #define gettime(tv)    clock_gettime( CLOCK_REALTIME, tv )
 #else
        struct timeval tv;
 #define        TS      "%05x"
+#define        TSf     ".%06ldZ"
 #define        Tfrac   tv.tv_usec
 #define        gettime(tv)     gettimeofday( tv, NULL )
 #endif
@@ -171,7 +187,7 @@ slap_debug_print( const char *data )
 
                if ( logfile_format > LFMT_DEBUG ) {
                        struct tm tm;
-                       if ( logfile_format == LFMT_SYSLOG_UTC )
+                       if ( !( logfile_format & LFMT_LOCALTIME ) )
                                ldap_pvt_gmtime( &tv.tv_sec, &tm );
                        else
                                ldap_pvt_localtime( &tv.tv_sec, &tm );
@@ -182,9 +198,15 @@ slap_debug_print( const char *data )
 #else
                        ptr = syslog_prefix;
 #endif
-                       strftime( ptr, sizeof( SYSLOG_STAMP ),
-                               "%b %d %H:%M:%S", &tm );
-                       ptr[ sizeof( SYSLOG_STAMP )-1 ] = ' ';
+                       if ( logfile_format & LFMT_SYSLOG ) {
+                               ptr += strftime( ptr, sizeof( SYSLOG_STAMP ),
+                                       "%b %d %H:%M:%S", &tm );
+                       }       else {
+                               ptr += strftime( ptr, sizeof( RFC3339_BASE ),
+                                       "%Y-%m-%dT%H:%M:%S", &tm );
+                               ptr += snprintf( ptr, sizeof( RFC3339_FRAC ), TSf, Tfrac );
+                       }
+                       *ptr = ' ';
 #ifdef _WIN32
                        len = datalen + splen;
 #else
@@ -814,11 +836,12 @@ reset:
                        }
                        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;
+                       len = strlen( global_host ) + 1 + strlen( serverName ) + 1 + sizeof(("[123456789]:")) +
+                               (( logfile_format & LFMT_RFC3339) ? sizeof( RFC3339_STAMP ) : sizeof( SYSLOG_STAMP ));
+                       syslog_prefix = ch_malloc( len );
+                       splen = sprintf( syslog_prefix, "%s %s %s[%d]: ", ( logfile_format & LFMT_RFC3339 ) ?
+                               RFC3339_STAMP : SYSLOG_STAMP, global_host, serverName, getpid() );
                        }
                        break;