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