00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "logging.h"
00022
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <stdarg.h>
00026 #include <string.h>
00027
00028 static debug_mask_t debug_mask = DBG_CRIT;
00029 static BD_LOG_FUNC log_func = NULL;
00030
00031 void bd_set_debug_handler(BD_LOG_FUNC f)
00032 {
00033 log_func = f;
00034 }
00035
00036 void bd_set_debug_mask(uint32_t mask)
00037 {
00038 debug_mask = mask;
00039 }
00040
00041 uint32_t bd_get_debug_mask(void)
00042 {
00043 return debug_mask;
00044 }
00045
00046 char *print_hex(char *out, const uint8_t *buf, int count)
00047 {
00048 int zz;
00049 for(zz = 0; zz < count; zz++) {
00050 sprintf(out + (zz * 2), "%02X", buf[zz]);
00051 }
00052
00053 return out;
00054 }
00055
00056 void bd_debug(const char *file, int line, uint32_t mask, const char *format, ...)
00057 {
00058 static int debug_init = 0;
00059 static FILE *logfile = NULL;
00060
00061
00062 if (!debug_init) {
00063 debug_init = 1;
00064 logfile = stderr;
00065
00066 char *env = NULL;
00067 if ((env = getenv("BD_DEBUG_MASK")))
00068 debug_mask = strtol(env, NULL, 0);
00069
00070
00071 if ((env = getenv("BD_DEBUG_FILE"))) {
00072 FILE *fp = fopen(env, "wb");
00073 if (fp) {
00074 logfile = fp;
00075 setvbuf(logfile, NULL, _IOLBF, 0);
00076 } else {
00077 fprintf(logfile, "%s:%d: Error opening log file %s\n", __FILE__, __LINE__, env);
00078 }
00079 }
00080 }
00081
00082 if (mask & debug_mask) {
00083 char buffer[512], *pt = buffer;
00084 va_list args;
00085
00086 pt += sprintf(buffer, "%s:%d: ", file, line);
00087
00088 va_start(args, format);
00089 vsprintf(pt, format, args);
00090 va_end(args);
00091
00092 if (log_func) {
00093 log_func(buffer);
00094 } else {
00095 fprintf(logfile, "%s", buffer);
00096 }
00097 }
00098 }