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