1 /* $NetBSD: debug.c,v 1.1.1.7 2019/08/08 13:31:12 christos Exp $ */ 2 3 /* $OpenLDAP$ */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2019 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 <sys/cdefs.h> 19 __RCSID("$NetBSD: debug.c,v 1.1.1.7 2019/08/08 13:31:12 christos Exp $"); 20 21 #include "portable.h" 22 23 #include <stdio.h> 24 25 #include <ac/stdarg.h> 26 #include <ac/stdlib.h> 27 #include <ac/string.h> 28 #include <ac/time.h> 29 #include <ac/ctype.h> 30 31 #ifdef LDAP_SYSLOG 32 #include <ac/syslog.h> 33 #endif 34 35 #include "ldap_log.h" 36 #include "ldap_defaults.h" 37 #include "lber.h" 38 #include "ldap_pvt.h" 39 40 static FILE *log_file = NULL; 41 static int debug_lastc = '\n'; 42 43 int lutil_debug_file( FILE *file ) 44 { 45 log_file = file; 46 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file ); 47 48 return 0; 49 } 50 51 void (lutil_debug)( int debug, int level, const char *fmt, ... ) 52 { 53 char buffer[4096]; 54 va_list vl; 55 int len, off; 56 57 if ( !(level & debug ) ) return; 58 59 #ifdef HAVE_WINSOCK 60 if( log_file == NULL ) { 61 log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" ); 62 63 if ( log_file == NULL ) { 64 log_file = fopen( "openldap.log", "w" ); 65 if ( log_file == NULL ) return; 66 } 67 68 ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file ); 69 } 70 #endif 71 72 if (debug_lastc == '\n') { 73 sprintf(buffer, "%08x ", (unsigned) time(0L)); 74 off = 9; 75 } else { 76 off = 0; 77 } 78 va_start( vl, fmt ); 79 len = vsnprintf( buffer+off, sizeof(buffer)-off, fmt, vl ); 80 if (len > sizeof(buffer)-off) 81 len = sizeof(buffer)-off; 82 debug_lastc = buffer[len+off-1]; 83 buffer[sizeof(buffer)-1] = '\0'; 84 if( log_file != NULL ) { 85 fputs( buffer, log_file ); 86 fflush( log_file ); 87 } 88 fputs( buffer, stderr ); 89 va_end( vl ); 90 } 91 92 #if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG) 93 #undef syslog 94 void eb_syslog( int pri, const char *fmt, ... ) 95 { 96 char buffer[4096]; 97 va_list vl; 98 99 va_start( vl, fmt ); 100 vsnprintf( buffer, sizeof(buffer), fmt, vl ); 101 buffer[sizeof(buffer)-1] = '\0'; 102 103 /* The syslog function appears to only work with pure EBCDIC */ 104 __atoe(buffer); 105 #pragma convlit(suspend) 106 syslog( pri, "%s", buffer ); 107 #pragma convlit(resume) 108 va_end( vl ); 109 } 110 #endif 111