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