055edcb3c4bac3ba121a364af01878a67b2e73c5
[sfrench/samba-autobuild/.git] / lib / util / debug_s4.h
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba debug defines
4    Copyright (C) Andrew Tridgell 2003
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef _SAMBA_DEBUG_H_
21 #define _SAMBA_DEBUG_H_
22
23 /**
24  * @file
25  * @brief Debugging macros
26  */
27
28 /* the debug operations structure - contains function pointers to 
29    various debug implementations of each operation */
30 struct debug_ops {
31         /* function to log (using DEBUG) suspicious usage of data structure */
32         void (*log_suspicious_usage)(const char* from, const char* info);
33                                 
34         /* function to log (using printf) suspicious usage of data structure.
35          * To be used in circumstances when using DEBUG would cause loop. */
36         void (*print_suspicious_usage)(const char* from, const char* info);
37         
38         /* function to return process/thread id */
39         uint32_t (*get_task_id)(void);
40         
41         /* function to log process/thread id */
42         void (*log_task_id)(int fd);
43 };
44
45 #define DEBUGLEVEL *debug_level
46 extern int DEBUGLEVEL;
47
48 #define DEBUGLVL(level) ((level) <= DEBUGLEVEL)
49 #define _DEBUG(level, body, header) do { \
50         if (DEBUGLVL(level)) { \
51                 if (header) { \
52                         dbghdr(level, __location__, __FUNCTION__); \
53                 } \
54                 dbgtext body; \
55         } \
56 } while (0)
57 /** 
58  * Write to the debug log.
59  */
60 #define DEBUG(level, body) _DEBUG(level, body, true)
61 /**
62  * Add data to an existing debug log entry.
63  */
64 #define DEBUGADD(level, body) _DEBUG(level, body, false)
65
66 /* Compatiblity macros for the source3 calling convention */
67 #define DEBUGC(class, level, body) _DEBUG(level, body, true)
68 #define DEBUGADDC(class, level, body) _DEBUG(level, body, false)
69
70 /**
71  * Obtain indentation string for the debug log. 
72  *
73  * Level specified by n.
74  */
75 #define DEBUGTAB(n) do_debug_tab(n)
76
77 /** Possible destinations for the debug log (in order of precedence -
78  * once set to DEBUG_FILE, it is not possible to reset to DEBUG_STDOUT
79  * for example.  This makes it easy to override for debug to stderr on
80  * the command line, as the smb.conf cannot reset it back to
81  * file-based logging */
82 enum debug_logtype {DEBUG_DEFAULT_STDOUT = 0, 
83                     DEBUG_DEFAULT_STDERR, 
84                     DEBUG_STDOUT, 
85                     DEBUG_FILE, 
86                     DEBUG_STDERR};
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 dbghdr(int level, const char *location, const char *func);
93
94 _PUBLIC_ void dbghdrclass(int level, int cls, const char *location, const char *func);
95
96 /**
97   reopen the log file (usually called because the log file name might have changed)
98 */
99 _PUBLIC_ void reopen_logs(void);
100
101 /** 
102  * this global variable determines what messages are printed 
103  */
104 _PUBLIC_ void debug_schedule_reopen_logs(void);
105
106 /**
107   control the name of the logfile and whether logging will be to stdout, stderr
108   or a file
109 */
110 _PUBLIC_ void setup_logging(const char *prog_name, enum debug_logtype new_logtype);
111
112 /**
113   return a string constant containing n tabs
114   no more than 10 tabs are returned
115 */
116 _PUBLIC_ const char *do_debug_tab(int n);
117
118 /**
119   log suspicious usage - print comments and backtrace
120 */      
121 _PUBLIC_ void log_suspicious_usage(const char *from, const char *info);
122
123 /**
124   print suspicious usage - print comments and backtrace
125 */      
126 _PUBLIC_ void print_suspicious_usage(const char* from, const char* info);
127 _PUBLIC_ uint32_t get_task_id(void);
128 _PUBLIC_ void log_task_id(void);
129
130 /**
131   register a set of debug handlers. 
132 */
133 _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops);
134
135 /**
136   the backend for debug messages. Note that the DEBUG() macro has already
137   ensured that the log level has been met before this is called
138
139   @note You should never have to call this function directly. Call the DEBUG()
140   macro instead.
141 */
142 _PUBLIC_ void dbgtext(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
143
144 struct _XFILE;
145 extern struct _XFILE *dbf;
146
147 /* setup talloc logging */
148 void debug_setup_talloc_log(void);
149
150 #endif