2 Unix SMB/Netbios implementation.
4 Samba utility functions
6 Copyright (C) Andrew Tridgell 1992-1998
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/>.
29 /* -------------------------------------------------------------------------- **
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
76 /* -------------------------------------------------------------------------- **
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
83 * DEBUGLEVEL - System-wide debug message limit. Messages with message-
84 * levels higher than DEBUGLEVEL will not be processed.
89 BOOL append_log
= False
;
93 /* -------------------------------------------------------------------------- **
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 /* -------------------------------------------------------------------------- **
120 /* ************************************************************************** **
121 * get ready for syslog stuff
122 * ************************************************************************** **
124 void setup_logging( const char *pname
, BOOL interactive
)
129 stdout_logging
= True
;
132 } /* setup_logging */
134 /* ************************************************************************** **
135 * Write an debug message on the debugfile.
136 * This is called by dbghdr() and format_debug_text().
137 * ************************************************************************** **
140 int Debug1( const char *format_str
, ... )
146 const char *format_str
;
149 int old_errno
= errno
;
154 va_start( ap
, format_str
);
157 format_str
= va_arg( ap
, const char * );
159 (void)vfprintf( dbf
, format_str
, ap
);
167 mode_t oldumask
= umask( 022 );
170 dbf
= sys_fopen( debugf
, "a" );
172 dbf
= sys_fopen( debugf
, "w" );
173 (void)umask( oldumask
);
188 va_start( ap
, format_str
);
191 format_str
= va_arg( ap
, const char * );
193 (void)vfprintf( dbf
, format_str
, ap
);
204 /* ************************************************************************** **
205 * Print the buffer content via Debug1(), then reset the buffer.
210 * ************************************************************************** **
212 static void bufr_print( void )
214 format_bufr
[format_pos
] = '\0';
215 (void)Debug1( "%s", format_bufr
);
219 /* ************************************************************************** **
220 * Format the debug message text.
222 * Input: msg - Text to be added to the "current" debug message text.
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
234 * ************************************************************************** **
236 static void format_debug_text( char *msg
)
239 BOOL timestamp
= (!stdout_logging
&& (lp_timestamp_logs() ||
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] = ' ';
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. */
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
)
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.
279 * ************************************************************************** **
281 void dbgflush( void )
287 /* ************************************************************************** **
288 * Print a Debug Header.
290 * Input: level - Debug level of the message (not the system-wide debug
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__
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
)
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.
325 /* Don't print a header if we're logging to stdout. */
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
);
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 * ************************************************************************** **
356 BOOL
dbgtext( const char *format_str
, ... )
361 va_start( ap
, format_str
);
362 vslprintf( msgbuf
, sizeof(msgbuf
)-1, format_str
, ap
);
365 format_debug_text( msgbuf
);
371 BOOL
dbgtext( va_alist
)
379 format_str
= va_arg( ap
, char * );
380 vslprintf( msgbuf
, sizeof(msgbuf
)-1, format_str
, ap
);
383 format_debug_text( msgbuf
);
390 /* ************************************************************************** */