3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdarg.h>
13 #include <ac/string.h>
18 * We don't just set ber_pvt_err_file to stderr here, because in NT,
19 * stderr is a symbol imported from a DLL. As such, the compiler
20 * doesn't recognize the symbol as having a constant address. Thus
21 * we set ber_pvt_err_file to stderr later, when it first gets
24 FILE *ber_pvt_err_file;
29 BER_ERRNO_FN ber_int_errno_fn;
31 int * ber_errno_addr(void)
33 static int ber_int_errno = LBER_ERROR_NONE;
35 if( ber_int_errno_fn ) {
36 return (*ber_int_errno_fn)();
39 return &ber_int_errno;
46 ber_error_print( char *data )
48 assert( data != NULL );
50 if (!ber_pvt_err_file)
51 ber_pvt_err_file = stderr;
53 fputs( data, ber_pvt_err_file );
55 /* Print to both streams */
56 if (ber_pvt_err_file != stderr)
58 fputs( data, stderr );
62 fflush( ber_pvt_err_file );
65 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
71 static int ber_log_check( int errlvl, int loglvl )
73 return errlvl & loglvl ? 1 : 0;
76 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
81 assert( fmt != NULL );
83 if ( !ber_log_check( errlvl, loglvl )) {
90 buf[sizeof(buf) - 1] = '\0';
91 vsnprintf( buf, sizeof(buf)-1, fmt, ap );
93 vsprintf( buf, fmt, ap ); /* hope it's not too long */
96 #error "vsprintf() required."
101 (*ber_pvt_log_print)( buf );
105 static int ber_log_puts(int errlvl, int loglvl, char *buf)
107 assert( buf != NULL );
109 if ( !ber_log_check( errlvl, loglvl )) {
113 (*ber_pvt_log_print)( buf );
118 * Print arbitrary stuff, for debugging.
122 ber_log_bprint(int errlvl,
127 assert( data != NULL );
129 if ( !ber_log_check( errlvl, loglvl )) {
133 ber_bprint(data, len);
139 LDAP_CONST char *data,
142 static const char hexdig[] = "0123456789abcdef";
145 char buf[ BPLEN + sizeof("\t%s\n") ];
148 assert( data != NULL );
150 memset( out, 0, BPLEN );
153 sprintf( buf, "\t%s\n", ( i == 0 ) ? "(end)" : out );
154 (*ber_pvt_log_print)( buf );
159 if ( isgraph( (unsigned char)*data )) {
164 out[ i ] = hexdig[ ( *data & 0xf0U ) >> 4 ];
165 out[ i+1 ] = hexdig[ *data & 0x0fU ];
173 if ( i > BPLEN - 2 ) {
174 char data[128 + BPLEN];
175 sprintf( data, "\t%s\n", out );
176 (*ber_pvt_log_print)(data);
177 memset( out, 0, BPLEN );
192 assert( ber != NULL );
193 assert( BER_VALID( ber ) );
195 if ( !ber_log_check( errlvl, loglvl )) {
199 ber_dump(ber, inout);
210 assert( ber != NULL );
211 assert( BER_VALID( ber ) );
213 sprintf( buf, "ber_dump: buf 0x%lx, ptr 0x%lx, end 0x%lx\n",
216 (long) ber->ber_end );
218 (*ber_pvt_log_print)( buf );
221 sprintf( buf, " current len %ld, contents:\n",
222 (long) (ber->ber_end - ber->ber_ptr) );
223 ber_bprint( ber->ber_ptr, ber->ber_end - ber->ber_ptr );
226 sprintf( buf, " current len %ld, contents:\n",
227 (long) (ber->ber_ptr - ber->ber_buf) );
229 ber_bprint( ber->ber_buf, ber->ber_ptr - ber->ber_buf );
239 assert( sos != NULL );
241 if ( !ber_log_check( errlvl, loglvl )) {
255 assert( sos != NULL );
257 (*ber_pvt_log_print)( "*** sos dump ***\n" );
259 while ( sos != NULL ) {
260 sprintf( buf, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n",
261 (long) sos->sos_clen,
262 (long) sos->sos_first,
263 (long) sos->sos_ptr );
264 (*ber_pvt_log_print)( buf );
266 sprintf( buf, " current len %ld contents:\n",
267 (long) (sos->sos_ptr - sos->sos_first) );
268 (*ber_pvt_log_print)( buf );
270 ber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first );
275 (*ber_pvt_log_print)( "*** end dump ***\n" );