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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "dynconfig/dynconfig.h"
28 * @brief Debug logging
32 * this global variable determines what messages are printed
35 _PUBLIC_ int *debug_level = &_debug_level;
36 static int debug_all_class_hack = 1;
37 int *DEBUGLEVEL_CLASS = &debug_all_class_hack; /* For samba 3 */
38 static bool debug_all_class_isset_hack = true;
39 bool *DEBUGLEVEL_CLASS_ISSET = &debug_all_class_isset_hack; /* For samba 3 */
40 XFILE *dbf = NULL; /* For Samba 3*/
42 /* the registered mutex handlers */
48 /* state variables for the debug system */
51 enum debug_logtype logtype;
52 const char *prog_name;
55 static bool reopen_logs_scheduled;
56 static bool check_reopen_logs(void)
58 if (state.fd == 0 || reopen_logs_scheduled) {
59 reopen_logs_scheduled = false;
69 _PUBLIC_ void debug_schedule_reopen_logs(void)
71 reopen_logs_scheduled = true;
74 static void log_timestring(int level, const char *location, const char *func)
79 if (!check_reopen_logs()) return;
81 if (state.logtype != DEBUG_FILE) return;
83 t = timestring(NULL, time(NULL));
86 asprintf(&s, "[%s, %d %s:%s()]\n", t, level, location, func);
90 write(state.fd, s, strlen(s));
95 the backend for debug messages. Note that the DEBUG() macro has already
96 ensured that the log level has been met before this is called
98 _PUBLIC_ void dbghdr(int level, const char *location, const char *func)
100 log_timestring(level, location, func);
105 _PUBLIC_ void dbghdrclass(int level, int dclass, const char *location, const char *func)
107 /* Simple wrapper, Samba 4 doesn't do debug classes */
108 dbghdr(level, location, func);
112 the backend for debug messages. Note that the DEBUG() macro has already
113 ensured that the log level has been met before this is called
115 @note You should never have to call this function directly. Call the DEBUG()
118 _PUBLIC_ void dbgtext(const char *format, ...)
123 if (!check_reopen_logs()) return;
125 va_start(ap, format);
126 vasprintf(&s, format, ap);
129 write(state.fd, s, strlen(s));
133 _PUBLIC_ const char *logfile = NULL;
136 reopen the log file (usually called because the log file name might have changed)
138 _PUBLIC_ void reopen_logs(void)
141 int old_fd = state.fd;
143 switch (state.logtype) {
153 if (logfile && (*logfile) == '/') {
154 fname = strdup(logfile);
156 asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, state.prog_name);
159 int newfd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0600);
161 DEBUG(1, ("Failed to open new logfile: %s\n", fname));
168 DEBUG(1, ("Failed to find name for file-based logfile!\n"));
180 control the name of the logfile and whether logging will be to stdout, stderr
183 _PUBLIC_ void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
185 if (state.logtype < new_logtype) {
186 state.logtype = new_logtype;
189 state.prog_name = prog_name;
195 return a string constant containing n tabs
196 no more than 10 tabs are returned
198 _PUBLIC_ const char *do_debug_tab(int n)
200 const char *tabs[] = {"", "\t", "\t\t", "\t\t\t", "\t\t\t\t", "\t\t\t\t\t",
201 "\t\t\t\t\t\t", "\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t",
202 "\t\t\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t\t\t"};
203 return tabs[MIN(n, 10)];
208 log suspicious usage - print comments and backtrace
210 _PUBLIC_ void log_suspicious_usage(const char *from, const char *info)
212 if (!debug_handlers.ops.log_suspicious_usage) return;
214 debug_handlers.ops.log_suspicious_usage(from, info);
219 print suspicious usage - print comments and backtrace
221 _PUBLIC_ void print_suspicious_usage(const char* from, const char* info)
223 if (!debug_handlers.ops.print_suspicious_usage) return;
225 debug_handlers.ops.print_suspicious_usage(from, info);
228 _PUBLIC_ uint32_t get_task_id(void)
230 if (debug_handlers.ops.get_task_id) {
231 return debug_handlers.ops.get_task_id();
236 _PUBLIC_ void log_task_id(void)
238 if (!debug_handlers.ops.log_task_id) return;
240 if (!check_reopen_logs()) return;
242 debug_handlers.ops.log_task_id(state.fd);
246 register a set of debug handlers.
248 _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops)
250 debug_handlers.name = name;
251 debug_handlers.ops = *ops;