zlog.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * This file is part of the zlog Library.
  3. *
  4. * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING in base directory.
  7. */
  8. #ifndef __zlog_h
  9. #define __zlog_h
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #include <stdarg.h> /* for va_list */
  14. #include <stdio.h> /* for size_t */
  15. # if defined __GNUC__
  16. # define ZLOG_CHECK_PRINTF(m,n) __attribute__((format(printf,m,n)))
  17. # else
  18. # define ZLOG_CHECK_PRINTF(m,n)
  19. # endif
  20. typedef struct zlog_category_s zlog_category_t;
  21. int zlog_init(const char *config);
  22. int zlog_reload(const char *config);
  23. void zlog_fini(void);
  24. void zlog_profile(void);
  25. zlog_category_t *zlog_get_category(const char *cname);
  26. int zlog_level_enabled(zlog_category_t *category, const int level);
  27. int zlog_put_mdc(const char *key, const char *value);
  28. char *zlog_get_mdc(const char *key);
  29. void zlog_remove_mdc(const char *key);
  30. void zlog_clean_mdc(void);
  31. int zlog_level_switch(zlog_category_t * category, int level);
  32. int zlog_level_enabled(zlog_category_t * category, int level);
  33. void zlog(zlog_category_t * category,
  34. const char *file, size_t filelen,
  35. const char *func, size_t funclen,
  36. long line, int level,
  37. const char *format, ...) ZLOG_CHECK_PRINTF(8,9);
  38. void vzlog(zlog_category_t * category,
  39. const char *file, size_t filelen,
  40. const char *func, size_t funclen,
  41. long line, int level,
  42. const char *format, va_list args);
  43. void hzlog(zlog_category_t * category,
  44. const char *file, size_t filelen,
  45. const char *func, size_t funclen,
  46. long line, int level,
  47. const void *buf, size_t buflen);
  48. int dzlog_init(const char *confpath, const char *cname);
  49. int dzlog_set_category(const char *cname);
  50. void dzlog(const char *file, size_t filelen,
  51. const char *func, size_t funclen,
  52. long line, int level,
  53. const char *format, ...) ZLOG_CHECK_PRINTF(7,8);
  54. void vdzlog(const char *file, size_t filelen,
  55. const char *func, size_t funclen,
  56. long line, int level,
  57. const char *format, va_list args);
  58. void hdzlog(const char *file, size_t filelen,
  59. const char *func, size_t funclen,
  60. long line, int level,
  61. const void *buf, size_t buflen);
  62. typedef struct zlog_msg_s {
  63. char *buf;
  64. size_t len;
  65. char *path;
  66. } zlog_msg_t;
  67. typedef int (*zlog_record_fn)(zlog_msg_t *msg);
  68. int zlog_set_record(const char *rname, zlog_record_fn record);
  69. const char *zlog_version(void);
  70. /******* useful macros, can be redefined at user's h file **********/
  71. typedef enum {
  72. ZLOG_LEVEL_DEBUG = 20,
  73. ZLOG_LEVEL_INFO = 40,
  74. ZLOG_LEVEL_NOTICE = 60,
  75. ZLOG_LEVEL_WARN = 80,
  76. ZLOG_LEVEL_ERROR = 100,
  77. ZLOG_LEVEL_FATAL = 120
  78. } zlog_level;
  79. #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  80. # if defined __GNUC__ && __GNUC__ >= 2
  81. # define __func__ __FUNCTION__
  82. # else
  83. # define __func__ "<unknown>"
  84. # endif
  85. #endif
  86. #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  87. /* zlog macros */
  88. #define zlog_fatal(cat, ...) \
  89. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  90. ZLOG_LEVEL_FATAL, __VA_ARGS__)
  91. #define zlog_error(cat, ...) \
  92. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  93. ZLOG_LEVEL_ERROR, __VA_ARGS__)
  94. #define zlog_warn(cat, ...) \
  95. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  96. ZLOG_LEVEL_WARN, __VA_ARGS__)
  97. #define zlog_notice(cat, ...) \
  98. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  99. ZLOG_LEVEL_NOTICE, __VA_ARGS__)
  100. #define zlog_info(cat, ...) \
  101. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  102. ZLOG_LEVEL_INFO, __VA_ARGS__)
  103. #define zlog_debug(cat, ...) \
  104. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  105. ZLOG_LEVEL_DEBUG, __VA_ARGS__)
  106. /* dzlog macros */
  107. #define dzlog_fatal(...) \
  108. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  109. ZLOG_LEVEL_FATAL, __VA_ARGS__)
  110. #define dzlog_error(...) \
  111. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  112. ZLOG_LEVEL_ERROR, __VA_ARGS__)
  113. #define dzlog_warn(...) \
  114. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  115. ZLOG_LEVEL_WARN, __VA_ARGS__)
  116. #define dzlog_notice(...) \
  117. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  118. ZLOG_LEVEL_NOTICE, __VA_ARGS__)
  119. #define dzlog_info(...) \
  120. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  121. ZLOG_LEVEL_INFO, __VA_ARGS__)
  122. #define dzlog_debug(...) \
  123. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  124. ZLOG_LEVEL_DEBUG, __VA_ARGS__)
  125. #elif defined __GNUC__
  126. /* zlog macros */
  127. #define zlog_fatal(cat, format, args...) \
  128. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  129. ZLOG_LEVEL_FATAL, format, ##args)
  130. #define zlog_error(cat, format, args...) \
  131. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  132. ZLOG_LEVEL_ERROR, format, ##args)
  133. #define zlog_warn(cat, format, args...) \
  134. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  135. ZLOG_LEVEL_WARN, format, ##args)
  136. #define zlog_notice(cat, format, args...) \
  137. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  138. ZLOG_LEVEL_NOTICE, format, ##args)
  139. #define zlog_info(cat, format, args...) \
  140. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  141. ZLOG_LEVEL_INFO, format, ##args)
  142. #define zlog_debug(cat, format, args...) \
  143. zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  144. ZLOG_LEVEL_DEBUG, format, ##args)
  145. /* dzlog macros */
  146. #define dzlog_fatal(format, args...) \
  147. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  148. ZLOG_LEVEL_FATAL, format, ##args)
  149. #define dzlog_error(format, args...) \
  150. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  151. ZLOG_LEVEL_ERROR, format, ##args)
  152. #define dzlog_warn(format, args...) \
  153. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  154. ZLOG_LEVEL_WARN, format, ##args)
  155. #define dzlog_notice(format, args...) \
  156. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  157. ZLOG_LEVEL_NOTICE, format, ##args)
  158. #define dzlog_info(format, args...) \
  159. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  160. ZLOG_LEVEL_INFO, format, ##args)
  161. #define dzlog_debug(format, args...) \
  162. dzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  163. ZLOG_LEVEL_DEBUG, format, ##args)
  164. #endif
  165. /* vzlog macros */
  166. #define vzlog_fatal(cat, format, args) \
  167. vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  168. ZLOG_LEVEL_FATAL, format, args)
  169. #define vzlog_error(cat, format, args) \
  170. vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  171. ZLOG_LEVEL_ERROR, format, args)
  172. #define vzlog_warn(cat, format, args) \
  173. vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  174. ZLOG_LEVEL_WARN, format, args)
  175. #define vzlog_notice(cat, format, args) \
  176. vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  177. ZLOG_LEVEL_NOTICE, format, args)
  178. #define vzlog_info(cat, format, args) \
  179. vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  180. ZLOG_LEVEL_INFO, format, args)
  181. #define vzlog_debug(cat, format, args) \
  182. vzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  183. ZLOG_LEVEL_DEBUG, format, args)
  184. /* hzlog macros */
  185. #define hzlog_fatal(cat, buf, buf_len) \
  186. hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  187. ZLOG_LEVEL_FATAL, buf, buf_len)
  188. #define hzlog_error(cat, buf, buf_len) \
  189. hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  190. ZLOG_LEVEL_ERROR, buf, buf_len)
  191. #define hzlog_warn(cat, buf, buf_len) \
  192. hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  193. ZLOG_LEVEL_WARN, buf, buf_len)
  194. #define hzlog_notice(cat, buf, buf_len) \
  195. hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  196. ZLOG_LEVEL_NOTICE, buf, buf_len)
  197. #define hzlog_info(cat, buf, buf_len) \
  198. hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  199. ZLOG_LEVEL_INFO, buf, buf_len)
  200. #define hzlog_debug(cat, buf, buf_len) \
  201. hzlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  202. ZLOG_LEVEL_DEBUG, buf, buf_len)
  203. /* vdzlog macros */
  204. #define vdzlog_fatal(format, args) \
  205. vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  206. ZLOG_LEVEL_FATAL, format, args)
  207. #define vdzlog_error(format, args) \
  208. vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  209. ZLOG_LEVEL_ERROR, format, args)
  210. #define vdzlog_warn(format, args) \
  211. vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  212. ZLOG_LEVEL_WARN, format, args)
  213. #define vdzlog_notice(format, args) \
  214. vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  215. ZLOG_LEVEL_NOTICE, format, args)
  216. #define vdzlog_info(format, args) \
  217. vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  218. ZLOG_LEVEL_INFO, format, args)
  219. #define vdzlog_debug(format, args) \
  220. vdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  221. ZLOG_LEVEL_DEBUG, format, args)
  222. /* hdzlog macros */
  223. #define hdzlog_fatal(buf, buf_len) \
  224. hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  225. ZLOG_LEVEL_FATAL, buf, buf_len)
  226. #define hdzlog_error(buf, buf_len) \
  227. hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  228. ZLOG_LEVEL_ERROR, buf, buf_len)
  229. #define hdzlog_warn(buf, buf_len) \
  230. hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  231. ZLOG_LEVEL_WARN, buf, buf_len)
  232. #define hdzlog_notice(buf, buf_len) \
  233. hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  234. ZLOG_LEVEL_NOTICE, buf, buf_len)
  235. #define hdzlog_info(buf, buf_len) \
  236. hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  237. ZLOG_LEVEL_INFO, buf, buf_len)
  238. #define hdzlog_debug(buf, buf_len) \
  239. hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
  240. ZLOG_LEVEL_DEBUG, buf, buf_len)
  241. /* enabled macros */
  242. #define zlog_fatal_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_FATAL)
  243. #define zlog_error_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_ERROR)
  244. #define zlog_warn_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_WARN)
  245. #define zlog_notice_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_NOTICE)
  246. #define zlog_info_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_INFO)
  247. #define zlog_debug_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_DEBUG)
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. #endif