s3:include: move MAX_DEBUG_LEVEL from local.h to debug.h
[sfrench/samba-autobuild/.git] / source3 / include / 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 /* -------------------------------------------------------------------------- **
27  * Debugging code.  See also debug.c
28  */
29
30 /* the maximum debug level to compile into the code. This assumes a good
31    optimising compiler that can remove unused code
32    for embedded or low-memory systems set this to a value like 2 to get
33    only important messages. This gives *much* smaller binaries
34 */
35 #ifndef MAX_DEBUG_LEVEL
36 #define MAX_DEBUG_LEVEL 1000
37 #endif
38
39 /* mkproto.awk has trouble with ifdef'd function definitions (it ignores
40  * the #ifdef directive and will read both definitions, thus creating two
41  * diffferent prototype declarations), so we must do these by hand.
42  */
43 /* I know the __attribute__ stuff is ugly, but it does ensure we get the 
44    arguments to DEBUG() right. We have got them wrong too often in the 
45    past.
46    The PRINTFLIKE comment does the equivalent for SGI MIPSPro.
47  */
48 /* PRINTFLIKE1 */
49 int  Debug1( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
50 /* PRINTFLIKE1 */
51 bool dbgtext( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
52 bool dbghdrclass( int level, int cls, const char *location, const char *func);
53 bool dbghdr( int level, const char *location, const char *func);
54
55 #if defined(sgi) && (_COMPILER_VERSION >= 730)
56 #pragma mips_frequency_hint NEVER Debug1
57 #pragma mips_frequency_hint NEVER dbgtext
58 #pragma mips_frequency_hint NEVER dbghdrclass
59 #pragma mips_frequency_hint NEVER dbghdr
60 #endif
61
62 extern XFILE *dbf;
63
64 /* If we have these macros, we can add additional info to the header. */
65
66 #ifdef HAVE_FUNCTION_MACRO
67 #define FUNCTION_MACRO  (__FUNCTION__)
68 #else
69 #define FUNCTION_MACRO  ("")
70 #endif
71
72 /* 
73  * Redefine DEBUGLEVEL because so we don't have to change every source file
74  * that *unnecessarily* references it. Source files neeed not extern reference 
75  * DEBUGLEVEL, as it's extern in includes.h (which all source files include).
76  * Eventually, all these references should be removed, and all references to
77  * DEBUGLEVEL should be references to DEBUGLEVEL_CLASS[DBGC_ALL]. This could
78  * still be through a macro still called DEBUGLEVEL. This cannot be done now
79  * because some references would expand incorrectly.
80  */
81 #define DEBUGLEVEL *debug_level
82 extern int DEBUGLEVEL;
83
84 /*
85  * Define all new debug classes here. A class is represented by an entry in
86  * the DEBUGLEVEL_CLASS array. Index zero of this arrray is equivalent to the
87  * old DEBUGLEVEL. Any source file that does NOT add the following lines:
88  *
89  *   #undef  DBGC_CLASS
90  *   #define DBGC_CLASS DBGC_<your class name here>
91  *
92  * at the start of the file (after #include "includes.h") will default to
93  * using index zero, so it will behaive just like it always has. 
94  */
95 #define DBGC_ALL                0 /* index equivalent to DEBUGLEVEL */
96
97 #define DBGC_TDB                1
98 #define DBGC_PRINTDRIVERS       2
99 #define DBGC_LANMAN             3
100 #define DBGC_SMB                4
101 #define DBGC_RPC_PARSE          5
102 #define DBGC_RPC_SRV            6
103 #define DBGC_RPC_CLI            7
104 #define DBGC_PASSDB             8
105 #define DBGC_SAM                9
106 #define DBGC_AUTH               10
107 #define DBGC_WINBIND            11
108 #define DBGC_VFS                12
109 #define DBGC_IDMAP              13
110 #define DBGC_QUOTA              14
111 #define DBGC_ACLS               15
112 #define DBGC_LOCKING            16
113 #define DBGC_MSDFS              17
114 #define DBGC_DMAPI              18
115 #define DBGC_REGISTRY           19
116
117 /* So you can define DBGC_CLASS before including debug.h */
118 #ifndef DBGC_CLASS
119 #define DBGC_CLASS            0     /* override as shown above */
120 #endif
121
122 extern int  *DEBUGLEVEL_CLASS;
123 extern bool *DEBUGLEVEL_CLASS_ISSET;
124
125 /* Debugging macros
126  *
127  * DEBUGLVL()
128  *   If the 'file specific' debug class level >= level OR the system-wide 
129  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
130  *   generate a header using the default macros for file, line, and 
131  *   function name. Returns True if the debug level was <= DEBUGLEVEL.
132  * 
133  *   Example: if( DEBUGLVL( 2 ) ) dbgtext( "Some text.\n" );
134  *
135  * DEBUGLVLC()
136  *   If the 'macro specified' debug class level >= level OR the system-wide 
137  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then 
138  *   generate a header using the default macros for file, line, and 
139  *   function name. Returns True if the debug level was <= DEBUGLEVEL.
140  * 
141  *   Example: if( DEBUGLVLC( DBGC_TDB, 2 ) ) dbgtext( "Some text.\n" );
142  *
143  * DEBUG()
144  *   If the 'file specific' debug class level >= level OR the system-wide 
145  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then 
146  *   generate a header using the default macros for file, line, and 
147  *   function name. Each call to DEBUG() generates a new header *unless* the 
148  *   previous debug output was unterminated (i.e. no '\n').
149  *   See debug.c:dbghdr() for more info.
150  *
151  *   Example: DEBUG( 2, ("Some text and a value %d.\n", value) );
152  *
153  * DEBUGC()
154  *   If the 'macro specified' debug class level >= level OR the system-wide 
155  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then 
156  *   generate a header using the default macros for file, line, and 
157  *   function name. Each call to DEBUG() generates a new header *unless* the 
158  *   previous debug output was unterminated (i.e. no '\n').
159  *   See debug.c:dbghdr() for more info.
160  *
161  *   Example: DEBUGC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
162  *
163  *  DEBUGADD(), DEBUGADDC()
164  *    Same as DEBUG() and DEBUGC() except the text is appended to the previous
165  *    DEBUG(), DEBUGC(), DEBUGADD(), DEBUGADDC() with out another interviening 
166  *    header.
167  *
168  *    Example: DEBUGADD( 2, ("Some text and a value %d.\n", value) );
169  *             DEBUGADDC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
170  *
171  * Note: If the debug class has not be redeined (see above) then the optimizer 
172  * will remove the extra conditional test.
173  */
174
175 /*
176  * From talloc.c:
177  */
178
179 /* these macros gain us a few percent of speed on gcc */
180 #if (__GNUC__ >= 3)
181 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
182    as its first argument */
183 #ifndef likely
184 #define likely(x)   __builtin_expect(!!(x), 1)
185 #endif
186 #ifndef unlikely
187 #define unlikely(x) __builtin_expect(!!(x), 0)
188 #endif
189 #else
190 #ifndef likely
191 #define likely(x) (x)
192 #endif
193 #ifndef unlikely
194 #define unlikely(x) (x)
195 #endif
196 #endif
197
198 #define CHECK_DEBUGLVL( level ) \
199   ( ((level) <= MAX_DEBUG_LEVEL) && \
200      unlikely((DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))||  \
201      (!DEBUGLEVEL_CLASS_ISSET[ DBGC_CLASS ] && \
202       DEBUGLEVEL_CLASS[ DBGC_ALL   ] >= (level))  ) )
203
204 #define DEBUGLVL( level ) \
205   ( CHECK_DEBUGLVL(level) \
206    && dbghdrclass( level, DBGC_CLASS, __location__, FUNCTION_MACRO ) )
207
208
209 #define DEBUGLVLC( dbgc_class, level ) \
210   ( ((level) <= MAX_DEBUG_LEVEL) && \
211      unlikely((DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))||  \
212      (!DEBUGLEVEL_CLASS_ISSET[ dbgc_class ] && \
213       DEBUGLEVEL_CLASS[ DBGC_ALL   ] >= (level))  ) \
214    && dbghdrclass( level, DBGC_CLASS, __location__, FUNCTION_MACRO) )
215
216
217 #define DEBUG( level, body ) \
218   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
219            unlikely((DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))||  \
220            (!DEBUGLEVEL_CLASS_ISSET[ DBGC_CLASS ] && \
221             DEBUGLEVEL_CLASS[ DBGC_ALL   ] >= (level))  ) \
222        && (dbghdrclass( level, DBGC_CLASS, __location__, FUNCTION_MACRO )) \
223        && (dbgtext body) )
224
225 #define DEBUGC( dbgc_class, level, body ) \
226   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
227            unlikely((DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))||  \
228            (!DEBUGLEVEL_CLASS_ISSET[ dbgc_class ] && \
229             DEBUGLEVEL_CLASS[ DBGC_ALL   ] >= (level))  ) \
230        && (dbghdrclass( level, DBGC_CLASS, __location__, FUNCTION_MACRO)) \
231        && (dbgtext body) )
232
233 #define DEBUGADD( level, body ) \
234   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
235            unlikely((DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))||  \
236            (!DEBUGLEVEL_CLASS_ISSET[ DBGC_CLASS ] && \
237             DEBUGLEVEL_CLASS[ DBGC_ALL   ] >= (level))  ) \
238        && (dbgtext body) )
239
240 #define DEBUGADDC( dbgc_class, level, body ) \
241   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
242           unlikely((DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))||  \
243            (!DEBUGLEVEL_CLASS_ISSET[ dbgc_class ] && \
244             DEBUGLEVEL_CLASS[ DBGC_ALL   ] >= (level))  ) \
245        && (dbgtext body) )
246
247 /* Print a separator to the debug log. */
248 #define DEBUGSEP(level)\
249         DEBUG((level),("===============================================================\n"))
250
251 #endif