Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / src / vfs / smbfs / helpers / lib / debug.c
blob6b5e3586de2e3627c07f218a074f3c87785c4047
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba utility functions
6 Copyright (C) Andrew Tridgell 1992-1998
8 Copyright (C) 2011
9 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "includes.h"
29 /* -------------------------------------------------------------------------- **
30 * Defines...
32 * FORMAT_BUFR_MAX - Index of the last byte of the format buffer;
33 * format_bufr[FORMAT_BUFR_MAX] should always be reserved
34 * for a terminating nul byte.
37 #define FORMAT_BUFR_MAX ( sizeof( format_bufr ) - 1 )
39 /* -------------------------------------------------------------------------- **
40 * This module implements Samba's debugging utility.
42 * The syntax of a debugging log file is represented as:
44 * <debugfile> :== { <debugmsg> }
46 * <debugmsg> :== <debughdr> '\n' <debugtext>
48 * <debughdr> :== '[' TIME ',' LEVEL ']' [ [FILENAME ':'] [FUNCTION '()'] ]
50 * <debugtext> :== { <debugline> }
52 * <debugline> :== TEXT '\n'
54 * TEXT is a string of characters excluding the newline character.
55 * LEVEL is the DEBUG level of the message (an integer in the range 0..10).
56 * TIME is a timestamp.
57 * FILENAME is the name of the file from which the debug message was generated.
58 * FUNCTION is the function from which the debug message was generated.
60 * Basically, what that all means is:
62 * - A debugging log file is made up of debug messages.
64 * - Each debug message is made up of a header and text. The header is
65 * separated from the text by a newline.
67 * - The header begins with the timestamp and debug level of the message
68 * enclosed in brackets. The filename and function from which the
69 * message was generated may follow. The filename is terminated by a
70 * colon, and the function name is terminated by parenthesis.
72 * - The message text is made up of zero or more lines, each terminated by
73 * a newline.
76 /* -------------------------------------------------------------------------- **
77 * External variables.
79 * dbf - Global debug file handle.
80 * debugf - Debug file name.
81 * append_log - If True, then the output file will be opened in append
82 * mode.
83 * DEBUGLEVEL - System-wide debug message limit. Messages with message-
84 * levels higher than DEBUGLEVEL will not be processed.
87 FILE *dbf = NULL;
88 pstring debugf = "";
89 BOOL append_log = False;
90 int DEBUGLEVEL = 1;
93 /* -------------------------------------------------------------------------- **
94 * Internal variables.
96 * stdout_logging - Default False, if set to True then dbf will be set to
97 * stdout and debug output will go to dbf only, and not
98 * to syslog. Set in setup_logging() and read in Debug1().
100 * syslog_level - Internal copy of the message debug level. Written by
101 * dbghdr() and read by Debug1().
103 * format_bufr - Used to format debug messages. The dbgtext() function
104 * prints debug messages to a string, and then passes the
105 * string to format_debug_text(), which uses format_bufr
106 * to build the formatted output.
108 * format_pos - Marks the first free byte of the format_bufr.
111 static BOOL stdout_logging = False;
112 static pstring format_bufr = { '\0' };
113 static size_t format_pos = 0;
116 /* -------------------------------------------------------------------------- **
117 * Functions...
120 /* ************************************************************************** **
121 * get ready for syslog stuff
122 * ************************************************************************** **
124 void setup_logging( const char *pname, BOOL interactive )
126 (void) pname;
127 if( interactive )
129 stdout_logging = True;
130 dbf = stderr;
132 } /* setup_logging */
134 /* ************************************************************************** **
135 * Write an debug message on the debugfile.
136 * This is called by dbghdr() and format_debug_text().
137 * ************************************************************************** **
139 #ifdef HAVE_STDARG_H
140 int Debug1( const char *format_str, ... )
142 #else
143 int Debug1(va_alist)
144 va_dcl
146 const char *format_str;
147 #endif
148 va_list ap;
149 int old_errno = errno;
151 if( stdout_logging )
153 #ifdef HAVE_STDARG_H
154 va_start( ap, format_str );
155 #else
156 va_start( ap );
157 format_str = va_arg( ap, const char * );
158 #endif
159 (void)vfprintf( dbf, format_str, ap );
160 va_end( ap );
161 errno = old_errno;
162 return( 0 );
165 if( !dbf && *debugf)
167 mode_t oldumask = umask( 022 );
169 if( append_log )
170 dbf = sys_fopen( debugf, "a" );
171 else
172 dbf = sys_fopen( debugf, "w" );
173 (void)umask( oldumask );
174 if( dbf )
176 setbuf( dbf, NULL );
178 else
180 errno = old_errno;
181 return(0);
185 if (dbf)
187 #ifdef HAVE_STDARG_H
188 va_start( ap, format_str );
189 #else
190 va_start( ap );
191 format_str = va_arg( ap, const char * );
192 #endif
193 (void)vfprintf( dbf, format_str, ap );
194 va_end( ap );
195 (void)fflush( dbf );
198 errno = old_errno;
200 return( 0 );
201 } /* Debug1 */
204 /* ************************************************************************** **
205 * Print the buffer content via Debug1(), then reset the buffer.
207 * Input: none
208 * Output: none
210 * ************************************************************************** **
212 static void bufr_print( void )
214 format_bufr[format_pos] = '\0';
215 (void)Debug1( "%s", format_bufr );
216 format_pos = 0;
217 } /* bufr_print */
219 /* ************************************************************************** **
220 * Format the debug message text.
222 * Input: msg - Text to be added to the "current" debug message text.
224 * Output: none.
226 * Notes: The purpose of this is two-fold. First, each call to syslog()
227 * (used by Debug1(), see above) generates a new line of syslog
228 * output. This is fixed by storing the partial lines until the
229 * newline character is encountered. Second, printing the debug
230 * message lines when a newline is encountered allows us to add
231 * spaces, thus indenting the body of the message and making it
232 * more readable.
234 * ************************************************************************** **
236 static void format_debug_text( char *msg )
238 size_t i;
239 BOOL timestamp = (!stdout_logging && (lp_timestamp_logs() ||
240 !(lp_loaded())));
242 for( i = 0; msg[i]; i++ )
244 /* Indent two spaces at each new line. */
245 if(timestamp && 0 == format_pos)
247 format_bufr[0] = format_bufr[1] = ' ';
248 format_pos = 2;
251 /* If there's room, copy the character to the format buffer. */
252 if( format_pos < FORMAT_BUFR_MAX )
253 format_bufr[format_pos++] = msg[i];
255 /* If a newline is encountered, print & restart. */
256 if( '\n' == msg[i] )
257 bufr_print();
259 /* If the buffer is full dump it out, reset it, and put out a line
260 * continuation indicator.
262 if( format_pos >= FORMAT_BUFR_MAX )
264 bufr_print();
265 (void)Debug1( " +>\n" );
269 /* Just to be safe... */
270 format_bufr[format_pos] = '\0';
271 } /* format_debug_text */
273 /* ************************************************************************** **
274 * Flush debug output, including the format buffer content.
276 * Input: none
277 * Output: none
279 * ************************************************************************** **
281 void dbgflush( void )
283 bufr_print();
284 (void)fflush( dbf );
285 } /* dbgflush */
287 /* ************************************************************************** **
288 * Print a Debug Header.
290 * Input: level - Debug level of the message (not the system-wide debug
291 * level.
292 * file - Pointer to a string containing the name of the file
293 * from which this function was called, or an empty string
294 * if the __FILE__ macro is not implemented.
295 * func - Pointer to a string containing the name of the function
296 * from which this function was called, or an empty string
297 * if the __FUNCTION__ macro is not implemented.
298 * line - line number of the call to dbghdr, assuming __LINE__
299 * works.
301 * Output: Always True. This makes it easy to fudge a call to dbghdr()
302 * in a macro, since the function can be called as part of a test.
303 * Eg: ( (level <= DEBUGLEVEL) && (dbghdr(level,"",line)) )
305 * Notes: This function takes care of setting syslog_level.
307 * ************************************************************************** **
309 BOOL dbghdr( int level, const char *file, const char *func, int line )
311 if( format_pos )
313 /* This is a fudge. If there is stuff sitting in the format_bufr, then
314 * the *right* thing to do is to call
315 * format_debug_text( "\n" );
316 * to write the remainder, and then proceed with the new header.
317 * Unfortunately, there are several places in the code at which
318 * the DEBUG() macro is used to build partial lines. That in mind,
319 * we'll work under the assumption that an incomplete line indicates
320 * that a new header is *not* desired.
322 return( True );
325 /* Don't print a header if we're logging to stdout. */
326 if( stdout_logging )
327 return( True );
329 /* Print the header if timestamps are turned on. If parameters are
330 * not yet loaded, then default to timestamps on.
332 if( lp_timestamp_logs() || !(lp_loaded()) )
334 /* Print it all out at once to prevent split syslog output. */
335 (void)Debug1( "[%s, %d] %s:%s(%d)\n",
336 timestring(), level, file, func, line );
339 return( True );
340 } /* dbghdr */
342 /* ************************************************************************** **
343 * Add text to the body of the "current" debug message via the format buffer.
345 * Input: format_str - Format string, as used in printf(), et. al.
346 * ... - Variable argument list.
348 * ..or.. va_alist - Old style variable parameter list starting point.
350 * Output: Always True. See dbghdr() for more info, though this is not
351 * likely to be used in the same way.
353 * ************************************************************************** **
355 #ifdef HAVE_STDARG_H
356 BOOL dbgtext( const char *format_str, ... )
358 va_list ap;
359 pstring msgbuf;
361 va_start( ap, format_str );
362 vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
363 va_end( ap );
365 format_debug_text( msgbuf );
367 return( True );
368 } /* dbgtext */
370 #else
371 BOOL dbgtext( va_alist )
372 va_dcl
374 char *format_str;
375 va_list ap;
376 pstring msgbuf;
378 va_start( ap );
379 format_str = va_arg( ap, char * );
380 vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
381 va_end( ap );
383 format_debug_text( msgbuf );
385 return( True );
386 } /* dbgtext */
388 #endif
390 /* ************************************************************************** */