Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / debug.h
1 /*
2  * CINELERRA
3  * Copyright (C) 2007-2020 William Morrow
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21
22 #ifndef DEBUG_H
23 #define DEBUG_H
24
25 #undef DEBUG
26 #ifdef DEBUG
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 /* Debug macros turned on/off using the environment variable DEBUG.
33    Set this from the command line as 'DEBUG=foo,bar cinelerra'. Examples:
34      DEBUG=file.C      -> all debug statements in file.C
35      DEBUG=func        -> all in functions named 'func'
36      DEBUG=func*       -> all in functions (or files) starting with 'func'
37      DEBUG=file*       -> all in files (or functions) starting with 'file'
38      DEBUG=func1,func2 -> either in func1 or in func2
39      DEBUG="func, file.C" -> starting with func or in file.C
40      DEBUG=*           -> just print all debug statements
41    Wildcard character '*' can only go at the end of an identifier.
42    Whitespace after comma is allowed, but before comma is bad.
43    Printing can also be controlled at compile time using DEBUG_PRINT_ON/OFF.
44    Code for debug statements is only compiled "#ifdef DEBUG" at compile.
45 */
46
47 // NOTE: gcc drops '~' from destructors in __func__, so ~FOO becomes FOO
48
49 static int debug_print_all = 0;
50
51 static int debug_should_print(const char *file,
52                               const char *func)
53 {
54         if (debug_print_all) return 1;
55
56         char *debug = getenv("DEBUG");
57         if (! debug) return 0;
58
59         char *next = debug;
60         for (char *test = debug; next != NULL; test = next + 1) {
61                 next = strchr(test, ',');
62                 int length = next ? next - test - 1 : strlen(test) - 1;
63
64                 if (test[length] == '*') {
65                         if (! strncmp(test, file, length)) return 1;
66                         if (! strncmp(test, func, length)) return 1;
67                 }
68                 else {
69                         if (! strncmp(test, file, strlen(file))) return 1;
70                         if (! strncmp(test, func, strlen(func))) return 1;
71                 }
72
73                 if (next) while(isspace(*next)) next++;
74         }
75
76         return 0;
77 }
78
79 #define DEBUG_PRINT_ON() debug_print_all = 1
80 #define DEBUG_PRINT_OFF() debug_print_all = 0
81 #define DEBUG_PRINT(format, args...)                                   \
82     printf("%s:%d %s(): " format "\n", __FILE__, __LINE__, __func__, ## args)
83
84
85 // assert debug warning if test fails
86 #define ADEBUG(test, args...)                                          \
87     if (debug_should_print(__FILE__, __func__)) { \
88             if (! test) DEBUG_PRINT("ASSERT FAILED (" #test ") " args) \
89     }
90
91 // do debug statements
92 #define DDEBUG(actions...)                                             \
93     if (debug_should_print(__FILE__, __func__)) { \
94             actions;                                                   \
95     }
96
97 // print debug statement
98 #define PDEBUG(format, args...)                                        \
99     if (debug_should_print(__FILE__, __func__)) { \
100             DEBUG_PRINT(format, ## args);                              \
101     }
102
103 // this debug statement (PDEBUG including %p this)
104 #define TDEBUG(format, args...)                                        \
105     if (debug_should_print(__FILE__, __func__)) { \
106             DEBUG_PRINT("%p " format, this, ##args);                   \
107     }
108
109 #else  /* not DEBUG */
110
111 #define DEBUG_ON()
112 #define DEBUG_OFF()
113 #define ADEBUG(test, args...)
114 #define DDEBUG(actions...)
115 #define PDEBUG(format, args...)
116 #define TDEBUG(format, args...)
117
118 #endif /* DEBUG */
119
120 #endif /* DEBUG_H */