1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-08-18 03:00:59 +08:00
|
|
|
/*
|
2002-01-05 05:17:25 +08:00
|
|
|
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
|
1999-08-18 03:00:59 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1999-06-22 06:52:28 +08:00
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/stdarg.h>
|
2001-12-18 09:24:31 +08:00
|
|
|
#include <ac/stdlib.h>
|
1999-06-22 06:52:28 +08:00
|
|
|
#include <ac/string.h>
|
2000-10-13 04:01:12 +08:00
|
|
|
#include <ac/time.h>
|
2002-07-12 04:33:24 +08:00
|
|
|
#include <ac/ctype.h>
|
1999-06-22 06:52:28 +08:00
|
|
|
|
2002-05-11 01:48:54 +08:00
|
|
|
#ifdef LDAP_SYSLOG
|
|
|
|
#include <ac/syslog.h>
|
|
|
|
#endif
|
|
|
|
|
1999-06-22 06:52:28 +08:00
|
|
|
#include "ldap_log.h"
|
|
|
|
#include "ldap_defaults.h"
|
2000-05-11 17:39:33 +08:00
|
|
|
#include "lber.h"
|
2002-07-12 07:26:34 +08:00
|
|
|
#include "ldap_pvt.h"
|
1999-06-22 06:52:28 +08:00
|
|
|
|
2000-10-13 04:01:12 +08:00
|
|
|
struct DEBUGLEVEL
|
|
|
|
{
|
|
|
|
char *subsystem;
|
|
|
|
int level;
|
|
|
|
};
|
|
|
|
|
2002-07-12 04:33:24 +08:00
|
|
|
int ldap_loglevels[LDAP_SUBSYS_NUM];
|
2000-10-13 04:01:12 +08:00
|
|
|
static long numLevels = 0;
|
|
|
|
|
2000-10-18 08:29:21 +08:00
|
|
|
static FILE *log_file = NULL;
|
|
|
|
static int global_level = 0;
|
|
|
|
|
|
|
|
#ifdef LDAP_SYSLOG
|
|
|
|
static int use_syslog = 0;
|
|
|
|
|
|
|
|
static int debug2syslog(int l) {
|
|
|
|
switch (l) {
|
2002-05-11 01:48:54 +08:00
|
|
|
case LDAP_LEVEL_EMERG: return LOG_EMERG;
|
|
|
|
case LDAP_LEVEL_ALERT: return LOG_ALERT;
|
|
|
|
case LDAP_LEVEL_CRIT: return LOG_CRIT;
|
|
|
|
case LDAP_LEVEL_ERR: return LOG_ERR;
|
|
|
|
case LDAP_LEVEL_WARNING: return LOG_WARNING;
|
|
|
|
case LDAP_LEVEL_NOTICE: return LOG_NOTICE;
|
|
|
|
case LDAP_LEVEL_INFO: return LOG_INFO;
|
2000-10-18 08:29:21 +08:00
|
|
|
}
|
2002-05-11 01:48:54 +08:00
|
|
|
return LOG_DEBUG;
|
2000-10-18 08:29:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
2000-10-13 04:01:12 +08:00
|
|
|
|
2000-10-18 02:56:24 +08:00
|
|
|
static char *lutil_levels[] = {"emergency", "alert", "critical",
|
2001-01-18 01:05:43 +08:00
|
|
|
"error", "warning", "notice",
|
|
|
|
"information", "entry", "args",
|
|
|
|
"results", "detail1", "detail2",
|
|
|
|
NULL};
|
2000-10-18 02:56:24 +08:00
|
|
|
|
2002-07-12 04:33:24 +08:00
|
|
|
static char *lutil_subsys[LDAP_SUBSYS_NUM] = {"global","operation", "transport",
|
|
|
|
"connection", "filter", "ber",
|
|
|
|
"config", "acl", "cache", "index",
|
|
|
|
"ldif", "tools", "slapd", "slurpd",
|
|
|
|
"backend", "back_bdb", "back_ldbm",
|
|
|
|
"back_ldap", "back_meta", "back_mon" };
|
|
|
|
|
|
|
|
int lutil_mnem2subsys( const char *subsys )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
|
|
|
|
{
|
|
|
|
if ( !strcasecmp( subsys, lutil_subsys[i] ) )
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2002-07-12 07:26:34 +08:00
|
|
|
return -1;
|
2002-07-12 04:33:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void lutil_set_all_backends( level )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
|
|
|
|
{
|
|
|
|
if ( !strncasecmp( "back_", lutil_subsys[i], strlen("back_") ) )
|
|
|
|
{
|
|
|
|
ldap_loglevels[i] = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-23 13:00:39 +08:00
|
|
|
int lutil_mnem2level( const char *level )
|
2000-10-18 02:56:24 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i = 0; lutil_levels[i] != NULL; i++ )
|
|
|
|
{
|
2001-01-18 01:05:43 +08:00
|
|
|
if ( !strcasecmp( level, lutil_levels[i] ) )
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
2000-10-18 02:56:24 +08:00
|
|
|
}
|
2002-07-12 07:26:34 +08:00
|
|
|
return -1;
|
2000-10-18 02:56:24 +08:00
|
|
|
}
|
|
|
|
|
2002-07-12 07:26:34 +08:00
|
|
|
static int addSubsys( const char *subsys, int level )
|
2000-10-13 04:01:12 +08:00
|
|
|
{
|
2002-07-24 02:26:33 +08:00
|
|
|
int subsys_num;
|
2001-01-18 01:05:43 +08:00
|
|
|
|
2002-07-12 04:33:24 +08:00
|
|
|
if ( !strcasecmp( subsys, "backend" ) )
|
2002-07-12 07:26:34 +08:00
|
|
|
{
|
2002-07-12 04:33:24 +08:00
|
|
|
lutil_set_all_backends( level );
|
2002-07-12 07:26:34 +08:00
|
|
|
return level;
|
|
|
|
}
|
2002-07-12 04:33:24 +08:00
|
|
|
else
|
2000-10-13 04:01:12 +08:00
|
|
|
{
|
2002-07-12 04:33:24 +08:00
|
|
|
subsys_num = lutil_mnem2subsys(subsys);
|
2002-07-12 07:26:34 +08:00
|
|
|
if(subsys_num < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unknown Subsystem name [ %s ] - Discarded\n",
|
|
|
|
subsys);
|
|
|
|
fflush(stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-07-12 04:33:24 +08:00
|
|
|
ldap_loglevels[subsys_num] = level;
|
2002-07-12 07:26:34 +08:00
|
|
|
return level;
|
2000-10-13 04:01:12 +08:00
|
|
|
}
|
2002-07-12 07:26:34 +08:00
|
|
|
return -1;
|
2000-10-13 04:01:12 +08:00
|
|
|
}
|
|
|
|
|
2002-07-12 07:26:34 +08:00
|
|
|
int lutil_set_debug_level( const char* subsys, int level )
|
2000-10-13 04:01:12 +08:00
|
|
|
{
|
2002-07-12 07:26:34 +08:00
|
|
|
return( addSubsys( subsys, level ) );
|
2000-10-13 04:01:12 +08:00
|
|
|
}
|
|
|
|
|
1999-06-22 06:52:28 +08:00
|
|
|
int lutil_debug_file( FILE *file )
|
|
|
|
{
|
1999-09-02 15:40:25 +08:00
|
|
|
log_file = file;
|
2000-05-11 17:39:33 +08:00
|
|
|
ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
|
1999-06-22 06:52:28 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-10-18 10:27:45 +08:00
|
|
|
void lutil_log_int(
|
|
|
|
FILE* file,
|
2001-07-23 13:00:39 +08:00
|
|
|
const char *subsys, int level,
|
2000-10-18 10:27:45 +08:00
|
|
|
const char *fmt, va_list vl )
|
2000-10-13 04:01:12 +08:00
|
|
|
{
|
2002-01-11 14:56:24 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
2000-10-13 04:01:12 +08:00
|
|
|
time_t now;
|
|
|
|
struct tm *today;
|
2002-01-11 14:56:24 +08:00
|
|
|
#endif
|
2002-07-24 02:26:33 +08:00
|
|
|
size_t i;
|
2002-07-12 04:33:24 +08:00
|
|
|
char * t_subsys;
|
|
|
|
char * tmp;
|
2000-10-13 04:01:12 +08:00
|
|
|
|
2002-07-12 04:33:24 +08:00
|
|
|
t_subsys = strdup(subsys);
|
|
|
|
|
|
|
|
for(tmp = t_subsys, i = 0; i < strlen(t_subsys); i++, tmp++)
|
|
|
|
*tmp = TOUPPER( (unsigned char) *tmp );
|
2000-10-13 04:01:12 +08:00
|
|
|
|
2000-10-18 08:29:21 +08:00
|
|
|
#ifdef LDAP_SYSLOG
|
|
|
|
/* we're configured to use syslog */
|
|
|
|
if( use_syslog ) {
|
2000-10-18 10:27:45 +08:00
|
|
|
vsyslog( debug2syslog(level), fmt, vl );
|
2000-10-18 08:29:21 +08:00
|
|
|
return;
|
2000-10-13 04:01:12 +08:00
|
|
|
}
|
2000-10-18 08:29:21 +08:00
|
|
|
#endif
|
2000-10-13 04:01:12 +08:00
|
|
|
|
|
|
|
#if 0
|
2000-10-18 08:29:21 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
|
|
|
if( log_file == NULL ) {
|
|
|
|
log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
|
|
|
|
|
|
|
|
if ( log_file == NULL )
|
|
|
|
log_file = fopen( "openldap.log", "w" );
|
|
|
|
|
|
|
|
if ( log_file == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
|
2000-10-13 04:01:12 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2000-10-18 08:29:21 +08:00
|
|
|
|
2000-10-18 22:58:34 +08:00
|
|
|
if( file == NULL ) {
|
2000-10-18 08:29:21 +08:00
|
|
|
/*
|
|
|
|
* Use stderr unless file was specified via:
|
|
|
|
* ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
|
|
|
|
*/
|
|
|
|
file = stderr;
|
|
|
|
}
|
|
|
|
|
2000-10-18 10:27:45 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
|
|
|
/*
|
|
|
|
* Stick the time in the buffer to output when using Winsock
|
|
|
|
* as NT can't pipe to a timestamp program like Unix can.
|
|
|
|
* This, of course, makes some logs hard to read.
|
|
|
|
*/
|
|
|
|
time( &now );
|
|
|
|
today = localtime( &now );
|
|
|
|
fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
|
|
|
|
today->tm_year + 1900, today->tm_mon + 1,
|
|
|
|
today->tm_mday, today->tm_hour,
|
|
|
|
today->tm_min, today->tm_sec );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* format the output data.
|
|
|
|
*/
|
2002-07-12 04:33:24 +08:00
|
|
|
|
|
|
|
fprintf(file, "\n%s:: ", t_subsys );
|
2000-10-18 10:27:45 +08:00
|
|
|
vfprintf( file, fmt, vl );
|
2002-07-12 04:33:24 +08:00
|
|
|
fflush( file );
|
2000-10-13 04:01:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-01-18 01:05:43 +08:00
|
|
|
* The primary logging routine. Takes the subsystem being logged from, the
|
2000-10-13 04:01:12 +08:00
|
|
|
* level of the log output and the format and data. Send this on to the
|
|
|
|
* internal routine with the print file, if any.
|
|
|
|
*/
|
2002-07-12 04:33:24 +08:00
|
|
|
void lutil_log( const int subsys, int level, const char *fmt, ... )
|
2000-10-13 04:01:12 +08:00
|
|
|
{
|
|
|
|
FILE* outfile = NULL;
|
|
|
|
va_list vl;
|
|
|
|
va_start( vl, fmt );
|
|
|
|
ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
|
2002-07-12 04:33:24 +08:00
|
|
|
lutil_log_int( outfile, lutil_subsys[subsys], level, fmt, vl );
|
2000-10-13 04:01:12 +08:00
|
|
|
va_end( vl );
|
|
|
|
}
|
|
|
|
|
|
|
|
void lutil_log_initialize(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/*
|
|
|
|
* Start by setting the hook for the libraries to use this logging
|
|
|
|
* routine.
|
|
|
|
*/
|
|
|
|
ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
|
|
|
|
|
|
|
|
if ( argc == 0 ) return;
|
|
|
|
/*
|
|
|
|
* Now go through the command line options to set the debugging
|
|
|
|
* levels
|
|
|
|
*/
|
|
|
|
for( i = 0; i < argc; i++ )
|
|
|
|
{
|
2002-07-12 04:33:24 +08:00
|
|
|
char *next = argv[i];
|
|
|
|
|
|
|
|
if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
|
|
|
|
{
|
|
|
|
char subsys[64];
|
|
|
|
int level;
|
|
|
|
char *optarg = argv[i+1];
|
|
|
|
char *index = strchr( optarg, '=' );
|
|
|
|
if ( index != NULL )
|
|
|
|
{
|
|
|
|
*index = 0;
|
|
|
|
strcpy ( subsys, optarg );
|
|
|
|
level = atoi( index+1 );
|
|
|
|
if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
|
|
|
|
lutil_set_debug_level( subsys, level );
|
|
|
|
*index = '=';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
global_level = atoi( optarg );
|
|
|
|
ldap_loglevels[0] = global_level;
|
|
|
|
/*
|
|
|
|
* if a negative number was used, make the global level the
|
|
|
|
* maximum sane level.
|
|
|
|
*/
|
|
|
|
if ( global_level < 0 )
|
|
|
|
{
|
|
|
|
global_level = 65535;
|
|
|
|
ldap_loglevels[0] = 65535;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-10-13 04:01:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-15 11:48:27 +08:00
|
|
|
void (lutil_debug)( int debug, int level, const char *fmt, ... )
|
1999-06-22 06:52:28 +08:00
|
|
|
{
|
|
|
|
char buffer[4096];
|
|
|
|
va_list vl;
|
|
|
|
|
|
|
|
if ( !(level & debug ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef HAVE_WINSOCK
|
2000-09-15 11:08:05 +08:00
|
|
|
if( log_file == NULL ) {
|
1999-06-22 06:59:23 +08:00
|
|
|
log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
|
1999-06-22 06:52:28 +08:00
|
|
|
|
2000-09-15 11:08:05 +08:00
|
|
|
if ( log_file == NULL )
|
1999-06-22 06:59:23 +08:00
|
|
|
log_file = fopen( "openldap.log", "w" );
|
1999-06-22 06:52:28 +08:00
|
|
|
|
|
|
|
if ( log_file == NULL )
|
|
|
|
return;
|
2000-05-11 17:39:33 +08:00
|
|
|
|
2000-09-15 11:08:05 +08:00
|
|
|
ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
|
1999-06-22 06:52:28 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
va_start( vl, fmt );
|
|
|
|
|
1999-06-29 06:04:31 +08:00
|
|
|
#ifdef HAVE_VSNPRINTF
|
1999-06-22 06:52:28 +08:00
|
|
|
vsnprintf( buffer, sizeof(buffer), fmt, vl );
|
1999-06-29 06:04:31 +08:00
|
|
|
#else
|
|
|
|
vsprintf( buffer, fmt, vl );
|
|
|
|
#endif
|
1999-06-22 06:52:28 +08:00
|
|
|
buffer[sizeof(buffer)-1] = '\0';
|
|
|
|
|
|
|
|
if( log_file != NULL ) {
|
|
|
|
fputs( buffer, log_file );
|
|
|
|
fflush( log_file );
|
|
|
|
}
|
|
|
|
|
1999-06-29 09:02:48 +08:00
|
|
|
fputs( buffer, stderr );
|
1999-06-22 06:52:28 +08:00
|
|
|
va_end( vl );
|
|
|
|
}
|