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	"packagefs"
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(), DEBUG_THREAD);			\
104	x;																		\
105	dbg_printf_end();														\
106}
107#define DEBUG_CONTEXT_FUNCTION(prefix, x)									\
108{																			\
109	dbg_printf_begin();														\
110	__out(DEBUG_APP ": [%" B_PRIdBIGTIME ": %5" B_PRId32 "] %s" prefix, system_time(), DEBUG_THREAD,	\
111		__PRETTY_FUNCTION__);												\
112	x;																		\
113	dbg_printf_end();														\
114}
115#define DEBUG_CONTEXT_LINE(x)												\
116{																			\
117	dbg_printf_begin();														\
118	__out(DEBUG_APP ": [%" B_PRIdBIGTIME ": %5" B_PRId32 "] %s:%d: ", system_time(), DEBUG_THREAD,	\
119		__PRETTY_FUNCTION__, __LINE__);										\
120	x;																		\
121	dbg_printf_end();														\
122}
123
124#define TPRINT(x...)	DEBUG_CONTEXT( __out(x) )
125#define TREPORT_ERROR(status)												\
126	DEBUG_CONTEXT_LINE( __out("%s\n", strerror(status)) )
127#define TRETURN_ERROR(err)													\
128{																			\
129	status_t _status = err;													\
130	if (_status < B_OK)														\
131		TREPORT_ERROR(_status);												\
132	return _status;															\
133}
134#define TSET_ERROR(var, err)												\
135{																			\
136	status_t _status = err;													\
137	if (_status < B_OK)														\
138		TREPORT_ERROR(_status);												\
139		var = _status;														\
140}
141#define TFUNCTION(x...)		DEBUG_CONTEXT_FUNCTION( ": ", __out(x) )
142#define TFUNCTION_START()	DEBUG_CONTEXT_FUNCTION( "\n",  )
143#define TFUNCTION_END()		DEBUG_CONTEXT_FUNCTION( " done\n",  )
144
145#if DEBUG
146	#define PRINT(x...)				TPRINT(x)
147	#define REPORT_ERROR(status)	TREPORT_ERROR(status)
148	#define RETURN_ERROR(err)		TRETURN_ERROR(err)
149	#define SET_ERROR(var, err)		TSET_ERROR(var, err)
150	#define FATAL(x...)				DEBUG_CONTEXT( __out(x) )
151	#define ERROR(x...)				DEBUG_CONTEXT( __out(x) )
152	#define WARN(x...)				DEBUG_CONTEXT( __out(x) )
153	#define INFORM(x...)			DEBUG_CONTEXT( __out(x) )
154	#define FUNCTION(x...)			TFUNCTION(x)
155	#define FUNCTION_START()		TFUNCTION_START()
156	#define FUNCTION_END()			TFUNCTION_END()
157	#define DARG(x)					x
158	#define D(x)					{x;};
159#else
160	#define PRINT(x...)				;
161	#define REPORT_ERROR(status)	;
162	#define RETURN_ERROR(status)	return status;
163	#define SET_ERROR(var, err)		var = err;
164	#define FATAL(x...)				DEBUG_CONTEXT( __out(x) )
165	#define ERROR(x...)				DEBUG_CONTEXT( __out(x) )
166	#define WARN(x...)				DEBUG_CONTEXT( __out(x) )
167	#define INFORM(x...)			DEBUG_CONTEXT( __out(x) )
168	#define FUNCTION(x...)			;
169	#define FUNCTION_START()		;
170	#define FUNCTION_END()			;
171	#define DARG(x)
172	#define D(x)					;
173#endif
174
175#ifndef TOUCH
176#define TOUCH(var) (void)var
177#endif
178
179
180#endif	// DEBUG_SUPPORT_H
181