seed random number generator with time
[c11tester.git] / common.h
1 /** @file common.h
2  *  @brief General purpose macros.
3  */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include "config.h"
11 #include "printf.h"
12
13 extern int model_out;
14
15 #define model_print(fmt, ...) do { \
16                 char mprintbuf[2048];                                                \
17                 int printbuflen=snprintf_(mprintbuf, 2048, fmt, ## __VA_ARGS__);     \
18                 int lenleft = printbuflen < 2048 ? printbuflen : 2048;                   \
19                 int totalwritten = 0; \
20                 while(lenleft) {                                                    \
21                         int byteswritten=write(model_out, &mprintbuf[totalwritten], lenleft); \
22                         lenleft-=byteswritten;                                            \
23                         totalwritten+=byteswritten;                                       \
24                 }                                                                   \
25 } while (0)
26
27 #ifdef CONFIG_DEBUG
28 #define DEBUG(fmt, ...) do { model_print("*** %15s:%-4d %25s() *** " fmt, __FILE__, __LINE__, __func__, ## __VA_ARGS__); } while (0)
29 #define DBG() DEBUG("\n")
30 #define DBG_ENABLED() (1)
31 #else
32 #define DEBUG(fmt, ...)
33 #define DBG()
34 #define DBG_ENABLED() (0)
35 #endif
36
37 void assert_hook(void);
38
39 #ifdef CONFIG_ASSERT
40 #define ASSERT(expr) \
41         do { \
42                 if (!(expr)) { \
43                         fprintf(stderr, "Error: assertion failed in %s at line %d\n", __FILE__, __LINE__); \
44                         /* print_trace(); // Trace printing may cause dynamic memory allocation */ \
45                         assert_hook();                           \
46                         _Exit(EXIT_FAILURE); \
47                 } \
48         } while (0)
49 #else
50 #define ASSERT(expr) \
51         do { } while (0)
52 #endif  /* CONFIG_ASSERT */
53
54 #define error_msg(...) fprintf(stderr, "Error: " __VA_ARGS__)
55
56 void print_trace(void);
57 #endif  /* __COMMON_H__ */