From c000cad09d0b54c455c99271bfb996c2dfe13073 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 6 May 2024 03:23:53 +0200 Subject: Adding upstream version 2.4.47+dfsg. Signed-off-by: Daniel Baumann --- libraries/liblber/debug.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 libraries/liblber/debug.c (limited to 'libraries/liblber/debug.c') diff --git a/libraries/liblber/debug.c b/libraries/liblber/debug.c new file mode 100644 index 0000000..31f6bd8 --- /dev/null +++ b/libraries/liblber/debug.c @@ -0,0 +1,105 @@ +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . + * + * Copyright 1998-2018 The OpenLDAP Foundation. + * 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 + * . + */ + +#include "portable.h" + +#include + +#include +#include +#include +#include +#include + +#ifdef LDAP_SYSLOG +#include +#endif + +#include "ldap_log.h" +#include "ldap_defaults.h" +#include "lber.h" +#include "ldap_pvt.h" + +static FILE *log_file = NULL; +static int debug_lastc = '\n'; + +int lutil_debug_file( FILE *file ) +{ + log_file = file; + ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file ); + + return 0; +} + +void (lutil_debug)( int debug, int level, const char *fmt, ... ) +{ + char buffer[4096]; + va_list vl; + int len, off; + + if ( !(level & debug ) ) return; + +#ifdef HAVE_WINSOCK + if( log_file == NULL ) { + log_file = fopen( LDAP_RUNDIR LDAP_DIRSEP "openldap.log", "w" ); + + if ( log_file == NULL ) { + log_file = fopen( "openldap.log", "w" ); + if ( log_file == NULL ) return; + } + + ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, log_file ); + } +#endif + + if (debug_lastc == '\n') { + sprintf(buffer, "%08x ", (unsigned) time(0L)); + off = 9; + } else { + off = 0; + } + va_start( vl, fmt ); + len = vsnprintf( buffer+off, sizeof(buffer)-off, fmt, vl ); + if (len > sizeof(buffer)-off) + len = sizeof(buffer)-off; + debug_lastc = buffer[len+off-1]; + buffer[sizeof(buffer)-1] = '\0'; + if( log_file != NULL ) { + fputs( buffer, log_file ); + fflush( log_file ); + } + fputs( buffer, stderr ); + va_end( vl ); +} + +#if defined(HAVE_EBCDIC) && defined(LDAP_SYSLOG) +#undef syslog +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'; + + /* The syslog function appears to only work with pure EBCDIC */ + __atoe(buffer); +#pragma convlit(suspend) + syslog( pri, "%s", buffer ); +#pragma convlit(resume) + va_end( vl ); +} +#endif -- cgit v1.2.3