2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2003
5 Copyright (C) James J Myers 2003
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "system/filesys.h"
24 #include "system/time.h"
25 #include "dynconfig.h"
29 * @brief Debug logging
33 * this global variable determines what messages are printed
35 _PUBLIC_ int DEBUGLEVEL;
37 /* the registered mutex handlers */
43 /* state variables for the debug system */
46 enum debug_logtype logtype;
47 const char *prog_name;
50 static BOOL reopen_logs_scheduled;
51 static BOOL check_reopen_logs(void)
53 if (state.fd == 0 || reopen_logs_scheduled) {
54 reopen_logs_scheduled = False;
58 if (state.fd <= 0) return False;
63 _PUBLIC_ void debug_schedule_reopen_logs(void)
65 reopen_logs_scheduled = True;
68 static void log_timestring(int level, const char *location, const char *func)
73 if (!check_reopen_logs()) return;
75 if (state.logtype != DEBUG_FILE) return;
77 t = timestring(NULL, time(NULL));
80 asprintf(&s, "[%s, %d %s:%s()]\n", t, level, location, func);
84 write(state.fd, s, strlen(s));
89 the backend for debug messages. Note that the DEBUG() macro has already
90 ensured that the log level has been met before this is called
92 _PUBLIC_ void do_debug_header(int level, const char *location, const char *func)
94 log_timestring(level, location, func);
99 the backend for debug messages. Note that the DEBUG() macro has already
100 ensured that the log level has been met before this is called
102 @note You should never have to call this function directly. Call the DEBUG()
105 _PUBLIC_ void do_debug(const char *format, ...) _PRINTF_ATTRIBUTE(1,2)
110 if (!check_reopen_logs()) return;
112 va_start(ap, format);
113 vasprintf(&s, format, ap);
116 write(state.fd, s, strlen(s));
120 _PUBLIC_ const char *logfile = NULL;
123 reopen the log file (usually called because the log file name might have changed)
125 _PUBLIC_ void reopen_logs(void)
128 int old_fd = state.fd;
130 switch (state.logtype) {
140 if ((*logfile) == '/') {
141 fname = strdup(logfile);
143 asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, state.prog_name);
146 int newfd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0600);
148 DEBUG(1, ("Failed to open new logfile: %s\n", fname));
155 DEBUG(1, ("Failed to find name for file-based logfile!\n"));
167 control the name of the logfile and whether logging will be to stdout, stderr
170 _PUBLIC_ void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
172 if (state.logtype < new_logtype) {
173 state.logtype = new_logtype;
176 state.prog_name = prog_name;
182 return a string constant containing n tabs
183 no more than 10 tabs are returned
185 _PUBLIC_ const char *do_debug_tab(int n)
187 const char *tabs[] = {"", "\t", "\t\t", "\t\t\t", "\t\t\t\t", "\t\t\t\t\t",
188 "\t\t\t\t\t\t", "\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t",
189 "\t\t\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t\t\t"};
190 return tabs[MIN(n, 10)];
195 log suspicious usage - print comments and backtrace
197 _PUBLIC_ void log_suspicious_usage(const char *from, const char *info)
199 if (!debug_handlers.ops.log_suspicious_usage) return;
201 debug_handlers.ops.log_suspicious_usage(from, info);
206 print suspicious usage - print comments and backtrace
208 _PUBLIC_ void print_suspicious_usage(const char* from, const char* info)
210 if (!debug_handlers.ops.print_suspicious_usage) return;
212 debug_handlers.ops.print_suspicious_usage(from, info);
215 _PUBLIC_ uint32_t get_task_id(void)
217 if (debug_handlers.ops.get_task_id) {
218 return debug_handlers.ops.get_task_id();
223 _PUBLIC_ void log_task_id(void)
225 if (!debug_handlers.ops.log_task_id) return;
227 if (!check_reopen_logs()) return;
229 debug_handlers.ops.log_task_id(state.fd);
233 register a set of debug handlers.
235 _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops)
237 debug_handlers.name = name;
238 debug_handlers.ops = *ops;