2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
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
];
28 for( i
= 0; i
< 32; i
++ )
30 if( val
& ( 1 << i
) )
37 void vdebug(int dtype
, const char *fmt
, va_list ap
)
42 /* errno could be changed by vsprintf or perror */
47 FILE * channel
= debug_output_channel
[ffs(dtype
)];
53 vsprintf(msgbuff
, fmt
, ap
);
55 vsnprintf(msgbuff
, sizeof(msgbuff
), fmt
, ap
);
58 /* DEBUG_ERROR (aka DEBUG_SYSERROR) */
59 if (dtype
== DEBUG_ERROR
)
61 const char * errmsg
= "";
64 errmsg
= strerror(errno
);
67 fprintf(channel
, "%s: %s\n", msgbuff
, errmsg
);
70 fprintf(channel
, "%s\n", msgbuff
);
74 if (dtype
== DEBUG_SYSERROR
|| dtype
== DEBUG_APPERROR
)
75 MessageBox(NULL
, msgbuff
, "Application Error", MB_OK
);
82 void vmdebug(int dtype
, const char * fmt
, va_list ap
)
84 FILE * chn
[DEBUG_NUM_FACILITIES
];
87 memcpy(chn
, debug_output_channel
, sizeof(FILE*) * DEBUG_NUM_FACILITIES
);
89 for (i
= 0; i
< DEBUG_NUM_FACILITIES
; i
++)
93 for (i
= 0; i
< DEBUG_NUM_FACILITIES
; i
++)
95 if ((dtype
& (1 << i
)) && chn
[i
])
98 if (debuglvl
& (1 << i
))
102 vdebug(1 << i
, fmt
, ap
);
104 for (j
= i
+ 1; j
< DEBUG_NUM_FACILITIES
; j
++)
105 if (chn
[j
] == chn
[i
])
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];
120 if ( !debuglvl
& DEBUG_STATUS
)
125 /* print the heading/banner */
126 vdebug(DEBUG_STATUS
, fmt
, ap
);
128 memset(hexbuff
, 0, 49);
129 memset(printbuff
, 0, 17);
133 sprintf(hexbuff
+ (count
*3), "%02x ", (int)*((unsigned char *)ptr
));
136 printbuff
[count
] = *ptr
;
138 printbuff
[count
] = '.';
145 debug(DEBUG_STATUS
, "%s %s", hexbuff
, printbuff
);
146 memset(hexbuff
, 0, 49);
147 memset(printbuff
, 0, 17);
152 while ( count
% 16 != 0 ) {
153 sprintf(hexbuff
+ (count
* 3), "xx ");
154 printbuff
[count
++] = '.';
156 debug(DEBUG_STATUS
, "%s %s", hexbuff
, printbuff
);
163 to_hex( char* dest
, const char* src
, size_t n
)
167 sprintf( dest
, "%02x ", (int)*((unsigned char *)src
));
175 void debug_set_error_file(FILE *f
)
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
)
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
;