debug: Standalone compile without includes.h
[samba.git] / lib / util / debug.h
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB debug stuff
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) John H Terpstra 1996-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
7    Copyright (C) Paul Ashton 1998
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #ifndef _DEBUG_H
24 #define _DEBUG_H
25
26 #include <stdbool.h>
27 #include <stddef.h>
28
29 #include "attr.h"
30
31
32 /* -------------------------------------------------------------------------- **
33  * Debugging code.  See also debug.c
34  */
35
36 /* the maximum debug level to compile into the code. This assumes a good
37    optimising compiler that can remove unused code
38    for embedded or low-memory systems set this to a value like 2 to get
39    only important messages. This gives *much* smaller binaries
40 */
41 #ifndef MAX_DEBUG_LEVEL
42 #define MAX_DEBUG_LEVEL 1000
43 #endif
44
45 bool dbgtext( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
46 bool dbghdrclass( int level, int cls, const char *location, const char *func);
47 bool dbghdr( int level, const char *location, const char *func);
48
49 /*
50  * Redefine DEBUGLEVEL because so we don't have to change every source file
51  * that *unnecessarily* references it.
52  */
53 #define DEBUGLEVEL DEBUGLEVEL_CLASS[DBGC_ALL]
54
55 /*
56  * Define all new debug classes here. A class is represented by an entry in
57  * the DEBUGLEVEL_CLASS array. Index zero of this arrray is equivalent to the
58  * old DEBUGLEVEL. Any source file that does NOT add the following lines:
59  *
60  *   #undef  DBGC_CLASS
61  *   #define DBGC_CLASS DBGC_<your class name here>
62  *
63  * at the start of the file (after #include "includes.h") will default to
64  * using index zero, so it will behaive just like it always has.
65  */
66 #define DBGC_ALL                0 /* index equivalent to DEBUGLEVEL */
67
68 #define DBGC_TDB                1
69 #define DBGC_PRINTDRIVERS       2
70 #define DBGC_LANMAN             3
71 #define DBGC_SMB                4
72 #define DBGC_RPC_PARSE          5
73 #define DBGC_RPC_SRV            6
74 #define DBGC_RPC_CLI            7
75 #define DBGC_PASSDB             8
76 #define DBGC_SAM                9
77 #define DBGC_AUTH               10
78 #define DBGC_WINBIND            11
79 #define DBGC_VFS                12
80 #define DBGC_IDMAP              13
81 #define DBGC_QUOTA              14
82 #define DBGC_ACLS               15
83 #define DBGC_LOCKING            16
84 #define DBGC_MSDFS              17
85 #define DBGC_DMAPI              18
86 #define DBGC_REGISTRY           19
87 #define DBGC_SCAVENGER          20
88 #define DBGC_DNS                21
89 #define DBGC_LDB                22
90
91 /* Always ensure this is updated when new fixed classes area added, to ensure the array in debug.c is the right size */
92 #define DBGC_MAX_FIXED          22
93
94 /* So you can define DBGC_CLASS before including debug.h */
95 #ifndef DBGC_CLASS
96 #define DBGC_CLASS            0     /* override as shown above */
97 #endif
98
99 extern int  *DEBUGLEVEL_CLASS;
100
101 /* Debugging macros
102  *
103  * DEBUGLVL()
104  *   If the 'file specific' debug class level >= level OR the system-wide
105  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
106  *   generate a header using the default macros for file, line, and
107  *   function name. Returns True if the debug level was <= DEBUGLEVEL.
108  *
109  *   Example: if( DEBUGLVL( 2 ) ) dbgtext( "Some text.\n" );
110  *
111  * DEBUG()
112  *   If the 'file specific' debug class level >= level OR the system-wide
113  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
114  *   generate a header using the default macros for file, line, and
115  *   function name. Each call to DEBUG() generates a new header *unless* the
116  *   previous debug output was unterminated (i.e. no '\n').
117  *   See debug.c:dbghdr() for more info.
118  *
119  *   Example: DEBUG( 2, ("Some text and a value %d.\n", value) );
120  *
121  * DEBUGC()
122  *   If the 'macro specified' debug class level >= level OR the system-wide
123  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
124  *   generate a header using the default macros for file, line, and
125  *   function name. Each call to DEBUG() generates a new header *unless* the
126  *   previous debug output was unterminated (i.e. no '\n').
127  *   See debug.c:dbghdr() for more info.
128  *
129  *   Example: DEBUGC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
130  *
131  *  DEBUGADD(), DEBUGADDC()
132  *    Same as DEBUG() and DEBUGC() except the text is appended to the previous
133  *    DEBUG(), DEBUGC(), DEBUGADD(), DEBUGADDC() with out another interviening
134  *    header.
135  *
136  *    Example: DEBUGADD( 2, ("Some text and a value %d.\n", value) );
137  *             DEBUGADDC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
138  *
139  * Note: If the debug class has not be redeined (see above) then the optimizer
140  * will remove the extra conditional test.
141  */
142
143 /*
144  * From talloc.c:
145  */
146
147 /* these macros gain us a few percent of speed on gcc */
148 #if (__GNUC__ >= 3)
149 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
150    as its first argument */
151 #ifndef likely
152 #define likely(x)   __builtin_expect(!!(x), 1)
153 #endif
154 #ifndef unlikely
155 #define unlikely(x) __builtin_expect(!!(x), 0)
156 #endif
157 #else
158 #ifndef likely
159 #define likely(x) (x)
160 #endif
161 #ifndef unlikely
162 #define unlikely(x) (x)
163 #endif
164 #endif
165
166 #define CHECK_DEBUGLVL( level ) \
167   ( ((level) <= MAX_DEBUG_LEVEL) && \
168     unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level)))
169
170 #define CHECK_DEBUGLVLC( dbgc_class, level ) \
171   ( ((level) <= MAX_DEBUG_LEVEL) && \
172     unlikely(DEBUGLEVEL_CLASS[ dbgc_class ] >= (level)))
173
174 #define DEBUGLVL( level ) \
175   ( CHECK_DEBUGLVL(level) \
176    && dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ ) )
177
178 #define DEBUGLVLC( dbgc_class, level ) \
179   ( CHECK_DEBUGLVLC( dbgc_class, level ) \
180    && dbghdrclass( level, dbgc_class, __location__, __FUNCTION__ ) )
181
182 #define DEBUG( level, body ) \
183   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
184           unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))           \
185        && (dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ )) \
186        && (dbgtext body) )
187
188 #define DEBUGC( dbgc_class, level, body ) \
189   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
190           unlikely(DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))           \
191        && (dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ )) \
192        && (dbgtext body) )
193
194 #define DEBUGADD( level, body ) \
195   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
196           unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))   \
197        && (dbgtext body) )
198
199 #define DEBUGADDC( dbgc_class, level, body ) \
200   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
201           unlikely((DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))) \
202        && (dbgtext body) )
203
204 /* Print a separator to the debug log. */
205 #define DEBUGSEP(level)\
206         DEBUG((level),("===============================================================\n"))
207
208 /* The following definitions come from lib/debug.c  */
209
210 /** Possible destinations for the debug log (in order of precedence -
211  * once set to DEBUG_FILE, it is not possible to reset to DEBUG_STDOUT
212  * for example.  This makes it easy to override for debug to stderr on
213  * the command line, as the smb.conf cannot reset it back to
214  * file-based logging */
215 enum debug_logtype {
216         DEBUG_DEFAULT_STDERR = 0,
217         DEBUG_DEFAULT_STDOUT = 1,
218         DEBUG_FILE = 2,
219         DEBUG_STDOUT = 3,
220         DEBUG_STDERR = 4,
221         DEBUG_CALLBACK = 5
222 };
223
224 struct debug_settings {
225         size_t max_log_size;
226         int syslog;
227         bool syslog_only;
228         bool timestamp_logs;
229         bool debug_prefix_timestamp;
230         bool debug_hires_timestamp;
231         bool debug_pid;
232         bool debug_uid;
233         bool debug_class;
234 };
235
236 void setup_logging(const char *prog_name, enum debug_logtype new_logtype);
237
238 void debug_close_dbf(void);
239 void gfree_debugsyms(void);
240 int debug_add_class(const char *classname);
241 int debug_lookup_classname(const char *classname);
242 bool debug_parse_levels(const char *params_str);
243 void debug_setup_talloc_log(void);
244 void debug_set_logfile(const char *name);
245 void debug_set_settings(struct debug_settings *settings);
246 bool reopen_logs_internal( void );
247 void force_check_log_size( void );
248 bool need_to_check_log_size( void );
249 void check_log_size( void );
250 void dbgflush( void );
251 bool dbghdrclass(int level, int cls, const char *location, const char *func);
252 bool dbghdr(int level, const char *location, const char *func);
253 bool debug_get_output_is_stderr(void);
254 bool debug_get_output_is_stdout(void);
255 void debug_schedule_reopen_logs(void);
256 char *debug_list_class_names_and_levels(void);
257
258 typedef void (*debug_callback_fn)(void *private_ptr, int level, const char *msg);
259
260 /**
261    Set a callback for all debug messages.  Use in dlz_bind9 to push output to the bind logs
262  */
263 void debug_set_callback(void *private_ptr, debug_callback_fn fn);
264
265 #endif