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