1#ifndef DEBUG_H
2#define 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#include <OS.h>
11#include <SupportDefs.h>
12
13// define all macros we work with -- undefined macros are set default
14#ifndef USER
15#	define USER 0
16#endif
17#ifndef DEBUG
18#	define DEBUG 0
19#endif
20#if !DEBUG
21#	undef DEBUG_PRINT
22#	define DEBUG_PRINT 0
23#endif
24#ifndef DEBUG_PRINT
25#	define DEBUG_PRINT 0
26#endif
27#ifndef DEBUG_PRINT_FILE
28#	define DEBUG_PRINT_FILE "/var/log/reiserfs.log"
29#endif
30
31// define the debug output function
32#if USER
33#	include <stdio.h>
34#	if DEBUG_PRINT
35#		define __out dbg_printf
36#	else
37#		define __out printf
38#	endif
39#else
40#	include <KernelExport.h>
41#	include <null.h>
42#	if DEBUG_PRINT
43#		define __out dbg_printf
44#	else
45#		define __out dprintf
46#	endif
47#endif
48
49// functions exported by this module
50#if DEBUG_PRINT
51	status_t init_debugging();
52	status_t exit_debugging();
53	void dbg_printf(const char *format,...);
54	void dbg_printf_begin();
55	void dbg_printf_end();
56#else
57	static inline status_t init_debugging() { return B_OK; }
58	static inline status_t exit_debugging() { return B_OK; }
59	static inline void dbg_printf(const char *,...) {}
60	static inline void dbg_printf_begin() {}
61	static inline void dbg_printf_end() {}
62#endif
63
64// Short overview over the debug output macros:
65//	PRINT()
66//		is for general messages that very unlikely should appear in a release build
67//	FATAL()
68//		this is for fatal messages, when something has really gone wrong
69//	INFORM()
70//		general information, as disk size, etc.
71//	REPORT_ERROR(status_t)
72//		prints out error information
73//	RETURN_ERROR(status_t)
74//		calls REPORT_ERROR() and return the value
75//	D()
76//		the statements in D() are only included if DEBUG is defined
77
78#define DEBUG_APP		"reiserfs"
79#define DEBUG_PREFIX	DEBUG_APP " [%ld]: "
80#define DEBUG_THREAD	find_thread(NULL)
81#define DEBUG_CONTEXT(x)	{ dbg_printf_begin(); __out(DEBUG_APP " [%Ld: %5ld] ", system_time(), DEBUG_THREAD); x; dbg_printf_end(); }
82#define DEBUG_CONTEXT_FUNCTION(prefix, x)	{ dbg_printf_begin(); __out(DEBUG_APP " [%Ld: %5ld] %s()" prefix, system_time(), DEBUG_THREAD, __FUNCTION__); x; dbg_printf_end(); }
83#define DEBUG_CONTEXT_LINE(x)	{ dbg_printf_begin(); __out(DEBUG_APP " [%Ld: %5ld] %s():%d: ", system_time(), DEBUG_THREAD, __FUNCTION__, __LINE__); x; dbg_printf_end(); }
84
85#define TPRINT(x) DEBUG_CONTEXT( __out x )
86#define TREPORT_ERROR(status) DEBUG_CONTEXT_LINE( __out("%s\n", strerror(status)) )
87#define TRETURN_ERROR(err) { status_t _status = err; if (_status < B_OK) TREPORT_ERROR(_status); return _status;}
88#define TSET_ERROR(var, err) { status_t _status = err; if (_status < B_OK) TREPORT_ERROR(_status); var = _status; }
89#define TFUNCTION(x) DEBUG_CONTEXT_FUNCTION( ": ", __out x )
90#define TFUNCTION_START() DEBUG_CONTEXT_FUNCTION( "\n",  )
91#define TFUNCTION_END() DEBUG_CONTEXT_FUNCTION( " done\n",  )
92
93#if DEBUG
94	#define PRINT(x) TPRINT(x)
95	#define REPORT_ERROR(status) TREPORT_ERROR(status)
96	#define RETURN_ERROR(err) TRETURN_ERROR(err)
97	#define SET_ERROR(var, err) TSET_ERROR(var, err)
98	#define FATAL(x) DEBUG_CONTEXT( __out x )
99	#define INFORM(x) DEBUG_CONTEXT( __out x )
100	#define FUNCTION(x) TFUNCTION(x)
101	#define FUNCTION_START() TFUNCTION_START()
102	#define FUNCTION_END() TFUNCTION_END()
103	#define D(x) {x;};
104#else
105	#define PRINT(x) ;
106	#define REPORT_ERROR(status) ;
107	#define RETURN_ERROR(status) return status;
108	#define SET_ERROR(var, err) var = err;
109	#define FATAL(x) DEBUG_CONTEXT( __out x )
110	#define INFORM(x) DEBUG_CONTEXT( __out x )
111	#define FUNCTION(x) ;
112	#define FUNCTION_START() ;
113	#define FUNCTION_END() ;
114	#define D(x) ;
115#endif
116
117#ifndef TOUCH
118#define TOUCH(var) (void)var
119#endif
120
121#endif	/* DEBUG_H */
122