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];
34 static FILE *log_file = NULL;
35 static int global_level = 0;
38 static int use_syslog = 0;
40 static int debug2syslog(int l) {
42 case LDAP_LEVEL_EMERG: return LOG_EMERG;
43 case LDAP_LEVEL_ALERT: return LOG_ALERT;
44 case LDAP_LEVEL_CRIT: return LOG_CRIT;
45 case LDAP_LEVEL_ERR: return LOG_ERR;
46 case LDAP_LEVEL_WARNING: return LOG_WARNING;
47 case LDAP_LEVEL_NOTICE: return LOG_NOTICE;
48 case LDAP_LEVEL_INFO: return LOG_INFO;
54 static char *lutil_levels[] = {"emergency", "alert", "critical",
55 "error", "warning", "notice",
56 "information", "entry", "args",
57 "results", "detail1", "detail2",
60 static char *lutil_subsys[LDAP_SUBSYS_NUM] = {"global","operation", "transport",
61 "connection", "filter", "ber",
62 "config", "acl", "cache", "index",
63 "ldif", "tools", "slapd", "slurpd",
64 "backend", "back_bdb", "back_ldbm",
65 "back_ldap", "back_meta", "back_mon" };
67 int lutil_mnem2subsys( const char *subsys )
70 for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
72 if ( !strcasecmp( subsys, lutil_subsys[i] ) )
80 void lutil_set_all_backends( level )
84 for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
86 if ( !strncasecmp( "back_", lutil_subsys[i], strlen("back_") ) )
88 ldap_loglevels[i] = level;
93 int lutil_mnem2level( const char *level )
96 for( i = 0; lutil_levels[i] != NULL; i++ )
98 if ( !strcasecmp( level, lutil_levels[i] ) )
106 static int addSubsys( const char *subsys, int level )
110 if ( !strcasecmp( subsys, "backend" ) )
112 lutil_set_all_backends( level );
117 subsys_num = lutil_mnem2subsys(subsys);
120 fprintf(stderr, "Unknown Subsystem name [ %s ] - Discarded\n",
126 ldap_loglevels[subsys_num] = level;
132 int lutil_set_debug_level( const char* subsys, int level )
134 return( addSubsys( subsys, level ) );
137 int lutil_debug_file( FILE *file )
140 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
147 const char *subsys, int level,
148 const char *fmt, va_list vl )
158 t_subsys = strdup(subsys);
160 for(tmp = t_subsys, i = 0; i < strlen(t_subsys); i++, tmp++)
161 *tmp = TOUPPER( (unsigned char) *tmp );
164 /* we're configured to use syslog */
167 vsyslog( debug2syslog(level), fmt, vl );
170 vsnprintf( data, sizeof(data), fmt, vl );
171 syslog( debug2syslog(level), data );
179 if( log_file == NULL ) {
180 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
182 if ( log_file == NULL )
183 log_file = fopen( "openldap.log", "w" );
185 if ( log_file == NULL )
188 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
195 * Use stderr unless file was specified via:
196 * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
203 * Stick the time in the buffer to output when using Winsock
204 * as NT can't pipe to a timestamp program like Unix can.
205 * This, of course, makes some logs hard to read.
208 today = localtime( &now );
209 fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
210 today->tm_year + 1900, today->tm_mon + 1,
211 today->tm_mday, today->tm_hour,
212 today->tm_min, today->tm_sec );
216 * format the output data.
219 fprintf(file, "\n%s:: ", t_subsys );
220 vfprintf( file, fmt, vl );
225 * The primary logging routine. Takes the subsystem being logged from, the
226 * level of the log output and the format and data. Send this on to the
227 * internal routine with the print file, if any.
229 void lutil_log( const int subsys, int level, const char *fmt, ... )
231 FILE* outfile = NULL;
234 ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
235 lutil_log_int( outfile, lutil_subsys[subsys], level, fmt, vl );
239 void lutil_log_initialize(int argc, char **argv)
243 * Start by setting the hook for the libraries to use this logging
246 ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
248 if ( argc == 0 ) return;
250 * Now go through the command line options to set the debugging
253 for( i = 0; i < argc; i++ )
255 char *next = argv[i];
257 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
261 char *optarg = argv[i+1];
262 char *index = strchr( optarg, '=' );
266 strcpy ( subsys, optarg );
267 level = atoi( index+1 );
268 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
269 lutil_set_debug_level( subsys, level );
274 global_level = atoi( optarg );
275 ldap_loglevels[0] = global_level;
277 * if a negative number was used, make the global level the
278 * maximum sane level.
280 if ( global_level < 0 )
282 global_level = 65535;
283 ldap_loglevels[0] = 65535;
290 void (lutil_debug)( int debug, int level, const char *fmt, ... )
295 if ( !(level & debug ) )
299 if( log_file == NULL ) {
300 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
302 if ( log_file == NULL )
303 log_file = fopen( "openldap.log", "w" );
305 if ( log_file == NULL )
308 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
313 vsnprintf( buffer, sizeof(buffer), fmt, vl );
314 buffer[sizeof(buffer)-1] = '\0';
316 if( log_file != NULL ) {
317 fputs( buffer, log_file );
321 fputs( buffer, stderr );