logsys.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * Author: Steven Dake (sdake@redhat.com)
  6. * Author: Lon Hohberger (lhh@redhat.com)
  7. * Author: Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * All rights reserved.
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef LOGSYS_H_DEFINED
  38. #define LOGSYS_H_DEFINED
  39. #include <stdarg.h>
  40. #include <stdlib.h>
  41. #include <syslog.h>
  42. #include <pthread.h>
  43. #include <limits.h>
  44. #include <corosync/corotypes.h>
  45. #include <qb/qbconfig.h>
  46. #include <qb/qblog.h>
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /*
  51. * All of the LOGSYS_MODE's can be ORed together for combined behavior
  52. *
  53. * FORK and THREADED are ignored for SUBSYSTEMS
  54. */
  55. #define LOGSYS_MODE_OUTPUT_FILE (1<<0)
  56. #define LOGSYS_MODE_OUTPUT_STDERR (1<<1)
  57. #define LOGSYS_MODE_OUTPUT_SYSLOG (1<<2)
  58. #define LOGSYS_MODE_FORK (1<<3)
  59. #define LOGSYS_MODE_THREADED (1<<4)
  60. /*
  61. * Log priorities, compliant with syslog and SA Forum Log spec.
  62. */
  63. #define LOGSYS_LEVEL_EMERG LOG_EMERG
  64. #define LOGSYS_LEVEL_ALERT LOG_ALERT
  65. #define LOGSYS_LEVEL_CRIT LOG_CRIT
  66. #define LOGSYS_LEVEL_ERROR LOG_ERR
  67. #define LOGSYS_LEVEL_WARNING LOG_WARNING
  68. #define LOGSYS_LEVEL_NOTICE LOG_NOTICE
  69. #define LOGSYS_LEVEL_INFO LOG_INFO
  70. #define LOGSYS_LEVEL_DEBUG LOG_DEBUG
  71. #define LOGSYS_LEVEL_TRACE LOG_TRACE
  72. /*
  73. * logsys_logger bits
  74. *
  75. * SUBSYS_COUNT defines the maximum number of subsystems
  76. * SUBSYS_NAMELEN defines the maximum len of a subsystem name
  77. */
  78. #define LOGSYS_MAX_SUBSYS_COUNT 32
  79. #define LOGSYS_MAX_SUBSYS_NAMELEN 64
  80. #define LOGSYS_MAX_PERROR_MSG_LEN 128
  81. /*
  82. * Debug levels
  83. */
  84. #define LOGSYS_DEBUG_OFF 0
  85. #define LOGSYS_DEBUG_ON 1
  86. #define LOGSYS_DEBUG_TRACE 2
  87. #ifndef LOGSYS_UTILS_ONLY
  88. /**
  89. * @brief configuration bits that can only be done for the whole system
  90. * @param format
  91. * @return
  92. */
  93. extern int logsys_format_set (
  94. const char *format);
  95. /**
  96. * @brief logsys_format_get
  97. * @return
  98. */
  99. extern char *logsys_format_get (void);
  100. /**
  101. * @brief per system/subsystem settings.
  102. *
  103. * NOTE: once a subsystem is created and configured, changing
  104. * the default does NOT affect the subsystems.
  105. *
  106. * Pass a NULL subsystem to change them all
  107. *
  108. * @param subsys
  109. * @param facility
  110. * @return
  111. */
  112. extern int logsys_config_syslog_facility_set (
  113. const char *subsys,
  114. unsigned int facility);
  115. /**
  116. * @brief logsys_config_syslog_priority_set
  117. * @param subsys
  118. * @param priority
  119. * @return
  120. */
  121. extern int logsys_config_syslog_priority_set (
  122. const char *subsys,
  123. unsigned int priority);
  124. /**
  125. * @brief logsys_config_mode_set
  126. * @param subsys
  127. * @param mode
  128. * @return
  129. */
  130. extern int logsys_config_mode_set (
  131. const char *subsys,
  132. unsigned int mode);
  133. /**
  134. * @brief logsys_config_mode_get
  135. * @param subsys
  136. * @return
  137. */
  138. extern unsigned int logsys_config_mode_get (
  139. const char *subsys);
  140. /**
  141. * @brief logsys_config_apply
  142. */
  143. void logsys_config_apply(void);
  144. /**
  145. * @brief to close a logfile, just invoke this function with a NULL
  146. * file or if you want to change logfile, the old one will
  147. * be closed for you.
  148. *
  149. * @param subsys
  150. * @param error_string
  151. * @param file
  152. * @return
  153. */
  154. extern int logsys_config_file_set (
  155. const char *subsys,
  156. const char **error_string,
  157. const char *file);
  158. /**
  159. * @brief logsys_config_logfile_priority_set
  160. * @param subsys
  161. * @param priority
  162. * @return
  163. */
  164. extern int logsys_config_logfile_priority_set (
  165. const char *subsys,
  166. unsigned int priority);
  167. /**
  168. * @brief enabling debug, disable message priority filtering.
  169. * everything is sent everywhere. priority values
  170. * for file and syslog are not overwritten.
  171. *
  172. * @param subsys
  173. * @param value
  174. * @return
  175. */
  176. extern int logsys_config_debug_set (
  177. const char *subsys,
  178. unsigned int value);
  179. /**
  180. * @brief Return the debug flag for this subsys
  181. *
  182. * @param subsys
  183. * @return LOGSYS_DEBUG_OFF | LOGSYS_DEBUG_ON | LOGSYS_DEBUG_TRACE
  184. */
  185. extern int logsys_config_debug_get (
  186. const char *subsys);
  187. /*
  188. * External API - helpers
  189. *
  190. * convert facility/priority to/from name/values
  191. */
  192. /**
  193. * @brief logsys_priority_id_get
  194. * @param name
  195. * @return
  196. */
  197. extern int logsys_priority_id_get (
  198. const char *name);
  199. /**
  200. * @brief logsys_priority_name_get
  201. * @param priority
  202. * @return
  203. */
  204. extern const char *logsys_priority_name_get (
  205. unsigned int priority);
  206. /**
  207. * @brief _logsys_system_setup
  208. * @param mainsystem
  209. * @param mode
  210. * @param syslog_facility
  211. * @param syslog_priority
  212. * @return
  213. */
  214. extern int _logsys_system_setup(
  215. const char *mainsystem,
  216. unsigned int mode,
  217. int syslog_facility,
  218. int syslog_priority);
  219. /**
  220. * @brief logsys_system_fini
  221. */
  222. extern void logsys_system_fini (void);
  223. /**
  224. * @brief _logsys_config_subsys_get
  225. * @param subsys
  226. * @return
  227. */
  228. extern int _logsys_config_subsys_get (
  229. const char *subsys);
  230. /**
  231. * @brief _logsys_subsys_create
  232. * @param subsys
  233. * @param filename
  234. * @return
  235. */
  236. extern int _logsys_subsys_create (const char *subsys, const char *filename);
  237. /**
  238. * @brief logsys_thread_start
  239. * @return
  240. */
  241. extern int logsys_thread_start (void);
  242. extern void logsys_blackbox_set(int enable);
  243. extern void logsys_blackbox_prefork(void);
  244. extern void logsys_blackbox_postfork(void);
  245. extern cs_error_t logsys_reopen_log_files(void);
  246. /**
  247. * @brief logsys_subsys_id
  248. */
  249. static int logsys_subsys_id __attribute__((unused)) = LOGSYS_MAX_SUBSYS_COUNT;
  250. /**
  251. * @brief The LOGSYS_DECLARE_SYSTEM macro
  252. * @param name
  253. * @param mode
  254. * @param syslog_facility
  255. * @param syslog_priority
  256. */
  257. #define LOGSYS_DECLARE_SYSTEM(name,mode,syslog_facility,syslog_priority)\
  258. QB_LOG_INIT_DATA(logsys_qb_init); \
  259. __attribute__ ((constructor)) \
  260. static void logsys_system_init (void) \
  261. { \
  262. if (_logsys_system_setup (name,mode,syslog_facility,syslog_priority) < 0) { \
  263. fprintf (stderr, \
  264. "Unable to setup logging system: %s.\n", name); \
  265. exit (-1); \
  266. } \
  267. }
  268. /**
  269. * @brief The LOGSYS_DECLARE_SUBSYS macro
  270. * @param subsys
  271. */
  272. #define LOGSYS_DECLARE_SUBSYS(subsys) \
  273. __attribute__ ((constructor)) \
  274. static void logsys_subsys_init (void) \
  275. { \
  276. logsys_subsys_id = \
  277. _logsys_subsys_create ((subsys), __FILE__); \
  278. if (logsys_subsys_id == -1) { \
  279. fprintf (stderr, \
  280. "Unable to create logging subsystem: %s.\n", subsys); \
  281. exit (-1); \
  282. } \
  283. }
  284. /**
  285. * @brief The LOGSYS_PERROR macro
  286. * @param err_num
  287. * @param level
  288. * @param fmt
  289. * @param args
  290. */
  291. #define LOGSYS_PERROR(err_num, level, fmt, args...) do { \
  292. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  293. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  294. qb_log(level, fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  295. } while(0)
  296. #define log_printf(level, format, args...) qb_log(level, format, ##args)
  297. #define ENTER qb_enter
  298. #define LEAVE qb_leave
  299. #define TRACE1(format, args...) qb_log(LOG_TRACE, "TRACE1:" #format, ##args)
  300. #define TRACE2(format, args...) qb_log(LOG_TRACE, "TRACE2:" #format, ##args)
  301. #define TRACE3(format, args...) qb_log(LOG_TRACE, "TRACE3:" #format, ##args)
  302. #define TRACE4(format, args...) qb_log(LOG_TRACE, "TRACE4:" #format, ##args)
  303. #define TRACE5(format, args...) qb_log(LOG_TRACE, "TRACE5:" #format, ##args)
  304. #define TRACE6(format, args...) qb_log(LOG_TRACE, "TRACE6:" #format, ##args)
  305. #define TRACE7(format, args...) qb_log(LOG_TRACE, "TRACE7:" #format, ##args)
  306. #define TRACE8(format, args...) qb_log(LOG_TRACE, "TRACE8:" #format, ##args)
  307. #endif /* LOGSYS_UTILS_ONLY */
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311. #endif /* LOGSYS_H_DEFINED */