we always compile with C99 support now
[libfirm] / ir / common / debug.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Debug facility.
23  * @author  Michael Beck, Sebastian Hack
24  * @date    15.12.2004
25  */
26 #ifndef FIRM_COMMON_DEBUG_H
27 #define FIRM_COMMON_DEBUG_H
28
29 /* WITH DEBUG OUTPUT */
30 #ifdef DEBUG_libfirm
31
32 #include <stdio.h>
33
34 enum firm_dbg_level_t {
35         LEVEL_DEFAULT = 0, /**< Prints always. Use with DBG(). */
36         LEVEL_1 = 1,
37         LEVEL_2 = 2,
38         LEVEL_3 = 4,
39         LEVEL_4 = 8,
40         LEVEL_5 = 16,
41
42         SET_LEVEL_0 = 0,   /**< use with firm_dbg_set_mask(). */
43         SET_LEVEL_1 = 1,
44         SET_LEVEL_2 = 3,
45         SET_LEVEL_3 = 7,
46         SET_LEVEL_4 = 15,
47         SET_LEVEL_5 = 31,
48         SET_LEVEL_ALL = SET_LEVEL_5
49 };
50
51 typedef struct firm_dbg_module_t firm_dbg_module_t;
52
53 /* Internal function to the debug module. */
54 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
55
56 /* Internal function to the debug module. */
57 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *data);
58
59 /* Internal function to the debug module. */
60 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
61
62 /**
63  * Register a module to the firm debug facility.
64  * If the module has already been registered, no new module is allocated
65  * but the handle is returned. By default, all messages go to @c stderr
66  * and the debug mask is set to 0, i.e. the module is muted.
67  * @param name The name of the module to register.
68  * @return The module handle.
69  */
70 firm_dbg_module_t *firm_dbg_register(const char *name);
71
72 /**
73  * Set the mask of a module.
74  * @param module The module.
75  * @param mask The new mask for the module.
76  */
77 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask);
78
79 /**
80  * Get the mask of a module.
81  * @param module The module handle.
82  * @return The mask currently used by the module.
83  */
84 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module);
85
86 /**
87  * Set the output file of a module.
88  * @param module The module handle.
89  * @param file The new file to use by this handle.
90  */
91 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file);
92
93 #define _DBG_MAIN(func,args) \
94   _firm_dbg_print_msg(__FILE__, __LINE__, func, _firm_dbg_make_msg args)
95
96 #define _DBG(args)  _DBG_MAIN(__func__, args)
97 #define _DB(args) _firm_dbg_print args
98
99 /**
100  * Debug messages issued with this macro are always printed, even in
101  * retail versions.
102  * @see DBG()
103  */
104 #define DBG_RETAIL(args)    _DBG(args)
105 #define DB_RETAIL(args)     _DB(args)
106
107 /**
108  * Issue a debug message.
109  * @param args The arguments.
110  *
111  * The arguments is a list surrounded by parentheses. The items
112  * of the list are:
113  * - The module handle as returned by firm_dbg_register().
114  * - The debug mask that you want associate with this message.
115  * - A format string for the message to pass to ir_printf().
116  * - Further optional arguments are passed to ir_printf().
117  *
118  * The mask is anded against the module's mask. If both have some bits
119  * in common, the message is issued. If the given mask is 0, the message
120  * is always dumped regardless of the module's mask. You can also use
121  * the mask in a level based manner, see firm_dbg_level_t.
122  *
123  * Here is an example:
124  * @code
125  * DBG((my_mod, MASK_ERR, "ir node %n is not green", node))
126  * ...
127  * DBG((my_mod, LEVEL_DEFAULT, "entity %e has type %t", ent, type))
128  * @endcode
129  */
130 #define DBG(args)           _DBG(args)
131 #define DB(args)            _DB(args)
132
133 /** create a debug handle in debug mode */
134 #define FIRM_DBG_REGISTER(handle, name) handle = firm_dbg_register(name)
135 #define DEBUG_ONLY(code)   code
136 #define RELEASE_ONLY(code)
137
138 #else /* ndef DEBUG_libfirm */
139
140 /* DEBUG OUTPUT IS COMPLETELY DISABLED */
141
142 #define DBG(x)   (void)0
143 #define DB(x)    (void)0
144
145 /** create a debug handle in release mode */
146 #define FIRM_DBG_REGISTER(handle, name)  (void)0
147 #define DEBUG_ONLY(code)
148 #define RELEASE_ONLY(code) code
149
150 #define firm_dbg_set_mask(module, mask)  (void)0
151 #define firm_dbg_get_mask(module)        (void)0
152 #define firm_dbg_set_file(module, file)  (void)0
153
154 #endif /* DEBUG_libfirm */
155
156 #endif