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>
17 typedef void (*BER_LOG_FN) LDAP_P((FILE *file, char *subsys, int level, const char *fmt, va_list vl));
19 static int ber_log_check( int errlvl, int loglvl );
21 BER_LOG_FN ber_int_log_proc = NULL;
24 * We don't just set ber_pvt_err_file to stderr here, because in NT,
25 * stderr is a symbol imported from a DLL. As such, the compiler
26 * doesn't recognize the symbol as having a constant address. Thus
27 * we set ber_pvt_err_file to stderr later, when it first gets
30 FILE *ber_pvt_err_file = NULL;
35 BER_ERRNO_FN ber_int_errno_fn = NULL;
37 int * ber_errno_addr(void)
39 static int ber_int_errno = LBER_ERROR_NONE;
41 if( ber_int_errno_fn ) {
42 return (*ber_int_errno_fn)();
45 return &ber_int_errno;
51 void ber_error_print( LDAP_CONST char *data )
53 assert( data != NULL );
55 if (!ber_pvt_err_file) ber_pvt_err_file = stderr;
57 fputs( data, ber_pvt_err_file );
59 /* Print to both streams */
60 if (ber_pvt_err_file != stderr) {
61 fputs( data, stderr );
65 fflush( ber_pvt_err_file );
68 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
74 int ber_pvt_log_output( char *subsystem, int level, const char *fmt, ... )
80 if ( ber_int_log_proc != NULL )
82 ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl );
87 ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level );
89 buf[sizeof(buf) - 1] = '\0';
90 vsnprintf( buf, sizeof(buf)-1, fmt, vl );
92 vsprintf( buf, fmt, vl ); /* hope it's not too long */
95 #error "vsprintf() required.";
97 if ( ber_log_check( LDAP_DEBUG_BER, level ) )
98 (*ber_pvt_log_print)( buf );
105 static int ber_log_check( int errlvl, int loglvl )
107 return errlvl & loglvl ? 1 : 0;
110 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
115 assert( fmt != NULL );
117 if ( !ber_log_check( errlvl, loglvl )) {
123 #ifdef HAVE_VSNPRINTF
124 buf[sizeof(buf) - 1] = '\0';
125 vsnprintf( buf, sizeof(buf)-1, fmt, ap );
127 vsprintf( buf, fmt, ap ); /* hope it's not too long */
130 #error "vsprintf() required."
135 (*ber_pvt_log_print)( buf );
140 static int ber_log_puts(int errlvl, int loglvl, char *buf)
142 assert( buf != NULL );
144 if ( !ber_log_check( errlvl, loglvl )) {
148 (*ber_pvt_log_print)( buf );
154 * Print arbitrary stuff, for debugging.
158 ber_log_bprint(int errlvl,
163 assert( data != NULL );
165 if ( !ber_log_check( errlvl, loglvl )) {
169 ber_bprint(data, len);
175 LDAP_CONST char *data,
178 static const char hexdig[] = "0123456789abcdef";
185 assert( data != NULL );
187 /* in case len is zero */
191 for ( i = 0 ; i < len ; i++ ) {
196 if( i ) (*ber_pvt_log_print)( line );
197 memset( line, ' ', sizeof(line)-2 );
198 line[sizeof(line)-2] = '\n';
199 line[sizeof(line)-1] = '\0';
203 line[ 2 ] = hexdig[ 0x0f & (off >> 12) ];
204 line[ 3 ] = hexdig[ 0x0f & (off >> 8) ];
205 line[ 4 ] = hexdig[ 0x0f & (off >> 4) ];
206 line[ 5 ] = hexdig[ 0x0f & off ];
210 off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
211 line[ off ] = hexdig[ 0x0f & ( data[i] >> 4 ) ];
212 line[ off+1 ] = hexdig[ 0x0f & data[i] ];
214 off = BP_GRAPH + n + ((n >= 8)?1:0);
216 if ( isprint( data[i] )) {
217 line[ BP_GRAPH + n ] = data[i];
219 line[ BP_GRAPH + n ] = '.';
223 (*ber_pvt_log_print)( line );
233 static const char hexdig[] = "0123456789abcdef";
238 char *data = ber->ber_ptr;
241 len = ber_pvt_ber_remaining(ber);
243 len = ber_pvt_ber_write(ber);
246 sprintf( buf, "ber_dump: buf=0x%08lx ptr=0x%08lx end=0x%08lx len=%ld\n",
252 ber_pvt_log_output( subsys, level, "%s", buf );
258 assert( data != NULL );
260 /* in case len is zero */
264 for ( i = 0 ; i < len ; i++ ) {
269 if( i ) ber_pvt_log_output( subsys, level, "%s", line );
270 memset( line, ' ', sizeof(line)-2 );
271 line[sizeof(line)-2] = '\n';
272 line[sizeof(line)-1] = '\0';
276 line[ 2 ] = hexdig[ 0x0f & (off >> 12) ];
277 line[ 3 ] = hexdig[ 0x0f & (off >> 8) ];
278 line[ 4 ] = hexdig[ 0x0f & (off >> 4) ];
279 line[ 5 ] = hexdig[ 0x0f & off ];
283 off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
284 line[ off ] = hexdig[ 0x0f & ( data[i] >> 4 ) ];
285 line[ off+1 ] = hexdig[ 0x0f & data[i] ];
287 off = BP_GRAPH + n + ((n >= 8)?1:0);
289 if ( isprint( data[i] )) {
290 line[ BP_GRAPH + n ] = data[i];
292 line[ BP_GRAPH + n ] = '.';
296 ber_pvt_log_output( subsys, level, "%s", line );
307 assert( ber != NULL );
308 assert( BER_VALID( ber ) );
310 if ( !ber_log_check( errlvl, loglvl )) {
314 ber_dump(ber, inout);
326 assert( ber != NULL );
327 assert( BER_VALID( ber ) );
330 len = ber_pvt_ber_remaining(ber);
332 len = ber_pvt_ber_write(ber);
335 sprintf( buf, "ber_dump: buf=0x%08lx ptr=0x%08lx end=0x%08lx len=%ld\n",
341 (*ber_pvt_log_print)( buf );
343 ber_bprint( ber->ber_ptr, len );
352 assert( sos != NULL );
354 if ( !ber_log_check( errlvl, loglvl )) {
368 assert( sos != NULL );
370 (*ber_pvt_log_print)( "*** sos dump ***\n" );
372 while ( sos != NULL ) {
373 sprintf( buf, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n",
374 (long) sos->sos_clen,
375 (long) sos->sos_first,
376 (long) sos->sos_ptr );
377 (*ber_pvt_log_print)( buf );
379 sprintf( buf, " current len %ld contents:\n",
380 (long) (sos->sos_ptr - sos->sos_first) );
381 (*ber_pvt_log_print)( buf );
383 ber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first );
388 (*ber_pvt_log_print)( "*** end dump ***\n" );