r15191: Avoid uint_t as it's not standard.
[jelmer/samba4-debian.git] / source / 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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "system/time.h"
25 #include "dynconfig.h"
26
27 /**
28  * @file
29  * @brief Debug logging
30  **/
31
32 /** 
33  * this global variable determines what messages are printed 
34  */
35 _PUBLIC_ int DEBUGLEVEL;
36
37 /* the registered mutex handlers */
38 static struct {
39         const char *name;
40         struct debug_ops ops;
41 } debug_handlers;
42
43 /* state variables for the debug system */
44 static struct {
45         int fd;
46         enum debug_logtype logtype;
47         const char *prog_name;
48 } state;
49
50 static BOOL reopen_logs_scheduled;
51 static BOOL check_reopen_logs(void)
52 {
53         if (state.fd == 0 || reopen_logs_scheduled) {
54                 reopen_logs_scheduled = False;
55                 reopen_logs();
56         }
57
58         if (state.fd <= 0) return False;
59
60         return True;
61 }
62
63 _PUBLIC_ void debug_schedule_reopen_logs(void)
64 {
65         reopen_logs_scheduled = True;
66 }
67
68 static void log_timestring(int level, const char *location, const char *func)
69 {
70         char *t = NULL;
71         char *s = NULL;
72
73         if (!check_reopen_logs()) return;
74
75         if (state.logtype != DEBUG_FILE) return;
76
77         t = timestring(NULL, time(NULL));
78         if (!t) return;
79
80         asprintf(&s, "[%s, %d %s:%s()]\n", t, level, location, func);
81         talloc_free(t);
82         if (!s) return;
83
84         write(state.fd, s, strlen(s));
85         free(s);
86 }
87
88 /**
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
91 */
92 _PUBLIC_ void do_debug_header(int level, const char *location, const char *func)
93 {
94         log_timestring(level, location, func);
95         log_task_id();
96 }
97
98 /**
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
101
102   @note You should never have to call this function directly. Call the DEBUG()
103   macro instead.
104 */
105 _PUBLIC_ void do_debug(const char *format, ...) _PRINTF_ATTRIBUTE(1,2)
106 {
107         va_list ap;
108         char *s = NULL;
109
110         if (!check_reopen_logs()) return;
111
112         va_start(ap, format);
113         vasprintf(&s, format, ap);
114         va_end(ap);
115
116         write(state.fd, s, strlen(s));
117         free(s);
118 }
119
120 _PUBLIC_ const char *logfile = NULL;
121
122 /**
123   reopen the log file (usually called because the log file name might have changed)
124 */
125 _PUBLIC_ void reopen_logs(void)
126 {
127         char *fname = NULL;
128         int old_fd = state.fd;
129
130         switch (state.logtype) {
131         case DEBUG_STDOUT:
132                 state.fd = 1;
133                 break;
134
135         case DEBUG_STDERR:
136                 state.fd = 2;
137                 break;
138
139         case DEBUG_FILE:
140                 if ((*logfile) == '/') {
141                         fname = strdup(logfile);
142                 } else {
143                         asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, state.prog_name);
144                 }
145                 if (fname) {
146                         int newfd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0600);
147                         if (newfd == -1) {
148                                 DEBUG(1, ("Failed to open new logfile: %s\n", fname));
149                                 old_fd = -1;
150                         } else {
151                                 state.fd = newfd;
152                         }
153                         free(fname);
154                 } else {
155                         DEBUG(1, ("Failed to find name for file-based logfile!\n"));
156                 }
157
158                 break;
159         }
160
161         if (old_fd > 2) {
162                 close(old_fd);
163         }
164 }
165
166 /**
167   control the name of the logfile and whether logging will be to stdout, stderr
168   or a file
169 */
170 _PUBLIC_ void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
171 {
172         if (state.logtype < new_logtype) {
173                 state.logtype = new_logtype;
174         }
175         if (prog_name) {
176                 state.prog_name = prog_name;
177         }
178         reopen_logs();
179 }
180
181 /**
182   return a string constant containing n tabs
183   no more than 10 tabs are returned
184 */
185 _PUBLIC_ const char *do_debug_tab(int n)
186 {
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)];
191 }
192
193
194 /**
195   log suspicious usage - print comments and backtrace
196 */      
197 _PUBLIC_ void log_suspicious_usage(const char *from, const char *info)
198 {
199         if (!debug_handlers.ops.log_suspicious_usage) return;
200
201         debug_handlers.ops.log_suspicious_usage(from, info);
202 }
203
204
205 /**
206   print suspicious usage - print comments and backtrace
207 */      
208 _PUBLIC_ void print_suspicious_usage(const char* from, const char* info)
209 {
210         if (!debug_handlers.ops.print_suspicious_usage) return;
211
212         debug_handlers.ops.print_suspicious_usage(from, info);
213 }
214
215 _PUBLIC_ uint32_t get_task_id(void)
216 {
217         if (debug_handlers.ops.get_task_id) {
218                 return debug_handlers.ops.get_task_id();
219         }
220         return getpid();
221 }
222
223 _PUBLIC_ void log_task_id(void)
224 {
225         if (!debug_handlers.ops.log_task_id) return;
226
227         if (!check_reopen_logs()) return;
228
229         debug_handlers.ops.log_task_id(state.fd);
230 }
231
232 /**
233   register a set of debug handlers. 
234 */
235 _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops)
236 {
237         debug_handlers.name = name;
238         debug_handlers.ops = *ops;
239 }