debug.c 2.24 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/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1998-2021 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"
33

Kurt Zeilenga's avatar
Kurt Zeilenga committed
34
static FILE *log_file = NULL;
35
static int debug_lastc = '\n';
Kurt Zeilenga's avatar
Kurt Zeilenga committed
36

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
{
	char buffer[4096];
	va_list vl;
49
	int len, off;
50

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

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

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

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

66
67
68
69
70
71
	if (debug_lastc == '\n') {
		sprintf(buffer, "%08x ", (unsigned) time(0L));
		off = 9;
	} else {
		off = 0;
	}
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
72
	va_start( vl, fmt );
73
74
75
76
	len = vsnprintf( buffer+off, sizeof(buffer)-off, fmt, vl );
	if (len > sizeof(buffer)-off)
		len = sizeof(buffer)-off;
	debug_lastc = buffer[len+off-1];
77
78
79
80
81
	buffer[sizeof(buffer)-1] = '\0';
	if( log_file != NULL ) {
		fputs( buffer, log_file );
		fflush( log_file );
	}
Howard Chu's avatar
Howard Chu committed
82
	fputs( buffer, stderr );
83
84
	va_end( vl );
}
85
86

#if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG)
Howard Chu's avatar
Howard Chu committed
87
#undef syslog
88
89
90
91
92
93
94
95
96
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
97
	/* The syslog function appears to only work with pure EBCDIC */
98
	__atoe(buffer);
Howard Chu's avatar
Howard Chu committed
99
#pragma convlit(suspend)
100
	syslog( pri, "%s", buffer );
Howard Chu's avatar
Howard Chu committed
101
#pragma convlit(resume)
102
103
104
	va_end( vl );
}
#endif