asrt
Automated System Runtime Testing library
Loading...
Searching...
No Matches
log.h
1
11
12#ifndef ASRT_LOG_H
13#define ASRT_LOG_H
14
15#include <stdarg.h>
16#include <stdio.h>
17#include <time.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
24enum asrt_log_level
25{
26 ASRT_LOG_DEBUG = 1,
27 ASRT_LOG_INFO = 2,
28 ASRT_LOG_ERROR = 4,
29};
30
33void asrt_log( enum asrt_log_level level, char const* module, char const* fmt, ... );
34
36#define ASRT_DBG_LOG( module, ... ) asrt_log( ASRT_LOG_DEBUG, module, __VA_ARGS__ )
37
39#define ASRT_INF_LOG( module, ... ) asrt_log( ASRT_LOG_INFO, module, __VA_ARGS__ )
40
42#define ASRT_ERR_LOG( module, ... ) asrt_log( ASRT_LOG_ERROR, module, __VA_ARGS__ )
43
46#define ASRT_DEFINE_GPOS_LOG_IMPL \
47 void asrt_log_impl( \
48 enum asrt_log_level level, char const* module, char const* fmt, va_list args ) \
49 { \
50 \
51 char const* level_str = "UNK"; \
52 switch ( level ) { \
53 case ASRT_LOG_DEBUG: \
54 level_str = "\033[36mDEBUG\033[0m"; \
55 break; \
56 case ASRT_LOG_INFO: \
57 level_str = "\033[32mINFO\033[0m"; \
58 break; \
59 case ASRT_LOG_ERROR: \
60 level_str = "\033[31mERROR\033[0m"; \
61 break; \
62 default: \
63 break; \
64 } \
65 \
66 char timebuf[32]; \
67 time_t t = time( NULL ); \
68 struct tm tm; \
69 localtime_r( &t, &tm ); \
70 strftime( timebuf, sizeof( timebuf ), "%Y%m%d %H%M%S", &tm ); \
71 \
72 fprintf( stderr, "%s %s %s :: ", timebuf, module ? module : "-", level_str ); \
73 vfprintf( stderr, fmt, args ); \
74 fprintf( stderr, "\n" ); \
75 }
76
79#define ASRT_DEFINE_GPOS_LOG() \
80 ASRT_DEFINE_GPOS_LOG_IMPL \
81 void asrt_log( enum asrt_log_level level, char const* module, char const* fmt, ... ) \
82 { \
83 va_list args; \
84 va_start( args, fmt ); \
85 asrt_log_impl( level, module, fmt, args ); \
86 va_end( args ); \
87 }
88
89#ifdef __cplusplus
90}
91#endif
92
93#endif