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#if !USER
14#	include <KernelExport.h>
15#	include <OS.h>
16#endif
17#include <SupportDefs.h>
18
19
20// define all macros we work with -- undefined macros are set to defaults
21#ifndef USER
22#	define USER 0
23#endif
24#ifndef DEBUG
25#	define DEBUG 0
26#endif
27#if !DEBUG
28#	undef DEBUG_PRINT
29#	define DEBUG_PRINT 0
30#endif
31#ifndef DEBUG_PRINT
32#	define DEBUG_PRINT 0
33#endif
34#ifndef DEBUG_APP
35#	define DEBUG_APP	"unknown"
36#endif
37#ifndef DEBUG_PRINT_FILE
38#	define DEBUG_PRINT_FILE "/var/log/" DEBUG_APP ".log"
39#endif
40
41
42// define the debug output function
43#if USER
44#	include <stdio.h>
45#	if DEBUG_PRINT
46#		define __out dbg_printf
47#		define __outv dbg_vprintf
48#	else
49#		define __out printf
50#		define __outv vprintf
51#	endif
52#else
53#	include <KernelExport.h>
54#	include <null.h>
55#	if DEBUG_PRINT
56#		define __out dbg_printf
57#		define __outv dbg_vprintf
58#	else
59#		define __out dprintf
60#		define __outv dvprintf
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
76status_t init_debugging();
77status_t exit_debugging();
78void dbg_printf_begin();
79void dbg_printf_end();
80#if DEBUG_PRINT
81	void dbg_vprintf(const char* format, va_list args);
82	void dbg_printf(const char* format,...);
83#else
84	static inline void dbg_vprintf(const char*, va_list) {}
85	static inline void dbg_printf(const char*,...) {}
86#endif
87
88
89// Short overview of the debug output macros:
90//	PRINT()
91//		general messages that shouldn't appear in a release build
92//	FATAL()
93//		fatal messages, when something has really gone wrong
94//	ERROR()
95//		non-fatal error messages
96//	WARN()
97//		warning messages
98//	INFORM()
99//		general information, as disk size, etc.
100//	REPORT_ERROR(status_t)
101//		prints out error information
102//	RETURN_ERROR(status_t)
103//		calls REPORT_ERROR() and return the value
104//	D()
105//		the statements in D() are only included if DEBUG is defined
106
107
108#define DEBUG_THREAD	find_thread(NULL)
109#define DEBUG_CONTEXT(x)													\
110{																			\
111	dbg_printf_begin();														\
112	__out(DEBUG_APP ": [%" B_PRIdBIGTIME ": %5" B_PRId32 "] ",				\
113		system_time(), DEBUG_THREAD);										\
114	x;																		\
115	dbg_printf_end();														\
116}
117#define DEBUG_CONTEXT_FUNCTION(prefix, x)									\
118{																			\
119	dbg_printf_begin();														\
120	__out(DEBUG_APP ": [%" B_PRIdBIGTIME ": %5" B_PRId32 "] %s" prefix,		\
121		system_time(), DEBUG_THREAD, __PRETTY_FUNCTION__);					\
122	x;																		\
123	dbg_printf_end();														\
124}
125#define DEBUG_CONTEXT_LINE(x)												\
126{																			\
127	dbg_printf_begin();														\
128	__out(DEBUG_APP ": [%" B_PRIdBIGTIME ": %5" B_PRId32 "] %s:%d: ",		\
129		system_time(), DEBUG_THREAD, __PRETTY_FUNCTION__, __LINE__);		\
130	x;																		\
131	dbg_printf_end();														\
132}
133
134#define TPRINT(x...)			DEBUG_CONTEXT( __out(x) )
135#define TPRINTV(format, args)	DEBUG_CONTEXT( __outv(format, args) )
136#define TREPORT_ERROR(status)												\
137	DEBUG_CONTEXT_LINE( __out("%s\n", strerror(status)) )
138#define TRETURN_ERROR(err)													\
139{																			\
140	status_t _status = err;													\
141	if (_status < B_OK)														\
142		TREPORT_ERROR(_status);												\
143	return _status;															\
144}
145#define TSET_ERROR(var, err)												\
146{																			\
147	status_t _status = err;													\
148	if (_status < B_OK)														\
149		TREPORT_ERROR(_status);												\
150		var = _status;														\
151}
152#define TFUNCTION(x...)		DEBUG_CONTEXT_FUNCTION( ": ", __out(x) )
153#define TFUNCTION_START()	DEBUG_CONTEXT_FUNCTION( "\n",  )
154#define TFUNCTION_END()		DEBUG_CONTEXT_FUNCTION( " done\n",  )
155
156#if DEBUG
157	#define PRINT(x...)				TPRINT(x)
158	#define REPORT_ERROR(status)	TREPORT_ERROR(status)
159	#define RETURN_ERROR(err)		TRETURN_ERROR(err)
160	#define SET_ERROR(var, err)		TSET_ERROR(var, err)
161	#define FUNCTION(x...)			TFUNCTION(x)
162	#define FUNCTION_START()		TFUNCTION_START()
163	#define FUNCTION_END()			TFUNCTION_END()
164	#define DARG(x)					x
165	#define D(x)					{x;};
166#else
167	#define PRINT(x...)				;
168	#define REPORT_ERROR(status)	;
169	#define RETURN_ERROR(status)	return status;
170	#define SET_ERROR(var, err)		var = err;
171	#define FUNCTION(x...)			;
172	#define FUNCTION_START()		;
173	#define FUNCTION_END()			;
174	#define DARG(x)
175	#define D(x)					;
176#endif
177
178#define FATAL(x...)				TPRINT(x)
179#define ERROR(x...)				TPRINT(x)
180#define ERRORV(format, args)	TPRINTV(format, args)
181#define WARN(x...)				TPRINT(x)
182#define INFORM(x...)			TPRINT(x)
183
184#ifndef TOUCH
185#define TOUCH(var) (void)var
186#endif
187
188
189#endif	// DEBUG_SUPPORT_H
190