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