xref: /haiku/headers/os/support/Debug.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2007, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _DEBUG_H
6 #define _DEBUG_H
7 
8 
9 #include <BeBuild.h>
10 #include <OS.h>
11 
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 
17 /* Private */
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 	extern _IMPEXP_ROOT bool _rtDebugFlag;
22 
23 	_IMPEXP_ROOT bool _debugFlag(void);
24 	_IMPEXP_ROOT bool _setDebugFlag(bool);
25 
26 	_IMPEXP_ROOT int _debugPrintf(const char *, ...);
27 	_IMPEXP_ROOT int _sPrintf(const char *, ...);
28 	_IMPEXP_ROOT int _xdebugPrintf(const char *, ...);
29 	_IMPEXP_ROOT int _debuggerAssert(const char *, int, char *);
30 #ifdef __cplusplus
31 }
32 #endif
33 
34 /* Debug macros */
35 #if DEBUG
36 	#define SET_DEBUG_ENABLED(FLAG)	_setDebugFlag(FLAG)
37 	#define	IS_DEBUG_ENABLED()		_debugFlag()
38 
39 	#define SERIAL_PRINT(ARGS)		_sPrintf ARGS
40 	#define PRINT(ARGS) 			_debugPrintf ARGS
41 	#define PRINT_OBJECT(OBJ)		if (_rtDebugFlag) {			\
42 										PRINT(("%s\t", #OBJ));	\
43 										(OBJ).PrintToStream(); 	\
44 									} ((void)0)
45 	#define TRACE()					_debugPrintf("File: %s, Line: %d, Thread: %d\n", \
46 										__FILE__, __LINE__, find_thread(NULL))
47 
48 	#define SERIAL_TRACE()			_sPrintf("File: %s, Line: %d, Thread: %d\n", \
49 										__FILE__, __LINE__, find_thread(NULL))
50 
51 	#define DEBUGGER(MSG)			if (_rtDebugFlag) debugger(MSG)
52 	#if !defined(ASSERT)
53 		#define ASSERT(E)			(!(E) ? _debuggerAssert(__FILE__,__LINE__, #E) \
54 										: (int)0)
55 	#endif
56 
57 	#define ASSERT_WITH_MESSAGE(expr, msg) \
58 								(!(expr) ? _debuggerAssert( __FILE__,__LINE__, msg) \
59 										: (int)0)
60 
61 	#define TRESPASS()			DEBUGGER("Should not be here");
62 
63 	#define DEBUG_ONLY(arg)		arg
64 
65 #else /* DEBUG == 0 */
66 	#define SET_DEBUG_ENABLED(FLAG)			(void)0
67 	#define	IS_DEBUG_ENABLED()				(void)0
68 
69 	#define SERIAL_PRINT(ARGS)				(void)0
70 	#define PRINT(ARGS)						(void)0
71 	#define PRINT_OBJECT(OBJ)				(void)0
72 	#define TRACE()							(void)0
73 	#define SERIAL_TRACE()					(void)0
74 
75 	#define DEBUGGER(MSG)					(void)0
76 	#if !defined(ASSERT)
77 		#define ASSERT(E)					(void)0
78 	#endif
79 	#define ASSERT_WITH_MESSAGE(expr, msg)	(void)0
80 	#define TRESPASS()						(void)0
81 	#define DEBUG_ONLY(x)
82 #endif
83 
84 /* STATIC_ASSERT is a compile-time check that can be used to             */
85 /* verify static expressions such as: STATIC_ASSERT(sizeof(int64) == 8); */
86 #define STATIC_ASSERT(x)								\
87 	do {												\
88 		struct __staticAssertStruct__ {					\
89 			char __static_assert_failed__[2*(x) - 1];	\
90 		};												\
91 	} while (false)
92 
93 
94 #endif	/* _DEBUG_H */
95