openldap/libraries/liblber/debug.c

112 lines
2.4 KiB
C
Raw Normal View History

/* $OpenLDAP$ */
2003-11-26 07:17:08 +08:00
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
2005-01-02 04:49:32 +08:00
* Copyright 1998-2005 The OpenLDAP Foundation.
2003-11-26 07:17:08 +08:00
* 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>.
*/
#include "portable.h"
#include <stdio.h>
#include <ac/stdarg.h>
#include <ac/stdlib.h>
#include <ac/string.h>
2000-10-13 04:01:12 +08:00
#include <ac/time.h>
#include <ac/ctype.h>
2002-05-11 01:48:54 +08:00
#ifdef LDAP_SYSLOG
#include <ac/syslog.h>
#endif
#include "ldap_log.h"
#include "ldap_defaults.h"
#include "lber.h"
#include "ldap_pvt.h"
2000-10-18 08:29:21 +08:00
static FILE *log_file = NULL;
#ifdef LDAP_SYSLOG
static int use_syslog = 0;
static int debug2syslog(int l) {
switch (l) {
2003-07-08 11:46:20 +08:00
case LDAP_LEVEL_EMERG: return LOG_EMERG;
case LDAP_LEVEL_ALERT: return LOG_ALERT;
case LDAP_LEVEL_CRIT: return LOG_CRIT;
case LDAP_LEVEL_ERR: return LOG_ERR;
case LDAP_LEVEL_WARNING: return LOG_WARNING;
case LDAP_LEVEL_NOTICE: return LOG_NOTICE;
case LDAP_LEVEL_INFO: return LOG_INFO;
2000-10-18 08:29:21 +08:00
}
2002-05-11 01:48:54 +08:00
return LOG_DEBUG;
2000-10-18 08:29:21 +08:00
}
#endif
2000-10-13 04:01:12 +08:00
int lutil_debug_file( FILE *file )
{
1999-09-02 15:40:25 +08:00
log_file = file;
ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file );
return 0;
}
2000-09-15 11:48:27 +08:00
void (lutil_debug)( int debug, int level, const char *fmt, ... )
{
char buffer[4096];
va_list vl;
2003-07-08 11:46:20 +08:00
if ( !(level & debug ) ) return;
#ifdef HAVE_WINSOCK
2000-09-15 11:08:05 +08:00
if( log_file == NULL ) {
1999-06-22 06:59:23 +08:00
log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" );
2003-07-08 11:46:20 +08:00
if ( log_file == NULL ) {
1999-06-22 06:59:23 +08:00
log_file = fopen( "openldap.log", "w" );
2003-07-08 11:46:20 +08:00
if ( log_file == NULL ) return;
}
2000-09-15 11:08:05 +08:00
ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file );
}
#endif
2003-12-07 13:12:36 +08:00
va_start( vl, fmt );
vsnprintf( buffer, sizeof(buffer), fmt, vl );
buffer[sizeof(buffer)-1] = '\0';
if( log_file != NULL ) {
fputs( buffer, log_file );
fflush( log_file );
}
2002-07-26 10:56:01 +08:00
fputs( buffer, stderr );
va_end( vl );
}
2004-02-03 11:58:38 +08:00
#if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG)
2004-02-05 06:53:17 +08:00
#undef syslog
2004-02-03 11:58:38 +08:00
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';
2004-02-05 06:53:17 +08:00
/* The syslog function appears to only work with pure EBCDIC */
2004-02-03 11:58:38 +08:00
__atoe(buffer);
2004-02-05 06:53:17 +08:00
#pragma convlit(suspend)
2004-02-03 11:58:38 +08:00
syslog( pri, "%s", buffer );
2004-02-05 06:53:17 +08:00
#pragma convlit(resume)
2004-02-03 11:58:38 +08:00
va_end( vl );
}
#endif