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