]> git.sur5r.net Git - openldap/blob - libraries/liblber/debug.c
ITS#2657: authzid (and assertion) should apply to most everything
[openldap] / libraries / liblber / debug.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdarg.h>
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15 #include <ac/ctype.h>
16
17 #ifdef LDAP_SYSLOG
18 #include <ac/syslog.h>
19 #endif
20
21 #include "ldap_log.h"
22 #include "ldap_defaults.h"
23 #include "lber.h"
24 #include "ldap_pvt.h"
25
26 struct DEBUGLEVEL
27 {
28         char *subsystem;
29         int  level;
30 };
31
32 int ldap_loglevels[LDAP_SUBSYS_NUM];
33
34 static FILE *log_file = NULL;
35 static int global_level = 0;
36
37 #ifdef LDAP_SYSLOG
38 static int use_syslog = 0;
39
40 static int debug2syslog(int l) {
41         switch (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;
49         }
50         return LOG_DEBUG;
51 }
52 #endif
53
54 static char *lutil_levels[] = {
55         "emergency", "alert", "critical",
56         "error", "warning", "notice",
57         "information", "entry", "args",
58         "results", "detail1", "detail2",
59         NULL };
60
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" };
68
69 int lutil_mnem2subsys( const char *subsys )
70 {
71         int i;
72         for( i = 0; i < LDAP_SUBSYS_NUM; i++ ) {
73                 if ( !strcasecmp( subsys, lutil_subsys[i] ) ) {
74                         return i;
75                 }
76         }
77         return -1;
78 }
79
80 void lutil_set_all_backends( int level )
81 {
82         int i;
83
84         for( i = 0; i < LDAP_SUBSYS_NUM; i++ ) {
85                 if ( !strncasecmp( "back_", lutil_subsys[i], strlen("back_") ) ) {
86                         ldap_loglevels[i] = level;
87                 }
88         }
89 }
90
91 int lutil_mnem2level( const char *level )
92 {
93         int i;
94         for( i = 0; lutil_levels[i] != NULL; i++ ) {
95                 if ( !strcasecmp( level, lutil_levels[i] ) ) {
96                         return i;
97                 }
98         }
99         return -1;
100 }
101
102 static int addSubsys( const char *subsys, int level )
103 {
104         int subsys_num;
105
106         if ( !strcasecmp( subsys, "backend" ) ) {
107                 lutil_set_all_backends( level );
108                 return level;
109
110         } else {
111                 subsys_num = lutil_mnem2subsys(subsys);
112                 if(subsys_num < 0) {
113                         fprintf(stderr, _("Unknown Subsystem name [ %s ] - Discarded\n"), 
114                                 subsys);
115                         fflush(stderr);
116                         return -1;
117                 }
118
119                 ldap_loglevels[subsys_num] = level;
120                 return level;
121         }
122         return -1;
123 }
124
125 int lutil_set_debug_level( const char* subsys, int level )
126 {
127         return( addSubsys( subsys, level ) );
128 }
129
130 int lutil_debug_file( FILE *file )
131 {
132         log_file = file;
133         ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
134
135         return 0;
136 }
137
138 void lutil_log_int(
139         FILE* file,
140         const char *subsys, int level,
141         const char *fmt, va_list vl )
142 {
143 #ifdef HAVE_WINSOCK
144         time_t now;
145         struct tm *today;
146 #endif
147         size_t i;
148         char * t_subsys;
149         char * tmp;
150
151         t_subsys = strdup(subsys);
152         
153         for(tmp = t_subsys, i = 0; i < strlen(t_subsys); i++, tmp++) {
154                 *tmp = TOUPPER( (unsigned char) *tmp );
155         }
156
157 #ifdef LDAP_SYSLOG
158         /* we're configured to use syslog */
159         if( use_syslog ) {
160 #ifdef HAVE_VSYSLOG
161                 vsyslog( debug2syslog(level), fmt, vl );
162 #else
163                 char data[4096];
164                 vsnprintf( data, sizeof(data), fmt, vl );
165                 syslog( debug2syslog(level), data );
166 #endif
167                 return;
168         }
169 #endif
170
171 #if 0
172 #ifdef HAVE_WINSOCK
173         if( log_file == NULL ) {
174                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
175
176                 if ( log_file == NULL ) {
177                         log_file = fopen( "openldap.log", "w" );
178                         if ( log_file == NULL ) return;
179                 }
180
181                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
182         }
183 #endif
184 #endif
185
186         if( file == NULL ) {
187                 /*
188                  * Use stderr unless file was specified via:
189                  *   ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
190                  */
191                 file = stderr;
192         }
193
194 #ifdef HAVE_WINSOCK
195         /*
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.
199          */
200         time( &now );
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 );
206 #endif
207
208         /*
209          * format the output data.
210          */
211
212         fprintf(file, "\n%s:: ", t_subsys ); 
213         vfprintf( file, fmt, vl );
214         fflush( file );
215 }
216
217 /*
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.
221  */
222 void lutil_log( const int subsys, int level, const char *fmt, ... )
223 {
224         FILE* outfile = NULL;
225         va_list vl;
226         va_start( vl, fmt );
227         ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
228         lutil_log_int( outfile, lutil_subsys[subsys], level, fmt, vl );
229         va_end( vl );
230 }
231
232 void lutil_log_initialize(int argc, char **argv)
233 {
234         int i;
235         /*
236          * Start by setting the hook for the libraries to use this logging
237          * routine.
238          */
239         ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
240
241         if ( argc == 0 ) return;
242
243         /*
244          * Now go through the command line options to set the debugging
245          * levels
246          */
247         for( i = 0; i < argc; i++ ) {
248                 char *next = argv[i];
249         
250                 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' ) {
251                         char subsys[64];
252                         int level;
253                         char *optarg = argv[i+1];
254                         char *index = strchr( optarg, '=' );
255
256                         if ( index != NULL ) {
257                                 *index = 0;
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 );
262                                 *index = '=';
263
264                         } else {
265                                 global_level = atoi( optarg );
266                                 ldap_loglevels[0] = global_level;
267                                 /* 
268                                 * if a negative number was used, make the global level the
269                                 * maximum sane level.
270                                 */
271                                 if ( global_level < 0 ) {
272                                         global_level = 65535;
273                                         ldap_loglevels[0] = 65535;
274                                 }
275                         }
276                 }
277         }
278 }
279
280 void (lutil_debug)( int debug, int level, const char *fmt, ... )
281 {
282         char buffer[4096];
283         va_list vl;
284
285         if ( !(level & debug ) ) return;
286
287 #ifdef HAVE_WINSOCK
288         if( log_file == NULL ) {
289                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
290
291                 if ( log_file == NULL ) {
292                         log_file = fopen( "openldap.log", "w" );
293                         if ( log_file == NULL ) return;
294                 }
295
296                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
297         }
298 #endif
299         va_start( vl, fmt );
300
301         vsnprintf( buffer, sizeof(buffer), fmt, vl );
302         buffer[sizeof(buffer)-1] = '\0';
303
304         if( log_file != NULL ) {
305                 fputs( buffer, log_file );
306                 fflush( log_file );
307         }
308
309         fputs( buffer, stderr );
310         va_end( vl );
311 }