Back to index

openldap  2.4.31
debug.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 #include "portable.h"
00017 
00018 #include <stdio.h>
00019 
00020 #include <ac/stdarg.h>
00021 #include <ac/stdlib.h>
00022 #include <ac/string.h>
00023 #include <ac/time.h>
00024 #include <ac/ctype.h>
00025 
00026 #ifdef LDAP_SYSLOG
00027 #include <ac/syslog.h>
00028 #endif
00029 
00030 #include "ldap_log.h"
00031 #include "ldap_defaults.h"
00032 #include "lber.h"
00033 #include "ldap_pvt.h"
00034 
00035 static FILE *log_file = NULL;
00036 
00037 int lutil_debug_file( FILE *file )
00038 {
00039        log_file = file;
00040        ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
00041 
00042        return 0;
00043 }
00044 
00045 void (lutil_debug)( int debug, int level, const char *fmt, ... )
00046 {
00047        char buffer[4096];
00048        va_list vl;
00049 
00050        if ( !(level & debug ) ) return;
00051 
00052 #ifdef HAVE_WINSOCK
00053        if( log_file == NULL ) {
00054               log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
00055 
00056               if ( log_file == NULL ) {
00057                      log_file = fopen( "openldap.log", "w" );
00058                      if ( log_file == NULL ) return;
00059               }
00060 
00061               ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
00062        }
00063 #endif
00064 
00065        sprintf(buffer, "%08x ", (unsigned) time(0L));
00066        va_start( vl, fmt );
00067        vsnprintf( buffer+9, sizeof(buffer)-9, fmt, vl );
00068        buffer[sizeof(buffer)-1] = '\0';
00069        if( log_file != NULL ) {
00070               fputs( buffer, log_file );
00071               fflush( log_file );
00072        }
00073        fputs( buffer, stderr );
00074        va_end( vl );
00075 }
00076 
00077 #if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG)
00078 #undef syslog
00079 void eb_syslog( int pri, const char *fmt, ... )
00080 {
00081        char buffer[4096];
00082        va_list vl;
00083 
00084        va_start( vl, fmt );
00085        vsnprintf( buffer, sizeof(buffer), fmt, vl );
00086        buffer[sizeof(buffer)-1] = '\0';
00087 
00088        /* The syslog function appears to only work with pure EBCDIC */
00089        __atoe(buffer);
00090 #pragma convlit(suspend)
00091        syslog( pri, "%s", buffer );
00092 #pragma convlit(resume)
00093        va_end( vl );
00094 }
00095 #endif