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