3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdarg.h>
13 #include <ac/string.h>
17 #include "ldap_defaults.h"
32 static struct DEBUGLEVEL **levelArray;
33 static long numLevels = 0;
35 static FILE *log_file = NULL;
36 static int global_level = 0;
40 static int use_syslog = 0;
42 static int debug2syslog(int l) {
44 /* insert mapping cases here */
52 static char *lutil_levels[] = {"emergency", "alert", "critical",
53 "error", "warning", "notice",
54 "information", "entry", "args",
55 "results", "detail1", "detail2",
58 int lutil_mnem2level( char *level )
61 for( i = 0; lutil_levels[i] != NULL; i++ )
63 if ( !strcasecmp( level, lutil_levels[i] ) )
71 static void addSubsys( const char *subsys, int level )
74 for( i = 0; i < numLevels; i++ )
76 if ( levelArray[i] == NULL )
78 levelArray[i] = (struct DEBUGLEVEL*)ber_memalloc( sizeof( struct DEBUGLEVEL ) );
79 levelArray[i]->subsystem = (char*)ber_memalloc( strlen( subsys ) + 1 );
80 strcpy ( levelArray[i]->subsystem, subsys );
81 levelArray[i]->level = level;
84 if( !strcasecmp( subsys, levelArray[i]->subsystem ) )
86 levelArray[i]->level = level;
90 levelArray = (struct DEBUGLEVEL**)ber_memrealloc( levelArray, sizeof( struct DEBUGLEVEL* ) * (numLevels + 10) );
91 for( j = numLevels; j < (numLevels + 10); j++ )
96 levelArray[i] = (struct DEBUGLEVEL*)ber_memalloc( sizeof( struct DEBUGLEVEL ) );
97 levelArray[i]->subsystem = (char*)ber_memalloc( strlen( subsys ) + 1 );
98 strcpy( levelArray[i]->subsystem, subsys );
99 levelArray[i]->level = level;
103 void lutil_set_debug_level( char* subsys, int level )
105 addSubsys( subsys, level );
108 int lutil_debug_file( FILE *file )
111 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
118 char *subsys, int level,
119 const char *fmt, va_list vl )
125 if ( levelArray == NULL ) return; /* logging isn't set up */
128 * Look for the subsystem in the level array. When we find it,
129 * break out of the loop.
131 for( i = 0; i < numLevels; i++ ) {
132 if ( levelArray[i] == NULL ) return;
133 if ( ! strcasecmp( levelArray[i]->subsystem, subsys ) ) break;
137 * If we didn't find the subsystem, or the set level is less than
138 * the requested output level, don't output it.
140 if ( (level > global_level) &&
141 ((i > numLevels ) || ( level > levelArray[i]->level )) )
148 /* we're configured to use syslog */
150 vsyslog( debug2syslog(level), fmt, vl );
158 if( log_file == NULL ) {
159 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
161 if ( log_file == NULL )
162 log_file = fopen( "openldap.log", "w" );
164 if ( log_file == NULL )
167 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
174 * Use stderr unless file was specified via:
175 * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
182 * Stick the time in the buffer to output when using Winsock
183 * as NT can't pipe to a timestamp program like Unix can.
184 * This, of course, makes some logs hard to read.
187 today = localtime( &now );
188 fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
189 today->tm_year + 1900, today->tm_mon + 1,
190 today->tm_mday, today->tm_hour,
191 today->tm_min, today->tm_sec );
195 * format the output data.
197 vfprintf( file, fmt, vl );
201 * The primary logging routine. Takes the subsystem being logged from, the
202 * level of the log output and the format and data. Send this on to the
203 * internal routine with the print file, if any.
205 void lutil_log( char *subsys, int level, const char *fmt, ... )
207 FILE* outfile = NULL;
210 ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
211 lutil_log_int( outfile, subsys, level, fmt, vl );
215 void lutil_log_initialize(int argc, char **argv)
219 * Start by setting the hook for the libraries to use this logging
222 ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
224 if ( argc == 0 ) return;
226 * Now go through the command line options to set the debugging
229 for( i = 0; i < argc; i++ )
231 char *next = argv[i];
232 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
236 char *optarg = argv[i+1];
237 char *index = strchr( optarg, '=' );
241 strcpy ( subsys, optarg );
242 level = atoi( index+1 );
243 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
244 lutil_set_debug_level( subsys, level );
249 global_level = atoi( optarg );
255 void (lutil_debug)( int debug, int level, const char *fmt, ... )
260 if ( !(level & debug ) )
264 if( log_file == NULL ) {
265 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
267 if ( log_file == NULL )
268 log_file = fopen( "openldap.log", "w" );
270 if ( log_file == NULL )
273 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
278 #ifdef HAVE_VSNPRINTF
279 vsnprintf( buffer, sizeof(buffer), fmt, vl );
281 vsprintf( buffer, fmt, vl );
283 buffer[sizeof(buffer)-1] = '\0';
285 if( log_file != NULL ) {
286 fputs( buffer, log_file );
290 fputs( buffer, stderr );