r1053: Add Russian translation.
[cinelerra_cv.git] / guicast / debug.h
blob63b09229f0958780259d986c42cb3236d72e3986
1 #ifndef DEBUG_H
2 #define DEBUG_H
4 #ifdef DEBUG
6 #include <stdio.h>
7 #include <string.h>
8 #include <ctype.h>
10 /* Debug macros turned on/off using the environment variable DEBUG.
11 Set this from the command line as 'DEBUG=foo,bar cinelerra'. Examples:
12 DEBUG=file.C -> all debug statements in file.C
13 DEBUG=func -> all in functions named 'func'
14 DEBUG=func* -> all in functions (or files) starting with 'func'
15 DEBUG=file* -> all in files (or functions) starting with 'file'
16 DEBUG=func1,func2 -> either in func1 or in func2
17 DEBUG="func, file.C" -> starting with func or in file.C
18 DEBUG=* -> just print all debug statements
19 Wildcard character '*' can only go at the end of an identifier.
20 Whitespace after comma is allowed, but before comma is bad.
21 Printing can also be controlled at compile time using DEBUG_PRINT_ON/OFF.
22 Code for debug statements is only compiled "#ifdef DEBUG" at compile.
25 // NOTE: gcc drops '~' from destructors in __func__, so ~FOO becomes FOO
27 static int debug_print_all = 0;
29 static int debug_should_print(const char *file,
30 const char *func)
32 if (debug_print_all) return 1;
34 char *debug = getenv("DEBUG");
35 if (! debug) return 0;
37 char *next = debug;
38 for (char *test = debug; next != NULL; test = next + 1) {
39 next = strchr(test, ',');
40 int length = next ? next - test - 1 : strlen(test) - 1;
42 if (test[length] == '*') {
43 if (! strncmp(test, file, length)) return 1;
44 if (! strncmp(test, func, length)) return 1;
46 else {
47 if (! strncmp(test, file, strlen(file))) return 1;
48 if (! strncmp(test, func, strlen(func))) return 1;
51 if (next) while(isspace(*next)) next++;
54 return 0;
57 #define DEBUG_PRINT_ON() debug_print_all = 1
58 #define DEBUG_PRINT_OFF() debug_print_all = 0
59 #define DEBUG_PRINT(format, args...) \
60 printf("%s:%d %s(): " format "\n", __FILE__, __LINE__, __func__, ## args)
63 // assert debug warning if test fails
64 #define ADEBUG(test, args...) \
65 if (debug_should_print(__FILE__, __func__)) { \
66 if (! test) DEBUG_PRINT("ASSERT FAILED (" #test ") " args) \
69 // do debug statements
70 #define DDEBUG(actions...) \
71 if (debug_should_print(__FILE__, __func__)) { \
72 actions; \
75 // print debug statement
76 #define PDEBUG(format, args...) \
77 if (debug_should_print(__FILE__, __func__)) { \
78 DEBUG_PRINT(format, ## args); \
81 // this debug statement (PDEBUG including %p this)
82 #define TDEBUG(format, args...) \
83 if (debug_should_print(__FILE__, __func__)) { \
84 DEBUG_PRINT("%p " format, this, ##args); \
87 #else /* not DEBUG */
89 #define DEBUG_ON()
90 #define DEBUG_OFF()
91 #define ADEBUG(test, args...)
92 #define DDEBUG(actions...)
93 #define PDEBUG(format, args...)
94 #define TDEBUG(format, args...)
96 #endif /* DEBUG */
98 #endif /* DEBUG_H */