Debug.h revision 5b1f411f
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 OpenBeOS 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 ": %5ld] ", system_time(), DEBUG_THREAD); x; dbg_printf_end(); }
96#define DEBUG_CONTEXT_FUNCTION(prefix, x)	{ dbg_printf_begin(); \
97	__out(DEBUG_APP " [%" B_PRIdBIGTIME ": %5ld] %s()" prefix, system_time(), \
98	DEBUG_THREAD, __FUNCTION__); x; dbg_printf_end(); }
99#define DEBUG_CONTEXT_LINE(x)	{ dbg_printf_begin(); __out(DEBUG_APP " [%" \
100	B_PRIdBIGTIME ": %5ld] %s():%d: ", system_time(), DEBUG_THREAD, \
101	__FUNCTION__, __LINE__); x; dbg_printf_end(); }
102
103#define TPRINT(x) DEBUG_CONTEXT( __out x )
104#define TREPORT_ERROR(status) DEBUG_CONTEXT_LINE( __out("%s\n", strerror(status)) )
105#define TRETURN_ERROR(err) { status_t _status = err; if (_status < B_OK) TREPORT_ERROR(_status); return _status;}
106#define TSET_ERROR(var, err) { status_t _status = err; if (_status < B_OK) TREPORT_ERROR(_status); var = _status; }
107#define TFUNCTION(x) DEBUG_CONTEXT_FUNCTION( ": ", __out x )
108#define TFUNCTION_START() DEBUG_CONTEXT_FUNCTION( "\n",  )
109#define TFUNCTION_END() DEBUG_CONTEXT_FUNCTION( " done\n",  )
110
111#if DEBUG
112	#define PRINT(x) TPRINT(x)
113	#define REPORT_ERROR(status) TREPORT_ERROR(status)
114	#define RETURN_ERROR(err) TRETURN_ERROR(err)
115	#define SET_ERROR(var, err) TSET_ERROR(var, err)
116	#define FATAL(x) DEBUG_CONTEXT( __out x )
117	#define ERROR(x) DEBUG_CONTEXT( __out x )
118	#define WARN(x) DEBUG_CONTEXT( __out x )
119	#define INFORM(x) DEBUG_CONTEXT( __out x )
120	#define FUNCTION(x) TFUNCTION(x)
121	#define FUNCTION_START() TFUNCTION_START()
122	#define FUNCTION_END() TFUNCTION_END()
123	#define DARG(x) x
124	#define D(x) {x;};
125#else
126	#define PRINT(x) ;
127	#define REPORT_ERROR(status) ;
128	#define RETURN_ERROR(status) return status;
129	#define SET_ERROR(var, err) var = err;
130	#define FATAL(x) DEBUG_CONTEXT( __out x )
131	#define ERROR(x) DEBUG_CONTEXT( __out x )
132	#define WARN(x) DEBUG_CONTEXT( __out x )
133	#define INFORM(x) DEBUG_CONTEXT( __out x )
134	#define FUNCTION(x) ;
135	#define FUNCTION_START() ;
136	#define FUNCTION_END() ;
137	#define DARG(x)
138	#define D(x) ;
139#endif
140
141#ifndef TOUCH
142#define TOUCH(var) (void)var
143#endif
144
145#endif	/* USERLAND_FS_DEBUG_H */
146