FreeBSD isn't evil - just misguided
[cvsps-yd/commitid.git] / cbtcommon / debug.c
blob3f5ac52b55e1e5d5cd111571c40dd0245a46dd0c
1 /*
2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
4 */
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <errno.h>
9 #include <ctype.h>
10 #include <string.h>
12 #include "debug.h"
13 #include "rcsid.h"
15 #ifdef _WIN32
16 #include <windows.h>
17 #endif
19 RCSID("$Id: debug.c,v 1.14 2001/11/29 00:00:30 amb Exp $");
21 unsigned int debuglvl = ~0;
22 static FILE *debug_output_channel[DEBUG_NUM_FACILITIES];
24 #ifdef MACINTOSH
25 int ffs( int val )
27 int i = 0;
28 for( i = 0; i < 32; i++ )
30 if( val & ( 1 << i ) )
31 return i+1;
33 return 0;
35 #endif
37 void vdebug(int dtype, const char *fmt, va_list ap)
39 int keep_errno;
40 char msgbuff[8192];
42 /* errno could be changed by vsprintf or perror */
43 keep_errno = errno;
45 if (debuglvl & dtype)
47 FILE * channel = debug_output_channel[ffs(dtype)];
49 if (!channel)
50 channel = stderr;
52 #ifdef MACINTOSH
53 vsprintf(msgbuff, fmt, ap);
54 #else
55 vsnprintf(msgbuff, sizeof(msgbuff), fmt, ap);
56 #endif
58 /* DEBUG_ERROR (aka DEBUG_SYSERROR) */
59 if (dtype == DEBUG_ERROR)
61 const char * errmsg = "";
63 #ifndef MACINTOSH
64 errmsg = strerror(errno);
65 #endif
67 fprintf(channel, "%s: %s\n", msgbuff, errmsg);
69 else
70 fprintf(channel, "%s\n", msgbuff);
72 fflush(channel);
73 #ifdef _WIN32
74 if (dtype == DEBUG_SYSERROR || dtype == DEBUG_APPERROR)
75 MessageBox(NULL, msgbuff, "Application Error", MB_OK);
76 #endif
79 errno = keep_errno;
82 void vmdebug(int dtype, const char * fmt, va_list ap)
84 FILE * chn[DEBUG_NUM_FACILITIES];
85 int i;
87 memcpy(chn, debug_output_channel, sizeof(FILE*) * DEBUG_NUM_FACILITIES);
89 for (i = 0; i < DEBUG_NUM_FACILITIES; i++)
90 if (chn[i] == NULL)
91 chn[i] = stderr;
93 for (i = 0; i < DEBUG_NUM_FACILITIES; i++)
95 if ((dtype & (1 << i)) && chn[i])
98 if (debuglvl & (1 << i))
100 int j;
102 vdebug(1 << i, fmt, ap);
104 for (j = i + 1; j < DEBUG_NUM_FACILITIES; j++)
105 if (chn[j] == chn[i])
106 chn[j] = NULL;
112 /* FIXME: use actual debug output core routine vdebug... */
113 void hexdump(const char *ptr, int size, const char *fmt, ...)
115 static char hexbuff[49];
116 static char printbuff[17];
117 int count = 0;
118 va_list ap;
120 if ( !debuglvl & DEBUG_STATUS )
121 return;
123 va_start(ap, fmt);
125 /* print the heading/banner */
126 vdebug(DEBUG_STATUS, fmt, ap);
128 memset(hexbuff, 0, 49);
129 memset(printbuff, 0, 17);
131 while (size--)
133 sprintf(hexbuff + (count*3), "%02x ", (int)*((unsigned char *)ptr));
135 if (isprint(*ptr))
136 printbuff[count] = *ptr;
137 else
138 printbuff[count] = '.';
140 ptr++;
142 if ( count++ == 15 )
144 count = 0;
145 debug(DEBUG_STATUS, "%s %s", hexbuff, printbuff);
146 memset(hexbuff, 0, 49);
147 memset(printbuff, 0, 17);
151 if ( count > 0 ) {
152 while ( count % 16 != 0 ) {
153 sprintf(hexbuff + (count * 3), "xx ");
154 printbuff[count++] = '.';
156 debug(DEBUG_STATUS, "%s %s", hexbuff, printbuff);
159 va_end(ap);
162 void
163 to_hex( char* dest, const char* src, size_t n )
165 while ( n-- )
167 sprintf( dest, "%02x ", (int)*((unsigned char *)src));
168 dest += 3;
169 src++;
172 *dest = 0;
175 void debug_set_error_file(FILE *f)
177 int i;
178 for (i = 0; i < DEBUG_NUM_FACILITIES; i++)
179 debug_output_channel[i] = f;
182 void debug_set_error_facility(int fac, FILE * f)
184 int i;
186 for (i = 0; i < DEBUG_NUM_FACILITIES; i++)
187 if (!debug_output_channel[i])
188 debug_output_channel[i] = stderr;
190 debug_output_channel[ffs(fac)] = f;