debug: fixed default debug settings
[metze/samba/wip.git] / lib / util / debug.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba debug functions
4    Copyright (C) Andrew Tridgell 2003
5    Copyright (C) James J Myers   2003
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "dynconfig/dynconfig.h"
25
26 /**
27  * @file
28  * @brief Debug logging
29  **/
30
31 /** 
32  * this global variable determines what messages are printed 
33  */
34 int _debug_level = 0;
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
41 /* the registered mutex handlers */
42 static struct {
43         const char *name;
44         struct debug_ops ops;
45 } debug_handlers;
46
47 /* state variables for the debug system */
48 static struct {
49         int fd;
50         enum debug_logtype logtype;
51         const char *prog_name;
52         bool reopening_logs;
53 } state;
54
55 static bool reopen_logs_scheduled;
56 static bool check_reopen_logs(void)
57 {
58         if (state.fd == 0 || reopen_logs_scheduled) {
59                 reopen_logs_scheduled = false;
60                 reopen_logs();
61         }
62
63         if (state.fd <= 0) 
64                 return false;
65
66         return true;
67 }
68
69 _PUBLIC_ void debug_schedule_reopen_logs(void)
70 {
71         reopen_logs_scheduled = true;
72 }
73
74 static void log_timestring(int level, const char *location, const char *func)
75 {
76         char *t = NULL;
77         char *s = NULL;
78
79         if (!check_reopen_logs()) return;
80
81         if (state.logtype != DEBUG_FILE) return;
82
83         t = timestring(NULL, time(NULL));
84         if (!t) return;
85
86         asprintf(&s, "[%s, %d %s:%s()]\n", t, level, location, func);
87         talloc_free(t);
88         if (!s) return;
89
90         write(state.fd, s, strlen(s));
91         free(s);
92 }
93
94 /**
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
97 */
98 _PUBLIC_ void dbghdr(int level, const char *location, const char *func)
99 {
100         log_timestring(level, location, func);
101         log_task_id();
102 }
103
104
105 _PUBLIC_ void dbghdrclass(int level, int dclass, const char *location, const char *func)
106 {
107         /* Simple wrapper, Samba 4 doesn't do debug classes */
108         dbghdr(level, location, func);
109 }
110
111 /**
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
114
115   @note You should never have to call this function directly. Call the DEBUG()
116   macro instead.
117 */
118 _PUBLIC_ void dbgtext(const char *format, ...)
119 {
120         va_list ap;
121
122         if (!check_reopen_logs()) return;
123
124         va_start(ap, format);
125         vdprintf(state.fd, format, ap);
126         va_end(ap);
127 }
128
129 _PUBLIC_ const char *logfile = NULL;
130
131 /**
132   reopen the log file (usually called because the log file name might have changed)
133 */
134 _PUBLIC_ void reopen_logs(void)
135 {
136         char *fname = NULL;
137         int old_fd = state.fd;
138         if (state.reopening_logs) {
139                 return;
140         }
141
142         switch (state.logtype) {
143         case DEBUG_STDOUT:
144         case DEBUG_DEFAULT_STDOUT:
145                 state.fd = 1;
146                 break;
147
148         case DEBUG_STDERR:
149         case DEBUG_DEFAULT_STDERR:
150                 state.fd = 2;
151                 break;
152
153         case DEBUG_FILE:
154                 state.reopening_logs = true;
155                 if (logfile && (*logfile) == '/') {
156                         fname = strdup(logfile);
157                 } else {
158                         asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, state.prog_name);
159                 }
160                 if (fname) {
161                         int newfd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0600);
162                         if (newfd == -1) {
163                                 DEBUG(1, ("Failed to open new logfile: %s\n", fname));
164                                 old_fd = -1;
165                         } else {
166                                 state.fd = newfd;
167                         }
168                         free(fname);
169                 } else {
170                         DEBUG(1, ("Failed to find name for file-based logfile!\n"));
171                 }
172                 state.reopening_logs = false;
173
174                 break;
175         }
176
177         if (old_fd > 2) {
178                 close(old_fd);
179         }
180 }
181
182 /* setup for logging of talloc warnings */
183 static void talloc_log_fn(const char *msg)
184 {
185         DEBUG(0,("%s", msg));
186 }
187
188 void debug_setup_talloc_log(void)
189 {
190         talloc_set_log_fn(talloc_log_fn);
191 }
192
193 /**
194   control the name of the logfile and whether logging will be to stdout, stderr
195   or a file, and set up syslog
196
197   new_log indicates the destination for the debug log (an enum in
198   order of precedence - once set to DEBUG_FILE, it is not possible to
199   reset to DEBUG_STDOUT for example.  This makes it easy to override
200   for debug to stderr on the command line, as the smb.conf cannot
201   reset it back to file-based logging
202 */
203 _PUBLIC_ void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
204 {
205         debug_setup_talloc_log();
206         if (state.logtype < new_logtype) {
207                 state.logtype = new_logtype;
208         }
209         if (prog_name) {
210                 state.prog_name = prog_name;
211         }
212         reopen_logs();
213 }
214
215 /**
216   return a string constant containing n tabs
217   no more than 10 tabs are returned
218 */
219 _PUBLIC_ const char *do_debug_tab(int n)
220 {
221         const char *tabs[] = {"", "\t", "\t\t", "\t\t\t", "\t\t\t\t", "\t\t\t\t\t", 
222                               "\t\t\t\t\t\t", "\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t", 
223                               "\t\t\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t\t\t"};
224         return tabs[MIN(n, 10)];
225 }
226
227
228 /**
229   log suspicious usage - print comments and backtrace
230 */      
231 _PUBLIC_ void log_suspicious_usage(const char *from, const char *info)
232 {
233         if (!debug_handlers.ops.log_suspicious_usage) return;
234
235         debug_handlers.ops.log_suspicious_usage(from, info);
236 }
237
238
239 /**
240   print suspicious usage - print comments and backtrace
241 */      
242 _PUBLIC_ void print_suspicious_usage(const char* from, const char* info)
243 {
244         if (!debug_handlers.ops.print_suspicious_usage) return;
245
246         debug_handlers.ops.print_suspicious_usage(from, info);
247 }
248
249 _PUBLIC_ uint32_t get_task_id(void)
250 {
251         if (debug_handlers.ops.get_task_id) {
252                 return debug_handlers.ops.get_task_id();
253         }
254         return getpid();
255 }
256
257 _PUBLIC_ void log_task_id(void)
258 {
259         if (!debug_handlers.ops.log_task_id) return;
260
261         if (!check_reopen_logs()) return;
262
263         debug_handlers.ops.log_task_id(state.fd);
264 }
265
266 /**
267   register a set of debug handlers. 
268 */
269 _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops)
270 {
271         debug_handlers.name = name;
272         debug_handlers.ops = *ops;
273 }