xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblber/debug.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: debug.c,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/libraries/liblber/debug.c,v 1.21.2.4 2009/01/22 00:00:53 kurt Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2009 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 
18 #include "portable.h"
19 
20 #include <stdio.h>
21 
22 #include <ac/stdarg.h>
23 #include <ac/stdlib.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26 #include <ac/ctype.h>
27 
28 #ifdef LDAP_SYSLOG
29 #include <ac/syslog.h>
30 #endif
31 
32 #include "ldap_log.h"
33 #include "ldap_defaults.h"
34 #include "lber.h"
35 #include "ldap_pvt.h"
36 
37 static FILE *log_file = NULL;
38 
39 int lutil_debug_file( FILE *file )
40 {
41 	log_file = file;
42 	ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
43 
44 	return 0;
45 }
46 
47 void (lutil_debug)( int debug, int level, const char *fmt, ... )
48 {
49 	char buffer[4096];
50 	va_list vl;
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 	va_start( vl, fmt );
68 	vsnprintf( buffer, sizeof(buffer), fmt, vl );
69 	buffer[sizeof(buffer)-1] = '\0';
70 	if( log_file != NULL ) {
71 		fputs( buffer, log_file );
72 		fflush( log_file );
73 	}
74 	fputs( buffer, stderr );
75 	va_end( vl );
76 }
77 
78 #if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG)
79 #undef syslog
80 void eb_syslog( int pri, const char *fmt, ... )
81 {
82 	char buffer[4096];
83 	va_list vl;
84 
85 	va_start( vl, fmt );
86 	vsnprintf( buffer, sizeof(buffer), fmt, vl );
87 	buffer[sizeof(buffer)-1] = '\0';
88 
89 	/* The syslog function appears to only work with pure EBCDIC */
90 	__atoe(buffer);
91 #pragma convlit(suspend)
92 	syslog( pri, "%s", buffer );
93 #pragma convlit(resume)
94 	va_end( vl );
95 }
96 #endif
97