selftest: enable py3 for samba.tests.blackbox.check_output
[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 _SAMBA_DEBUG_H
24 #define _SAMBA_DEBUG_H
25
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <stdarg.h>
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_va(const char *, va_list ap) PRINTF_ATTRIBUTE(1,0);
46 bool dbgtext( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
47 bool dbghdrclass( int level, int cls, const char *location, const char *func);
48 bool dbghdr( int level, const char *location, const char *func);
49
50 /*
51  * Redefine DEBUGLEVEL because so we don't have to change every source file
52  * that *unnecessarily* references it.
53  */
54 #define DEBUGLEVEL DEBUGLEVEL_CLASS[DBGC_ALL]
55
56 /*
57  * Define all new debug classes here. A class is represented by an entry in
58  * the DEBUGLEVEL_CLASS array. Index zero of this arrray is equivalent to the
59  * old DEBUGLEVEL. Any source file that does NOT add the following lines:
60  *
61  *   #undef  DBGC_CLASS
62  *   #define DBGC_CLASS DBGC_<your class name here>
63  *
64  * at the start of the file (after #include "includes.h") will default to
65  * using index zero, so it will behaive just like it always has.
66  */
67 #define DBGC_ALL                0 /* index equivalent to DEBUGLEVEL */
68
69 #define DBGC_TDB                1
70 #define DBGC_PRINTDRIVERS       2
71 #define DBGC_LANMAN             3
72 #define DBGC_SMB                4
73 #define DBGC_RPC_PARSE          5
74 #define DBGC_RPC_SRV            6
75 #define DBGC_RPC_CLI            7
76 #define DBGC_PASSDB             8
77 #define DBGC_SAM                9
78 #define DBGC_AUTH               10
79 #define DBGC_WINBIND            11
80 #define DBGC_VFS                12
81 #define DBGC_IDMAP              13
82 #define DBGC_QUOTA              14
83 #define DBGC_ACLS               15
84 #define DBGC_LOCKING            16
85 #define DBGC_MSDFS              17
86 #define DBGC_DMAPI              18
87 #define DBGC_REGISTRY           19
88 #define DBGC_SCAVENGER          20
89 #define DBGC_DNS                21
90 #define DBGC_LDB                22
91 #define DBGC_TEVENT             23
92 #define DBGC_AUTH_AUDIT         24
93 #define DBGC_AUTH_AUDIT_JSON    25
94 #define DBGC_KERBEROS           26
95 #define DBGC_DRS_REPL           27
96 #define DBGC_SMB2               28
97 #define DBGC_SMB2_CREDITS       29
98
99 /* So you can define DBGC_CLASS before including debug.h */
100 #ifndef DBGC_CLASS
101 #define DBGC_CLASS            0     /* override as shown above */
102 #endif
103
104 extern int  *DEBUGLEVEL_CLASS;
105
106 /* Debugging macros
107  *
108  * DEBUGLVL()
109  *   If the 'file specific' debug class level >= level OR the system-wide
110  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
111  *   generate a header using the default macros for file, line, and
112  *   function name. Returns True if the debug level was <= DEBUGLEVEL.
113  *
114  *   Example: if( DEBUGLVL( 2 ) ) dbgtext( "Some text.\n" );
115  *
116  * DEBUG()
117  *   If the 'file specific' debug class level >= level OR the system-wide
118  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
119  *   generate a header using the default macros for file, line, and
120  *   function name. Each call to DEBUG() generates a new header *unless* the
121  *   previous debug output was unterminated (i.e. no '\n').
122  *   See debug.c:dbghdr() for more info.
123  *
124  *   Example: DEBUG( 2, ("Some text and a value %d.\n", value) );
125  *
126  * DEBUGC()
127  *   If the 'macro specified' debug class level >= level OR the system-wide
128  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
129  *   generate a header using the default macros for file, line, and
130  *   function name. Each call to DEBUG() generates a new header *unless* the
131  *   previous debug output was unterminated (i.e. no '\n').
132  *   See debug.c:dbghdr() for more info.
133  *
134  *   Example: DEBUGC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
135  *
136  *  DEBUGADD(), DEBUGADDC()
137  *    Same as DEBUG() and DEBUGC() except the text is appended to the previous
138  *    DEBUG(), DEBUGC(), DEBUGADD(), DEBUGADDC() with out another interviening
139  *    header.
140  *
141  *    Example: DEBUGADD( 2, ("Some text and a value %d.\n", value) );
142  *             DEBUGADDC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
143  *
144  * Note: If the debug class has not be redeined (see above) then the optimizer
145  * will remove the extra conditional test.
146  */
147
148 /*
149  * From talloc.c:
150  */
151
152 /* these macros gain us a few percent of speed on gcc */
153 #if (__GNUC__ >= 3)
154 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
155    as its first argument */
156 #ifndef likely
157 #define likely(x)   __builtin_expect(!!(x), 1)
158 #endif
159 #ifndef unlikely
160 #define unlikely(x) __builtin_expect(!!(x), 0)
161 #endif
162 #else
163 #ifndef likely
164 #define likely(x) (x)
165 #endif
166 #ifndef unlikely
167 #define unlikely(x) (x)
168 #endif
169 #endif
170
171 #define CHECK_DEBUGLVL( level ) \
172   ( ((level) <= MAX_DEBUG_LEVEL) && \
173     unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level)))
174
175 #define CHECK_DEBUGLVLC( dbgc_class, level ) \
176   ( ((level) <= MAX_DEBUG_LEVEL) && \
177     unlikely(DEBUGLEVEL_CLASS[ dbgc_class ] >= (level)))
178
179 #define DEBUGLVL( level ) \
180   ( CHECK_DEBUGLVL(level) \
181    && dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ ) )
182
183 #define DEBUGLVLC( dbgc_class, level ) \
184   ( CHECK_DEBUGLVLC( dbgc_class, level ) \
185    && dbghdrclass( level, dbgc_class, __location__, __FUNCTION__ ) )
186
187 #define DEBUG( level, body ) \
188   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
189           unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))           \
190        && (dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ )) \
191        && (dbgtext body) )
192
193 #define DEBUGC( dbgc_class, level, body ) \
194   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
195           unlikely(DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))           \
196        && (dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ )) \
197        && (dbgtext body) )
198
199 #define DEBUGADD( level, body ) \
200   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
201           unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))   \
202        && (dbgtext body) )
203
204 #define DEBUGADDC( dbgc_class, level, body ) \
205   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
206           unlikely((DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))) \
207        && (dbgtext body) )
208
209 /* Print a separator to the debug log. */
210 #define DEBUGSEP(level)\
211         DEBUG((level),("===============================================================\n"))
212
213 /* Prefix messages with the function name */
214 #define DBG_PREFIX(level, body ) \
215         (void)( ((level) <= MAX_DEBUG_LEVEL) &&                 \
216                 unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))     \
217                 && (dbghdrclass(level, DBGC_CLASS, __location__, __func__ )) \
218                 && (dbgtext("%s: ", __func__))                          \
219                 && (dbgtext body) )
220
221 /* Prefix messages with the function name - class specific */
222 #define DBGC_PREFIX(dbgc_class, level, body ) \
223         (void)( ((level) <= MAX_DEBUG_LEVEL) &&                 \
224                 unlikely(DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))     \
225                 && (dbghdrclass(level, dbgc_class, __location__, __func__ )) \
226                 && (dbgtext("%s: ", __func__))                          \
227                 && (dbgtext body) )
228
229 /*
230  * Debug levels matching RFC 3164
231  */
232 #define DBGLVL_ERR       0      /* error conditions */
233 #define DBGLVL_WARNING   1      /* warning conditions */
234 #define DBGLVL_NOTICE    3      /* normal, but significant, condition */
235 #define DBGLVL_INFO      5      /* informational message */
236 #define DBGLVL_DEBUG    10      /* debug-level message */
237
238 #define DBG_ERR(...)            DBG_PREFIX(DBGLVL_ERR,          (__VA_ARGS__))
239 #define DBG_WARNING(...)        DBG_PREFIX(DBGLVL_WARNING,      (__VA_ARGS__))
240 #define DBG_NOTICE(...)         DBG_PREFIX(DBGLVL_NOTICE,       (__VA_ARGS__))
241 #define DBG_INFO(...)           DBG_PREFIX(DBGLVL_INFO,         (__VA_ARGS__))
242 #define DBG_DEBUG(...)          DBG_PREFIX(DBGLVL_DEBUG,        (__VA_ARGS__))
243
244 #define DBGC_ERR(dbgc_class, ...)       DBGC_PREFIX(dbgc_class, \
245                                                 DBGLVL_ERR, (__VA_ARGS__))
246 #define DBGC_WARNING(dbgc_class, ...)   DBGC_PREFIX(dbgc_class, \
247                                                 DBGLVL_WARNING, (__VA_ARGS__))
248 #define DBGC_NOTICE(dbgc_class, ...)    DBGC_PREFIX(dbgc_class, \
249                                                 DBGLVL_NOTICE,  (__VA_ARGS__))
250 #define DBGC_INFO(dbgc_class, ...)      DBGC_PREFIX(dbgc_class, \
251                                                 DBGLVL_INFO,    (__VA_ARGS__))
252 #define DBGC_DEBUG(dbgc_class, ...)     DBGC_PREFIX(dbgc_class, \
253                                                 DBGLVL_DEBUG,   (__VA_ARGS__))
254
255 #define D_ERR(...)              DEBUG(DBGLVL_ERR,       (__VA_ARGS__))
256 #define D_WARNING(...)          DEBUG(DBGLVL_WARNING,   (__VA_ARGS__))
257 #define D_NOTICE(...)           DEBUG(DBGLVL_NOTICE,    (__VA_ARGS__))
258 #define D_INFO(...)             DEBUG(DBGLVL_INFO,      (__VA_ARGS__))
259 #define D_DEBUG(...)            DEBUG(DBGLVL_DEBUG,     (__VA_ARGS__))
260
261 #define DC_ERR(...)             DEBUGC(dbgc_class, \
262                                         DBGLVL_ERR,     (__VA_ARGS__))
263 #define DC_WARNING(...)         DEBUGC(dbgc_class, \
264                                         DBGLVL_WARNING, (__VA_ARGS__))
265 #define DC_NOTICE(...)          DEBUGC(dbgc_class, \
266                                         DBGLVL_NOTICE,  (__VA_ARGS__))
267 #define DC_INFO(...)            DEBUGC(dbgc_class, \
268                                         DBGLVL_INFO,    (__VA_ARGS__))
269 #define DC_DEBUG(...)           DEBUGC(dbgc_class, \
270                                         DBGLVL_DEBUG,   (__VA_ARGS__))
271
272 /* The following definitions come from lib/debug.c  */
273
274 /** Possible destinations for the debug log (in order of precedence -
275  * once set to DEBUG_FILE, it is not possible to reset to DEBUG_STDOUT
276  * for example.  This makes it easy to override for debug to stderr on
277  * the command line, as the smb.conf cannot reset it back to
278  * file-based logging */
279 enum debug_logtype {
280         DEBUG_DEFAULT_STDERR = 0,
281         DEBUG_DEFAULT_STDOUT = 1,
282         DEBUG_FILE = 2,
283         DEBUG_STDOUT = 3,
284         DEBUG_STDERR = 4,
285         DEBUG_CALLBACK = 5
286 };
287
288 struct debug_settings {
289         size_t max_log_size;
290         bool timestamp_logs;
291         bool debug_prefix_timestamp;
292         bool debug_hires_timestamp;
293         bool debug_pid;
294         bool debug_uid;
295         bool debug_class;
296 };
297
298 void setup_logging(const char *prog_name, enum debug_logtype new_logtype);
299
300 void gfree_debugsyms(void);
301 int debug_add_class(const char *classname);
302 bool debug_parse_levels(const char *params_str);
303 void debug_setup_talloc_log(void);
304 void debug_set_logfile(const char *name);
305 void debug_set_settings(struct debug_settings *settings,
306                         const char *logging_param,
307                         int syslog_level, bool syslog_only);
308 bool reopen_logs_internal( void );
309 void force_check_log_size( void );
310 bool need_to_check_log_size( void );
311 void check_log_size( void );
312 void dbgflush( void );
313 bool dbghdrclass(int level, int cls, const char *location, const char *func);
314 bool debug_get_output_is_stderr(void);
315 bool debug_get_output_is_stdout(void);
316 void debug_schedule_reopen_logs(void);
317 char *debug_list_class_names_and_levels(void);
318
319 typedef void (*debug_callback_fn)(void *private_ptr, int level, const char *msg);
320
321 /**
322    Set a callback for all debug messages.  Use in dlz_bind9 to push output to the bind logs
323  */
324 void debug_set_callback(void *private_ptr, debug_callback_fn fn);
325
326 char *debug_get_ringbuf(void);
327 size_t debug_get_ringbuf_size(void);
328
329 #endif /* _SAMBA_DEBUG_H */