r13655: Use new name of build header
[samba.git] / source / lib / 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 /* this global variable determines what messages are printed */
28 int DEBUGLEVEL;
29
30
31 /* the registered mutex handlers */
32 static struct {
33         const char *name;
34         struct debug_ops ops;
35 } debug_handlers;
36
37 /* state variables for the debug system */
38 static struct {
39         int fd;
40         enum debug_logtype logtype;
41         const char *prog_name;
42 } state;
43
44 /*
45   the backend for debug messages. Note that the DEBUG() macro has already
46   ensured that the log level has been met before this is called
47 */
48 void do_debug_header(int level, const char *location, const char *func)
49 {
50         log_timestring(level, location, func);
51         log_task_id();
52 }
53
54 /*
55   the backend for debug messages. Note that the DEBUG() macro has already
56   ensured that the log level has been met before this is called
57 */
58 void do_debug(const char *format, ...)
59 {
60         va_list ap;
61         char *s = NULL;
62
63         if (state.fd == 0) {
64                 reopen_logs();
65         }
66
67         if (state.fd <= 0) return;
68
69         va_start(ap, format);
70         vasprintf(&s, format, ap);
71         va_end(ap);
72
73         write(state.fd, s, strlen(s));
74         free(s);
75 }
76
77 /*
78   reopen the log file (usually called because the log file name might have changed)
79 */
80 void reopen_logs(void)
81 {
82         const char *logfile = lp_logfile();
83         char *fname = NULL;
84         int old_fd = state.fd;
85
86         switch (state.logtype) {
87         case DEBUG_STDOUT:
88                 state.fd = 1;
89                 break;
90
91         case DEBUG_STDERR:
92                 state.fd = 2;
93                 break;
94
95         case DEBUG_FILE:
96                 if ((*logfile) == '/') {
97                         fname = strdup(logfile);
98                 } else {
99                         asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, state.prog_name);
100                 }
101                 if (fname) {
102                         int newfd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0600);
103                         if (newfd == -1) {
104                                 DEBUG(1, ("Failed to open new logfile: %s\n", fname));
105                         } else {
106                                 state.fd = newfd;
107                         }
108                         free(fname);
109                 } else {
110                         DEBUG(1, ("Failed to find name for file-based logfile!\n"));
111                 }
112
113                 break;
114         }
115
116         if (old_fd > 2) {
117                 close(old_fd);
118         }
119 }
120
121 /*
122   control the name of the logfile and whether logging will be to stdout, stderr
123   or a file
124 */
125 void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
126 {
127         if (state.logtype < new_logtype) {
128                 state.logtype = new_logtype;
129         }
130         if (prog_name) {
131                 state.prog_name = prog_name;
132         }
133         reopen_logs();
134 }
135
136 /*
137   return a string constant containing n tabs
138   no more than 10 tabs are returned
139 */
140 const char *do_debug_tab(uint_t n)
141 {
142         const char *tabs[] = {"", "\t", "\t\t", "\t\t\t", "\t\t\t\t", "\t\t\t\t\t", 
143                               "\t\t\t\t\t\t", "\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t", 
144                               "\t\t\t\t\t\t\t\t\t", "\t\t\t\t\t\t\t\t\t\t"};
145         return tabs[MIN(n, 10)];
146 }
147
148
149 /*
150   log/print suspicious usage - print comments and backtrace
151 */      
152 void log_suspicious_usage(const char *from, const char *info)
153 {
154         if (debug_handlers.ops.log_suspicious_usage) {
155                 debug_handlers.ops.log_suspicious_usage(from, info);
156         }
157 }
158 void print_suspicious_usage(const char* from, const char* info)
159 {
160         if (debug_handlers.ops.print_suspicious_usage) {
161                 debug_handlers.ops.print_suspicious_usage(from, info);
162         }
163 }
164
165 void log_timestring(int level, const char *location, const char *func)
166 {
167         char *t = NULL;
168         char *s = NULL;
169
170         if (state.logtype != DEBUG_FILE) return;
171
172         t = timestring(NULL, time(NULL));
173         if (!t) return;
174
175         asprintf(&s, "[%s, %d %s:%s()]\n", t, level, location, func);
176         talloc_free(t);
177         if (!s) return;
178
179         write(state.fd, s, strlen(s));
180         free(s);
181 }
182
183 uint32_t get_task_id(void)
184 {
185         if (debug_handlers.ops.get_task_id) {
186                 return debug_handlers.ops.get_task_id();
187         }
188         return getpid();
189 }
190
191 void log_task_id(void)
192 {
193         if (debug_handlers.ops.log_task_id) {
194                 debug_handlers.ops.log_task_id(state.fd);
195         }
196 }
197
198 /*
199   register a set of debug handlers. 
200 */
201 void register_debug_handlers(const char *name, struct debug_ops *ops)
202 {
203         debug_handlers.name = name;
204         debug_handlers.ops = *ops;
205 }