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