3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdarg.h>
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
18 #include <ac/syslog.h>
22 #include "ldap_defaults.h"
32 int ldap_loglevels[LDAP_SUBSYS_NUM];
33 static long numLevels = 0;
35 static FILE *log_file = NULL;
36 static int global_level = 0;
39 static int use_syslog = 0;
41 static int debug2syslog(int l) {
43 case LDAP_LEVEL_EMERG: return LOG_EMERG;
44 case LDAP_LEVEL_ALERT: return LOG_ALERT;
45 case LDAP_LEVEL_CRIT: return LOG_CRIT;
46 case LDAP_LEVEL_ERR: return LOG_ERR;
47 case LDAP_LEVEL_WARNING: return LOG_WARNING;
48 case LDAP_LEVEL_NOTICE: return LOG_NOTICE;
49 case LDAP_LEVEL_INFO: return LOG_INFO;
55 static char *lutil_levels[] = {"emergency", "alert", "critical",
56 "error", "warning", "notice",
57 "information", "entry", "args",
58 "results", "detail1", "detail2",
61 static char *lutil_subsys[LDAP_SUBSYS_NUM] = {"global","operation", "transport",
62 "connection", "filter", "ber",
63 "config", "acl", "cache", "index",
64 "ldif", "tools", "slapd", "slurpd",
65 "backend", "back_bdb", "back_ldbm",
66 "back_ldap", "back_meta", "back_mon" };
68 int lutil_mnem2subsys( const char *subsys )
71 for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
73 if ( !strcasecmp( subsys, lutil_subsys[i] ) )
81 void lutil_set_all_backends( level )
85 for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
87 if ( !strncasecmp( "back_", lutil_subsys[i], strlen("back_") ) )
89 ldap_loglevels[i] = level;
94 int lutil_mnem2level( const char *level )
97 for( i = 0; lutil_levels[i] != NULL; i++ )
99 if ( !strcasecmp( level, lutil_levels[i] ) )
107 static int addSubsys( const char *subsys, int level )
111 if ( !strcasecmp( subsys, "backend" ) )
113 lutil_set_all_backends( level );
118 subsys_num = lutil_mnem2subsys(subsys);
121 fprintf(stderr, "Unknown Subsystem name [ %s ] - Discarded\n",
127 ldap_loglevels[subsys_num] = level;
133 int lutil_set_debug_level( const char* subsys, int level )
135 return( addSubsys( subsys, level ) );
138 int lutil_debug_file( FILE *file )
141 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
148 const char *subsys, int level,
149 const char *fmt, va_list vl )
159 t_subsys = strdup(subsys);
161 for(tmp = t_subsys, i = 0; i < strlen(t_subsys); i++, tmp++)
162 *tmp = TOUPPER( (unsigned char) *tmp );
165 /* we're configured to use syslog */
167 vsyslog( debug2syslog(level), fmt, vl );
174 if( log_file == NULL ) {
175 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
177 if ( log_file == NULL )
178 log_file = fopen( "openldap.log", "w" );
180 if ( log_file == NULL )
183 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
190 * Use stderr unless file was specified via:
191 * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
198 * Stick the time in the buffer to output when using Winsock
199 * as NT can't pipe to a timestamp program like Unix can.
200 * This, of course, makes some logs hard to read.
203 today = localtime( &now );
204 fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
205 today->tm_year + 1900, today->tm_mon + 1,
206 today->tm_mday, today->tm_hour,
207 today->tm_min, today->tm_sec );
211 * format the output data.
214 fprintf(file, "\n%s:: ", t_subsys );
215 vfprintf( file, fmt, vl );
220 * The primary logging routine. Takes the subsystem being logged from, the
221 * level of the log output and the format and data. Send this on to the
222 * internal routine with the print file, if any.
224 void lutil_log( const int subsys, int level, const char *fmt, ... )
226 FILE* outfile = NULL;
229 ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
230 lutil_log_int( outfile, lutil_subsys[subsys], level, fmt, vl );
234 void lutil_log_initialize(int argc, char **argv)
238 * Start by setting the hook for the libraries to use this logging
241 ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
243 if ( argc == 0 ) return;
245 * Now go through the command line options to set the debugging
248 for( i = 0; i < argc; i++ )
250 char *next = argv[i];
252 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
256 char *optarg = argv[i+1];
257 char *index = strchr( optarg, '=' );
261 strcpy ( subsys, optarg );
262 level = atoi( index+1 );
263 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
264 lutil_set_debug_level( subsys, level );
269 global_level = atoi( optarg );
270 ldap_loglevels[0] = global_level;
272 * if a negative number was used, make the global level the
273 * maximum sane level.
275 if ( global_level < 0 )
277 global_level = 65535;
278 ldap_loglevels[0] = 65535;
285 void (lutil_debug)( int debug, int level, const char *fmt, ... )
290 if ( !(level & debug ) )
294 if( log_file == NULL ) {
295 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
297 if ( log_file == NULL )
298 log_file = fopen( "openldap.log", "w" );
300 if ( log_file == NULL )
303 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
308 #ifdef HAVE_VSNPRINTF
309 vsnprintf( buffer, sizeof(buffer), fmt, vl );
311 vsprintf( buffer, fmt, vl );
313 buffer[sizeof(buffer)-1] = '\0';
315 if( log_file != NULL ) {
316 fputs( buffer, log_file );
320 fputs( buffer, stderr );