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