Update NTK.
[nondaw.git] / sequencer / src / debug.h
blob126d6fcdbdf6943747bce4b175ae9a8df482f1ca
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles */
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 by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
9 /* */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
13 /* more details. */
14 /* */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
20 /* debug.h
22 * 11/21/2003 - Jonathan Moore Liles
24 * Debuging support.
26 * Disable by defining the preprocessor variable NDEBUG prior to inclusion.
28 * The following macros sould be defined as string literals
30 * name value
32 * __MODULE__ Name of module. eg. "libfoo"
34 * __FILE__ Name of file. eg. "foo.c"
36 * __FUNCTION__ Name of enclosing function. eg. "bar"
38 * (inteter literal)
39 * __LINE__ Number of enclosing line.
42 * __FILE__, and __LINE__ are automatically defined by standard CPP
43 * implementations. __FUNCTION__ is more or less unique to GNU, and isn't
44 * strictly a preprocessor macro, but rather a reserved word in the compiler.
45 * There is a sed script available with this toolset that is able to fake
46 * __FUNCTION__ (among other things) with an extra preprocesessing step.
48 * __MODULE__ is nonstandard and should be defined the enclosing program(s).
49 * Autoconf defines PACKAGE as the module name, and these routines will use its
50 * value instead if __MODULE__ is undefined.
52 * The following routines are provided (as macros) and take the same arguments
53 * as printf():
55 * MESSAGE( const char *format, ... )
56 * WARNING( const char *format, ... )
57 * ASSERTION( const char *format, ... )
59 * Calling MESSAGE or WARNING prints the message to stderr along with module,
60 * file and line information, as well as appropriate emphasis. Calling
61 * ASSERTION will do the same, and then call abort() to end the program. It is
62 * unwise to supply any of these marcros with arguments that produce side
63 * effects. As, doing so will most likely result in Heisenbugs; program
64 * behavior that changes when debugging is disabled.
69 #ifndef _DEBUG_H
70 #define _DEBUG_H
72 #ifndef __MODULE__
73 #ifdef PACKAGE
74 #define __MODULE__ PACKAGE
75 #else
76 #define __MODULE__ NULL
77 #endif
78 #endif
80 #ifndef __GNUC__
81 #define __FUNCTION__ NULL
82 #endif
84 #include <string.h>
85 #include <stdio.h>
86 #include <stdarg.h>
88 typedef enum {
89 W_MESSAGE = 0,
90 W_WARNING,
91 W_ASSERTION
92 } warning_t;
94 void
95 warnf ( warning_t level,
96 const char *module,
97 const char *file,
98 const char *function, size_t line, const char *fmt, ... );
101 #ifndef NDEBUG
102 #define DMESSAGE( fmt, args... ) warnf( W_MESSAGE, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args )
103 #define DWARNING( fmt, args... ) warnf( W_WARNING, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args )
104 #define ASSERT( pred, fmt, args... ) do { if ( ! (pred) ) { warnf( W_ASSERTION, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args ); abort(); } } while ( 0 )
105 #else
106 #define DMESSAGE( fmt, args... )
107 #define DWARNING( fmt, args... )
108 #define ASSERT( pred, fmt, args... )
109 #endif
111 /* these are always defined */
112 #define MESSAGE( fmt, args... ) warnf( W_MESSAGE, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args )
113 #define WARNING( fmt, args... ) warnf( W_WARNING, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args )
114 #define ASSERTION( fmt, args... ) ( warnf( W_ASSERTION, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args ), abort() )
116 #endif