ESDM
Middleware for Earth System Data
esdm-debug.h
1 #ifndef ESDM_DEBUG_H
2 #define ESDM_DEBUG_H
3 
4 #include <assert.h>
5 #include <stdint.h>
6 #include <unistd.h>
7 
8 void esdmI_log_dump();
9 
10 //While this macro evaluates `expression` twice, that's perfectly ok:
11 // * An assert with a side effect is erroneous anyways, as it can be compiled out, and thus should have no impact on execution, whatsoever.
12 // * The assert prints its stringified argument when it fails, so we need to pass on the tokens for the assert to print.
13 #define eassert(expression)\
14  do { \
15  if(! (expression)){\
16  esdmI_log_dump(); \
17  assert(expression);\
18  exit(2); \
19  }\
20  } while (0)
21 
22 void esdm_log(uint32_t loglevel, const char *format, ...);
23 
24 #define ESDM_LOG(fmt) ESDM_LOG_FMT(ESDM_LOGLEVEL_DEBUG, "%s", fmt)
25 #define ESDM_LOG_FMT(loglevel, fmt, ...) esdm_log(loglevel, "%s:%d %s(): " fmt "\n", __FILE__, __LINE__, __func__, __VA_ARGS__)
26 #define ESDM_LOG_COM_FMT(loglevel, component, fmt, ...) esdm_log(loglevel, "[%s] %s:%d %s(): " fmt "\n", component, __FILE__, __LINE__, __func__, __VA_ARGS__)
27 
28 #ifdef DEBUG_OFF
29 // remove debug messages in total
30 # define ESDM_DEBUG(fmt)
31 # define ESDM_DEBUG_FMT(fmt, ...)
32 # define ESDM_DEBUG_COM_FMT(com, fmt, ...)
33 #else
34 # define ESDM_DEBUG(fmt) ESDM_LOG_FMT(ESDM_LOGLEVEL_DEBUG, "%s", fmt)
35 # define ESDM_DEBUG_FMT(fmt, ...) ESDM_LOG_FMT(ESDM_LOGLEVEL_DEBUG, fmt, __VA_ARGS__)
36 # define ESDM_DEBUG_COM_FMT(component, fmt, ...) ESDM_LOG_COM_FMT(ESDM_LOGLEVEL_DEBUG, component, fmt, __VA_ARGS__)
37 #endif
38 
39 //These macros terminate the process.
40 //If you only want to log an error, use the ESDM_LOG*() macros instead.
41 #define ESDM_ERROR(fmt) \
42  do { \
43  ESDM_LOG_FMT(ESDM_LOGLEVEL_ERROR, "ERROR %s", fmt); \
44  exit(1); \
45  } while (0)
46 #define ESDM_ERROR_FMT(fmt, ...) \
47  do { \
48  ESDM_LOG_FMT(ESDM_LOGLEVEL_ERROR, "ERROR " fmt, __VA_ARGS__); \
49  exit(1); \
50  } while (0)
51 #define ESDM_ERROR_COM_FMT(component, fmt, ...) \
52  do { \
53  ESDM_LOG_COM_FMT(ESDM_LOGLEVEL_ERROR, component, "ERROR " fmt, __VA_ARGS__); \
54  exit(1); \
55  } while (0)
56 
57 #define ESDM_INFO(fmt) \
58  do { \
59  ESDM_LOG_FMT(ESDM_LOGLEVEL_INFO, "INFO %s", fmt); \
60  } while (0)
61 #define ESDM_INFO_FMT(fmt, ...) \
62  do { \
63  ESDM_LOG_FMT(ESDM_LOGLEVEL_INFO, "INFO " fmt, __VA_ARGS__); \
64  } while (0)
65 #define ESDM_INFO_COM_FMT(component, fmt, ...) \
66  do { \
67  ESDM_LOG_COM_FMT(ESDM_LOGLEVEL_INFO, component, "INFO " fmt, __VA_ARGS__); \
68  } while (0)
69 
70 #define ESDM_WARN(fmt) \
71  do { \
72  ESDM_LOG_FMT(ESDM_LOGLEVEL_WARNING, "WARN %s", fmt); \
73  } while (0)
74 #define ESDM_WARN_FMT(fmt, ...) \
75  do { \
76  ESDM_LOG_FMT(ESDM_LOGLEVEL_WARNING, "WARN " fmt, __VA_ARGS__); \
77  } while (0)
78 #define ESDM_WARN_COM_FMT(component, fmt, ...) \
79  do { \
80  ESDM_LOG_COM_FMT(ESDM_LOGLEVEL_WARNING, component, "WARN " fmt, __VA_ARGS__); \
81  } while (0)
82 
83 
84 #endif