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>
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( const 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( const 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 const char *subsys, int level,
116 const char *fmt, va_list vl )
124 if ( levelArray == NULL ) return; /* logging isn't set up */
127 * Look for the subsystem in the level array. When we find it,
128 * break out of the loop.
130 for( i = 0; i < numLevels; i++ ) {
131 if ( levelArray[i] == NULL ) break;
132 if ( ! strcasecmp( levelArray[i]->subsystem, subsys ) ) break;
136 * If we didn't find the subsystem, or the set level is less than
137 * the requested output level, don't output it.
139 if ( (level > global_level) &&
140 ((i > numLevels ) || (levelArray[i] == NULL) || ( level > levelArray[i]->level )) )
147 /* we're configured to use syslog */
149 vsyslog( debug2syslog(level), fmt, vl );
157 if( log_file == NULL ) {
158 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
160 if ( log_file == NULL )
161 log_file = fopen( "openldap.log", "w" );
163 if ( log_file == NULL )
166 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
173 * Use stderr unless file was specified via:
174 * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
181 * Stick the time in the buffer to output when using Winsock
182 * as NT can't pipe to a timestamp program like Unix can.
183 * This, of course, makes some logs hard to read.
186 today = localtime( &now );
187 fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
188 today->tm_year + 1900, today->tm_mon + 1,
189 today->tm_mday, today->tm_hour,
190 today->tm_min, today->tm_sec );
194 * format the output data.
196 vfprintf( file, fmt, vl );
200 * The primary logging routine. Takes the subsystem being logged from, the
201 * level of the log output and the format and data. Send this on to the
202 * internal routine with the print file, if any.
204 void lutil_log( const char *subsys, int level, const char *fmt, ... )
206 FILE* outfile = NULL;
209 ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
210 lutil_log_int( outfile, subsys, level, fmt, vl );
214 void lutil_log_initialize(int argc, char **argv)
218 * Start by setting the hook for the libraries to use this logging
221 ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
223 if ( argc == 0 ) return;
225 * Now go through the command line options to set the debugging
228 for( i = 0; i < argc; i++ )
230 char *next = argv[i];
231 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
235 char *optarg = argv[i+1];
236 char *index = strchr( optarg, '=' );
240 strcpy ( subsys, optarg );
241 level = atoi( index+1 );
242 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
243 lutil_set_debug_level( subsys, level );
248 global_level = atoi( optarg );
250 * if a negative number was used, make the global level the
251 * maximum sane level.
253 if ( global_level < 0 ) global_level = 65535;
259 void (lutil_debug)( int debug, int level, const char *fmt, ... )
264 if ( !(level & debug ) )
268 if( log_file == NULL ) {
269 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
271 if ( log_file == NULL )
272 log_file = fopen( "openldap.log", "w" );
274 if ( log_file == NULL )
277 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
282 #ifdef HAVE_VSNPRINTF
283 vsnprintf( buffer, sizeof(buffer), fmt, vl );
285 vsprintf( buffer, fmt, vl );
287 buffer[sizeof(buffer)-1] = '\0';
289 if( log_file != NULL ) {
290 fputs( buffer, log_file );
294 fputs( buffer, stderr );