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