1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <!DOCTYPE chapter PUBLIC "-//Samba-Team//DTD DocBook V4.2-Based Variant V1.0//EN" "http://www.samba.org/samba/DTD/samba-doc">
6 <firstname>Chris</firstname><surname>Hertel</surname>
8 <pubdate>July 1998</pubdate>
11 <title>The samba DEBUG system</title>
14 <title>New Output Syntax</title>
17 The syntax of a debugging log file is represented as:
20 <para><programlisting>
21 >debugfile< :== { >debugmsg< }
23 >debugmsg< :== >debughdr< '\n' >debugtext<
25 >debughdr< :== '[' TIME ',' LEVEL ']' FILE ':' [FUNCTION] '(' LINE ')'
27 >debugtext< :== { >debugline< }
29 >debugline< :== TEXT '\n'
30 </programlisting></para>
33 TEXT is a string of characters excluding the newline character.
37 LEVEL is the DEBUG level of the message (an integer in the range
46 FILE is the name of the file from which the debug message was
51 FUNCTION is the function from which the debug message was generated.
55 LINE is the line number of the debug statement that generated the
59 <para>Basically, what that all means is:</para>
62 A debugging log file is made up of debug messages.
65 Each debug message is made up of a header and text. The header is
66 separated from the text by a newline.
69 The header begins with the timestamp and debug level of the
70 message enclosed in brackets. The filename, function, and line
71 number at which the message was generated follow. The filename is
72 terminated by a colon, and the function name is terminated by the
73 parenthesis which contain the line number. Depending upon the
74 compiler, the function name may be missing (it is generated by the
75 __FUNCTION__ macro, which is not universally implemented, dangit).
78 The message text is made up of zero or more lines, each terminated
83 <para>Here's some example output:</para>
85 <para><programlisting>
86 [1998/08/03 12:55:25, 1] nmbd.c:(659)
87 Netbios nameserver version 1.9.19-prealpha started.
88 Copyright Andrew Tridgell 1994-1997
89 [1998/08/03 12:55:25, 3] loadparm.c:(763)
90 Initializing global parameters
91 </programlisting></para>
94 Note that in the above example the function names are not listed on
95 the header line. That's because the example above was generated on an
96 SGI Indy, and the SGI compiler doesn't support the __FUNCTION__ macro.
102 <title>The DEBUG() Macro</title>
105 Use of the DEBUG() macro is unchanged. DEBUG() takes two parameters.
106 The first is the message level, the second is the body of a function
107 call to the Debug1() function.
110 <para>That's confusing.</para>
112 <para>Here's an example which may help a bit. If you would write</para>
114 <para><programlisting>
115 printf( "This is a %s message.\n", "debug" );
116 </programlisting></para>
119 to send the output to stdout, then you would write
122 <para><programlisting>
123 DEBUG( 0, ( "This is a %s message.\n", "debug" ) );
124 </programlisting></para>
127 to send the output to the debug file. All of the normal printf()
128 formatting escapes work.
132 Note that in the above example the DEBUG message level is set to 0.
133 Messages at level 0 always print. Basically, if the message level is
134 less than or equal to the global value DEBUGLEVEL, then the DEBUG
135 statement is processed.
139 The output of the above example would be something like:
142 <para><programlisting>
143 [1998/07/30 16:00:51, 0] file.c:function(128)
144 This is a debug message.
145 </programlisting></para>
148 Each call to DEBUG() creates a new header *unless* the output produced
149 by the previous call to DEBUG() did not end with a '\n'. Output to the
150 debug file is passed through a formatting buffer which is flushed
151 every time a newline is encountered. If the buffer is not empty when
152 DEBUG() is called, the new input is simply appended.
156 ...but that's really just a Kludge. It was put in place because
157 DEBUG() has been used to write partial lines. Here's a simple (dumb)
158 example of the kind of thing I'm talking about:
161 <para><programlisting>
162 DEBUG( 0, ("The test returned " ) );
166 DEBUG(0, ("False") );
168 </programlisting></para>
171 Without the format buffer, the output (assuming test() returned true)
172 would look like this:
175 <para><programlisting>
176 [1998/07/30 16:00:51, 0] file.c:function(256)
178 [1998/07/30 16:00:51, 0] file.c:function(258)
180 [1998/07/30 16:00:51, 0] file.c:function(261)
182 </programlisting></para>
184 <para>Which isn't much use. The format buffer kludge fixes this problem.
190 <title>The DEBUGADD() Macro</title>
193 In addition to the kludgey solution to the broken line problem
194 described above, there is a clean solution. The DEBUGADD() macro never
195 generates a header. It will append new text to the current debug
196 message even if the format buffer is empty. The syntax of the
197 DEBUGADD() macro is the same as that of the DEBUG() macro.
200 <para><programlisting>
201 DEBUG( 0, ("This is the first line.\n" ) );
202 DEBUGADD( 0, ("This is the second line.\nThis is the third line.\n" ) );
203 </programlisting></para>
205 <para>Produces</para>
207 <para><programlisting>
208 [1998/07/30 16:00:51, 0] file.c:function(512)
209 This is the first line.
210 This is the second line.
211 This is the third line.
212 </programlisting></para>
217 <title>The DEBUGLVL() Macro</title>
220 One of the problems with the DEBUG() macro was that DEBUG() lines
221 tended to get a bit long. Consider this example from
225 <para><programlisting>
226 DEBUG(3,("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
227 type, global_myname, subrec->subnet_name, work->work_group));
228 </programlisting></para>
231 One solution to this is to break it down using DEBUG() and DEBUGADD(),
235 <para><programlisting>
236 DEBUG( 3, ( "send_local_master_announcement: " ) );
237 DEBUGADD( 3, ( "type %x for name %s ", type, global_myname ) );
238 DEBUGADD( 3, ( "on subnet %s ", subrec->subnet_name ) );
239 DEBUGADD( 3, ( "for workgroup %s\n", work->work_group ) );
240 </programlisting></para>
243 A similar, but arguably nicer approach is to use the DEBUGLVL() macro.
244 This macro returns True if the message level is less than or equal to
245 the global DEBUGLEVEL value, so:
248 <para><programlisting>
251 dbgtext( "send_local_master_announcement: " );
252 dbgtext( "type %x for name %s ", type, global_myname );
253 dbgtext( "on subnet %s ", subrec->subnet_name );
254 dbgtext( "for workgroup %s\n", work->work_group );
256 </programlisting></para>
258 <para>(The dbgtext() function is explained below.)</para>
260 <para>There are a few advantages to this scheme:</para>
263 The test is performed only once.
266 You can allocate variables off of the stack that will only be used
267 within the DEBUGLVL() block.
270 Processing that is only relevant to debug output can be contained
271 within the DEBUGLVL() block.
278 <title>New Functions</title>
281 <title>dbgtext()</title>
283 This function prints debug message text to the debug file (and
284 possibly to syslog) via the format buffer. The function uses a
285 variable argument list just like printf() or Debug1(). The
286 input is printed into a buffer using the vslprintf() function,
287 and then passed to format_debug_text().
289 If you use DEBUGLVL() you will probably print the body of the
290 message using dbgtext().
295 <title>dbghdr()</title>
297 This is the function that writes a debug message header.
298 Headers are not processed via the format buffer. Also note that
299 if the format buffer is not empty, a call to dbghdr() will not
300 produce any output. See the comments in dbghdr() for more info.
304 It is not likely that this function will be called directly. It
305 is used by DEBUG() and DEBUGADD().
310 <title>format_debug_text()</title>
312 This is a static function in debug.c. It stores the output text
313 for the body of the message in a buffer until it encounters a
314 newline. When the newline character is found, the buffer is
315 written to the debug file via the Debug1() function, and the
316 buffer is reset. This allows us to add the indentation at the
317 beginning of each line of the message body, and also ensures
318 that the output is written a line at a time (which cleans up