a3e2a8f7b70686757ef0487120e220fd162e1beb
[libfirm] / ir / common / debug.h
1 /**
2  * Debug facility.
3  * @author Michael Beck, Sebastian Hack
4  * @date 15.12.2004
5  */
6
7 #ifndef _FIRM_DEBUG_H
8 #define _FIRM_DEBUG_H
9
10 #include "firm_config.h"
11
12 #ifdef WITH_LIBCORE
13
14 #include <libcore/debug.h>
15
16 /* use the newer debug implementation in libcore */
17 typedef dbg_module_t firm_dbg_module_t;
18
19 extern firm_dbg_module_t *firm_dbg_register(const char *name);
20
21 #define firm_dbg_set_mask(module, mask) dbg_set_mask(module, mask)
22 #define firm_dbg_get_mask(module)       dbg_get_mask(module)
23 #define firm_dbg_set_file(module, file) dbg_set_file(module, file)
24
25 #else
26 /* use the builtin debug implementation */
27
28 #include <stdio.h>
29
30 enum firm_dbg_level_t {
31         LEVEL_DEFAULT = 0, /**< Prints always. Use with DBG(). */
32         LEVEL_1 = 1,
33         LEVEL_2 = 2,
34         LEVEL_3 = 4,
35         LEVEL_4 = 8,
36         LEVEL_5 = 16,
37
38         SET_LEVEL_1 = 1,   /**< use with firm_dbg_set_mask(). */
39         SET_LEVEL_2 = 3,
40         SET_LEVEL_3 = 7,
41         SET_LEVEL_4 = 15,
42         SET_LEVEL_5 = 31
43 };
44
45 typedef struct _firm_dbg_module_t firm_dbg_module_t;
46
47 /* Internal function to the debug module. */
48 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
49
50 /* Internal function to the debug module. */
51 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *data);
52
53 /**
54  * Register a module to the firm debug facility.
55  * If the module has already been registered, no new module is allocated
56  * but the handle is returned. By default, all messages go to @c stderr
57  * and the debug mask is set to 0, i.e. the module is muted.
58  * @param name The name of the module to register.
59  * @return The module handle.
60  */
61 firm_dbg_module_t *firm_dbg_register(const char *name);
62
63 /**
64  * Set the mask of a module.
65  * @param module The module.
66  * @param mask The new mask for the module.
67  */
68 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask);
69
70 /**
71  * Get the mask of a module.
72  * @param module The module handle.
73  * @return The mask currently used by the module.
74  */
75 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module);
76
77 /**
78  * Set the output file of a module.
79  * @param module The module handle.
80  * @param file The new file to use by this handle.
81  */
82 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file);
83
84 #define _DBG_MAIN(func,args) \
85         _firm_dbg_print_msg(__FILE__, __LINE__, func, _firm_dbg_make_msg args)
86
87 /* If we have C99 use the __func__ variable for calling functions name. */
88 #if defined(__STD_VERSION__) && __STD_VERSION >= 199901L
89 #define _DBG(args)      _DBG_MAIN(__func__, args)
90 #else
91
92 /* Else, check for gcc and use the proprietary __FUNCTION__ macro. */
93 #ifdef __GNUC__
94 #define _DBG(args)      _DBG_MAIN(__FUNCTION__, args)
95 #else
96
97 /* Else go without the name of the calling function. */
98 #define _DBG(args)      _DBG_MAIN("", args)
99 #endif
100 #endif
101
102 /**
103  * Debug messages issued with this macro are always printed, even in
104  * retail versions.
105  * @see DBG()
106  */
107 #define DBG_RETAIL(args)                _DBG(args)
108
109 #ifdef DEBUG_libfirm
110
111 /**
112  * Issue a debug message.
113  * @param args The arguments.
114  *
115  * The arguments is a list surrounded by parentheses. The items
116  * of the list are:
117  * - The module handle as returned by firm_dbg_register().
118  * - The debug mask that you want associate with this message.
119  * - A format string for the message to pass to ir_printf().
120  * - Further optional arguments are passed to ir_printf().
121  *
122  * The mask is anded against the module's mask. If both have some bits
123  * in common, the message is issued. If the given mask is 0, the message
124  * is always dumped regardless of the module's mask. You can also use
125  * the mask in a level based manner, see firm_dbg_level_t.
126  *
127  * Here is an example:
128  * @code
129  * DBG((my_mod, MASK_ERR, "ir node %n is not green", node))
130  * ...
131  * DBG((my_mod, LEVEL_DEFAULT, "entity %e has type %t", ent, type))
132  * @endcode
133  */
134 #define DBG(args)                                       _DBG(args)
135
136 #else
137 #define DBG(args)
138 #endif
139
140 #endif /* WITH_LIBCORE */
141
142 #endif /* _FIRM_DEBUG_H */