00001 #ifndef DEBUG_H
00002 #define DEBUG_H
00003
00004 #ifdef DEBUG
00005
00006 #include <stdio.h>
00007 #include <string.h>
00008 #include <ctype.h>
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 static int debug_print_all = 0;
00028
00029 static int debug_should_print(const char *file,
00030 const char *func)
00031 {
00032 if (debug_print_all) return 1;
00033
00034 char *debug = getenv("DEBUG");
00035 if (! debug) return 0;
00036
00037 char *next = debug;
00038 for (char *test = debug; next != NULL; test = next + 1) {
00039 next = strchr(test, ',');
00040 int length = next ? next - test - 1 : strlen(test) - 1;
00041
00042 if (test[length] == '*') {
00043 if (! strncmp(test, file, length)) return 1;
00044 if (! strncmp(test, func, length)) return 1;
00045 }
00046 else {
00047 if (! strncmp(test, file, strlen(file))) return 1;
00048 if (! strncmp(test, func, strlen(func))) return 1;
00049 }
00050
00051 if (next) while(isspace(*next)) next++;
00052 }
00053
00054 return 0;
00055 }
00056
00057 #define DEBUG_PRINT_ON() debug_print_all = 1
00058 #define DEBUG_PRINT_OFF() debug_print_all = 0
00059 #define DEBUG_PRINT(format, args...) \
00060 printf("%s:%d %s(): " format "\n", __FILE__, __LINE__, __func__, ## args)
00061
00062
00063
00064 #define ADEBUG(test, args...) \
00065 if (debug_should_print(__FILE__, __func__)) { \
00066 if (! test) DEBUG_PRINT("ASSERT FAILED (" #test ") " args) \
00067 }
00068
00069
00070 #define DDEBUG(actions...) \
00071 if (debug_should_print(__FILE__, __func__)) { \
00072 actions; \
00073 }
00074
00075
00076 #define PDEBUG(format, args...) \
00077 if (debug_should_print(__FILE__, __func__)) { \
00078 DEBUG_PRINT(format, ## args); \
00079 }
00080
00081
00082 #define TDEBUG(format, args...) \
00083 if (debug_should_print(__FILE__, __func__)) { \
00084 DEBUG_PRINT("%p " format, this, ##args); \
00085 }
00086
00087 #else
00088
00089 #define DEBUG_ON()
00090 #define DEBUG_OFF()
00091 #define ADEBUG(test, args...)
00092 #define DDEBUG(actions...)
00093 #define PDEBUG(format, args...)
00094 #define TDEBUG(format, args...)
00095
00096 #endif
00097
00098 #endif