logsys.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * configuration bits that can only be done for the whole system
  89. */
  90. extern int logsys_format_set (
  91. const char *format);
  92. extern char *logsys_format_get (void);
  93. /*
  94. * per system/subsystem settings.
  95. *
  96. * NOTE: once a subsystem is created and configured, changing
  97. * the default does NOT affect the subsystems.
  98. *
  99. * Pass a NULL subsystem to change them all
  100. */
  101. extern int logsys_config_syslog_facility_set (
  102. const char *subsys,
  103. unsigned int facility);
  104. extern int logsys_config_syslog_priority_set (
  105. const char *subsys,
  106. unsigned int priority);
  107. extern int logsys_config_mode_set (
  108. const char *subsys,
  109. unsigned int mode);
  110. extern unsigned int logsys_config_mode_get (
  111. const char *subsys);
  112. void logsys_config_apply(void);
  113. /*
  114. * to close a logfile, just invoke this function with a NULL
  115. * file or if you want to change logfile, the old one will
  116. * be closed for you.
  117. */
  118. extern int logsys_config_file_set (
  119. const char *subsys,
  120. const char **error_string,
  121. const char *file);
  122. extern int logsys_config_logfile_priority_set (
  123. const char *subsys,
  124. unsigned int priority);
  125. /*
  126. * enabling debug, disable message priority filtering.
  127. * everything is sent everywhere. priority values
  128. * for file and syslog are not overwritten.
  129. */
  130. extern int logsys_config_debug_set (
  131. const char *subsys,
  132. unsigned int value);
  133. /*
  134. * External API - helpers
  135. *
  136. * convert facility/priority to/from name/values
  137. */
  138. extern int logsys_priority_id_get (
  139. const char *name);
  140. extern const char *logsys_priority_name_get (
  141. unsigned int priority);
  142. extern int _logsys_system_setup(
  143. const char *mainsystem,
  144. unsigned int mode,
  145. int syslog_facility,
  146. int syslog_priority);
  147. extern void logsys_system_fini (void);
  148. extern int _logsys_config_subsys_get (
  149. const char *subsys);
  150. extern int _logsys_subsys_create (const char *subsys, const char *filename);
  151. extern int logsys_thread_start (void);
  152. static int logsys_subsys_id __attribute__((unused)) = LOGSYS_MAX_SUBSYS_COUNT;
  153. #define LOGSYS_DECLARE_SYSTEM(name,mode,syslog_facility,syslog_priority)\
  154. __attribute__ ((constructor)) \
  155. static void logsys_system_init (void) \
  156. { \
  157. if (_logsys_system_setup (name,mode,syslog_facility,syslog_priority) < 0) { \
  158. fprintf (stderr, \
  159. "Unable to setup logging system: %s.\n", name); \
  160. exit (-1); \
  161. } \
  162. }
  163. #ifdef QB_HAVE_ATTRIBUTE_SECTION
  164. #define LOGSYS_DECLARE_SECTION assert(__start___verbose != __stop___verbose)
  165. #else
  166. #define LOGSYS_DECLARE_SECTION
  167. #endif
  168. #define LOGSYS_DECLARE_SUBSYS(subsys) \
  169. __attribute__ ((constructor)) \
  170. static void logsys_subsys_init (void) \
  171. { \
  172. LOGSYS_DECLARE_SECTION; \
  173. logsys_subsys_id = \
  174. _logsys_subsys_create ((subsys), __FILE__); \
  175. if (logsys_subsys_id == -1) { \
  176. fprintf (stderr, \
  177. "Unable to create logging subsystem: %s.\n", subsys); \
  178. exit (-1); \
  179. } \
  180. }
  181. #define LOGSYS_PERROR(err_num, level, fmt, args...) do { \
  182. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  183. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  184. qb_log(level, fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  185. } while(0)
  186. #define log_printf(level, format, args...) qb_log(level, format, ##args)
  187. #define ENTER qb_enter
  188. #define LEAVE qb_leave
  189. #define TRACE1(format, args...) qb_log(LOG_TRACE, "TRACE1:" #format, ##args)
  190. #define TRACE2(format, args...) qb_log(LOG_TRACE, "TRACE2:" #format, ##args)
  191. #define TRACE3(format, args...) qb_log(LOG_TRACE, "TRACE3:" #format, ##args)
  192. #define TRACE4(format, args...) qb_log(LOG_TRACE, "TRACE4:" #format, ##args)
  193. #define TRACE5(format, args...) qb_log(LOG_TRACE, "TRACE5:" #format, ##args)
  194. #define TRACE6(format, args...) qb_log(LOG_TRACE, "TRACE6:" #format, ##args)
  195. #define TRACE7(format, args...) qb_log(LOG_TRACE, "TRACE7:" #format, ##args)
  196. #define TRACE8(format, args...) qb_log(LOG_TRACE, "TRACE8:" #format, ##args)
  197. #endif /* LOGSYS_UTILS_ONLY */
  198. #ifdef __cplusplus
  199. }
  200. #endif
  201. #endif /* LOGSYS_H_DEFINED */