]> git.sur5r.net Git - openldap/blob - libraries/liblber/debug.c
improve parsing - first step
[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
150 #ifdef LDAP_SYSLOG
151         /* we're configured to use syslog */
152         if( use_syslog ) {
153 #ifdef HAVE_VSYSLOG
154                 vsyslog( debug2syslog(level), fmt, vl );
155 #else
156                 char data[4096];
157                 vsnprintf( data, sizeof(data), fmt, vl );
158                 syslog( debug2syslog(level), data );
159 #endif
160                 return;
161         }
162 #endif
163
164 #if 0
165 #ifdef HAVE_WINSOCK
166         if( log_file == NULL ) {
167                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
168
169                 if ( log_file == NULL ) {
170                         log_file = fopen( "openldap.log", "w" );
171                         if ( log_file == NULL ) return;
172                 }
173
174                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
175         }
176 #endif
177 #endif
178
179         if( file == NULL ) {
180                 /*
181                  * Use stderr unless file was specified via:
182                  *   ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
183                  */
184                 file = stderr;
185         }
186
187 #ifdef HAVE_WINSOCK
188         /*
189          * Stick the time in the buffer to output when using Winsock
190          * as NT can't pipe to a timestamp program like Unix can.
191          * This, of course, makes some logs hard to read.
192          */
193         time( &now );
194         today = localtime( &now );
195         fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
196                 today->tm_year + 1900, today->tm_mon + 1,
197                 today->tm_mday, today->tm_hour,
198                 today->tm_min, today->tm_sec );
199 #endif
200
201         /*
202          * format the output data.
203          */
204
205         fprintf(file, "\n%s:: ", subsys ); 
206         vfprintf( file, fmt, vl );
207         fflush( file );
208 }
209
210 /*
211  * The primary logging routine.  Takes the subsystem being logged from, the
212  * level of the log output and the format and data.  Send this on to the
213  * internal routine with the print file, if any.
214  */
215 void lutil_log( const int subsys, int level, const char *fmt, ... )
216 {
217         FILE* outfile = NULL;
218         va_list vl;
219         va_start( vl, fmt );
220         ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
221         lutil_log_int( outfile, debug_subsys[subsys], level, fmt, vl );
222         va_end( vl );
223 }
224
225 void lutil_log_initialize(int argc, char **argv)
226 {
227         int i;
228         /*
229          * Start by setting the hook for the libraries to use this logging
230          * routine.
231          */
232         ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
233
234         if ( argc == 0 ) return;
235
236         /*
237          * Now go through the command line options to set the debugging
238          * levels
239          */
240         for( i = 0; i < argc; i++ ) {
241                 char *next = argv[i];
242         
243                 if ( i < argc-1 && next[0] == '-' && next[1] == 'd' ) {
244                         char subsys[64];
245                         int level;
246                         char *optarg = argv[i+1];
247                         char *index = strchr( optarg, '=' );
248
249                         if ( index != NULL ) {
250                                 *index = 0;
251                                 strcpy ( subsys, optarg );
252                                 level = atoi( index+1 );
253                                 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
254                                 lutil_set_debug_level( subsys, level );
255                                 *index = '=';
256
257                         } else {
258                                 global_level = atoi( optarg );
259                                 ldap_loglevels[0] = global_level;
260                                 /* 
261                                  * if a negative number was used, make the global level the
262                                  * maximum sane level.
263                                  */
264                                 if ( global_level < 0 ) {
265                                         global_level = 65535;
266                                         ldap_loglevels[0] = 65535;
267                                 }
268                         }
269                 }
270         }
271 }
272
273 void (lutil_debug)( int debug, int level, const char *fmt, ... )
274 {
275         char buffer[4096];
276         va_list vl;
277
278         if ( !(level & debug ) ) return;
279
280 #ifdef HAVE_WINSOCK
281         if( log_file == NULL ) {
282                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
283
284                 if ( log_file == NULL ) {
285                         log_file = fopen( "openldap.log", "w" );
286                         if ( log_file == NULL ) return;
287                 }
288
289                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
290         }
291 #endif
292
293         va_start( vl, fmt );
294         vsnprintf( buffer, sizeof(buffer), fmt, vl );
295         buffer[sizeof(buffer)-1] = '\0';
296         if( log_file != NULL ) {
297                 fputs( buffer, log_file );
298                 fflush( log_file );
299         }
300         fputs( buffer, stderr );
301         va_end( vl );
302 }
303
304 #if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG)
305 #undef syslog
306 void eb_syslog( int pri, const char *fmt, ... )
307 {
308         char buffer[4096];
309         va_list vl;
310
311         va_start( vl, fmt );
312         vsnprintf( buffer, sizeof(buffer), fmt, vl );
313         buffer[sizeof(buffer)-1] = '\0';
314
315         /* The syslog function appears to only work with pure EBCDIC */
316         __atoe(buffer);
317 #pragma convlit(suspend)
318         syslog( pri, "%s", buffer );
319 #pragma convlit(resume)
320         va_end( vl );
321 }
322 #endif