hj.shao | 57aca4a | 2025-07-17 22:52:58 -0700 | [diff] [blame^] | 1 | #ifndef GSW_LOG_INTERFACE_H
|
| 2 | #define GSW_LOG_INTERFACE_H
|
| 3 |
|
| 4 | #include <stdio.h>
|
| 5 | #include <stdlib.h>
|
| 6 | #include <string.h>
|
| 7 | #include <unistd.h>
|
| 8 | #include <fcntl.h>
|
| 9 | #include <stdarg.h>
|
| 10 | #include <dlfcn.h>
|
| 11 | #include <syslog.h>
|
| 12 |
|
| 13 | #ifndef LOG_ERR_LEVEL
|
| 14 | #define LOG_ERR_LEVEL 3 /* error conditions */
|
| 15 | #endif
|
| 16 | #ifndef LOG_WARN_LEVEL
|
| 17 | #define LOG_WARN_LEVEL 4 /* warning conditions */
|
| 18 | #endif
|
| 19 | #ifndef LOG_INFO_LEVEL
|
| 20 | #define LOG_INFO_LEVEL 6 /* informational */
|
| 21 | #endif
|
| 22 | #ifndef LOG_DEBUG_LEVEL
|
| 23 | #define LOG_DEBUG_LEVEL 7 /* debug-level messages */
|
| 24 | #endif
|
| 25 | #ifndef LOG_VERBOSE_LEVEL
|
| 26 | #define LOG_VERBOSE_LEVEL 8
|
| 27 | #endif
|
| 28 |
|
| 29 | typedef void (*mbtk_log)(int level, const char *fmt, ...);
|
| 30 |
|
| 31 | extern mbtk_log gsw_fun_ptr_log;
|
| 32 |
|
| 33 | int init_log_func(void);
|
| 34 |
|
| 35 | #define LOG(level, tag, fmt, args...) \
|
| 36 | do { \
|
| 37 | char *file_ptr_1001 = __FILE__; \
|
| 38 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 39 | char line_1001[10] = {0}; \
|
| 40 | sprintf(line_1001, "%d", __LINE__); \
|
| 41 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 42 | if(*ptr_1001 == '/') \
|
| 43 | break; \
|
| 44 | ptr_1001--; \
|
| 45 | } \
|
| 46 | if(0 == init_log_func()) {\
|
| 47 | gsw_fun_ptr_log(level, "%s#%s: [%s] " fmt, ptr_1001 + 1, line_1001, tag, ##args); \
|
| 48 | } else { \
|
| 49 | printf("%s#%s: [LOG_INIT_ERROR] [%s] " fmt, ptr_1001 + 1, line_1001, tag, ##args); \
|
| 50 | }\
|
| 51 | } while(0)
|
| 52 |
|
| 53 | #define _GET_MACRO(_1, _2, _3, NAME, ...) NAME
|
| 54 |
|
| 55 | #define LOGV(...) _GET_MACRO(__VA_ARGS__, LOGV_2, LOGV_1)(__VA_ARGS__)
|
| 56 | #define LOGV_1(fmt, args...) LOG(LOG_VERBOSE_LEVEL, "DEFAULT", fmt, ##args)
|
| 57 | #define LOGV_2(tag, fmt, args...) LOG(LOG_VERBOSE_LEVEL, tag, fmt, ##args)
|
| 58 |
|
| 59 | #define LOGI(...) _GET_MACRO(__VA_ARGS__, LOGI_2, LOGI_1)(__VA_ARGS__)
|
| 60 | #define LOGI_1(fmt, args...) LOG(LOG_INFO_LEVEL, "DEFAULT", fmt, ##args)
|
| 61 | #define LOGI_2(tag, fmt, args...) LOG(LOG_INFO_LEVEL, tag, fmt, ##args)
|
| 62 |
|
| 63 | #define LOGW(...) _GET_MACRO(__VA_ARGS__, LOGW_2, LOGW_1)(__VA_ARGS__)
|
| 64 | #define LOGW_1(fmt, args...) LOG(LOG_WARN_LEVEL, "DEFAULT", fmt, ##args)
|
| 65 | #define LOGW_2(tag, fmt, args...) LOG(LOG_WARN_LEVEL, tag, fmt, ##args)
|
| 66 |
|
| 67 | #define LOGE(...) _GET_MACRO(__VA_ARGS__, LOGE_2, LOGE_1)(__VA_ARGS__)
|
| 68 | #define LOGE_1(fmt, args...) LOG(LOG_ERR_LEVEL, "DEFAULT", fmt, ##args)
|
| 69 | #define LOGE_2(tag, fmt, args...) LOG(LOG_ERR_LEVEL, tag, fmt, ##args)
|
| 70 |
|
| 71 | #endif
|