3 * Copyright 1998-2003 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[] = {
55 "emergency", "alert", "critical",
56 "error", "warning", "notice",
57 "information", "entry", "args",
58 "results", "detail1", "detail2",
61 static char *lutil_subsys[LDAP_SUBSYS_NUM] = {
62 "global","operation", "transport",
63 "connection", "filter", "ber",
64 "config", "acl", "cache", "index",
65 "ldif", "tools", "slapd", "slurpd",
66 "backend", "back_bdb", "back_ldbm",
67 "back_ldap", "back_meta", "back_mon" };
69 int lutil_mnem2subsys( const char *subsys )
72 for( i = 0; i < LDAP_SUBSYS_NUM; i++ ) {
73 if ( !strcasecmp( subsys, lutil_subsys[i] ) ) {
80 void lutil_set_all_backends( int level )
84 for( i = 0; i < LDAP_SUBSYS_NUM; i++ ) {
85 if ( !strncasecmp( "back_", lutil_subsys[i], strlen("back_") ) ) {
86 ldap_loglevels[i] = level;
91 int lutil_mnem2level( const char *level )
94 for( i = 0; lutil_levels[i] != NULL; i++ ) {
95 if ( !strcasecmp( level, lutil_levels[i] ) ) {
102 static int addSubsys( const char *subsys, int level )
106 if ( !strcasecmp( subsys, "backend" ) ) {
107 lutil_set_all_backends( level );
111 subsys_num = lutil_mnem2subsys(subsys);
113 fprintf(stderr, _("Unknown Subsystem name [ %s ] - Discarded\n"),
119 ldap_loglevels[subsys_num] = level;
125 int lutil_set_debug_level( const char* subsys, int level )
127 return( addSubsys( subsys, level ) );
130 int lutil_debug_file( FILE *file )
133 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
140 const char *subsys, int level,
141 const char *fmt, va_list vl )
151 t_subsys = strdup(subsys);
153 for(tmp = t_subsys, i = 0; i < strlen(t_subsys); i++, tmp++) {
154 *tmp = TOUPPER( (unsigned char) *tmp );
158 /* we're configured to use syslog */
161 vsyslog( debug2syslog(level), fmt, vl );
164 vsnprintf( data, sizeof(data), fmt, vl );
165 syslog( debug2syslog(level), data );
173 if( log_file == NULL ) {
174 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
176 if ( log_file == NULL ) {
177 log_file = fopen( "openldap.log", "w" );
178 if ( log_file == NULL ) return;
181 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
188 * Use stderr unless file was specified via:
189 * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
196 * Stick the time in the buffer to output when using Winsock
197 * as NT can't pipe to a timestamp program like Unix can.
198 * This, of course, makes some logs hard to read.
201 today = localtime( &now );
202 fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
203 today->tm_year + 1900, today->tm_mon + 1,
204 today->tm_mday, today->tm_hour,
205 today->tm_min, today->tm_sec );
209 * format the output data.
212 fprintf(file, "\n%s:: ", t_subsys );
213 vfprintf( file, fmt, vl );
218 * The primary logging routine. Takes the subsystem being logged from, the
219 * level of the log output and the format and data. Send this on to the
220 * internal routine with the print file, if any.
222 void lutil_log( const int subsys, int level, const char *fmt, ... )
224 FILE* outfile = NULL;
227 ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
228 lutil_log_int( outfile, lutil_subsys[subsys], level, fmt, vl );
232 void lutil_log_initialize(int argc, char **argv)
236 * Start by setting the hook for the libraries to use this logging
239 ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
241 if ( argc == 0 ) return;
244 * Now go through the command line options to set the debugging
247 for( i = 0; i < argc; i++ ) {
248 char *next = argv[i];
250 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' ) {
253 char *optarg = argv[i+1];
254 char *index = strchr( optarg, '=' );
256 if ( index != NULL ) {
258 strcpy ( subsys, optarg );
259 level = atoi( index+1 );
260 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
261 lutil_set_debug_level( subsys, level );
265 global_level = atoi( optarg );
266 ldap_loglevels[0] = global_level;
268 * if a negative number was used, make the global level the
269 * maximum sane level.
271 if ( global_level < 0 ) {
272 global_level = 65535;
273 ldap_loglevels[0] = 65535;
280 void (lutil_debug)( int debug, int level, const char *fmt, ... )
285 if ( !(level & debug ) ) return;
288 if( log_file == NULL ) {
289 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
291 if ( log_file == NULL ) {
292 log_file = fopen( "openldap.log", "w" );
293 if ( log_file == NULL ) return;
296 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
301 vsnprintf( buffer, sizeof(buffer), fmt, vl );
302 buffer[sizeof(buffer)-1] = '\0';
304 if( log_file != NULL ) {
305 fputs( buffer, log_file );
309 fputs( buffer, stderr );