ITS#10140 Add microsecond timestamp format for local file logging

This commit is contained in:
Greg Noe 2025-01-17 13:52:12 -08:00
parent f1556ba98a
commit 9a52a3c28b
3 changed files with 34 additions and 11 deletions

View 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. only go to stderr and are not recorded anywhere else.
Specifying a logfile copies messages to both stderr and the logfile. Specifying a logfile copies messages to both stderr and the logfile.
.TP .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 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 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 in hexadecimal, followed by a thread ID. The other options are to

View 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. only go to stderr and are not recorded anywhere else.
Specifying a logfile copies messages to both stderr and the logfile. Specifying a logfile copies messages to both stderr and the logfile.
.TP .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 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 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 in hexadecimal, followed by a thread ID. The other options are to

View file

@ -46,14 +46,21 @@ static char *syslog_prefix;
static int splen; static int splen;
static int logfile_rotfail, logfile_openfail; 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; 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[] = { static slap_verbmasks logformat_key[] = {
{ BER_BVC("default"), LFMT_DEFAULT }, { BER_BVC("default"), LFMT_DEFAULT },
{ BER_BVC("debug"), LFMT_DEBUG }, { BER_BVC("debug"), LFMT_DEBUG },
{ BER_BVC("syslog-utc"), LFMT_SYSLOG_UTC }, { BER_BVC("syslog-utc"), LFMT_SYSLOG_UTC },
{ BER_BVC("syslog-localtime"), LFMT_SYSLOG_LOCAL }, { BER_BVC("syslog-localtime"), LFMT_SYSLOG_LOCAL },
{ BER_BVC("rfc3339-utc"), LFMT_RFC3339_UTC },
{ BER_BVNULL, 0 } { BER_BVNULL, 0 }
}; };
@ -69,6 +76,13 @@ static char logpaths[2][MAXPATHLEN];
static int logpathlen; static int logpathlen;
#define SYSLOG_STAMP "Mmm dd hh:mm:ss" #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 void
slap_debug_print( const char *data ) slap_debug_print( const char *data )
@ -84,11 +98,13 @@ slap_debug_print( const char *data )
#ifdef HAVE_CLOCK_GETTIME #ifdef HAVE_CLOCK_GETTIME
struct timespec tv; struct timespec tv;
#define TS "%08x" #define TS "%08x"
#define TSf ".%09ldZ"
#define Tfrac tv.tv_nsec #define Tfrac tv.tv_nsec
#define gettime(tv) clock_gettime( CLOCK_REALTIME, tv ) #define gettime(tv) clock_gettime( CLOCK_REALTIME, tv )
#else #else
struct timeval tv; struct timeval tv;
#define TS "%05x" #define TS "%05x"
#define TSf ".%06ldZ"
#define Tfrac tv.tv_usec #define Tfrac tv.tv_usec
#define gettime(tv) gettimeofday( tv, NULL ) #define gettime(tv) gettimeofday( tv, NULL )
#endif #endif
@ -171,7 +187,7 @@ slap_debug_print( const char *data )
if ( logfile_format > LFMT_DEBUG ) { if ( logfile_format > LFMT_DEBUG ) {
struct tm tm; struct tm tm;
if ( logfile_format == LFMT_SYSLOG_UTC ) if ( !( logfile_format & LFMT_LOCALTIME ) )
ldap_pvt_gmtime( &tv.tv_sec, &tm ); ldap_pvt_gmtime( &tv.tv_sec, &tm );
else else
ldap_pvt_localtime( &tv.tv_sec, &tm ); ldap_pvt_localtime( &tv.tv_sec, &tm );
@ -182,9 +198,15 @@ slap_debug_print( const char *data )
#else #else
ptr = syslog_prefix; ptr = syslog_prefix;
#endif #endif
strftime( ptr, sizeof( SYSLOG_STAMP ), if ( logfile_format & LFMT_SYSLOG ) {
"%b %d %H:%M:%S", &tm ); ptr += strftime( ptr, sizeof( SYSLOG_STAMP ),
ptr[ sizeof( SYSLOG_STAMP )-1 ] = ' '; "%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 #ifdef _WIN32
len = datalen + splen; len = datalen + splen;
#else #else
@ -814,11 +836,12 @@ reset:
} }
if ( syslog_prefix ) if ( syslog_prefix )
ch_free( 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; 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; break;