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