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];
33 static long numLevels = 0;
35 static FILE *log_file = NULL;
36 static int global_level = 0;
39 static int use_syslog = 0;
41 static int debug2syslog(int l) {
43 case LDAP_LEVEL_EMERG: return LOG_EMERG;
44 case LDAP_LEVEL_ALERT: return LOG_ALERT;
45 case LDAP_LEVEL_CRIT: return LOG_CRIT;
46 case LDAP_LEVEL_ERR: return LOG_ERR;
47 case LDAP_LEVEL_WARNING: return LOG_WARNING;
48 case LDAP_LEVEL_NOTICE: return LOG_NOTICE;
49 case LDAP_LEVEL_INFO: return LOG_INFO;
55 static char *lutil_levels[] = {"emergency", "alert", "critical",
56 "error", "warning", "notice",
57 "information", "entry", "args",
58 "results", "detail1", "detail2",
61 static char *lutil_subsys[LDAP_SUBSYS_NUM] = {"global","operation", "transport",
62 "connection", "filter", "ber",
63 "config", "acl", "cache", "index",
64 "ldif", "tools", "slapd", "slurpd",
65 "backend", "back_bdb", "back_ldbm",
66 "back_ldap", "back_meta", "back_mon" };
68 int lutil_mnem2subsys( const char *subsys )
71 for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
73 if ( !strcasecmp( subsys, lutil_subsys[i] ) )
81 void lutil_set_all_backends( level )
85 for( i = 0; i < LDAP_SUBSYS_NUM; i++ )
87 if ( !strncasecmp( "back_", lutil_subsys[i], strlen("back_") ) )
89 ldap_loglevels[i] = level;
94 int lutil_mnem2level( const char *level )
97 for( i = 0; lutil_levels[i] != NULL; i++ )
99 if ( !strcasecmp( level, lutil_levels[i] ) )
107 static int addSubsys( const char *subsys, int level )
111 if ( !strcasecmp( subsys, "backend" ) )
113 lutil_set_all_backends( level );
118 subsys_num = lutil_mnem2subsys(subsys);
121 fprintf(stderr, "Unknown Subsystem name [ %s ] - Discarded\n",
127 ldap_loglevels[subsys_num] = level;
133 int lutil_set_debug_level( const char* subsys, int level )
135 return( addSubsys( subsys, level ) );
138 int lutil_debug_file( FILE *file )
141 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
148 const char *subsys, int level,
149 const char *fmt, va_list vl )
159 t_subsys = strdup(subsys);
161 for(tmp = t_subsys, i = 0; i < strlen(t_subsys); i++, tmp++)
162 *tmp = TOUPPER( (unsigned char) *tmp );
165 /* we're configured to use syslog */
168 vsyslog( debug2syslog(level), fmt, vl );
171 vsnprintf( data, sizeof(data), fmt, vl );
172 syslog( debug2syslog(level), data );
180 if( log_file == NULL ) {
181 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
183 if ( log_file == NULL )
184 log_file = fopen( "openldap.log", "w" );
186 if ( log_file == NULL )
189 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
196 * Use stderr unless file was specified via:
197 * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
204 * Stick the time in the buffer to output when using Winsock
205 * as NT can't pipe to a timestamp program like Unix can.
206 * This, of course, makes some logs hard to read.
209 today = localtime( &now );
210 fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
211 today->tm_year + 1900, today->tm_mon + 1,
212 today->tm_mday, today->tm_hour,
213 today->tm_min, today->tm_sec );
217 * format the output data.
220 fprintf(file, "\n%s:: ", t_subsys );
221 vfprintf( file, fmt, vl );
226 * The primary logging routine. Takes the subsystem being logged from, the
227 * level of the log output and the format and data. Send this on to the
228 * internal routine with the print file, if any.
230 void lutil_log( const int subsys, int level, const char *fmt, ... )
232 FILE* outfile = NULL;
235 ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
236 lutil_log_int( outfile, lutil_subsys[subsys], level, fmt, vl );
240 void lutil_log_initialize(int argc, char **argv)
244 * Start by setting the hook for the libraries to use this logging
247 ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
249 if ( argc == 0 ) return;
251 * Now go through the command line options to set the debugging
254 for( i = 0; i < argc; i++ )
256 char *next = argv[i];
258 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
262 char *optarg = argv[i+1];
263 char *index = strchr( optarg, '=' );
267 strcpy ( subsys, optarg );
268 level = atoi( index+1 );
269 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
270 lutil_set_debug_level( subsys, level );
275 global_level = atoi( optarg );
276 ldap_loglevels[0] = global_level;
278 * if a negative number was used, make the global level the
279 * maximum sane level.
281 if ( global_level < 0 )
283 global_level = 65535;
284 ldap_loglevels[0] = 65535;
291 void (lutil_debug)( int debug, int level, const char *fmt, ... )
296 if ( !(level & debug ) )
300 if( log_file == NULL ) {
301 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
303 if ( log_file == NULL )
304 log_file = fopen( "openldap.log", "w" );
306 if ( log_file == NULL )
309 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
314 #ifdef HAVE_VSNPRINTF
315 vsnprintf( buffer, sizeof(buffer), fmt, vl );
317 vsprintf( buffer, fmt, vl );
319 buffer[sizeof(buffer)-1] = '\0';
321 if( log_file != NULL ) {
322 fputs( buffer, log_file );
326 fputs( buffer, stderr );