]> git.sur5r.net Git - openldap/blob - libraries/liblutil/debug.c
fix file test
[openldap] / libraries / liblutil / debug.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 #include <stdlib.h>
11
12 #include <ac/stdarg.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15
16 #include "ldap_log.h"
17 #include "ldap_defaults.h"
18 #include "lber.h"
19
20 struct M2S
21 {
22         char *mnemonic;
23         int  subsys;
24 };
25
26 struct DEBUGLEVEL
27 {
28         char *subsystem;
29         int  level;
30 };
31
32 static struct DEBUGLEVEL **levelArray;
33 static long   numLevels = 0;
34
35 static FILE *log_file = NULL;
36 static int global_level = 0;
37
38 #if 0
39 #ifdef LDAP_SYSLOG
40 static int use_syslog = 0;
41
42 static int debug2syslog(int l) {
43         switch (l) {
44         /* insert mapping cases here */
45         default:
46         }
47         return LOG_DEBUG
48 }
49 #endif
50 #endif
51
52 static char *lutil_levels[] = {"emergency", "alert", "critical",
53                            "error", "warning", "notice",
54                            "information", "entry", "args",
55                            "results", "detail1", "detail2",
56                            NULL};
57
58 int lutil_mnem2level( char *level )
59 {
60     int i;
61     for( i = 0; lutil_levels[i] != NULL; i++ )
62     {
63         if ( !strcasecmp( level, lutil_levels[i] ) )
64         {
65             return i;
66         }
67     }
68     return 0;
69 }
70
71 static void addSubsys( const char *subsys, int level )
72 {
73         int i, j;
74         for( i = 0; i < numLevels; i++ )
75         {
76                 if ( levelArray[i] == NULL )
77                 {
78                         levelArray[i] = (struct DEBUGLEVEL*)ber_memalloc( sizeof( struct DEBUGLEVEL ) );
79                         levelArray[i]->subsystem = (char*)ber_memalloc( strlen( subsys ) + 1 );
80                         strcpy ( levelArray[i]->subsystem, subsys );
81                         levelArray[i]->level = level;
82                         return;
83                 }
84                 if( !strcasecmp( subsys, levelArray[i]->subsystem ) )
85                 {
86                         levelArray[i]->level = level;
87                         return;
88                 }
89         }
90         levelArray = (struct DEBUGLEVEL**)ber_memrealloc( levelArray, sizeof( struct DEBUGLEVEL* ) * (numLevels + 10) );
91         for( j = numLevels; j < (numLevels + 10); j++ )
92         {
93                 levelArray[j] = NULL;
94         }
95         numLevels += 10;
96         levelArray[i] = (struct DEBUGLEVEL*)ber_memalloc( sizeof( struct DEBUGLEVEL ) );
97         levelArray[i]->subsystem = (char*)ber_memalloc( strlen( subsys ) + 1 );
98         strcpy( levelArray[i]->subsystem, subsys );
99         levelArray[i]->level = level;
100         return;
101 }
102
103 void lutil_set_debug_level( char* subsys, int level )
104 {
105         addSubsys( subsys, level );
106 }
107
108 int lutil_debug_file( FILE *file )
109 {
110         log_file = file;
111         ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
112
113         return 0;
114 }
115
116 void lutil_log_int(
117         FILE* file,
118         char *subsys, int level,
119         const char *fmt, va_list vl )
120 {
121         time_t now;
122         struct tm *today;
123         int i;
124
125         if ( levelArray == NULL ) return; /* logging isn't set up */
126
127         /*
128          * Look for the subsystem in the level array.  When we find it,
129          * break out of the loop.
130          */
131         for( i = 0; i < numLevels; i++ ) {
132                 if ( levelArray[i] == NULL ) return; 
133                 if ( ! strcasecmp( levelArray[i]->subsystem, subsys ) ) break;
134         }
135
136         /*
137          * If we didn't find the subsystem, or the set level is less than
138          * the requested output level, don't output it.
139          */
140         if ( (level > global_level) &&
141                 ((i > numLevels ) || ( level > levelArray[i]->level )) )
142         {
143                 return;
144         }
145
146 #if 0
147 #ifdef LDAP_SYSLOG
148         /* we're configured to use syslog */
149         if( use_syslog ) {
150                 vsyslog( debug2syslog(level), fmt, vl );
151                 return;
152         }
153 #endif
154 #endif
155
156 #if 0
157 #ifdef HAVE_WINSOCK
158         if( log_file == NULL ) {
159                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
160
161                 if ( log_file == NULL )
162                         log_file = fopen( "openldap.log", "w" );
163
164                 if ( log_file == NULL )
165                         return;
166
167                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
168         }
169 #endif
170 #endif
171
172         if( file == NULL ) {
173                 /*
174                  * Use stderr unless file was specified via:
175                  *   ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
176                  */
177                 file = stderr;
178         }
179
180 #ifdef HAVE_WINSOCK
181         /*
182          * Stick the time in the buffer to output when using Winsock
183          * as NT can't pipe to a timestamp program like Unix can.
184          * This, of course, makes some logs hard to read.
185      */
186         time( &now );
187         today = localtime( &now );
188         fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
189                 today->tm_year + 1900, today->tm_mon + 1,
190                 today->tm_mday, today->tm_hour,
191                 today->tm_min, today->tm_sec );
192 #endif
193
194         /*
195          * format the output data.
196          */
197         vfprintf( file, fmt, vl );
198 }
199
200 /*
201  * The primary logging routine.  Takes the subsystem being logged from, the
202  * level of the log output and the format and data.  Send this on to the
203  * internal routine with the print file, if any.
204  */
205 void lutil_log( char *subsys, int level, const char *fmt, ... )
206 {
207         FILE* outfile = NULL;
208         va_list vl;
209         va_start( vl, fmt );
210         ber_get_option( NULL, LBER_OPT_LOG_PRINT_FILE, &outfile );
211         lutil_log_int( outfile, subsys, level, fmt, vl );
212         va_end( vl );
213 }
214
215 void lutil_log_initialize(int argc, char **argv)
216 {
217     int i;
218     /*
219      * Start by setting the hook for the libraries to use this logging
220      * routine.
221      */
222     ber_set_option( NULL, LBER_OPT_LOG_PROC, (void*)lutil_log_int );
223
224     if ( argc == 0 ) return;
225     /*
226      * Now go through the command line options to set the debugging
227      * levels
228      */
229     for( i = 0; i < argc; i++ )
230     {
231         char *next = argv[i];
232         if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
233         {
234             char subsys[64];
235             int level;
236             char *optarg = argv[i+1];
237             char *index = strchr( optarg, '=' );
238             if ( index != NULL )
239             {
240                 *index = 0;
241                 strcpy ( subsys, optarg );
242                 level = atoi( index+1 );
243                 if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
244                 lutil_set_debug_level( subsys, level );
245                 *index = '=';
246             }
247             else
248             {
249                 global_level = atoi( optarg );
250             }
251         }
252     }
253 }
254
255 void (lutil_debug)( int debug, int level, const char *fmt, ... )
256 {
257         char buffer[4096];
258         va_list vl;
259
260         if ( !(level & debug ) )
261                 return;
262
263 #ifdef HAVE_WINSOCK
264         if( log_file == NULL ) {
265                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
266
267                 if ( log_file == NULL )
268                         log_file = fopen( "openldap.log", "w" );
269
270                 if ( log_file == NULL )
271                         return;
272
273                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
274         }
275 #endif
276         va_start( vl, fmt );
277
278 #ifdef HAVE_VSNPRINTF
279         vsnprintf( buffer, sizeof(buffer), fmt, vl );
280 #else
281         vsprintf( buffer, fmt, vl );
282 #endif
283         buffer[sizeof(buffer)-1] = '\0';
284
285         if( log_file != NULL ) {
286                 fputs( buffer, log_file );
287                 fflush( log_file );
288         }
289
290     fputs( buffer, stderr );
291         va_end( vl );
292 }