Merge branch 'master' of ssh://git.samba.org/data/git/samba into crypt
[samba.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 } state;
53
54 static bool reopen_logs_scheduled;
55 static bool check_reopen_logs(void)
56 {
57         if (state.fd == 0 || reopen_logs_scheduled) {
58                 reopen_logs_scheduled = false;
59                 reopen_logs();
60         }
61
62         if (state.fd <= 0) 
63                 return false;
64
65         return true;
66 }
67
68 _PUBLIC_ void debug_schedule_reopen_logs(void)
69 {
70         reopen_logs_scheduled = true;
71 }
72
73 static void log_timestring(int level, const char *location, const char *func)
74 {
75         char *t = NULL;
76         char *s = NULL;
77
78         if (!check_reopen_logs()) return;
79
80         if (state.logtype != DEBUG_FILE) return;
81
82         t = timestring(NULL, time(NULL));
83         if (!t) return;
84
85         asprintf(&s, "[%s, %d %s:%s()]\n", t, level, location, func);
86         talloc_free(t);
87         if (!s) return;
88
89         write(state.fd, s, strlen(s));
90         free(s);
91 }
92
93 /**
94   the backend for debug messages. Note that the DEBUG() macro has already
95   ensured that the log level has been met before this is called
96 */
97 _PUBLIC_ void dbghdr(int level, const char *location, const char *func)
98 {
99         log_timestring(level, location, func);
100         log_task_id();
101 }
102
103
104 _PUBLIC_ void dbghdrclass(int level, int class, const char *location, const char *func)
105 {
106         /* Simple wrapper, Samba 4 doesn't do debug classes */
107         dbghdr(level, location, func);
108 }
109
110 /**
111   the backend for debug messages. Note that the DEBUG() macro has already
112   ensured that the log level has been met before this is called
113
114   @note You should never have to call this function directly. Call the DEBUG()
115   macro instead.
116 */
117 _PUBLIC_ void dbgtext(const char *format, ...)
118 {
119         va_list ap;
120         char *s = NULL;
121
122         if (!check_reopen_logs()) return;
123
124         va_start(ap, format);
125         vasprintf(&s, format, ap);
126         va_end(ap);
127
128         write(state.fd, s, strlen(s));
129         free(s);
130 }
131
132 _PUBLIC_ const char *logfile = NULL;
133
134 /**
135   reopen the log file (usually called because the log file name might have changed)
136 */
137 _PUBLIC_ void reopen_logs(void)
138 {
139         char *fname = NULL;
140         int old_fd = state.fd;
141
142         switch (state.logtype) {
143         case DEBUG_STDOUT:
144                 state.fd = 1;
145                 break;
146
147         case DEBUG_STDERR:
148                 state.fd = 2;
149                 break;
150
151         case DEBUG_FILE:
152                 if (logfile && (*logfile) == '/') {
153                         fname = strdup(logfile);
154                 } else {
155                         asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, state.prog_name);
156                 }
157                 if (fname) {
158                         int newfd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0600);
159                         if (newfd == -1) {
160                                 DEBUG(1, ("Failed to open new logfile: %s\n", fname));
161                                 old_fd = -1;
162                         } else {
163                                 state.fd = newfd;
164                         }
165                         free(fname);
166                 } else {
167                         DEBUG(1, ("Failed to find name for file-based logfile!\n"));
168                 }
169
170                 break;
171         }
172
173         if (old_fd > 2) {
174                 close(old_fd);
175         }
176 }
177
178 /**
179   control the name of the logfile and whether logging will be to stdout, stderr
180   or a file
181 */
182 _PUBLIC_ void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
183 {
184         if (state.logtype < new_logtype) {
185                 state.logtype = new_logtype;
186         }
187         if (prog_name) {
188                 state.prog_name = prog_name;
189         }
190         reopen_logs();
191 }
192
193 /**
194   return a string constant containing n tabs
195   no more than 10 tabs are returned
196 */
197 _PUBLIC_ const char *do_debug_tab(int n)
198 {
199         const char *tabs[] = {"", "\t", "\t\t", "\t\t\t", "\t\t\t\t", "\t\t\t\t\t", 
200                               "\t\t\t\t\t\t", "\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"};
202         return tabs[MIN(n, 10)];
203 }
204
205
206 /**
207   log suspicious usage - print comments and backtrace
208 */      
209 _PUBLIC_ void log_suspicious_usage(const char *from, const char *info)
210 {
211         if (!debug_handlers.ops.log_suspicious_usage) return;
212
213         debug_handlers.ops.log_suspicious_usage(from, info);
214 }
215
216
217 /**
218   print suspicious usage - print comments and backtrace
219 */      
220 _PUBLIC_ void print_suspicious_usage(const char* from, const char* info)
221 {
222         if (!debug_handlers.ops.print_suspicious_usage) return;
223
224         debug_handlers.ops.print_suspicious_usage(from, info);
225 }
226
227 _PUBLIC_ uint32_t get_task_id(void)
228 {
229         if (debug_handlers.ops.get_task_id) {
230                 return debug_handlers.ops.get_task_id();
231         }
232         return getpid();
233 }
234
235 _PUBLIC_ void log_task_id(void)
236 {
237         if (!debug_handlers.ops.log_task_id) return;
238
239         if (!check_reopen_logs()) return;
240
241         debug_handlers.ops.log_task_id(state.fd);
242 }
243
244 /**
245   register a set of debug handlers. 
246 */
247 _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops)
248 {
249         debug_handlers.name = name;
250         debug_handlers.ops = *ops;
251 }