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