Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

debug.h

Go to the documentation of this file.
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 /* Debug macros turned on/off using the environment variable DEBUG.  
00011    Set this from the command line as 'DEBUG=foo,bar cinelerra'. Examples:
00012      DEBUG=file.C      -> all debug statements in file.C
00013      DEBUG=func        -> all in functions named 'func'
00014      DEBUG=func*       -> all in functions (or files) starting with 'func' 
00015      DEBUG=file*       -> all in files (or functions) starting with 'file' 
00016      DEBUG=func1,func2 -> either in func1 or in func2
00017      DEBUG="func, file.C" -> starting with func or in file.C
00018      DEBUG=*           -> just print all debug statements
00019    Wildcard character '*' can only go at the end of an identifier.
00020    Whitespace after comma is allowed, but before comma is bad.
00021    Printing can also be controlled at compile time using DEBUG_PRINT_ON/OFF.
00022    Code for debug statements is only compiled "#ifdef DEBUG" at compile.
00023 */
00024    
00025 // NOTE: gcc drops '~' from destructors in __func__, so ~FOO becomes FOO
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 // assert debug warning if test fails 
00064 #define ADEBUG(test, args...)                                          \
00065     if (debug_should_print(__FILE__, __func__)) { \
00066             if (! test) DEBUG_PRINT("ASSERT FAILED (" #test ") " args) \
00067     }
00068 
00069 // do debug statements
00070 #define DDEBUG(actions...)                                             \
00071     if (debug_should_print(__FILE__, __func__)) { \
00072             actions;                                                   \
00073     }
00074 
00075 // print debug statement
00076 #define PDEBUG(format, args...)                                        \
00077     if (debug_should_print(__FILE__, __func__)) { \
00078             DEBUG_PRINT(format, ## args);                              \
00079     }
00080        
00081 // this debug statement (PDEBUG including %p this)
00082 #define TDEBUG(format, args...)                                        \
00083     if (debug_should_print(__FILE__, __func__)) { \
00084             DEBUG_PRINT("%p " format, this, ##args);                   \
00085     }
00086 
00087 #else  /* not DEBUG */
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 /* DEBUG */
00097         
00098 #endif /* DEBUG_H */

Generated on Sun Jan 8 13:26:34 2006 for Guicast-svn by  doxygen 1.4.4