]> git.sur5r.net Git - openldap/blob - libraries/liblber/debug.c
More NEW_LOGGING removal
[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 static FILE *log_file = NULL;
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 int lutil_debug_file( FILE *file )
55 {
56         log_file = file;
57         ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
58
59         return 0;
60 }
61
62 void (lutil_debug)( int debug, int level, const char *fmt, ... )
63 {
64         char buffer[4096];
65         va_list vl;
66
67         if ( !(level & debug ) ) return;
68
69 #ifdef HAVE_WINSOCK
70         if( log_file == NULL ) {
71                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
72
73                 if ( log_file == NULL ) {
74                         log_file = fopen( "openldap.log", "w" );
75                         if ( log_file == NULL ) return;
76                 }
77
78                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
79         }
80 #endif
81
82         va_start( vl, fmt );
83         vsnprintf( buffer, sizeof(buffer), fmt, vl );
84         buffer[sizeof(buffer)-1] = '\0';
85         if( log_file != NULL ) {
86                 fputs( buffer, log_file );
87                 fflush( log_file );
88         }
89         fputs( buffer, stderr );
90         va_end( vl );
91 }
92
93 #if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG)
94 #undef syslog
95 void eb_syslog( int pri, const char *fmt, ... )
96 {
97         char buffer[4096];
98         va_list vl;
99
100         va_start( vl, fmt );
101         vsnprintf( buffer, sizeof(buffer), fmt, vl );
102         buffer[sizeof(buffer)-1] = '\0';
103
104         /* The syslog function appears to only work with pure EBCDIC */
105         __atoe(buffer);
106 #pragma convlit(suspend)
107         syslog( pri, "%s", buffer );
108 #pragma convlit(resume)
109         va_end( vl );
110 }
111 #endif