]> git.sur5r.net Git - openldap/blob - libraries/liblber/debug.c
Happy new year
[openldap] / libraries / liblber / debug.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdarg.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24 #include <ac/ctype.h>
25
26 #ifdef LDAP_SYSLOG
27 #include <ac/syslog.h>
28 #endif
29
30 #include "ldap_log.h"
31 #include "ldap_defaults.h"
32 #include "lber.h"
33 #include "ldap_pvt.h"
34
35 int ldap_loglevels[LDAP_SUBSYS_NUM];
36
37 static FILE *log_file = NULL;
38 static int global_level = 0;
39
40 #ifdef LDAP_SYSLOG
41 static int use_syslog = 0;
42
43 static int debug2syslog(int l) {
44         switch (l) {
45         case LDAP_LEVEL_EMERG: return LOG_EMERG;
46         case LDAP_LEVEL_ALERT: return LOG_ALERT;
47         case LDAP_LEVEL_CRIT: return LOG_CRIT;
48         case LDAP_LEVEL_ERR: return LOG_ERR;
49         case LDAP_LEVEL_WARNING: return LOG_WARNING;
50         case LDAP_LEVEL_NOTICE: return LOG_NOTICE;
51         case LDAP_LEVEL_INFO: return LOG_INFO;
52         }
53         return LOG_DEBUG;
54 }
55 #endif
56
57 static char *debug_levels[] = {
58         "emergency", "alert", "critical",
59         "error", "warning", "notice",
60         "information", "entry", "args",
61         "results", "detail1", "detail2",
62         NULL };
63
64 static char *debug_subsys[LDAP_SUBSYS_NUM] = {
65         "GLOBAL", "OPERATION", "TRANSPORT",
66         "CONNECTION", "FILTER", "BER", 
67         "CONFIG", "ACL", "CACHE", "INDEX", 
68         "LDIF", "TOOLS", "SLAPD", "SLURPD",
69         "BACKEND", "BACK_BDB", "BACK_LDBM", 
70         "BACK_LDAP", "BACK_META", "BACK_MON" };
71
72 int lutil_mnem2subsys( const char *subsys )
73 {
74         int i;
75         for( i = 0; i < LDAP_SUBSYS_NUM; i++ ) {
76                 if ( !strcasecmp( subsys, debug_subsys[i] ) ) {
77                         return i;
78                 }
79         }
80         return -1;
81 }
82
83 void lutil_set_all_backends( int level )
84 {
85         int i;
86         for( i = 0; i < LDAP_SUBSYS_NUM; i++ ) {
87                 if ( !strncasecmp( "BACK_", debug_subsys[i], sizeof("BACK_")-1 ) ) {
88                         ldap_loglevels[i] = level;
89                 }
90         }
91 }
92
93 int lutil_mnem2level( const char *level )
94 {
95         int i;
96         for( i = 0; debug_levels[i] != NULL; i++ ) {
97                 if ( !strcasecmp( level, debug_levels[i] ) ) {
98                         return i;
99                 }
100         }
101         return -1;
102 }
103
104 static int addSubsys( const char *subsys, int level )
105 {
106         int subsys_num;
107
108         if ( !strcasecmp( subsys, "BACKEND" ) ) {
109                 lutil_set_all_backends( level );
110                 return level;
111
112         } else {
113                 subsys_num = lutil_mnem2subsys(subsys);
114                 if(subsys_num < 0) {
115                         fprintf(stderr, _("Unknown Subsystem name [ %s ] - Discarded\n"), 
116                                 subsys);
117                         fflush(stderr);
118                         return -1;
119                 }
120
121                 ldap_loglevels[subsys_num] = level;
122                 return level;
123         }
124         return -1;
125 }
126
127 int lutil_set_debug_level( const char* subsys, int level )
128 {
129         return( addSubsys( subsys, level ) );
130 }
131
132 int lutil_debug_file( FILE *file )
133 {
134         log_file = file;
135         ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
136
137         return 0;
138 }
139
140 void lutil_log_int(
141         FILE* file,
142         const char *subsys, int level,
143         const char *fmt, va_list vl )
144 {
145 #ifdef HAVE_WINSOCK
146         time_t now;
147         struct tm *today;
148 #endif
149         size_t i;
150         char * tmp;
151
152 #ifdef LDAP_SYSLOG
153         /* we're configured to use syslog */
154         if( use_syslog ) {
155 #ifdef HAVE_VSYSLOG
156                 vsyslog( debug2syslog(level), fmt, vl );
157 #else
158                 char data[4096];
159                 vsnprintf( data, sizeof(data), fmt, vl );
160                 syslog( debug2syslog(level), data );
161 #endif
162                 return;
163         }
164 #endif
165
166 #if 0
167 #ifdef HAVE_WINSOCK
168         if( log_file == NULL ) {
169                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
170
171                 if ( log_file == NULL ) {
172                         log_file = fopen( "openldap.log", "w" );
173                         if ( log_file == NULL ) return;
174                 }
175
176                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
177         }
178 #endif
179 #endif
180
181         if( file == NULL ) {
182                 /*
183                  * Use stderr unless file was specified via:
184                  *   ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
185                  */
186                 file = stderr;
187         }
188
189 #ifdef HAVE_WINSOCK
190         /*
191          * Stick the time in the buffer to output when using Winsock
192          * as NT can't pipe to a timestamp program like Unix can.
193          * This, of course, makes some logs hard to read.
194          */
195         time( &now );
196         today = localtime( &now );
197         fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
198                 today->tm_year + 1900, today->tm_mon + 1,
199                 today->tm_mday, today->tm_hour,
200                 today->tm_min, today->tm_sec );
201 #endif
202
203         /*
204          * format the output data.
205          */
206
207         fprintf(file, "\n%s:: ", subsys ); 
208         vfprintf( file, fmt, vl );
209         fflush( file );
210 }
211
212 /*
213  * The primary logging routine.  Takes the subsystem being logged from, the
214  * level of the log output and the format and data.  Send this on to the
215  * internal routine with the print file, if any.
216  */
217 void lutil_log( const int subsys, int level, const char *fmt, ... )
218 {
219         FILE* outfile = NULL;
220         va_list vl;
221         va_start( vl, fmt );
222         ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
223         lutil_log_int( outfile, debug_subsys[subsys], level, fmt, vl );
224         va_end( vl );
225 }
226
227 void lutil_log_initialize(int argc, char **argv)
228 {
229         int i;
230         /*
231          * Start by setting the hook for the libraries to use this logging
232          * routine.
233          */
234         ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
235
236         if ( argc == 0 ) return;
237
238         /*
239          * Now go through the command line options to set the debugging
240          * levels
241          */
242         for( i = 0; i < argc; i++ ) {
243                 char *next = argv[i];
244         
245                 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' ) {
246                         char subsys[64];
247                         int level;
248                         char *optarg = argv[i+1];
249                         char *index = strchr( optarg, '=' );
250
251                         if ( index != NULL ) {
252                                 *index = 0;
253                                 strcpy ( subsys, optarg );
254                                 level = atoi( index+1 );
255                                 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
256                                 lutil_set_debug_level( subsys, level );
257                                 *index = '=';
258
259                         } else {
260                                 global_level = atoi( optarg );
261                                 ldap_loglevels[0] = global_level;
262                                 /* 
263                                  * if a negative number was used, make the global level the
264                                  * maximum sane level.
265                                  */
266                                 if ( global_level < 0 ) {
267                                         global_level = 65535;
268                                         ldap_loglevels[0] = 65535;
269                                 }
270                         }
271                 }
272         }
273 }
274
275 void (lutil_debug)( int debug, int level, const char *fmt, ... )
276 {
277         char buffer[4096];
278         va_list vl;
279
280         if ( !(level & debug ) ) return;
281
282 #ifdef HAVE_WINSOCK
283         if( log_file == NULL ) {
284                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
285
286                 if ( log_file == NULL ) {
287                         log_file = fopen( "openldap.log", "w" );
288                         if ( log_file == NULL ) return;
289                 }
290
291                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
292         }
293 #endif
294
295         va_start( vl, fmt );
296         vsnprintf( buffer, sizeof(buffer), fmt, vl );
297         buffer[sizeof(buffer)-1] = '\0';
298         if( log_file != NULL ) {
299                 fputs( buffer, log_file );
300                 fflush( log_file );
301         }
302         fputs( buffer, stderr );
303         va_end( vl );
304 }