2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles */
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. */
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 */
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 /*******************************************************************************/
22 * 11/21/2003 - Jonathan Moore Liles
26 * Disable by defining the preprocessor variable NDEBUG prior to inclusion.
28 * The following macros sould be defined as string literals
32 * __MODULE__ Name of module. eg. "libfoo"
34 * __FILE__ Name of file. eg. "foo.c"
36 * __FUNCTION__ Name of enclosing function. eg. "bar"
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
55 * MESSAGE( const char *format, ... )
56 * WARNING( const char *format, ... )
57 * FATAL( 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 * FATAL 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.
74 #define __MODULE__ PACKAGE
76 #define __MODULE__ NULL
81 #define __FUNCTION__ NULL
91 warnf ( warning_t level
,
94 const char *function
, int line
, const char *fmt
, ... );
98 #define DMESSAGE( fmt, args... ) warnf( W_MESSAGE, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args )
99 #define DWARNING( fmt, args... ) warnf( W_WARNING, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args )
100 #define ASSERT( pred, fmt, args... ) do { if ( ! (pred) ) { warnf( W_FATAL, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args ); abort(); } } while ( 0 )
102 #define DMESSAGE( fmt, args... )
103 #define DWARNING( fmt, args... )
104 #define ASSERT( pred, fmt, args... )
107 /* these are always defined */
108 #define MESSAGE( fmt, args... ) warnf( W_MESSAGE, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args )
109 #define WARNING( fmt, args... ) warnf( W_WARNING, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args )
110 #define FATAL( fmt, args... ) ( warnf( W_FATAL, __MODULE__, __FILE__, __FUNCTION__, __LINE__, fmt, ## args ), abort() )