1 /* $NetBSD: debug.c,v 1.1.1.4 2014/05/28 09:58:40 tron Exp $ */ 2 3 /* $OpenLDAP$ */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2014 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 sprintf(buffer, "%08x ", (unsigned) time(0L)); 68 va_start( vl, fmt ); 69 vsnprintf( buffer+9, sizeof(buffer)-9, fmt, vl ); 70 buffer[sizeof(buffer)-1] = '\0'; 71 if( log_file != NULL ) { 72 fputs( buffer, log_file ); 73 fflush( log_file ); 74 } 75 fputs( buffer, stderr ); 76 va_end( vl ); 77 } 78 79 #if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG) 80 #undef syslog 81 void eb_syslog( int pri, const char *fmt, ... ) 82 { 83 char buffer[4096]; 84 va_list vl; 85 86 va_start( vl, fmt ); 87 vsnprintf( buffer, sizeof(buffer), fmt, vl ); 88 buffer[sizeof(buffer)-1] = '\0'; 89 90 /* The syslog function appears to only work with pure EBCDIC */ 91 __atoe(buffer); 92 #pragma convlit(suspend) 93 syslog( pri, "%s", buffer ); 94 #pragma convlit(resume) 95 va_end( vl ); 96 } 97 #endif 98