logsys.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <qb/qbconfig.h>
  45. #include <qb/qblog.h>
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /*
  50. * All of the LOGSYS_MODE's can be ORed together for combined behavior
  51. *
  52. * FORK and THREADED are ignored for SUBSYSTEMS
  53. */
  54. #define LOGSYS_MODE_OUTPUT_FILE (1<<0)
  55. #define LOGSYS_MODE_OUTPUT_STDERR (1<<1)
  56. #define LOGSYS_MODE_OUTPUT_SYSLOG (1<<2)
  57. #define LOGSYS_MODE_FORK (1<<3)
  58. #define LOGSYS_MODE_THREADED (1<<4)
  59. /*
  60. * Log priorities, compliant with syslog and SA Forum Log spec.
  61. */
  62. #define LOGSYS_LEVEL_EMERG LOG_EMERG
  63. #define LOGSYS_LEVEL_ALERT LOG_ALERT
  64. #define LOGSYS_LEVEL_CRIT LOG_CRIT
  65. #define LOGSYS_LEVEL_ERROR LOG_ERR
  66. #define LOGSYS_LEVEL_WARNING LOG_WARNING
  67. #define LOGSYS_LEVEL_NOTICE LOG_NOTICE
  68. #define LOGSYS_LEVEL_INFO LOG_INFO
  69. #define LOGSYS_LEVEL_DEBUG LOG_DEBUG
  70. #define LOGSYS_LEVEL_TRACE LOG_TRACE
  71. /*
  72. * logsys_logger bits
  73. *
  74. * SUBSYS_COUNT defines the maximum number of subsystems
  75. * SUBSYS_NAMELEN defines the maximum len of a subsystem name
  76. */
  77. #define LOGSYS_MAX_SUBSYS_COUNT 32
  78. #define LOGSYS_MAX_SUBSYS_NAMELEN 64
  79. #define LOGSYS_MAX_PERROR_MSG_LEN 128
  80. /*
  81. * Debug levels
  82. */
  83. #define LOGSYS_DEBUG_OFF 0
  84. #define LOGSYS_DEBUG_ON 1
  85. #define LOGSYS_DEBUG_TRACE 2
  86. #ifndef LOGSYS_UTILS_ONLY
  87. /**
  88. * @brief configuration bits that can only be done for the whole system
  89. * @param format
  90. * @return
  91. */
  92. extern int logsys_format_set (
  93. const char *format);
  94. /**
  95. * @brief logsys_format_get
  96. * @return
  97. */
  98. extern char *logsys_format_get (void);
  99. /**
  100. * @brief per system/subsystem settings.
  101. *
  102. * NOTE: once a subsystem is created and configured, changing
  103. * the default does NOT affect the subsystems.
  104. *
  105. * Pass a NULL subsystem to change them all
  106. *
  107. * @param subsys
  108. * @param facility
  109. * @return
  110. */
  111. extern int logsys_config_syslog_facility_set (
  112. const char *subsys,
  113. unsigned int facility);
  114. /**
  115. * @brief logsys_config_syslog_priority_set
  116. * @param subsys
  117. * @param priority
  118. * @return
  119. */
  120. extern int logsys_config_syslog_priority_set (
  121. const char *subsys,
  122. unsigned int priority);
  123. /**
  124. * @brief logsys_config_mode_set
  125. * @param subsys
  126. * @param mode
  127. * @return
  128. */
  129. extern int logsys_config_mode_set (
  130. const char *subsys,
  131. unsigned int mode);
  132. /**
  133. * @brief logsys_config_mode_get
  134. * @param subsys
  135. * @return
  136. */
  137. extern unsigned int logsys_config_mode_get (
  138. const char *subsys);
  139. /**
  140. * @brief logsys_config_apply
  141. */
  142. void logsys_config_apply(void);
  143. /**
  144. * @brief to close a logfile, just invoke this function with a NULL
  145. * file or if you want to change logfile, the old one will
  146. * be closed for you.
  147. *
  148. * @param subsys
  149. * @param error_string
  150. * @param file
  151. * @return
  152. */
  153. extern int logsys_config_file_set (
  154. const char *subsys,
  155. const char **error_string,
  156. const char *file);
  157. /**
  158. * @brief logsys_config_logfile_priority_set
  159. * @param subsys
  160. * @param priority
  161. * @return
  162. */
  163. extern int logsys_config_logfile_priority_set (
  164. const char *subsys,
  165. unsigned int priority);
  166. /**
  167. * @brief enabling debug, disable message priority filtering.
  168. * everything is sent everywhere. priority values
  169. * for file and syslog are not overwritten.
  170. *
  171. * @param subsys
  172. * @param value
  173. * @return
  174. */
  175. extern int logsys_config_debug_set (
  176. const char *subsys,
  177. unsigned int value);
  178. /*
  179. * External API - helpers
  180. *
  181. * convert facility/priority to/from name/values
  182. */
  183. /**
  184. * @brief logsys_priority_id_get
  185. * @param name
  186. * @return
  187. */
  188. extern int logsys_priority_id_get (
  189. const char *name);
  190. /**
  191. * @brief logsys_priority_name_get
  192. * @param priority
  193. * @return
  194. */
  195. extern const char *logsys_priority_name_get (
  196. unsigned int priority);
  197. /**
  198. * @brief _logsys_system_setup
  199. * @param mainsystem
  200. * @param mode
  201. * @param syslog_facility
  202. * @param syslog_priority
  203. * @return
  204. */
  205. extern int _logsys_system_setup(
  206. const char *mainsystem,
  207. unsigned int mode,
  208. int syslog_facility,
  209. int syslog_priority);
  210. /**
  211. * @brief logsys_system_fini
  212. */
  213. extern void logsys_system_fini (void);
  214. /**
  215. * @brief _logsys_config_subsys_get
  216. * @param subsys
  217. * @return
  218. */
  219. extern int _logsys_config_subsys_get (
  220. const char *subsys);
  221. /**
  222. * @brief _logsys_subsys_create
  223. * @param subsys
  224. * @param filename
  225. * @return
  226. */
  227. extern int _logsys_subsys_create (const char *subsys, const char *filename);
  228. /**
  229. * @brief logsys_thread_start
  230. * @return
  231. */
  232. extern int logsys_thread_start (void);
  233. extern void logsys_blackbox_set(int enable);
  234. extern void logsys_blackbox_prefork(void);
  235. extern void logsys_blackbox_postfork(void);
  236. /**
  237. * @brief logsys_subsys_id
  238. */
  239. static int logsys_subsys_id __attribute__((unused)) = LOGSYS_MAX_SUBSYS_COUNT;
  240. /**
  241. * @brief The LOGSYS_DECLARE_SYSTEM macro
  242. * @param name
  243. * @param mode
  244. * @param syslog_facility
  245. * @param syslog_priority
  246. */
  247. #define LOGSYS_DECLARE_SYSTEM(name,mode,syslog_facility,syslog_priority)\
  248. QB_LOG_INIT_DATA(logsys_qb_init); \
  249. __attribute__ ((constructor)) \
  250. static void logsys_system_init (void) \
  251. { \
  252. if (_logsys_system_setup (name,mode,syslog_facility,syslog_priority) < 0) { \
  253. fprintf (stderr, \
  254. "Unable to setup logging system: %s.\n", name); \
  255. exit (-1); \
  256. } \
  257. }
  258. /**
  259. * @brief The LOGSYS_DECLARE_SUBSYS macro
  260. * @param subsys
  261. */
  262. #define LOGSYS_DECLARE_SUBSYS(subsys) \
  263. __attribute__ ((constructor)) \
  264. static void logsys_subsys_init (void) \
  265. { \
  266. logsys_subsys_id = \
  267. _logsys_subsys_create ((subsys), __FILE__); \
  268. if (logsys_subsys_id == -1) { \
  269. fprintf (stderr, \
  270. "Unable to create logging subsystem: %s.\n", subsys); \
  271. exit (-1); \
  272. } \
  273. }
  274. /**
  275. * @brief The LOGSYS_PERROR macro
  276. * @param err_num
  277. * @param level
  278. * @param fmt
  279. * @param args
  280. */
  281. #define LOGSYS_PERROR(err_num, level, fmt, args...) do { \
  282. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  283. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  284. qb_log(level, fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  285. } while(0)
  286. #define log_printf(level, format, args...) qb_log(level, format, ##args)
  287. #define ENTER qb_enter
  288. #define LEAVE qb_leave
  289. #define TRACE1(format, args...) qb_log(LOG_TRACE, "TRACE1:" #format, ##args)
  290. #define TRACE2(format, args...) qb_log(LOG_TRACE, "TRACE2:" #format, ##args)
  291. #define TRACE3(format, args...) qb_log(LOG_TRACE, "TRACE3:" #format, ##args)
  292. #define TRACE4(format, args...) qb_log(LOG_TRACE, "TRACE4:" #format, ##args)
  293. #define TRACE5(format, args...) qb_log(LOG_TRACE, "TRACE5:" #format, ##args)
  294. #define TRACE6(format, args...) qb_log(LOG_TRACE, "TRACE6:" #format, ##args)
  295. #define TRACE7(format, args...) qb_log(LOG_TRACE, "TRACE7:" #format, ##args)
  296. #define TRACE8(format, args...) qb_log(LOG_TRACE, "TRACE8:" #format, ##args)
  297. #endif /* LOGSYS_UTILS_ONLY */
  298. #ifdef __cplusplus
  299. }
  300. #endif
  301. #endif /* LOGSYS_H_DEFINED */