Fix apparent bug...
[satcheck.git] / common.h
1 /*      Copyright (c) 2015 Regents of the University of California
2  *
3  *      Author: Brian Demsky <bdemsky@uci.edu>
4  *
5  *      This program is free software; you can redistribute it and/or
6  *      modify it under the terms of the GNU General Public License
7  *      version 2 as published by the Free Software Foundation.
8  */
9
10 /** @file common.h
11  *  @brief General purpose macros.
12  */
13
14 #ifndef __COMMON_H__
15 #define __COMMON_H__
16
17 #include <stdio.h>
18 #include "config.h"
19
20 extern int model_out;
21 extern int model_err;
22 extern int switch_alloc;
23
24 #define model_dprintf(fd, fmt, ...) do { switch_alloc = 1; dprintf(fd, fmt, ## __VA_ARGS__); switch_alloc = 0; } while (0)
25
26 #define model_print(fmt, ...) do { model_dprintf(model_out, fmt, ## __VA_ARGS__); } while (0)
27
28 #define model_print_err(fmt, ...) do { model_dprintf(model_err, fmt, ## __VA_ARGS__); } while (0)
29
30
31
32 #ifdef CONFIG_DEBUG
33 #define DEBUG(fmt, ...) do { model_print("*** %15s:%-4d %25s() *** " fmt, __FILE__, __LINE__, __func__, ## __VA_ARGS__); } while (0)
34 #define DBG() DEBUG("\n")
35 #define DBG_ENABLED() (1)
36 #else
37 #define DEBUG(fmt, ...)
38 #define DBG()
39 #define DBG_ENABLED() (0)
40 #endif
41
42 void assert_hook(void);
43
44 #ifdef CONFIG_ASSERT
45 #define ASSERT(expr) \
46         do { \
47                 if (!(expr)) { \
48                         fprintf(stderr, "Error: assertion failed in %s at line %d\n", __FILE__, __LINE__); \
49                         /* print_trace(); // Trace printing may cause dynamic memory allocation */ \
50                         assert_hook();                           \
51                         exit(EXIT_FAILURE); \
52                 } \
53         } while (0)
54 #else
55 #define ASSERT(expr) \
56         do { } while (0)
57 #endif/* CONFIG_ASSERT */
58
59 #define error_msg(...) fprintf(stderr, "Error: " __VA_ARGS__)
60
61 void print_trace(void);
62 #endif/* __COMMON_H__ */