Merge branch 'selftest' of git://git.samba.org/jelmer/samba
[ira/wip.git] / lib / util / debug.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 /**
21  * @file
22  * @brief Debugging macros
23  */
24
25 /* the debug operations structure - contains function pointers to 
26    various debug implementations of each operation */
27 struct debug_ops {
28         /* function to log (using DEBUG) suspicious usage of data structure */
29         void (*log_suspicious_usage)(const char* from, const char* info);
30                                 
31         /* function to log (using printf) suspicious usage of data structure.
32          * To be used in circumstances when using DEBUG would cause loop. */
33         void (*print_suspicious_usage)(const char* from, const char* info);
34         
35         /* function to return process/thread id */
36         uint32_t (*get_task_id)(void);
37         
38         /* function to log process/thread id */
39         void (*log_task_id)(int fd);
40 };
41
42 #define DEBUGLEVEL *debug_level
43 extern int DEBUGLEVEL;
44
45 #define debug_ctx() (_debug_ctx?_debug_ctx:(_debug_ctx=talloc_new(NULL)))
46
47 #define DEBUGLVL(level) ((level) <= DEBUGLEVEL)
48 #define _DEBUG(level, body, header) do { \
49         if (DEBUGLVL(level)) { \
50                 void* _debug_ctx=NULL; \
51                 if (header) { \
52                         dbghdr(level, __location__, __FUNCTION__); \
53                 } \
54                 dbgtext body; \
55                 talloc_free(_debug_ctx); \
56         } \
57 } while (0)
58 /** 
59  * Write to the debug log.
60  */
61 #define DEBUG(level, body) _DEBUG(level, body, true)
62 /**
63  * Add data to an existing debug log entry.
64  */
65 #define DEBUGADD(level, body) _DEBUG(level, body, false)
66
67 /**
68  * Obtain indentation string for the debug log. 
69  *
70  * Level specified by n.
71  */
72 #define DEBUGTAB(n) do_debug_tab(n)
73
74 /** Possible destinations for the debug log */
75 enum debug_logtype {DEBUG_STDOUT = 0, DEBUG_FILE = 1, DEBUG_STDERR = 2};
76
77 /**
78   the backend for debug messages. Note that the DEBUG() macro has already
79   ensured that the log level has been met before this is called
80 */
81 _PUBLIC_ void dbghdr(int level, const char *location, const char *func);
82
83 _PUBLIC_ void dbghdrclass(int level, int class, const char *location, const char *func);
84
85 /**
86   reopen the log file (usually called because the log file name might have changed)
87 */
88 _PUBLIC_ void reopen_logs(void);
89
90 /** 
91  * this global variable determines what messages are printed 
92  */
93 _PUBLIC_ void debug_schedule_reopen_logs(void);
94
95 /**
96   control the name of the logfile and whether logging will be to stdout, stderr
97   or a file
98 */
99 _PUBLIC_ void setup_logging(const char *prog_name, enum debug_logtype new_logtype);
100
101 /**
102   return a string constant containing n tabs
103   no more than 10 tabs are returned
104 */
105 _PUBLIC_ const char *do_debug_tab(int n);
106
107 /**
108   log suspicious usage - print comments and backtrace
109 */      
110 _PUBLIC_ void log_suspicious_usage(const char *from, const char *info);
111
112 /**
113   print suspicious usage - print comments and backtrace
114 */      
115 _PUBLIC_ void print_suspicious_usage(const char* from, const char* info);
116 _PUBLIC_ uint32_t get_task_id(void);
117 _PUBLIC_ void log_task_id(void);
118
119 /**
120   register a set of debug handlers. 
121 */
122 _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops);
123
124 /**
125   the backend for debug messages. Note that the DEBUG() macro has already
126   ensured that the log level has been met before this is called
127
128   @note You should never have to call this function directly. Call the DEBUG()
129   macro instead.
130 */
131 _PUBLIC_ void dbgtext(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
132
133 extern XFILE *dbf;