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#endif
16#include <OS.h>
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	"bindfs"
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#	else
48#		define __out printf
49#	endif
50#else
51#	include <KernelExport.h>
52#	include <null.h>
53#	if DEBUG_PRINT
54#		define __out dbg_printf
55#	else
56#		define __out dprintf
57#	endif
58#endif
59
60
61// define the PANIC() macro
62#ifndef PANIC
63#	if USER
64#		define PANIC(str)	debugger(str)
65#	else
66#		define PANIC(str)	panic(str)
67#	endif
68#endif
69
70
71// functions exported by this module
72status_t init_debugging();
73status_t exit_debugging();
74void dbg_printf_begin();
75void dbg_printf_end();
76#if DEBUG_PRINT
77	void dbg_printf(const char *format,...);
78#else
79	static inline void dbg_printf(const char *,...) {}
80#endif
81
82
83// Short overview over the debug output macros:
84//	PRINT()
85//		is for general messages that very unlikely should appear in a release
86//		build
87//	FATAL()
88//		this is for fatal messages, when something has really gone wrong
89//	INFORM()
90//		general information, as disk size, etc.
91//	REPORT_ERROR(status_t)
92//		prints out error information
93//	RETURN_ERROR(status_t)
94//		calls REPORT_ERROR() and return the value
95//	D()
96//		the statements in D() are only included if DEBUG is defined
97
98
99#define DEBUG_THREAD	find_thread(NULL)
100#define DEBUG_CONTEXT(x)													\
101{																			\
102	dbg_printf_begin();														\
103	__out(DEBUG_APP ": [%" B_PRIdBIGTIME ": %5" B_PRId32 "] ", system_time(),\
104		DEBUG_THREAD);														\
105	x;																		\
106	dbg_printf_end();														\
107}
108#define DEBUG_CONTEXT_FUNCTION(prefix, x)									\
109{																			\
110	dbg_printf_begin();														\
111	__out(DEBUG_APP ": [%" B_PRIdBIGTIME ": %5" B_PRId32 "] %s" prefix, 		\
112		system_time(), DEBUG_THREAD, __PRETTY_FUNCTION__);					\
113	x;																		\
114	dbg_printf_end();														\
115}
116#define DEBUG_CONTEXT_LINE(x)												\
117{																			\
118	dbg_printf_begin();														\
119	__out(DEBUG_APP ": [%" B_PRId64 ": %5" B_PRId32 "] %s:%d: ",				\
120		system_time(), DEBUG_THREAD, __PRETTY_FUNCTION__, __LINE__);		\
121	x;																		\
122	dbg_printf_end();														\
123}
124
125#define TPRINT(x...)	DEBUG_CONTEXT( __out(x) )
126#define TREPORT_ERROR(status)												\
127	DEBUG_CONTEXT_LINE( __out("%s\n", strerror(status)) )
128#define TRETURN_ERROR(err)													\
129{																			\
130	status_t _status = err;													\
131	if (_status < B_OK)														\
132		TREPORT_ERROR(_status);												\
133	return _status;															\
134}
135#define TSET_ERROR(var, err)												\
136{																			\
137	status_t _status = err;													\
138	if (_status < B_OK)														\
139		TREPORT_ERROR(_status);												\
140		var = _status;														\
141}
142#define TFUNCTION(x...)		DEBUG_CONTEXT_FUNCTION( ": ", __out(x) )
143#define TFUNCTION_START()	DEBUG_CONTEXT_FUNCTION( "\n",  )
144#define TFUNCTION_END()		DEBUG_CONTEXT_FUNCTION( " done\n",  )
145
146#if DEBUG
147	#define PRINT(x...)				TPRINT(x)
148	#define REPORT_ERROR(status)	TREPORT_ERROR(status)
149	#define RETURN_ERROR(err)		TRETURN_ERROR(err)
150	#define SET_ERROR(var, err)		TSET_ERROR(var, err)
151	#define FATAL(x...)				DEBUG_CONTEXT( __out(x) )
152	#define ERROR(x...)				DEBUG_CONTEXT( __out(x) )
153	#define WARN(x...)				DEBUG_CONTEXT( __out(x) )
154	#define INFORM(x...)			DEBUG_CONTEXT( __out(x) )
155	#define FUNCTION(x...)			TFUNCTION(x)
156	#define FUNCTION_START()		TFUNCTION_START()
157	#define FUNCTION_END()			TFUNCTION_END()
158	#define DARG(x)					x
159	#define D(x)					{x;};
160#else
161	#define PRINT(x...)				;
162	#define REPORT_ERROR(status)	;
163	#define RETURN_ERROR(status)	return status;
164	#define SET_ERROR(var, err)		var = err;
165	#define FATAL(x...)				DEBUG_CONTEXT( __out(x) )
166	#define ERROR(x...)				DEBUG_CONTEXT( __out(x) )
167	#define WARN(x...)				DEBUG_CONTEXT( __out(x) )
168	#define INFORM(x...)			DEBUG_CONTEXT( __out(x) )
169	#define FUNCTION(x...)			;
170	#define FUNCTION_START()		;
171	#define FUNCTION_END()			;
172	#define DARG(x)
173	#define D(x)					;
174#endif
175
176#ifndef TOUCH
177#define TOUCH(var) (void)var
178#endif
179
180
181#endif	// DEBUG_SUPPORT_H
182