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' };
114 static size_t format_pos
= 0;
117 /* -------------------------------------------------------------------------- **
121 /* ************************************************************************** **
122 * get ready for syslog stuff
123 * ************************************************************************** **
126 setup_logging (const char *pname
, BOOL interactive
)
131 stdout_logging
= True
;
134 } /* setup_logging */
136 /* ************************************************************************** **
137 * Write an debug message on the debugfile.
138 * This is called by dbghdr() and format_debug_text().
139 * ************************************************************************** **
143 Debug1 (const char *format_str
, ...)
150 const char *format_str
;
153 int old_errno
= errno
;
158 va_start (ap
, format_str
);
161 format_str
= va_arg (ap
, const char *);
163 (void) vfprintf (dbf
, format_str
, ap
);
171 mode_t oldumask
= umask (022);
174 dbf
= sys_fopen (debugf
, "a");
176 dbf
= sys_fopen (debugf
, "w");
177 (void) umask (oldumask
);
192 va_start (ap
, format_str
);
195 format_str
= va_arg (ap
, const char *);
197 (void) vfprintf (dbf
, format_str
, ap
);
208 /* ************************************************************************** **
209 * Print the buffer content via Debug1(), then reset the buffer.
214 * ************************************************************************** **
219 format_bufr
[format_pos
] = '\0';
220 (void) Debug1 ("%s", format_bufr
);
224 /* ************************************************************************** **
225 * Format the debug message text.
227 * Input: msg - Text to be added to the "current" debug message text.
231 * Notes: The purpose of this is two-fold. First, each call to syslog()
232 * (used by Debug1(), see above) generates a new line of syslog
233 * output. This is fixed by storing the partial lines until the
234 * newline character is encountered. Second, printing the debug
235 * message lines when a newline is encountered allows us to add
236 * spaces, thus indenting the body of the message and making it
239 * ************************************************************************** **
242 format_debug_text (char *msg
)
245 BOOL timestamp
= (!stdout_logging
&& (lp_timestamp_logs () || !(lp_loaded ())));
247 for (i
= 0; msg
[i
]; i
++)
249 /* Indent two spaces at each new line. */
250 if (timestamp
&& 0 == format_pos
)
252 format_bufr
[0] = format_bufr
[1] = ' ';
256 /* If there's room, copy the character to the format buffer. */
257 if (format_pos
< FORMAT_BUFR_MAX
)
258 format_bufr
[format_pos
++] = msg
[i
];
260 /* If a newline is encountered, print & restart. */
264 /* If the buffer is full dump it out, reset it, and put out a line
265 * continuation indicator.
267 if (format_pos
>= FORMAT_BUFR_MAX
)
270 (void) Debug1 (" +>\n");
274 /* Just to be safe... */
275 format_bufr
[format_pos
] = '\0';
276 } /* format_debug_text */
278 /* ************************************************************************** **
279 * Flush debug output, including the format buffer content.
284 * ************************************************************************** **
293 /* ************************************************************************** **
294 * Print a Debug Header.
296 * Input: level - Debug level of the message (not the system-wide debug
298 * file - Pointer to a string containing the name of the file
299 * from which this function was called, or an empty string
300 * if the __FILE__ macro is not implemented.
301 * func - Pointer to a string containing the name of the function
302 * from which this function was called, or an empty string
303 * if the __FUNCTION__ macro is not implemented.
304 * line - line number of the call to dbghdr, assuming __LINE__
307 * Output: Always True. This makes it easy to fudge a call to dbghdr()
308 * in a macro, since the function can be called as part of a test.
309 * Eg: ( (level <= DEBUGLEVEL) && (dbghdr(level,"",line)) )
311 * Notes: This function takes care of setting syslog_level.
313 * ************************************************************************** **
316 dbghdr (int level
, const char *file
, const char *func
, int line
)
320 /* This is a fudge. If there is stuff sitting in the format_bufr, then
321 * the *right* thing to do is to call
322 * format_debug_text( "\n" );
323 * to write the remainder, and then proceed with the new header.
324 * Unfortunately, there are several places in the code at which
325 * the DEBUG() macro is used to build partial lines. That in mind,
326 * we'll work under the assumption that an incomplete line indicates
327 * that a new header is *not* desired.
332 /* Don't print a header if we're logging to stdout. */
336 /* Print the header if timestamps are turned on. If parameters are
337 * not yet loaded, then default to timestamps on.
339 if (lp_timestamp_logs () || !(lp_loaded ()))
341 /* Print it all out at once to prevent split syslog output. */
342 (void) Debug1 ("[%s, %d] %s:%s(%d)\n", timestring (), level
, file
, func
, line
);
348 /* ************************************************************************** **
349 * Add text to the body of the "current" debug message via the format buffer.
351 * Input: format_str - Format string, as used in printf(), et. al.
352 * ... - Variable argument list.
354 * ..or.. va_alist - Old style variable parameter list starting point.
356 * Output: Always True. See dbghdr() for more info, though this is not
357 * likely to be used in the same way.
359 * ************************************************************************** **
363 dbgtext (const char *format_str
, ...)
368 va_start (ap
, format_str
);
369 vslprintf (msgbuf
, sizeof (msgbuf
) - 1, format_str
, ap
);
372 format_debug_text (msgbuf
);
387 format_str
= va_arg (ap
, char *);
388 vslprintf (msgbuf
, sizeof (msgbuf
) - 1, format_str
, ap
);
391 format_debug_text (msgbuf
);
398 /* ************************************************************************** */