Commit d76b8cbb authored by Howard Chu's avatar Howard Chu
Browse files

ITS#9715 consolidate logging functionality into logging.c

parent 90fc3496
......@@ -46,6 +46,7 @@
#include "lutil.h"
#include "lutil_ldap.h"
#include "lload-config.h"
#include "../slapd/slap-cfglog.h"
#ifdef _WIN32
#define LUTIL_ATOULX lutil_atoullx
......@@ -121,7 +122,6 @@ static ConfigDriver config_restrict_oid;
static ConfigDriver config_tcp_buffer;
#endif /* LDAP_TCP_BUFFER */
static ConfigDriver config_restrict;
static ConfigDriver config_loglevel;
static ConfigDriver config_include;
static ConfigDriver config_feature;
#ifdef HAVE_TLS
......@@ -156,9 +156,6 @@ enum {
CFG_TLS_SHARE_CTX,
CFG_CONCUR,
CFG_THREADS,
CFG_LOGFILE,
CFG_LOGFILE_ONLY,
CFG_LOGFILE_ROTATE,
CFG_MIRRORMODE,
CFG_IOTHREADS,
CFG_MAXBUF_CLIENT,
......@@ -291,22 +288,22 @@ static ConfigTable config_back_cf_table[] = {
#endif /* BALANCER_MODULE */
{ "logfile", "file", 2, 2, 0,
ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
&config_generic,
&config_logging,
NULL, NULL, NULL
},
{ "logfile-only", "on|off", 2, 2, 0,
ARG_ON_OFF|ARG_MAGIC|CFG_LOGFILE_ONLY,
&config_generic,
&config_logging,
NULL, NULL, NULL
},
{ "logfile-rotate", "max> <Mbyte> <hours", 4, 4, 0,
ARG_MAGIC|CFG_LOGFILE_ROTATE,
&config_generic,
&config_logging,
NULL, NULL, NULL
},
{ "loglevel", "level", 2, 0, 0,
ARG_MAGIC,
&config_loglevel,
ARG_MAGIC|CFG_LOGLEVEL,
&config_logging,
NULL, NULL, NULL
},
{ "pidfile", "file", 2, 2, 0,
......@@ -1039,60 +1036,6 @@ config_generic( ConfigArgs *c )
}
} break;
case CFG_LOGFILE: {
int rc = logfile_open( c->value_string );
ch_free( c->value_string );
return rc;
} break;
case CFG_LOGFILE_ONLY:
slap_debug = slap_debug_orig;
if ( c->value_int ) {
slap_debug |= config_syslog;
ldap_syslog = 0;
} else {
ldap_syslog = config_syslog;
}
logfile_only = c->value_int;
break;
case CFG_LOGFILE_ROTATE: {
unsigned lf_max, lf_mbyte, lf_hour;
if ( lutil_atoux( &lf_max, c->argv[1], 0 ) != 0 ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"invalid max value \"%s\"",
c->argv[0], c->argv[1] );
goto fail;
}
if ( !lf_max || lf_max > 99 ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"invalid max value \"%s\" must be 1-99",
c->argv[0], c->argv[1] );
goto fail;
}
if ( lutil_atoux( &lf_mbyte, c->argv[2], 0 ) != 0 ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"invalid Mbyte value \"%s\"",
c->argv[0], c->argv[2] );
goto fail;
}
if ( lutil_atoux( &lf_hour, c->argv[3], 0 ) != 0 ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"invalid hours value \"%s\"",
c->argv[0], c->argv[3] );
goto fail;
}
if ( !lf_mbyte && !lf_hour ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"Mbyte and hours cannot both be zero",
c->argv[0] );
goto fail;
}
logfile_max = lf_max;
logfile_fslimit = lf_mbyte * 1048576; /* Megabytes to bytes */
logfile_age = lf_hour * 3600; /* hours to seconds */
} break;
case CFG_RESCOUNT:
lload_conn_max_pdus_per_cycle = c->value_uint;
break;
......@@ -2033,146 +1976,6 @@ config_restrict( ConfigArgs *c )
return 0;
}
static slap_verbmasks *loglevel_ops;
static int
loglevel_init( void )
{
slap_verbmasks lo[] = {
{ BER_BVC("Any"), (slap_mask_t)LDAP_DEBUG_ANY },
{ BER_BVC("Trace"), LDAP_DEBUG_TRACE },
{ BER_BVC("Packets"), LDAP_DEBUG_PACKETS },
{ BER_BVC("Args"), LDAP_DEBUG_ARGS },
{ BER_BVC("Conns"), LDAP_DEBUG_CONNS },
{ BER_BVC("BER"), LDAP_DEBUG_BER },
{ BER_BVC("Filter"), LDAP_DEBUG_FILTER },
{ BER_BVC("Config"), LDAP_DEBUG_CONFIG },
{ BER_BVC("ACL"), LDAP_DEBUG_ACL },
{ BER_BVC("Stats"), LDAP_DEBUG_STATS },
{ BER_BVC("Stats2"), LDAP_DEBUG_STATS2 },
{ BER_BVC("Shell"), LDAP_DEBUG_SHELL },
{ BER_BVC("Parse"), LDAP_DEBUG_PARSE },
{ BER_BVC("Sync"), LDAP_DEBUG_SYNC },
{ BER_BVC("None"), LDAP_DEBUG_NONE },
{ BER_BVNULL, 0 }
};
return slap_verbmasks_init( &loglevel_ops, lo );
}
static void
loglevel_destroy( void )
{
if ( loglevel_ops ) {
(void)slap_verbmasks_destroy( loglevel_ops );
}
loglevel_ops = NULL;
}
int
str2loglevel( const char *s, int *l )
{
int i;
if ( loglevel_ops == NULL ) {
loglevel_init();
}
i = verb_to_mask( s, loglevel_ops );
if ( BER_BVISNULL( &loglevel_ops[i].word ) ) {
return -1;
}
*l = loglevel_ops[i].mask;
return 0;
}
int
loglevel2bvarray( int l, BerVarray *bva )
{
if ( loglevel_ops == NULL ) {
loglevel_init();
}
if ( l == 0 ) {
struct berval bv = BER_BVC("0");
return value_add_one( bva, &bv );
}
return mask_to_verbs( loglevel_ops, l, bva );
}
int
loglevel_print( FILE *out )
{
int i;
if ( loglevel_ops == NULL ) {
loglevel_init();
}
fprintf( out, "Installed log subsystems:\n\n" );
for ( i = 0; !BER_BVISNULL( &loglevel_ops[i].word ); i++ ) {
unsigned mask = loglevel_ops[i].mask & 0xffffffffUL;
fprintf( out,
( mask == ( (slap_mask_t)-1 & 0xffffffffUL ) ?
"\t%-30s (-1, 0xffffffff)\n" :
"\t%-30s (%u, 0x%x)\n" ),
loglevel_ops[i].word.bv_val, mask, mask );
}
return 0;
}
static int
config_loglevel( ConfigArgs *c )
{
int i;
if ( loglevel_ops == NULL ) {
loglevel_init();
}
for ( i = 1; i < c->argc; i++ ) {
int level;
if ( isdigit( (unsigned char)c->argv[i][0] ) || c->argv[i][0] == '-' ) {
if ( lutil_atoix( &level, c->argv[i], 0 ) != 0 ) {
snprintf( c->cr_msg, sizeof(c->cr_msg),
"<%s> unable to parse level",
c->argv[0] );
Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
c->log, c->cr_msg, c->argv[i] );
return 1;
}
} else {
if ( str2loglevel( c->argv[i], &level ) ) {
snprintf( c->cr_msg, sizeof(c->cr_msg), "<%s> unknown level",
c->argv[0] );
Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
c->log, c->cr_msg, c->argv[i] );
return 1;
}
}
/* Explicitly setting a zero clears all the levels */
if ( level )
config_syslog |= level;
else
config_syslog = 0;
}
if ( slapMode & SLAP_SERVER_MODE ) {
if ( logfile_only ) {
slap_debug = slap_debug_orig | config_syslog;
ldap_syslog = 0;
} else {
ldap_syslog = config_syslog;
}
}
return 0;
}
static int
config_include( ConfigArgs *c )
{
......@@ -3784,7 +3587,7 @@ lload_config_destroy( void )
free( line );
if ( slapd_args_file ) free( slapd_args_file );
if ( slapd_pid_file ) free( slapd_pid_file );
loglevel_destroy();
slap_loglevel_destroy();
}
/* See if the given URL (in plain and parsed form) matches
......
......@@ -77,7 +77,7 @@ monitor_subsys_log_open(
{
BerVarray bva = NULL;
if ( loglevel2bvarray( ldap_syslog, &bva ) == 0 && bva != NULL ) {
if ( loglevel2bvarray( slap_syslog_get(), &bva ) == 0 && bva != NULL ) {
monitor_info_t *mi;
Entry *e;
......@@ -109,7 +109,7 @@ monitor_subsys_log_modify(
{
monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
int rc = LDAP_OTHER;
int newlevel = ldap_syslog;
int newlevel = slap_syslog_get();
Attribute *save_attrs;
Modifications *modlist = op->orm_modlist;
Modifications *ml;
......@@ -191,7 +191,7 @@ monitor_subsys_log_modify(
/*
* Do we need to protect this with a mutex?
*/
ldap_syslog = newlevel;
slap_syslog_set( newlevel );
#if 0 /* debug rather than log */
slap_debug = newlevel;
......
......@@ -38,6 +38,7 @@
#include <lutil.h>
#include "slap-config.h"
#include "slap-cfglog.h"
#define CONFIG_RDN "cn=config"
#define SCHEMA_RDN "cn=schema"
......@@ -129,7 +130,6 @@ static ConfigDriver config_disallows;
static ConfigDriver config_requires;
static ConfigDriver config_security;
static ConfigDriver config_referral;
static ConfigDriver config_loglevel;
static ConfigDriver config_updatedn;
static ConfigDriver config_updateref;
static ConfigDriver config_extra_attrs;
......@@ -169,7 +169,6 @@ enum {
CFG_ATTR,
CFG_ATOPT,
CFG_ROOTDSE,
CFG_LOGFILE,
CFG_PLUGIN,
CFG_MODLOAD,
CFG_MODPATH,
......@@ -201,8 +200,6 @@ enum {
CFG_TLS_CACERT,
CFG_TLS_CERT,
CFG_TLS_KEY,
CFG_LOGFILE_ROTATE,
CFG_LOGFILE_ONLY,
CFG_LAST
};
......@@ -483,19 +480,19 @@ static ConfigTable config_back_cf_table[] = {
"SYNTAX OMsInteger SINGLE-VALUE )", NULL,
{ .v_int = LDAP_PVT_SASL_LOCAL_SSF } },
{ "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
&config_generic, "( OLcfgGlAt:27 NAME 'olcLogFile' "
&config_logging, "( OLcfgGlAt:27 NAME 'olcLogFile' "
"EQUALITY caseExactMatch "
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
{ "logfile-only", "on|off", 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|CFG_LOGFILE_ONLY,
&config_generic, "( OLcfgGlAt:102 NAME 'olcLogFileOnly' "
&config_logging, "( OLcfgGlAt:102 NAME 'olcLogFileOnly' "
"EQUALITY booleanMatch "
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
{ "logfile-rotate", "max> <Mbyte> <hours", 4, 4, 0, ARG_MAGIC|CFG_LOGFILE_ROTATE,
&config_generic, "( OLcfgGlAt:103 NAME 'olcLogFileRotate' "
&config_logging, "( OLcfgGlAt:103 NAME 'olcLogFileRotate' "
"EQUALITY caseIgnoreMatch "
"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
{ "loglevel", "level", 2, 0, 0, ARG_MAGIC,
&config_loglevel, "( OLcfgGlAt:28 NAME 'olcLogLevel' "
{ "loglevel", "level", 2, 0, 0, ARG_MAGIC|CFG_LOGLEVEL,
&config_logging, "( OLcfgGlAt:28 NAME 'olcLogLevel' "
"EQUALITY caseIgnoreMatch "
"SYNTAX OMsDirectoryString )", NULL, NULL },
{ "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
......@@ -1089,8 +1086,6 @@ static ADlist *sortVals;
static int new_daemon_threads;
static int config_syslog;
static int
config_resize_lthreads(ConfigArgs *c)
{
......@@ -1383,31 +1378,6 @@ config_generic(ConfigArgs *c) {
rc = 1;
}
break;
case CFG_LOGFILE: {
const char *logfileName = logfile_name();
if ( logfileName && *logfileName )
c->value_string = ch_strdup( logfileName );
else
rc = 1;
}
break;
case CFG_LOGFILE_ONLY:
c->value_int = logfile_only;
break;
case CFG_LOGFILE_ROTATE:
rc = 1;
if ( logfile_max ) {
char buf[64];
struct berval bv;
bv.bv_len = snprintf( buf, sizeof(buf), "%d %ld %ld", logfile_max,
(long) logfile_fslimit / 1048576, (long) logfile_age / 3600 );
if ( bv.bv_len > 0 && bv.bv_len < sizeof(buf) ) {
bv.bv_val = buf;
value_add_one( &c->rvalue_vals, &bv );
rc = 0;
}
}
break;
case CFG_LASTMOD:
c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
break;
......@@ -1638,20 +1608,6 @@ config_generic(ConfigArgs *c) {
passwd_salt = NULL;
break;
case CFG_LOGFILE:
logfile_close();
break;
case CFG_LOGFILE_ONLY:
/* remove loglevel from debuglevel */
slap_debug = slap_debug_orig;
ldap_syslog = config_syslog;
break;
case CFG_LOGFILE_ROTATE:
logfile_max = logfile_fslimit = logfile_age = 0;
break;
case CFG_SERVERID: {
ServerID *si, **sip;
......@@ -2425,66 +2381,6 @@ sortval_reject:
ldap_free_urldesc( lud );
}
break;
case CFG_LOGFILE: {
int rc = logfile_open( c->value_string );
ch_free( c->value_string );
return rc;
}
break;
case CFG_LOGFILE_ONLY:
slap_debug = slap_debug_orig;
if ( c->value_int ) {
slap_debug |= config_syslog;
ldap_syslog = 0;
} else {
ldap_syslog = config_syslog;
}
logfile_only = c->value_int;
break;
case CFG_LOGFILE_ROTATE: {
unsigned lf_max, lf_mbyte, lf_hour;
if ( lutil_atoux( &lf_max, c->argv[1], 0 ) != 0 ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"invalid max value \"%s\"", c->argv[0], c->argv[1] );
Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
c->log, c->cr_msg );
return 1;
}
if ( !lf_max || lf_max > 99 ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"invalid max value \"%s\" must be 1-99", c->argv[0], c->argv[1] );
Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
c->log, c->cr_msg );
return 1;
}
if ( lutil_atoux( &lf_mbyte, c->argv[2], 0 ) != 0 ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"invalid Mbyte value \"%s\"", c->argv[0], c->argv[2] );
Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
c->log, c->cr_msg );
return 1;
}
if ( lutil_atoux( &lf_hour, c->argv[3], 0 ) != 0 ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"invalid hours value \"%s\"", c->argv[0], c->argv[3] );
Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
c->log, c->cr_msg );
return 1;
}
if ( !lf_mbyte && !lf_hour ) {
snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> "
"Mbyte and hours cannot both be zero", c->argv[0] );
Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
c->log, c->cr_msg );
return 1;
}
logfile_max = lf_max;
logfile_fslimit = lf_mbyte * 1048576; /* Megabytes to bytes */
logfile_age = lf_hour * 3600; /* hours to seconds */
}
break;
case CFG_LASTMOD:
if(SLAP_NOLASTMODCMD(c->be)) {
......@@ -3821,244 +3717,6 @@ config_extra_attrs(ConfigArgs *c)
return 0;
}
static slap_verbmasks *loglevel_ops;
static int
loglevel_init( void )
{
slap_verbmasks lo[] = {
{ BER_BVC("Any"), (slap_mask_t) LDAP_DEBUG_ANY },
{ BER_BVC("Trace"), LDAP_DEBUG_TRACE },
{ BER_BVC("Packets"), LDAP_DEBUG_PACKETS },
{ BER_BVC("Args"), LDAP_DEBUG_ARGS },
{ BER_BVC("Conns"), LDAP_DEBUG_CONNS },
{ BER_BVC("BER"), LDAP_DEBUG_BER },
{ BER_BVC("Filter"), LDAP_DEBUG_FILTER },
{ BER_BVC("Config"), LDAP_DEBUG_CONFIG },
{ BER_BVC("ACL"), LDAP_DEBUG_ACL },
{ BER_BVC("Stats"), LDAP_DEBUG_STATS },
{ BER_BVC("Stats2"), LDAP_DEBUG_STATS2 },
{ BER_BVC("Shell"), LDAP_DEBUG_SHELL },
{ BER_BVC("Parse"), LDAP_DEBUG_PARSE },
#if 0 /* no longer used (nor supported) */
{ BER_BVC("Cache"), LDAP_DEBUG_CACHE },
{ BER_BVC("Index"), LDAP_DEBUG_INDEX },
#endif
{ BER_BVC("Sync"), LDAP_DEBUG_SYNC },
{ BER_BVC("None"), LDAP_DEBUG_NONE },
{ BER_BVNULL, 0 }
};
return slap_verbmasks_init( &loglevel_ops, lo );
}
static void
loglevel_destroy( void )
{
if ( loglevel_ops ) {
(void)slap_verbmasks_destroy( loglevel_ops );
}
loglevel_ops = NULL;
}
static slap_mask_t loglevel_ignore[] = { -1, 0 };
int
slap_loglevel_register( slap_mask_t m, struct berval *s )
{
int rc;
if ( loglevel_ops == NULL ) {
loglevel_init();
}
rc = slap_verbmasks_append( &loglevel_ops, m, s, loglevel_ignore );
if ( rc != 0 ) {
Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
m, s->bv_val );
}
return rc;
}
int
slap_loglevel_get( struct berval *s, int *l )
{
int rc;
slap_mask_t m, i;
if ( loglevel_ops == NULL ) {
loglevel_init();
}
for ( m = 0, i = 1; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
m |= loglevel_ops[ i ].mask;
}
for ( i = 1; m & i; i <<= 1 )
;
if ( i == 0 ) {
return -1;
}
rc = slap_verbmasks_append( &loglevel_ops, i, s, loglevel_ignore );
if ( rc != 0 ) {
Debug( LDAP_DEBUG_ANY, "slap_loglevel_get(%lu, \"%s\") failed\n",
i, s->bv_val );
} else {
*l = i;
slap_check_unknown_level( s->bv_val, i );
}
return rc;
}
int
str2loglevel( const char *s, int *l )
{
int i;
if ( loglevel_ops == NULL ) {
loglevel_init();
}
i = verb_to_mask( s, loglevel_ops );
if ( BER_BVISNULL( &loglevel_ops[ i ].word ) ) {
return -1;
}
*l = loglevel_ops[ i ].mask;
return 0;
}
const char *
loglevel2str( int l )
{
struct berval bv = BER_BVNULL;
loglevel2bv( l, &bv );
return bv.bv_val;
}
int
loglevel2bv( int l, struct berval *bv )
{
if ( loglevel_ops == NULL ) {
loglevel_init();
}