xref: /haiku/src/servers/package/DebugSupport.h (revision 73254051b196497dfee9ab89eb0c2f60cc305819)
1 /*
2  * Copyright 2003-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 200?, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef DEBUG_SUPPORT_H
7 #define DEBUG_SUPPORT_H
8 
9 
10 #include <string.h>
11 
12 
13 // define all macros we work with -- undefined macros are set to defaults
14 #ifndef USER
15 #	ifdef _KERNEL_MODE
16 #		define USER 0
17 #	else
18 #		define USER 1
19 #	endif
20 #endif
21 #ifndef DEBUG
22 #	define DEBUG 0
23 #endif
24 #if !DEBUG
25 #	undef DEBUG_PRINT
26 #	define DEBUG_PRINT 0
27 #endif
28 #ifndef DEBUG_PRINT
29 #	define DEBUG_PRINT 0
30 #endif
31 #ifndef DEBUG_APP
32 #	define DEBUG_APP	"package_daemon"
33 #endif
34 #ifndef DEBUG_PRINT_FILE
35 #	define DEBUG_PRINT_FILE "/var/log/" DEBUG_APP ".log"
36 #endif
37 
38 
39 #if !USER
40 #	include <KernelExport.h>
41 #endif
42 #include <OS.h>
43 #include <SupportDefs.h>
44 
45 
46 // define the debug output function
47 #if USER
48 #	include <stdio.h>
49 #	if DEBUG_PRINT
50 #		define __out dbg_printf
51 #	else
52 #		define __out debug_printf
53 #	endif
54 #else
55 #	include <KernelExport.h>
56 #	include <null.h>
57 #	if DEBUG_PRINT
58 #		define __out dbg_printf
59 #	else
60 #		define __out dprintf
61 #	endif
62 #endif
63 
64 
65 // define the PANIC() macro
66 #ifndef PANIC
67 #	if USER
68 #		define PANIC(str)	debugger(str)
69 #	else
70 #		define PANIC(str)	panic(str)
71 #	endif
72 #endif
73 
74 
75 // functions exported by this module
76 status_t init_debugging();
77 status_t exit_debugging();
78 void dbg_printf_begin();
79 void dbg_printf_end();
80 #if DEBUG_PRINT
81 	void dbg_printf(const char *format,...);
82 #else
83 	static inline void dbg_printf(const char *,...) {}
84 #endif
85 
86 
87 // Short overview over the debug output macros:
88 //	PRINT()
89 //		is for general messages that very unlikely should appear in a release
90 //		build
91 //	FATAL()
92 //		this is for fatal messages, when something has really gone wrong
93 //	INFORM()
94 //		general information, as disk size, etc.
95 //	REPORT_ERROR(status_t)
96 //		prints out error information
97 //	RETURN_ERROR(status_t)
98 //		calls REPORT_ERROR() and return the value
99 //	D()
100 //		the statements in D() are only included if DEBUG is defined
101 
102 
103 #define DEBUG_THREAD	find_thread(NULL)
104 #define DEBUG_CONTEXT(x)													\
105 {																			\
106 	dbg_printf_begin();														\
107 	__out(DEBUG_APP " [%" B_PRIdBIGTIME ": %5" B_PRId32 "] ",				\
108 		system_time(), DEBUG_THREAD);										\
109 	x;																		\
110 	dbg_printf_end();														\
111 }
112 #define DEBUG_CONTEXT_FUNCTION(prefix, x)									\
113 {																			\
114 	dbg_printf_begin();														\
115 	__out(DEBUG_APP " [%" B_PRIdBIGTIME ": %5" B_PRId32 "] %s" prefix,		\
116 		system_time(), DEBUG_THREAD,__PRETTY_FUNCTION__);					\
117 	x;																		\
118 	dbg_printf_end();														\
119 }
120 #define DEBUG_CONTEXT_LINE(x)												\
121 {																			\
122 	dbg_printf_begin();														\
123 	__out(DEBUG_APP " [%" B_PRIdBIGTIME ": %5" B_PRId32 "] %s:%d: ",		\
124 		system_time(), DEBUG_THREAD, __PRETTY_FUNCTION__, __LINE__);		\
125 	x;																		\
126 	dbg_printf_end();														\
127 }
128 
129 #define TPRINT(x...)	DEBUG_CONTEXT( __out(x) )
130 #define TREPORT_ERROR(status)												\
131 	DEBUG_CONTEXT_LINE( __out("%s\n", strerror(status)) )
132 #define TRETURN_ERROR(err)													\
133 {																			\
134 	status_t _status = err;													\
135 	if (_status < B_OK)														\
136 		TREPORT_ERROR(_status);												\
137 	return _status;															\
138 }
139 #define TSET_ERROR(var, err)												\
140 {																			\
141 	status_t _status = err;													\
142 	if (_status < B_OK)														\
143 		TREPORT_ERROR(_status);												\
144 		var = _status;														\
145 }
146 #define TFUNCTION(x...)		DEBUG_CONTEXT_FUNCTION( ": ", __out(x) )
147 #define TFUNCTION_START()	DEBUG_CONTEXT_FUNCTION( "\n",  )
148 #define TFUNCTION_END()		DEBUG_CONTEXT_FUNCTION( " done\n",  )
149 
150 #if DEBUG
151 	#define PRINT(x...)				TPRINT(x)
152 	#define REPORT_ERROR(status)	TREPORT_ERROR(status)
153 	#define RETURN_ERROR(err)		TRETURN_ERROR(err)
154 	#define SET_ERROR(var, err)		TSET_ERROR(var, err)
155 	#define FATAL(x...)				DEBUG_CONTEXT( __out(x) )
156 	#define ERROR(x...)				DEBUG_CONTEXT( __out(x) )
157 	#define WARN(x...)				DEBUG_CONTEXT( __out(x) )
158 	#define INFORM(x...)			DEBUG_CONTEXT( __out(x) )
159 	#define FUNCTION(x...)			TFUNCTION(x)
160 	#define FUNCTION_START()		TFUNCTION_START()
161 	#define FUNCTION_END()			TFUNCTION_END()
162 	#define DARG(x)					x
163 	#define D(x)					{x;};
164 #else
165 	#define PRINT(x...)				;
166 	#define REPORT_ERROR(status)	;
167 	#define RETURN_ERROR(status)	return status;
168 	#define SET_ERROR(var, err)		var = err;
169 	#define FATAL(x...)				DEBUG_CONTEXT( __out(x) )
170 	#define ERROR(x...)				DEBUG_CONTEXT( __out(x) )
171 	#define WARN(x...)				DEBUG_CONTEXT( __out(x) )
172 	#define INFORM(x...)			DEBUG_CONTEXT( __out(x) )
173 	#define FUNCTION(x...)			;
174 	#define FUNCTION_START()		;
175 	#define FUNCTION_END()			;
176 	#define DARG(x)
177 	#define D(x)					;
178 #endif
179 
180 #ifndef TOUCH
181 #define TOUCH(var) (void)var
182 #endif
183 
184 
185 #endif	// DEBUG_SUPPORT_H
186