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