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 )
68 for( i = 0; i < numLevels; i++ )
70 if ( levelArray[i] == NULL )
72 levelArray[i] = (struct DEBUGLEVEL*)ber_memalloc( sizeof( struct DEBUGLEVEL ) );
73 levelArray[i]->subsystem = (char*)ber_memalloc( strlen( subsys ) + 1 );
74 strcpy ( levelArray[i]->subsystem, subsys );
75 levelArray[i]->level = level;
78 if( !strcasecmp( subsys, levelArray[i]->subsystem ) )
80 levelArray[i]->level = level;
84 levelArray = (struct DEBUGLEVEL**)ber_memrealloc( levelArray, sizeof( struct DEBUGLEVEL* ) * (numLevels + 10) );
85 for( j = numLevels; j < (numLevels + 10); j++ )
90 levelArray[i] = (struct DEBUGLEVEL*)ber_memalloc( sizeof( struct DEBUGLEVEL ) );
91 levelArray[i]->subsystem = (char*)ber_memalloc( strlen( subsys ) + 1 );
92 strcpy( levelArray[i]->subsystem, subsys );
93 levelArray[i]->level = level;
97 void lutil_set_debug_level( char* subsys, int level )
99 addSubsys( subsys, level );
102 int lutil_debug_file( FILE *file )
105 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
112 char *subsys, int level,
113 const char *fmt, va_list vl )
119 if ( levelArray == NULL ) return; /* logging isn't set up */
122 * Look for the subsystem in the level array. When we find it,
123 * break out of the loop.
125 for( i = 0; i < numLevels; i++ ) {
126 if ( levelArray[i] == NULL ) return;
127 if ( ! strcasecmp( levelArray[i]->subsystem, subsys ) ) break;
131 * If we didn't find the subsystem, or the set level is less than
132 * the requested output level, don't output it.
134 if ( (level > global_level) &&
135 ((i > numLevels ) || ( level > levelArray[i]->level )) )
142 /* we're configured to use syslog */
144 vsyslog( debug2syslog(level), fmt, vl );
152 if( log_file == NULL ) {
153 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
155 if ( log_file == NULL )
156 log_file = fopen( "openldap.log", "w" );
158 if ( log_file == NULL )
161 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
168 * Use stderr unless file was specified via:
169 * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
176 * Stick the time in the buffer to output when using Winsock
177 * as NT can't pipe to a timestamp program like Unix can.
178 * This, of course, makes some logs hard to read.
181 today = localtime( &now );
182 fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
183 today->tm_year + 1900, today->tm_mon + 1,
184 today->tm_mday, today->tm_hour,
185 today->tm_min, today->tm_sec );
189 * format the output data.
191 vfprintf( file, fmt, vl );
195 * The primary logging routine. Takes the subsystem being logged from, the
196 * level of the log output and the format and data. Send this on to the
197 * internal routine with the print file, if any.
199 void lutil_log( char *subsys, int level, const char *fmt, ... )
201 FILE* outfile = NULL;
204 ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
205 lutil_log_int( outfile, subsys, level, fmt, vl );
209 void lutil_log_initialize(int argc, char **argv)
213 * Start by setting the hook for the libraries to use this logging
216 ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
218 if ( argc == 0 ) return;
220 * Now go through the command line options to set the debugging
223 for( i = 0; i < argc; i++ )
225 char *next = argv[i];
226 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
230 char *optarg = argv[i+1];
231 char *index = strchr( optarg, '=' );
235 strcpy ( subsys, optarg );
236 level = atoi( index+1 );
237 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
238 lutil_set_debug_level( subsys, level );
243 global_level = atoi( optarg );
249 void (lutil_debug)( int debug, int level, const char *fmt, ... )
254 if ( !(level & debug ) )
258 if( log_file == NULL ) {
259 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
261 if ( log_file == NULL )
262 log_file = fopen( "openldap.log", "w" );
264 if ( log_file == NULL )
267 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
272 #ifdef HAVE_VSNPRINTF
273 vsnprintf( buffer, sizeof(buffer), fmt, vl );
275 vsprintf( buffer, fmt, vl );
277 buffer[sizeof(buffer)-1] = '\0';
279 if( log_file != NULL ) {
280 fputs( buffer, log_file );
284 fputs( buffer, stderr );