always use firm builtin debug facilities (libcore ones are more or less a copy of...
[libfirm] / ir / common / debug.h
1 /*
2  * Copyright (C) 1995-2007 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  * @version $Id$
26  */
27 #ifndef FIRM_COMMON_DEBUG_H
28 #define FIRM_COMMON_DEBUG_H
29
30 #include "firm_config.h"
31
32 /* WITH DEBUG OUTPUT */
33 #ifdef DEBUG_libfirm
34
35 #include <stdio.h>
36
37 enum firm_dbg_level_t {
38   LEVEL_DEFAULT = 0, /**< Prints always. Use with DBG(). */
39   LEVEL_1 = 1,
40   LEVEL_2 = 2,
41   LEVEL_3 = 4,
42   LEVEL_4 = 8,
43   LEVEL_5 = 16,
44
45   SET_LEVEL_0 = 0,   /**< use with firm_dbg_set_mask(). */
46   SET_LEVEL_1 = 1,
47   SET_LEVEL_2 = 3,
48   SET_LEVEL_3 = 7,
49   SET_LEVEL_4 = 15,
50   SET_LEVEL_5 = 31,
51   SET_LEVEL_ALL = SET_LEVEL_5
52 };
53
54 typedef struct _firm_dbg_module_t firm_dbg_module_t;
55
56 /* Internal function to the debug module. */
57 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
58
59 /* Internal function to the debug module. */
60 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *data);
61
62 /* Internal function to the debug module. */
63 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
64
65 /**
66  * Register a module to the firm debug facility.
67  * If the module has already been registered, no new module is allocated
68  * but the handle is returned. By default, all messages go to @c stderr
69  * and the debug mask is set to 0, i.e. the module is muted.
70  * @param name The name of the module to register.
71  * @return The module handle.
72  */
73 firm_dbg_module_t *firm_dbg_register(const char *name);
74
75 /**
76  * Set the mask of a module.
77  * @param module The module.
78  * @param mask The new mask for the module.
79  */
80 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask);
81
82 /**
83  * Get the mask of a module.
84  * @param module The module handle.
85  * @return The mask currently used by the module.
86  */
87 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module);
88
89 /**
90  * Set the output file of a module.
91  * @param module The module handle.
92  * @param file The new file to use by this handle.
93  */
94 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file);
95
96 #define _DBG_MAIN(func,args) \
97   _firm_dbg_print_msg(__FILE__, __LINE__, func, _firm_dbg_make_msg args)
98
99 /* If we have C99 use the __func__ variable for calling functions name. */
100 #if defined(__STD_VERSION__) && __STD_VERSION >= 199901L
101 #define _DBG(args)      _DBG_MAIN(__func__, args)
102 #else
103
104 /* Else, check for gcc and use the proprietary __FUNCTION__ macro. */
105 #ifdef __GNUC__
106 #define _DBG(args)  _DBG_MAIN(__FUNCTION__, args)
107 #else
108
109 /* Else go without the name of the calling function. */
110 #define _DBG(args)  _DBG_MAIN("", args)
111 #endif  /* __GNUC__ */
112 #endif /* __STD_VERSION__ ... */
113
114 #define _DB(args) _firm_dbg_print args
115
116 /**
117  * Debug messages issued with this macro are always printed, even in
118  * retail versions.
119  * @see DBG()
120  */
121 #define DBG_RETAIL(args)    _DBG(args)
122 #define DB_RETAIL(args)     _DB(args)
123
124 /**
125  * Issue a debug message.
126  * @param args The arguments.
127  *
128  * The arguments is a list surrounded by parentheses. The items
129  * of the list are:
130  * - The module handle as returned by firm_dbg_register().
131  * - The debug mask that you want associate with this message.
132  * - A format string for the message to pass to ir_printf().
133  * - Further optional arguments are passed to ir_printf().
134  *
135  * The mask is anded against the module's mask. If both have some bits
136  * in common, the message is issued. If the given mask is 0, the message
137  * is always dumped regardless of the module's mask. You can also use
138  * the mask in a level based manner, see firm_dbg_level_t.
139  *
140  * Here is an example:
141  * @code
142  * DBG((my_mod, MASK_ERR, "ir node %n is not green", node))
143  * ...
144  * DBG((my_mod, LEVEL_DEFAULT, "entity %e has type %t", ent, type))
145  * @endcode
146  */
147 #define DBG(args)           _DBG(args)
148 #define DB(args)            _DB(args)
149
150 /** create a debug handle in debug mode */
151 #define FIRM_DBG_REGISTER(handle, name) handle = firm_dbg_register(name)
152 #define DEBUG_ONLY(code)   code
153 #define RELEASE_ONLY(code)
154
155 #else /* ndef DEBUG_libfirm */
156
157 /* DEBUG OUTPUT IS COMPLETELY DISABLED */
158
159 #define DBG(x)
160 #define DB(x)
161
162 /** create a debug handle in release mode */
163 #define FIRM_DBG_REGISTER(handle, name)
164 #define DEBUG_ONLY(code)
165 #define RELEASE_ONLY(code) code
166
167 #define firm_dbg_set_mask(module, mask)
168 #define firm_dbg_get_mask(module)
169 #define firm_dbg_set_file(module, file)
170
171 #endif /* DEBUG_libfirm */
172
173 #endif