Ticket #1649: Prepare for prerelease mc-4.7.0-pre3 (code cleanup)
[midnight-commander.git] / vfs / samba / lib / debug.c
blobf164b348823c194dd6327a182b3e88307b538ddf
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "includes.h"
24 /* -------------------------------------------------------------------------- **
25 * Defines...
27 * FORMAT_BUFR_MAX - Index of the last byte of the format buffer;
28 * format_bufr[FORMAT_BUFR_MAX] should always be reserved
29 * for a terminating nul byte.
32 #define FORMAT_BUFR_MAX ( sizeof( format_bufr ) - 1 )
34 /* -------------------------------------------------------------------------- **
35 * This module implements Samba's debugging utility.
37 * The syntax of a debugging log file is represented as:
39 * <debugfile> :== { <debugmsg> }
41 * <debugmsg> :== <debughdr> '\n' <debugtext>
43 * <debughdr> :== '[' TIME ',' LEVEL ']' [ [FILENAME ':'] [FUNCTION '()'] ]
45 * <debugtext> :== { <debugline> }
47 * <debugline> :== TEXT '\n'
49 * TEXT is a string of characters excluding the newline character.
50 * LEVEL is the DEBUG level of the message (an integer in the range 0..10).
51 * TIME is a timestamp.
52 * FILENAME is the name of the file from which the debug message was generated.
53 * FUNCTION is the function from which the debug message was generated.
55 * Basically, what that all means is:
57 * - A debugging log file is made up of debug messages.
59 * - Each debug message is made up of a header and text. The header is
60 * separated from the text by a newline.
62 * - The header begins with the timestamp and debug level of the message
63 * enclosed in brackets. The filename and function from which the
64 * message was generated may follow. The filename is terminated by a
65 * colon, and the function name is terminated by parenthesis.
67 * - The message text is made up of zero or more lines, each terminated by
68 * a newline.
71 /* -------------------------------------------------------------------------- **
72 * External variables.
74 * dbf - Global debug file handle.
75 * debugf - Debug file name.
76 * append_log - If True, then the output file will be opened in append
77 * mode.
78 * DEBUGLEVEL - System-wide debug message limit. Messages with message-
79 * levels higher than DEBUGLEVEL will not be processed.
82 FILE *dbf = NULL;
83 pstring debugf = "";
84 BOOL append_log = False;
85 int DEBUGLEVEL = 1;
88 /* -------------------------------------------------------------------------- **
89 * Internal variables.
91 * stdout_logging - Default False, if set to True then dbf will be set to
92 * stdout and debug output will go to dbf only, and not
93 * to syslog. Set in setup_logging() and read in Debug1().
95 * syslog_level - Internal copy of the message debug level. Written by
96 * dbghdr() and read by Debug1().
98 * format_bufr - Used to format debug messages. The dbgtext() function
99 * prints debug messages to a string, and then passes the
100 * string to format_debug_text(), which uses format_bufr
101 * to build the formatted output.
103 * format_pos - Marks the first free byte of the format_bufr.
106 static BOOL stdout_logging = False;
107 static pstring format_bufr = { '\0' };
108 static size_t format_pos = 0;
111 /* -------------------------------------------------------------------------- **
112 * Functions...
115 /* ************************************************************************** **
116 * get ready for syslog stuff
117 * ************************************************************************** **
119 void setup_logging( const char *pname, BOOL interactive )
121 (void) pname;
122 if( interactive )
124 stdout_logging = True;
125 dbf = stderr;
127 } /* setup_logging */
129 /* ************************************************************************** **
130 * Write an debug message on the debugfile.
131 * This is called by dbghdr() and format_debug_text().
132 * ************************************************************************** **
134 #ifdef HAVE_STDARG_H
135 int Debug1( const char *format_str, ... )
137 #else
138 int Debug1(va_alist)
139 va_dcl
141 const char *format_str;
142 #endif
143 va_list ap;
144 int old_errno = errno;
146 if( stdout_logging )
148 #ifdef HAVE_STDARG_H
149 va_start( ap, format_str );
150 #else
151 va_start( ap );
152 format_str = va_arg( ap, const char * );
153 #endif
154 (void)vfprintf( dbf, format_str, ap );
155 va_end( ap );
156 errno = old_errno;
157 return( 0 );
160 if( !dbf && *debugf)
162 mode_t oldumask = umask( 022 );
164 if( append_log )
165 dbf = sys_fopen( debugf, "a" );
166 else
167 dbf = sys_fopen( debugf, "w" );
168 (void)umask( oldumask );
169 if( dbf )
171 setbuf( dbf, NULL );
173 else
175 errno = old_errno;
176 return(0);
180 if (dbf)
182 #ifdef HAVE_STDARG_H
183 va_start( ap, format_str );
184 #else
185 va_start( ap );
186 format_str = va_arg( ap, const char * );
187 #endif
188 (void)vfprintf( dbf, format_str, ap );
189 va_end( ap );
190 (void)fflush( dbf );
193 errno = old_errno;
195 return( 0 );
196 } /* Debug1 */
199 /* ************************************************************************** **
200 * Print the buffer content via Debug1(), then reset the buffer.
202 * Input: none
203 * Output: none
205 * ************************************************************************** **
207 static void bufr_print( void )
209 format_bufr[format_pos] = '\0';
210 (void)Debug1( "%s", format_bufr );
211 format_pos = 0;
212 } /* bufr_print */
214 /* ************************************************************************** **
215 * Format the debug message text.
217 * Input: msg - Text to be added to the "current" debug message text.
219 * Output: none.
221 * Notes: The purpose of this is two-fold. First, each call to syslog()
222 * (used by Debug1(), see above) generates a new line of syslog
223 * output. This is fixed by storing the partial lines until the
224 * newline character is encountered. Second, printing the debug
225 * message lines when a newline is encountered allows us to add
226 * spaces, thus indenting the body of the message and making it
227 * more readable.
229 * ************************************************************************** **
231 static void format_debug_text( char *msg )
233 size_t i;
234 BOOL timestamp = (!stdout_logging && (lp_timestamp_logs() ||
235 !(lp_loaded())));
237 for( i = 0; msg[i]; i++ )
239 /* Indent two spaces at each new line. */
240 if(timestamp && 0 == format_pos)
242 format_bufr[0] = format_bufr[1] = ' ';
243 format_pos = 2;
246 /* If there's room, copy the character to the format buffer. */
247 if( format_pos < FORMAT_BUFR_MAX )
248 format_bufr[format_pos++] = msg[i];
250 /* If a newline is encountered, print & restart. */
251 if( '\n' == msg[i] )
252 bufr_print();
254 /* If the buffer is full dump it out, reset it, and put out a line
255 * continuation indicator.
257 if( format_pos >= FORMAT_BUFR_MAX )
259 bufr_print();
260 (void)Debug1( " +>\n" );
264 /* Just to be safe... */
265 format_bufr[format_pos] = '\0';
266 } /* format_debug_text */
268 /* ************************************************************************** **
269 * Flush debug output, including the format buffer content.
271 * Input: none
272 * Output: none
274 * ************************************************************************** **
276 void dbgflush( void )
278 bufr_print();
279 (void)fflush( dbf );
280 } /* dbgflush */
282 /* ************************************************************************** **
283 * Print a Debug Header.
285 * Input: level - Debug level of the message (not the system-wide debug
286 * level.
287 * file - Pointer to a string containing the name of the file
288 * from which this function was called, or an empty string
289 * if the __FILE__ macro is not implemented.
290 * func - Pointer to a string containing the name of the function
291 * from which this function was called, or an empty string
292 * if the __FUNCTION__ macro is not implemented.
293 * line - line number of the call to dbghdr, assuming __LINE__
294 * works.
296 * Output: Always True. This makes it easy to fudge a call to dbghdr()
297 * in a macro, since the function can be called as part of a test.
298 * Eg: ( (level <= DEBUGLEVEL) && (dbghdr(level,"",line)) )
300 * Notes: This function takes care of setting syslog_level.
302 * ************************************************************************** **
304 BOOL dbghdr( int level, const char *file, const char *func, int line )
306 if( format_pos )
308 /* This is a fudge. If there is stuff sitting in the format_bufr, then
309 * the *right* thing to do is to call
310 * format_debug_text( "\n" );
311 * to write the remainder, and then proceed with the new header.
312 * Unfortunately, there are several places in the code at which
313 * the DEBUG() macro is used to build partial lines. That in mind,
314 * we'll work under the assumption that an incomplete line indicates
315 * that a new header is *not* desired.
317 return( True );
320 /* Don't print a header if we're logging to stdout. */
321 if( stdout_logging )
322 return( True );
324 /* Print the header if timestamps are turned on. If parameters are
325 * not yet loaded, then default to timestamps on.
327 if( lp_timestamp_logs() || !(lp_loaded()) )
329 /* Print it all out at once to prevent split syslog output. */
330 (void)Debug1( "[%s, %d] %s:%s(%d)\n",
331 timestring(), level, file, func, line );
334 return( True );
335 } /* dbghdr */
337 /* ************************************************************************** **
338 * Add text to the body of the "current" debug message via the format buffer.
340 * Input: format_str - Format string, as used in printf(), et. al.
341 * ... - Variable argument list.
343 * ..or.. va_alist - Old style variable parameter list starting point.
345 * Output: Always True. See dbghdr() for more info, though this is not
346 * likely to be used in the same way.
348 * ************************************************************************** **
350 #ifdef HAVE_STDARG_H
351 BOOL dbgtext( const char *format_str, ... )
353 va_list ap;
354 pstring msgbuf;
356 va_start( ap, format_str );
357 vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
358 va_end( ap );
360 format_debug_text( msgbuf );
362 return( True );
363 } /* dbgtext */
365 #else
366 BOOL dbgtext( va_alist )
367 va_dcl
369 char *format_str;
370 va_list ap;
371 pstring msgbuf;
373 va_start( ap );
374 format_str = va_arg( ap, char * );
375 vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
376 va_end( ap );
378 format_debug_text( msgbuf );
380 return( True );
381 } /* dbgtext */
383 #endif
385 /* ************************************************************************** */