選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef __ELOG_H__
  2. #define __ELOG_H__
  3. #include <stddef.h>
  4. #include "elog-cpp.h"
  5. #include "elog-internal.h"
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. typedef struct {
  10. ptrdiff_t buflen;
  11. ptrdiff_t offset;
  12. char buffer[0];
  13. } __attribute__((__packed__)) elog_t;
  14. #define _msgparam_cast_apply(x) ((long) (x)),
  15. #define msgparam_cast_apply(...) EVAL(MAP(_msgparam_cast_apply, __VA_ARGS__))
  16. /*计算出有多少个参数, 参数数组*/
  17. #define ELOG(o, msg, ...) do { \
  18. __attribute__((section("elog"))) static const char p_msg[] = msg; \
  19. IF_ELSE(HAS_ARGS(__VA_ARGS__))( \
  20. elog_put(o, p_msg, ELOG_NARG(__VA_ARGS__), (msgparam_t[]){ msgparam_cast_apply(__VA_ARGS__) }); \
  21. )( \
  22. elog_put(o, p_msg, 0, NULL); \
  23. ) \
  24. } while(0)
  25. // 打印字符串%s
  26. #define ELOG_STR(o, msg, str) do { \
  27. __attribute__((section("elog"))) static const char p_msg[] = msg; \
  28. elog_put_str(o, p_msg, str); \
  29. } while(0)
  30. // 格式化字符串
  31. #include <stdarg.h>
  32. #define FORMAT_MAX 256
  33. #define STR_FIXED_LEN 0b1111
  34. static inline void _printf_and_put(elog_t *o, const char *format, ...)
  35. {
  36. char temp[FORMAT_MAX] = {0};
  37. va_list ap;
  38. va_start(ap, format);
  39. (void)vsnprintf(temp, sizeof(temp), format, ap);
  40. va_end(ap);
  41. elog_put_str(o, format, temp);
  42. }
  43. #define ELOG_PRINT(o, format, ...) do { \
  44. __attribute__((section("elog"))) static const char p_msg[] = format; \
  45. _printf_and_put(o, format, __VA_ARGS__); \
  46. } while(0)
  47. typedef void (elog_flush_func_t)(elog_entry_t *e, int len, void *ctx);
  48. extern elog_t *elog_init(void *arena, size_t size);
  49. extern int elog_put(elog_t *log, const char *const msg, int n, msgparam_t args[]);
  50. extern elog_entry_t *elog_peek(elog_t *log);
  51. extern void elog_flush(elog_t *log, elog_flush_func_t func, void *ctx);
  52. #ifdef __cplusplus
  53. }
  54. #endif
  55. #endif /* __ELOG_H__ */