3 * Copyright (C) 2007-2020 William Morrow
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.
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.
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
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.
47 // NOTE: gcc drops '~' from destructors in __func__, so ~FOO becomes FOO
49 static int debug_print_all = 0;
51 static int debug_should_print(const char *file,
54 if (debug_print_all) return 1;
56 char *debug = getenv("DEBUG");
57 if (! debug) return 0;
60 for (char *test = debug; next != NULL; test = next + 1) {
61 next = strchr(test, ',');
62 int length = next ? next - test - 1 : strlen(test) - 1;
64 if (test[length] == '*') {
65 if (! strncmp(test, file, length)) return 1;
66 if (! strncmp(test, func, length)) return 1;
69 if (! strncmp(test, file, strlen(file))) return 1;
70 if (! strncmp(test, func, strlen(func))) return 1;
73 if (next) while(isspace(*next)) next++;
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)
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) \
91 // do debug statements
92 #define DDEBUG(actions...) \
93 if (debug_should_print(__FILE__, __func__)) { \
97 // print debug statement
98 #define PDEBUG(format, args...) \
99 if (debug_should_print(__FILE__, __func__)) { \
100 DEBUG_PRINT(format, ## args); \
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); \
109 #else /* not DEBUG */
113 #define ADEBUG(test, args...)
114 #define DDEBUG(actions...)
115 #define PDEBUG(format, args...)
116 #define TDEBUG(format, args...)