#ifndef __ELOG_H__ #define __ELOG_H__ #include #include "elog-cpp.h" #include "elog-internal.h" #ifdef __cplusplus extern "C" { #endif typedef struct { ptrdiff_t buflen; ptrdiff_t offset; char buffer[0]; } __attribute__((__packed__)) elog_t; #define _msgparam_cast_apply(x) ((long) (x)), #define msgparam_cast_apply(...) EVAL(MAP(_msgparam_cast_apply, __VA_ARGS__)) /*计算出有多少个参数, 参数数组*/ #define ELOG(o, msg, ...) do { \ __attribute__((section("elog"))) static const char p_msg[] = msg; \ IF_ELSE(HAS_ARGS(__VA_ARGS__))( \ elog_put(o, p_msg, ELOG_NARG(__VA_ARGS__), (msgparam_t[]){ msgparam_cast_apply(__VA_ARGS__) }); \ )( \ elog_put(o, p_msg, 0, NULL); \ ) \ } while(0) // 打印字符串%s #define ELOG_STR(o, msg, str) do { \ __attribute__((section("elog"))) static const char p_msg[] = msg; \ elog_put_str(o, p_msg, str); \ } while(0) // 格式化字符串 #include #define FORMAT_MAX 256 #define STR_FIXED_LEN 0b1111 static inline void _printf_and_put(elog_t *o, const char *format, ...) { char temp[FORMAT_MAX] = {0}; va_list ap; va_start(ap, format); (void)vsnprintf(temp, sizeof(temp), format, ap); va_end(ap); elog_put_str(o, format, temp); } #define ELOG_PRINT(o, format, ...) do { \ __attribute__((section("elog"))) static const char p_msg[] = format; \ _printf_and_put(o, format, __VA_ARGS__); \ } while(0) typedef void (elog_flush_func_t)(elog_entry_t *e, int len, void *ctx); extern elog_t *elog_init(void *arena, size_t size); extern int elog_put(elog_t *log, const char *const msg, int n, msgparam_t args[]); extern elog_entry_t *elog_peek(elog_t *log); extern void elog_flush(elog_t *log, elog_flush_func_t func, void *ctx); #ifdef __cplusplus } #endif #endif /* __ELOG_H__ */