debug.c 2 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2006 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
7
8
9
10
11
12
13
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
14
 */
15
16
17
18
19
20

#include "portable.h"

#include <stdio.h>

#include <ac/stdarg.h>
21
#include <ac/stdlib.h>
22
#include <ac/string.h>
23
#include <ac/time.h>
Julius Enarusai's avatar
   
Julius Enarusai committed
24
#include <ac/ctype.h>
25

Howard Chu's avatar
Howard Chu committed
26
27
28
29
#ifdef LDAP_SYSLOG
#include <ac/syslog.h>
#endif

30
31
#include "ldap_log.h"
#include "ldap_defaults.h"
32
#include "lber.h"
Julius Enarusai's avatar
   
Julius Enarusai committed
33
#include "ldap_pvt.h"
34

Kurt Zeilenga's avatar
Kurt Zeilenga committed
35
36
static FILE *log_file = NULL;

37
38
int lutil_debug_file( FILE *file )
{
39
	log_file = file;
40
	ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
41
42
43
44

	return 0;
}

45
void (lutil_debug)( int debug, int level, const char *fmt, ... )
46
47
48
49
{
	char buffer[4096];
	va_list vl;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
50
	if ( !(level & debug ) ) return;
51
52

#ifdef HAVE_WINSOCK
Ben Collins's avatar
Ben Collins committed
53
	if( log_file == NULL ) {
54
		log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
55

Kurt Zeilenga's avatar
Kurt Zeilenga committed
56
		if ( log_file == NULL ) {
57
			log_file = fopen( "openldap.log", "w" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
58
59
			if ( log_file == NULL ) return;
		}
60

Ben Collins's avatar
Ben Collins committed
61
		ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
62
63
64
	}
#endif

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
65
	va_start( vl, fmt );
66
67
68
69
70
71
	vsnprintf( buffer, sizeof(buffer), fmt, vl );
	buffer[sizeof(buffer)-1] = '\0';
	if( log_file != NULL ) {
		fputs( buffer, log_file );
		fflush( log_file );
	}
Howard Chu's avatar
Howard Chu committed
72
	fputs( buffer, stderr );
73
74
	va_end( vl );
}
75
76

#if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG)
Howard Chu's avatar
Howard Chu committed
77
#undef syslog
78
79
80
81
82
83
84
85
86
void eb_syslog( int pri, const char *fmt, ... )
{
	char buffer[4096];
	va_list vl;

	va_start( vl, fmt );
	vsnprintf( buffer, sizeof(buffer), fmt, vl );
	buffer[sizeof(buffer)-1] = '\0';

Howard Chu's avatar
Howard Chu committed
87
	/* The syslog function appears to only work with pure EBCDIC */
88
	__atoe(buffer);
Howard Chu's avatar
Howard Chu committed
89
#pragma convlit(suspend)
90
	syslog( pri, "%s", buffer );
Howard Chu's avatar
Howard Chu committed
91
#pragma convlit(resume)
92
93
94
	va_end( vl );
}
#endif