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"
26 static struct DEBUGLEVEL **levelArray;
27 static long numLevels = 0;
29 static FILE *log_file = NULL;
30 static int global_level = 0;
34 static int use_syslog = 0;
36 static int debug2syslog(int l) {
38 /* insert mapping cases here */
46 static char *lutil_levels[] = {"emergency", "alert", "critical",
47 "error", "warning", "notice",
48 "information", "entry", "args",
49 "results", "detail1", "detail2",
52 int lutil_mnem2level( char *level )
55 for( i = 0; lutil_levels[i] != NULL; i++ )
57 if ( !strcasecmp( level, lutil_levels[i] ) )
65 static void addSubsys( const char *subsys, int level )
69 if ( !strcasecmp( subsys, "global") ) global_level = level;
71 for( i = 0; i < numLevels; i++ )
73 if ( levelArray[i] == NULL )
75 levelArray[i] = (struct DEBUGLEVEL*)ber_memalloc( sizeof( struct DEBUGLEVEL ) );
76 levelArray[i]->subsystem = (char*)ber_memalloc( strlen( subsys ) + 1 );
77 strcpy ( levelArray[i]->subsystem, subsys );
78 levelArray[i]->level = level;
81 if( !strcasecmp( subsys, levelArray[i]->subsystem ) )
83 levelArray[i]->level = level;
87 levelArray = (struct DEBUGLEVEL**)ber_memrealloc( levelArray, sizeof( struct DEBUGLEVEL* ) * (numLevels + 10) );
88 for( j = numLevels; j < (numLevels + 10); j++ )
93 levelArray[i] = (struct DEBUGLEVEL*)ber_memalloc( sizeof( struct DEBUGLEVEL ) );
94 levelArray[i]->subsystem = (char*)ber_memalloc( strlen( subsys ) + 1 );
95 strcpy( levelArray[i]->subsystem, subsys );
96 levelArray[i]->level = level;
100 void lutil_set_debug_level( char* subsys, int level )
102 addSubsys( subsys, level );
105 int lutil_debug_file( FILE *file )
108 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
115 char *subsys, int level,
116 const char *fmt, va_list vl )
122 if ( levelArray == NULL ) return; /* logging isn't set up */
125 * Look for the subsystem in the level array. When we find it,
126 * break out of the loop.
128 for( i = 0; i < numLevels; i++ ) {
129 if ( levelArray[i] == NULL ) break;
130 if ( ! strcasecmp( levelArray[i]->subsystem, subsys ) ) break;
134 * If we didn't find the subsystem, or the set level is less than
135 * the requested output level, don't output it.
137 if ( (level > global_level) &&
138 ((i > numLevels ) || (levelArray[i] == NULL) || ( level > levelArray[i]->level )) )
145 /* we're configured to use syslog */
147 vsyslog( debug2syslog(level), fmt, vl );
155 if( log_file == NULL ) {
156 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
158 if ( log_file == NULL )
159 log_file = fopen( "openldap.log", "w" );
161 if ( log_file == NULL )
164 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
171 * Use stderr unless file was specified via:
172 * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
179 * Stick the time in the buffer to output when using Winsock
180 * as NT can't pipe to a timestamp program like Unix can.
181 * This, of course, makes some logs hard to read.
184 today = localtime( &now );
185 fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
186 today->tm_year + 1900, today->tm_mon + 1,
187 today->tm_mday, today->tm_hour,
188 today->tm_min, today->tm_sec );
192 * format the output data.
194 vfprintf( file, fmt, vl );
198 * The primary logging routine. Takes the subsystem being logged from, the
199 * level of the log output and the format and data. Send this on to the
200 * internal routine with the print file, if any.
202 void lutil_log( char *subsys, int level, const char *fmt, ... )
204 FILE* outfile = NULL;
207 ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
208 lutil_log_int( outfile, subsys, level, fmt, vl );
212 void lutil_log_initialize(int argc, char **argv)
216 * Start by setting the hook for the libraries to use this logging
219 ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
221 if ( argc == 0 ) return;
223 * Now go through the command line options to set the debugging
226 for( i = 0; i < argc; i++ )
228 char *next = argv[i];
229 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
233 char *optarg = argv[i+1];
234 char *index = strchr( optarg, '=' );
238 strcpy ( subsys, optarg );
239 level = atoi( index+1 );
240 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
241 lutil_set_debug_level( subsys, level );
246 global_level = atoi( optarg );
248 * if a negative number was used, make the global level the
249 * maximum sane level.
251 if ( global_level < 0 ) global_level = 65535;
257 void (lutil_debug)( int debug, int level, const char *fmt, ... )
262 if ( !(level & debug ) )
266 if( log_file == NULL ) {
267 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
269 if ( log_file == NULL )
270 log_file = fopen( "openldap.log", "w" );
272 if ( log_file == NULL )
275 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
280 #ifdef HAVE_VSNPRINTF
281 vsnprintf( buffer, sizeof(buffer), fmt, vl );
283 vsprintf( buffer, fmt, vl );
285 buffer[sizeof(buffer)-1] = '\0';
287 if( log_file != NULL ) {
288 fputs( buffer, log_file );
292 fputs( buffer, stderr );