From: Howard Chu Date: Mon, 15 Nov 2021 22:21:37 +0000 (+0000) Subject: ITS#9745 add config keyword for logfile format X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1799a6e5eef83e1f8e963bef0e22fc7af077d23;p=thirdparty%2Fopenldap.git ITS#9745 add config keyword for logfile format --- diff --git a/doc/man/man5/lloadd.conf.5 b/doc/man/man5/lloadd.conf.5 index 7a938043f5..569e35db4b 100644 --- a/doc/man/man5/lloadd.conf.5 +++ b/doc/man/man5/lloadd.conf.5 @@ -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. diff --git a/doc/man/man5/slapd-config.5 b/doc/man/man5/slapd-config.5 index 3b2efedcd0..cd50854409 100644 --- a/doc/man/man5/slapd-config.5 +++ b/doc/man/man5/slapd-config.5 @@ -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. diff --git a/doc/man/man5/slapd.conf.5 b/doc/man/man5/slapd.conf.5 index 1a815b06cf..80811c03ea 100644 --- a/doc/man/man5/slapd.conf.5 +++ b/doc/man/man5/slapd.conf.5 @@ -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. diff --git a/servers/lloadd/config.c b/servers/lloadd/config.c index 299fdff5b7..fa4af75120 100644 --- a/servers/lloadd/config.c +++ b/servers/lloadd/config.c @@ -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, diff --git a/servers/slapd/bconfig.c b/servers/slapd/bconfig.c index a01be7a188..ad08090cae 100644 --- a/servers/slapd/bconfig.c +++ b/servers/slapd/bconfig.c @@ -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 $ " diff --git a/servers/slapd/logging.c b/servers/slapd/logging.c index 43b52d796b..392a3282f3 100644 --- a/servers/slapd/logging.c +++ b/servers/slapd/logging.c @@ -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; diff --git a/servers/slapd/main.c b/servers/slapd/main.c index 2ef7524543..ec61e1e9f6 100644 --- a/servers/slapd/main.c +++ b/servers/slapd/main.c @@ -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; diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index 34b7ec25a2..18dae88e12 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -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 diff --git a/servers/slapd/slap-cfglog.h b/servers/slapd/slap-cfglog.h index 8fc27c7400..70aad77cc1 100644 --- a/servers/slapd/slap-cfglog.h +++ b/servers/slapd/slap-cfglog.h @@ -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;