smbd: Simplify callers of notify_filter_string
[Samba.git] / lib / util / debug.h
blob4687ac074b1f4aba23037dfae4af053d4f430639
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
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.
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.
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/>.
23 #ifndef _SAMBA_DEBUG_H
24 #define _SAMBA_DEBUG_H
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdarg.h>
30 #include "attr.h"
33 /* -------------------------------------------------------------------------- **
34 * Debugging code. See also debug.c
37 /* the maximum debug level to compile into the code. This assumes a good
38 optimising compiler that can remove unused code
39 for embedded or low-memory systems set this to a value like 2 to get
40 only important messages. This gives *much* smaller binaries
42 #ifndef MAX_DEBUG_LEVEL
43 #define MAX_DEBUG_LEVEL 1000
44 #endif
46 bool dbgtext_va(const char *, va_list ap) PRINTF_ATTRIBUTE(1,0);
47 bool dbgtext( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
48 bool dbghdrclass( int level, int cls, const char *location, const char *func);
49 bool dbgsetclass(int level, int cls);
52 * Define all new debug classes here. A class is represented by an entry in
53 * the DEBUGLEVEL_CLASS array. Index zero of this array is equivalent to the
54 * old DEBUGLEVEL. Any source file that does NOT add the following lines:
56 * #undef DBGC_CLASS
57 * #define DBGC_CLASS DBGC_<your class name here>
59 * at the start of the file (after #include "includes.h") will default to
60 * using index zero, so it will behave just like it always has.
62 #define DBGC_ALL 0 /* index equivalent to DEBUGLEVEL */
64 #define DBGC_TDB 1
65 #define DBGC_PRINTDRIVERS 2
66 #define DBGC_LANMAN 3
67 #define DBGC_SMB 4
68 #define DBGC_RPC_PARSE 5
69 #define DBGC_RPC_SRV 6
70 #define DBGC_RPC_CLI 7
71 #define DBGC_PASSDB 8
72 #define DBGC_SAM 9
73 #define DBGC_AUTH 10
74 #define DBGC_WINBIND 11
75 #define DBGC_VFS 12
76 #define DBGC_IDMAP 13
77 #define DBGC_QUOTA 14
78 #define DBGC_ACLS 15
79 #define DBGC_LOCKING 16
80 #define DBGC_MSDFS 17
81 #define DBGC_DMAPI 18
82 #define DBGC_REGISTRY 19
83 #define DBGC_SCAVENGER 20
84 #define DBGC_DNS 21
85 #define DBGC_LDB 22
86 #define DBGC_TEVENT 23
87 #define DBGC_AUTH_AUDIT 24
88 #define DBGC_AUTH_AUDIT_JSON 25
89 #define DBGC_KERBEROS 26
90 #define DBGC_DRS_REPL 27
91 #define DBGC_SMB2 28
92 #define DBGC_SMB2_CREDITS 29
93 #define DBGC_DSDB_AUDIT 30
94 #define DBGC_DSDB_AUDIT_JSON 31
95 #define DBGC_DSDB_PWD_AUDIT 32
96 #define DBGC_DSDB_PWD_AUDIT_JSON 33
97 #define DBGC_DSDB_TXN_AUDIT 34
98 #define DBGC_DSDB_TXN_AUDIT_JSON 35
99 #define DBGC_DSDB_GROUP_AUDIT 36
100 #define DBGC_DSDB_GROUP_AUDIT_JSON 37
102 /* So you can define DBGC_CLASS before including debug.h */
103 #ifndef DBGC_CLASS
104 #define DBGC_CLASS 0 /* override as shown above */
105 #endif
107 #define DEBUGLEVEL debuglevel_get()
109 #define debuglevel_get() debuglevel_get_class(DBGC_ALL)
110 #define debuglevel_set(lvl) debuglevel_set_class(DBGC_ALL, (lvl))
112 /* Debugging macros
114 * DEBUGLVL()
115 * If the 'file specific' debug class level >= level OR the system-wide
116 * DEBUGLEVEL (synonym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
117 * generate a header using the default macros for file, line, and
118 * function name. Returns True if the debug level was <= DEBUGLEVEL.
120 * Example: if( DEBUGLVL( 2 ) ) dbgtext( "Some text.\n" );
122 * DEBUG()
123 * If the 'file specific' debug class level >= level OR the system-wide
124 * DEBUGLEVEL (synonym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
125 * generate a header using the default macros for file, line, and
126 * function name. Each call to DEBUG() generates a new header *unless* the
127 * previous debug output was unterminated (i.e. no '\n').
128 * See debug.c:dbghdr() for more info.
130 * Example: DEBUG( 2, ("Some text and a value %d.\n", value) );
132 * DEBUGC()
133 * If the 'macro specified' debug class level >= level OR the system-wide
134 * DEBUGLEVEL (synonym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
135 * generate a header using the default macros for file, line, and
136 * function name. Each call to DEBUG() generates a new header *unless* the
137 * previous debug output was unterminated (i.e. no '\n').
138 * See debug.c:dbghdr() for more info.
140 * Example: DEBUGC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
142 * DEBUGADD(), DEBUGADDC()
143 * Same as DEBUG() and DEBUGC() except the text is appended to the previous
144 * DEBUG(), DEBUGC(), DEBUGADD(), DEBUGADDC() with out another interviening
145 * header.
147 * Example: DEBUGADD( 2, ("Some text and a value %d.\n", value) );
148 * DEBUGADDC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
150 * Note: If the debug class has not be redefined (see above) then the optimizer
151 * will remove the extra conditional test.
155 * From talloc.c:
158 /* these macros gain us a few percent of speed on gcc */
159 #if (__GNUC__ >= 3)
160 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
161 as its first argument */
162 #ifndef likely
163 #define likely(x) __builtin_expect(!!(x), 1)
164 #endif
165 #ifndef unlikely
166 #define unlikely(x) __builtin_expect(!!(x), 0)
167 #endif
168 #else
169 #ifndef likely
170 #define likely(x) (x)
171 #endif
172 #ifndef unlikely
173 #define unlikely(x) (x)
174 #endif
175 #endif
177 int debuglevel_get_class(size_t idx);
178 void debuglevel_set_class(size_t idx, int level);
180 #define CHECK_DEBUGLVL( level ) \
181 ( ((level) <= MAX_DEBUG_LEVEL) && \
182 unlikely(debuglevel_get_class(DBGC_CLASS) >= (level)))
184 #define CHECK_DEBUGLVLC( dbgc_class, level ) \
185 ( ((level) <= MAX_DEBUG_LEVEL) && \
186 unlikely(debuglevel_get_class(dbgc_class) >= (level)))
188 #define DEBUGLVL( level ) \
189 ( CHECK_DEBUGLVL(level) \
190 && dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ ) )
192 #define DEBUGLVLC( dbgc_class, level ) \
193 ( CHECK_DEBUGLVLC( dbgc_class, level ) \
194 && dbghdrclass( level, dbgc_class, __location__, __FUNCTION__ ) )
196 #define DEBUG( level, body ) \
197 (void)( ((level) <= MAX_DEBUG_LEVEL) && \
198 unlikely(debuglevel_get_class(DBGC_CLASS) >= (level)) \
199 && (dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ )) \
200 && (dbgtext body) )
203 * @brief DEBUGLF is same as DEBUG with explicit location and function arguments
205 * To be used when passing location and function of a caller appearing earlier in
206 * the call stack instead of some helper function.
208 * @code
209 * DEBUGLF( 2, ("Some text.\n"), "foo.c:1", "foo" );
210 * DEBUGLF( 5, ("Some text.\n"), location, function );
211 * @endcode
213 * @return void.
215 #define DEBUGLF( level, body, location, function ) \
216 (void)( ((level) <= MAX_DEBUG_LEVEL) && \
217 unlikely(debuglevel_get_class(DBGC_CLASS) >= (level)) \
218 && (dbghdrclass( level, DBGC_CLASS, location, function )) \
219 && (dbgtext body) )
221 #define DEBUGC( dbgc_class, level, body ) \
222 (void)( ((level) <= MAX_DEBUG_LEVEL) && \
223 unlikely(debuglevel_get_class(dbgc_class) >= (level)) \
224 && (dbghdrclass( level, dbgc_class, __location__, __FUNCTION__ )) \
225 && (dbgtext body) )
227 #define DEBUGADD( level, body ) \
228 (void)( ((level) <= MAX_DEBUG_LEVEL) && \
229 unlikely(debuglevel_get_class(DBGC_CLASS) >= (level)) \
230 && (dbgsetclass(level, DBGC_CLASS)) \
231 && (dbgtext body) )
233 #define DEBUGADDC( dbgc_class, level, body ) \
234 (void)( ((level) <= MAX_DEBUG_LEVEL) && \
235 unlikely((debuglevel_get_class(dbgc_class) >= (level))) \
236 && (dbgsetclass(level, dbgc_class)) \
237 && (dbgtext body) )
239 /* Print a separator to the debug log. */
240 #define DEBUGSEP(level)\
241 DEBUG((level),("===============================================================\n"))
243 /* Prefix messages with the function name */
244 #define DBG_PREFIX(level, body ) \
245 (void)( ((level) <= MAX_DEBUG_LEVEL) && \
246 unlikely(debuglevel_get_class(DBGC_CLASS) >= (level)) \
247 && (dbghdrclass(level, DBGC_CLASS, __location__, __func__ )) \
248 && (dbgtext("%s: ", __func__)) \
249 && (dbgtext body) )
251 /* Prefix messages with the function name - class specific */
252 #define DBGC_PREFIX(dbgc_class, level, body ) \
253 (void)( ((level) <= MAX_DEBUG_LEVEL) && \
254 unlikely(debuglevel_get_class(dbgc_class) >= (level)) \
255 && (dbghdrclass(level, dbgc_class, __location__, __func__ )) \
256 && (dbgtext("%s: ", __func__)) \
257 && (dbgtext body) )
260 #ifdef DEVELOPER
261 #define DBG_DEV(...) \
262 (void)( (debug_developer_enabled()) \
263 && (dbgtext("%s:DEV:%d: ", __func__, getpid())) \
264 && (dbgtext(__VA_ARGS__)) )
265 #else
266 #define DBG_DEV(...) /* DBG_DEV was here */
267 #endif
270 * Debug levels matching RFC 3164
272 #define DBGLVL_ERR 0 /* error conditions */
273 #define DBGLVL_WARNING 1 /* warning conditions */
274 #define DBGLVL_NOTICE 3 /* normal, but significant, condition */
275 #define DBGLVL_INFO 5 /* informational message */
276 #define DBGLVL_DEBUG 10 /* debug-level message */
278 #define DBG_STARTUP_NOTICE(...) do { \
279 debug_set_forced_log_priority(DBGLVL_NOTICE); \
280 D_ERR(__VA_ARGS__); \
281 debug_set_forced_log_priority(-1); \
282 } while(0)
284 #define DBG_ERR(...) DBG_PREFIX(DBGLVL_ERR, (__VA_ARGS__))
285 #define DBG_WARNING(...) DBG_PREFIX(DBGLVL_WARNING, (__VA_ARGS__))
286 #define DBG_NOTICE(...) DBG_PREFIX(DBGLVL_NOTICE, (__VA_ARGS__))
287 #define DBG_INFO(...) DBG_PREFIX(DBGLVL_INFO, (__VA_ARGS__))
288 #define DBG_DEBUG(...) DBG_PREFIX(DBGLVL_DEBUG, (__VA_ARGS__))
290 #define DBGC_ERR(dbgc_class, ...) DBGC_PREFIX(dbgc_class, \
291 DBGLVL_ERR, (__VA_ARGS__))
292 #define DBGC_WARNING(dbgc_class, ...) DBGC_PREFIX(dbgc_class, \
293 DBGLVL_WARNING, (__VA_ARGS__))
294 #define DBGC_NOTICE(dbgc_class, ...) DBGC_PREFIX(dbgc_class, \
295 DBGLVL_NOTICE, (__VA_ARGS__))
296 #define DBGC_INFO(dbgc_class, ...) DBGC_PREFIX(dbgc_class, \
297 DBGLVL_INFO, (__VA_ARGS__))
298 #define DBGC_DEBUG(dbgc_class, ...) DBGC_PREFIX(dbgc_class, \
299 DBGLVL_DEBUG, (__VA_ARGS__))
301 #define D_ERR(...) DEBUG(DBGLVL_ERR, (__VA_ARGS__))
302 #define D_WARNING(...) DEBUG(DBGLVL_WARNING, (__VA_ARGS__))
303 #define D_NOTICE(...) DEBUG(DBGLVL_NOTICE, (__VA_ARGS__))
304 #define D_INFO(...) DEBUG(DBGLVL_INFO, (__VA_ARGS__))
305 #define D_DEBUG(...) DEBUG(DBGLVL_DEBUG, (__VA_ARGS__))
307 #define DC_ERR(...) DEBUGC(dbgc_class, \
308 DBGLVL_ERR, (__VA_ARGS__))
309 #define DC_WARNING(...) DEBUGC(dbgc_class, \
310 DBGLVL_WARNING, (__VA_ARGS__))
311 #define DC_NOTICE(...) DEBUGC(dbgc_class, \
312 DBGLVL_NOTICE, (__VA_ARGS__))
313 #define DC_INFO(...) DEBUGC(dbgc_class, \
314 DBGLVL_INFO, (__VA_ARGS__))
315 #define DC_DEBUG(...) DEBUGC(dbgc_class, \
316 DBGLVL_DEBUG, (__VA_ARGS__))
318 /* The following definitions come from lib/debug.c */
321 * Possible destinations for the debug log.
323 * Set via setup_logging(); higher values have precedence.
325 enum debug_logtype {
326 DEBUG_DEFAULT_STDERR = 0,
327 DEBUG_DEFAULT_STDOUT = 1,
328 DEBUG_FILE = 2,
329 DEBUG_STDOUT = 3,
330 DEBUG_STDERR = 4,
331 DEBUG_CALLBACK = 5
334 enum debug_syslog_format {
335 DEBUG_SYSLOG_FORMAT_NO = 0,
336 DEBUG_SYSLOG_FORMAT_IN_LOGS = 1,
337 DEBUG_SYSLOG_FORMAT_ALWAYS = 2,
340 struct debug_settings {
341 size_t max_log_size;
342 bool timestamp_logs;
343 bool debug_prefix_timestamp;
344 bool debug_hires_timestamp;
345 enum debug_syslog_format debug_syslog_format;
346 bool debug_pid;
347 bool debug_uid;
348 bool debug_class;
349 bool debug_no_stderr_redirect;
352 void setup_logging(const char *prog_name, enum debug_logtype new_logtype);
354 void gfree_debugsyms(void);
355 int debug_add_class(const char *classname);
356 bool debug_parse_levels(const char *params_str);
357 void debug_setup_talloc_log(void);
358 void debug_set_logfile(const char *name);
359 void debug_set_settings(struct debug_settings *settings,
360 const char *logging_param,
361 int syslog_level, bool syslog_only);
362 void debug_set_hostname(const char *name);
363 void debug_set_forced_log_priority(int forced_log_priority);
364 bool reopen_logs_internal( void );
365 void force_check_log_size( void );
366 bool need_to_check_log_size( void );
367 void check_log_size( void );
368 void dbgflush( void );
369 enum debug_logtype debug_get_log_type(void);
370 bool debug_get_output_is_stderr(void);
371 bool debug_get_output_is_stdout(void);
372 void debug_schedule_reopen_logs(void);
373 char *debug_list_class_names_and_levels(void);
374 bool debug_developer_enabled(void);
375 void debug_developer_enable(void);
376 void debug_developer_disable(void);
378 typedef void (*debug_callback_fn)(void *private_ptr, int level, const char *msg);
381 Set a callback for all debug messages. Use in dlz_bind9 to push output to the bind logs
383 void debug_set_callback(void *private_ptr, debug_callback_fn fn);
385 char *debug_get_ringbuf(void);
386 size_t debug_get_ringbuf_size(void);
388 /* Explicitly set new traceid. The old id is returned. */
389 uint64_t debug_traceid_set(uint64_t id);
391 /* Get the current traceid. */
392 uint64_t debug_traceid_get(void);
394 size_t *debug_call_depth_addr(void);
396 #endif /* _SAMBA_DEBUG_H */