]> git.sur5r.net Git - openldap/blob - libraries/liblber/debug.c
Happy New Year
[openldap] / libraries / liblber / debug.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2018 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 static int debug_lastc = '\n';
37
38 int lutil_debug_file( FILE *file )
39 {
40         log_file = file;
41         ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
42
43         return 0;
44 }
45
46 void (lutil_debug)( int debug, int level, const char *fmt, ... )
47 {
48         char buffer[4096];
49         va_list vl;
50         int len, off;
51
52         if ( !(level & debug ) ) return;
53
54 #ifdef HAVE_WINSOCK
55         if( log_file == NULL ) {
56                 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
57
58                 if ( log_file == NULL ) {
59                         log_file = fopen( "openldap.log", "w" );
60                         if ( log_file == NULL ) return;
61                 }
62
63                 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
64         }
65 #endif
66
67         if (debug_lastc == '\n') {
68                 sprintf(buffer, "%08x ", (unsigned) time(0L));
69                 off = 9;
70         } else {
71                 off = 0;
72         }
73         va_start( vl, fmt );
74         len = vsnprintf( buffer+off, sizeof(buffer)-off, fmt, vl );
75         if (len > sizeof(buffer)-off)
76                 len = sizeof(buffer)-off;
77         debug_lastc = buffer[len+off-1];
78         buffer[sizeof(buffer)-1] = '\0';
79         if( log_file != NULL ) {
80                 fputs( buffer, log_file );
81                 fflush( log_file );
82         }
83         fputs( buffer, stderr );
84         va_end( vl );
85 }
86
87 #if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG)
88 #undef syslog
89 void eb_syslog( int pri, const char *fmt, ... )
90 {
91         char buffer[4096];
92         va_list vl;
93
94         va_start( vl, fmt );
95         vsnprintf( buffer, sizeof(buffer), fmt, vl );
96         buffer[sizeof(buffer)-1] = '\0';
97
98         /* The syslog function appears to only work with pure EBCDIC */
99         __atoe(buffer);
100 #pragma convlit(suspend)
101         syslog( pri, "%s", buffer );
102 #pragma convlit(resume)
103         va_end( vl );
104 }
105 #endif