3 * @author Michael Beck, Sebastian Hack
27 static void firm_dbg_default_printer(struct obstack *obst, const char *fmt, va_list args)
29 static lc_arg_env_t *env = NULL;
32 env = firm_get_arg_env();
34 lc_evoprintf(env, obst, fmt, args);
38 firm_dbg_module_t *firm_dbg_register(const char *name)
40 return lc_dbg_register_with_printer(name, firm_dbg_default_printer);
45 static struct obstack dbg_obst;
46 static set *module_set;
51 struct _firm_dbg_module_t {
58 * Compares two modules by comparing it's names
60 static int module_cmp(const void *p1, const void *p2, size_t size)
62 const firm_dbg_module_t *m1 = p1;
63 const firm_dbg_module_t *m2 = p2;
64 return strcmp(m1->name, m2->name);
68 * initialize the debug module
70 static void firm_dbg_init(void)
72 obstack_init(&dbg_obst);
73 module_set = new_set(module_cmp, 16);
76 firm_dbg_module_t *firm_dbg_register(const char *name)
78 firm_dbg_module_t mod;
86 return set_insert(module_set, &mod, sizeof(mod), HASH_STR(name, strlen(name)));
89 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask)
94 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module)
99 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file)
105 * A message info: a pair of debug handle and message
107 typedef struct _msg_info_t {
109 const firm_dbg_module_t *mod;
113 * Formats a message given by a printf-like format and a va_list argument,
114 * puts the test on an obstack and return a msg_info.
116 static void *make_msg_info(const firm_dbg_module_t *mod, const char *fmt, va_list args)
118 static const char msg_header[] = "%s(%d) %s: ";
119 msg_info_t *res = obstack_alloc(&dbg_obst, sizeof(*res));
121 obstack_grow(&dbg_obst, msg_header, sizeof(msg_header) - 1);
122 ir_obst_vprintf(&dbg_obst, fmt, args);
123 obstack_1grow(&dbg_obst, '\0');
125 res->msg = obstack_finish(&dbg_obst);
130 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
134 if(mask == 0 || (mod->mask & mask)) {
137 res = make_msg_info(mod, fmt, args);
144 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *mi_ptr)
146 msg_info_t *mi = mi_ptr;
148 fprintf(mi->mod->file, mi->msg, filename, line, func);
149 obstack_free(&dbg_obst, mi);
153 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...)
155 if(mask == 0 || (mod->mask & mask)) {
159 ir_obst_vprintf(&dbg_obst, fmt, args);
160 obstack_1grow(&dbg_obst, '\0');
161 res = obstack_finish(&dbg_obst);
162 fprintf(mod->file, res);
163 obstack_free(&dbg_obst, res);
168 #endif /* WITH_LIBCORE */
170 #else /* DEBUG_libfirm */
172 /* some picky compiler don't allow empty files */
175 #endif /* DEBUG_libfirm */