1999-08-17 16:01:18 -07:00
/* debug.h for Eterm.
* 21 Feb 1998 , vendu .
*/
# ifndef _DEBUG_H
# define _DEBUG_H
1999-08-17 18:12:47 -07:00
# include <stdlib.h>
1999-10-28 08:22:58 -07:00
# include <time.h>
1999-08-17 18:12:47 -07:00
1999-08-17 16:01:18 -07:00
extern unsigned int debug_level ;
/* Assert macros stolen from my work on Ebar. If these macros break with your cpp, let me know -- mej@eterm.org */
1999-08-17 18:12:47 -07:00
# define NOP ((void)0)
1999-08-17 16:01:18 -07:00
# if defined(__FILE__) && defined(__LINE__)
1999-08-17 18:12:47 -07:00
# ifdef __FUNCTION__
1999-10-28 08:22:58 -07:00
# define __DEBUG() fprintf(stderr, "[%lu] %12s | %4d | %30s: ", (unsigned long) time(NULL), __FILE__, __LINE__, __FUNCTION__)
1999-08-17 18:12:47 -07:00
# else
1999-10-28 08:22:58 -07:00
# define __DEBUG() fprintf(stderr, "[%lu] %12s | %4d: ", (unsigned long) time(NULL), __FILE__, __LINE__)
1999-08-17 18:12:47 -07:00
# endif
1999-08-17 16:01:18 -07:00
# endif
1999-08-17 18:12:47 -07:00
# if defined(__FILE__) && defined(__LINE__)
# define ASSERT(x) do {if (!(x)) {if (debug_level>=1) {fatal_error("ASSERT failed at %s:%d: %s", __FILE__, __LINE__, #x);} \
else { print_warning ( " ASSERT failed at %s:%d: %s " , __FILE__ , __LINE__ , # x ) ; } } } while ( 0 )
# define ASSERT_RVAL(x, val) do {if (!(x)) {if (debug_level>=1) {fatal_error("ASSERT failed at %s:%d: %s", __FILE__, __LINE__, #x);} \
else { print_warning ( " ASSERT failed at %s:%d: %s " , __FILE__ , __LINE__ , # x ) ; } \
return ( val ) ; } } while ( 0 )
1999-09-21 19:34:13 -07:00
# define ASSERT_NOTREACHED() do {if (debug_level>=1) {fatal_error("ASSERT failed at %s:%d: This code should not be reached.", __FILE__, __LINE__);} \
else { print_warning ( " ASSERT failed at %s:%d: This code should not be reached. " , __FILE__ , __LINE__ ) ; } \
} while ( 0 )
# define ASSERT_NOTREACHED_RVAL(val) do {if (debug_level>=1) {fatal_error("ASSERT failed at %s:%d: This code should not be reached.", __FILE__, __LINE__);} \
else { print_warning ( " ASSERT failed at %s:%d: This code should not be reached. " , __FILE__ , __LINE__ ) ; } \
return ( val ) ; } while ( 0 )
1999-08-17 18:12:47 -07:00
# define ABORT() fatal_error("Aborting at %s:%d.", __FILE__, __LINE__)
# else
# define ASSERT(x) do {if (!(x)) {if (debug_level>=1) {fatal_error("ASSERT failed: %s", #x);} \
else { print_warning ( " ASSERT failed: %s " , # x ) ; } } } while ( 0 )
# define ASSERT_RVAL(x, val) do {if (!(x)) {if (debug_level>=1) {fatal_error("ASSERT failed: %s", #x);} \
else { print_warning ( " ASSERT failed: %s " , # x ) ; } return ( val ) ; } } while ( 0 )
1999-09-21 19:34:13 -07:00
# define ASSERT_NOTREACHED() return
# define ASSERT_NOTREACHED_RVAL(x) return (x)
1999-08-17 18:12:47 -07:00
# define ABORT() fatal_error("Aborting.")
1999-08-17 16:01:18 -07:00
# endif
# ifndef __DEBUG
1999-08-17 18:12:47 -07:00
# define __DEBUG() NOP
1999-08-17 16:01:18 -07:00
# endif
1999-08-17 18:12:47 -07:00
# define REQUIRE(x) do {if (!(x)) {if (debug_level>=1) {__DEBUG(); real_dprintf("REQUIRE failed: %s\n", #x);} return;}} while (0)
# define REQUIRE_RVAL(x, v) do {if (!(x)) {if (debug_level>=1) {__DEBUG(); real_dprintf("REQUIRE failed: %s\n", #x);} return (v);}} while (0)
1999-09-21 19:34:13 -07:00
# define NONULL(x) ((x) ? (x) : ("<null>"))
1999-08-17 18:12:47 -07:00
1999-08-17 16:01:18 -07:00
/* Macros for printing debugging messages */
# if DEBUG >= 1
# ifndef DPRINTF
1999-10-08 11:49:57 -07:00
# define DPRINTF(x) do { __DEBUG(); real_dprintf x; } while (0)
1999-08-17 16:01:18 -07:00
# endif
# define DPRINTF1(x) do { if (debug_level >= 1) {__DEBUG(); real_dprintf x;} } while (0)
# define DPRINTF2(x) do { if (debug_level >= 2) {__DEBUG(); real_dprintf x;} } while (0)
# define DPRINTF3(x) do { if (debug_level >= 3) {__DEBUG(); real_dprintf x;} } while (0)
# define DPRINTF4(x) do { if (debug_level >= 4) {__DEBUG(); real_dprintf x;} } while (0)
# define DPRINTF5(x) do { if (debug_level >= 5) {__DEBUG(); real_dprintf x;} } while (0)
# else
# ifndef DPRINTF
# define DPRINTF(x) NOP
# endif
# define DPRINTF1(x) NOP
# define DPRINTF2(x) NOP
# define DPRINTF3(x) NOP
# define DPRINTF4(x) NOP
# define DPRINTF5(x) NOP
# endif
1999-08-17 18:12:47 -07:00
/* Use this for stuff that you only want turned on in dire situations */
# define D_NEVER(x) NOP
1999-08-17 16:01:18 -07:00
/* Debugging macros/defines which set the debugging levels for each output type.
To change the debugging level at which something appears , change the number in
both the DEBUG_ definition and the D_ macro ( if there is one ) . - - mej */
# define DEBUG_SCREEN 1
# define D_SCREEN(x) DPRINTF1(x)
# define DEBUG_CMD 1
# define D_CMD(x) DPRINTF1(x)
# define DEBUG_TTY 1
# define D_TTY(x) DPRINTF1(x)
# define DEBUG_SELECTION 1
# define D_SELECT(x) DPRINTF1(x)
# define DEBUG_UTMP 1
# define D_UTMP(x) DPRINTF1(x)
# define DEBUG_OPTIONS 1
# define D_OPTIONS(x) DPRINTF1(x)
# define DEBUG_IMLIB 1
# define D_IMLIB(x) DPRINTF1(x)
# define DEBUG_PIXMAP 1
# define D_PIXMAP(x) DPRINTF1(x)
# define DEBUG_EVENTS 1
# define D_EVENTS(x) DPRINTF1(x)
# define DEBUG_X11 2
# define D_X11(x) DPRINTF2(x)
1999-08-17 18:12:47 -07:00
# define DEBUG_ENL 2
# define D_ENL(x) DPRINTF2(x)
1999-08-17 16:01:18 -07:00
# define DEBUG_SCROLLBAR 2
# define D_SCROLLBAR(x) DPRINTF2(x)
1999-08-17 18:12:47 -07:00
# define DEBUG_TIMER 2
# define D_TIMER(x) DPRINTF2(x)
1999-08-17 16:01:18 -07:00
# define DEBUG_MENU 3
1999-08-17 18:12:47 -07:00
# define D_MENU(x) DPRINTF3(x)
1999-09-21 19:34:13 -07:00
# define DEBUG_FONT 3
# define D_FONT(x) DPRINTF3(x)
1999-08-17 16:01:18 -07:00
# define DEBUG_TTYMODE 3
# define D_TTYMODE(x) DPRINTF3(x)
# define DEBUG_COLORS 3
# define D_COLORS(x) DPRINTF3(x)
# define DEBUG_MALLOC 4
# define D_MALLOC(x) DPRINTF4(x)
1999-08-17 18:12:47 -07:00
# define DEBUG_ACTIONS 4
# define D_ACTIONS(x) DPRINTF4(x)
1999-08-17 16:01:18 -07:00
# define DEBUG_X 5
1999-08-17 18:12:47 -07:00
# define DEBUG_PARSE 9999
# define D_PARSE(x) D_NEVER(x)
# define DEBUG_STRINGS 9999
# define D_STRINGS(x) D_NEVER(x)
# if (SIZEOF_LONG == 8)
# define MEMSET_LONG() l |= l<<32
# else
# define MEMSET_LONG() ((void)0)
# endif
# define MEMSET(s, c, count) do { \
char * end = ( char * ) ( s ) + ( count ) ; \
long l ; \
long * l_dest = ( long * ) ( s ) ; \
char * c_dest ; \
\
/* areas of less than 4 * sizeof(long) are set in 1-byte chunks. */ \
if ( ( count ) > = 4 * sizeof ( long ) ) { \
/* fill l with c. */ \
l = ( c ) | ( c ) < < 8 ; \
l | = l < < 16 ; \
MEMSET_LONG ( ) ; \
\
/* fill in 1-byte chunks until boundary of long is reached. */ \
if ( ( unsigned long ) l_dest & ( unsigned long ) ( sizeof ( long ) - 1 ) ) { \
c_dest = ( char * ) l_dest ; \
while ( ( unsigned long ) c_dest & ( unsigned long ) ( sizeof ( long ) - 1 ) ) { \
* ( c_dest + + ) = ( c ) ; \
} \
l_dest = ( long * ) c_dest ; \
} \
\
/* fill in long-size chunks as long as possible. */ \
while ( ( ( unsigned long ) ( end - ( char * ) l_dest ) ) > = sizeof ( long ) ) { \
* ( l_dest + + ) = l ; \
} \
} \
\
/* fill the tail in 1-byte chunks. */ \
if ( ( char * ) l_dest < end ) { \
c_dest = ( char * ) l_dest ; \
* ( c_dest + + ) = ( c ) ; \
while ( c_dest < end ) { \
* ( c_dest + + ) = ( c ) ; \
} \
} \
} while ( 0 )
1999-08-17 16:01:18 -07:00
# endif /* _DEBUG_H */