logsys.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/qblog.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /*
  49. * All of the LOGSYS_MODE's can be ORed together for combined behavior
  50. *
  51. * FORK and THREADED are ignored for SUBSYSTEMS
  52. */
  53. #define LOGSYS_MODE_OUTPUT_FILE (1<<0)
  54. #define LOGSYS_MODE_OUTPUT_STDERR (1<<1)
  55. #define LOGSYS_MODE_OUTPUT_SYSLOG (1<<2)
  56. #define LOGSYS_MODE_FORK (1<<3)
  57. #define LOGSYS_MODE_THREADED (1<<4)
  58. /*
  59. * Log priorities, compliant with syslog and SA Forum Log spec.
  60. */
  61. #define LOGSYS_LEVEL_EMERG LOG_EMERG
  62. #define LOGSYS_LEVEL_ALERT LOG_ALERT
  63. #define LOGSYS_LEVEL_CRIT LOG_CRIT
  64. #define LOGSYS_LEVEL_ERROR LOG_ERR
  65. #define LOGSYS_LEVEL_WARNING LOG_WARNING
  66. #define LOGSYS_LEVEL_NOTICE LOG_NOTICE
  67. #define LOGSYS_LEVEL_INFO LOG_INFO
  68. #define LOGSYS_LEVEL_DEBUG LOG_DEBUG
  69. /*
  70. * logsys_logger bits
  71. *
  72. * SUBSYS_COUNT defines the maximum number of subsystems
  73. * SUBSYS_NAMELEN defines the maximum len of a subsystem name
  74. */
  75. #define LOGSYS_MAX_SUBSYS_COUNT 64
  76. #define LOGSYS_MAX_SUBSYS_NAMELEN 64
  77. #define LOGSYS_MAX_PERROR_MSG_LEN 128
  78. #ifndef LOGSYS_UTILS_ONLY
  79. /*
  80. * configuration bits that can only be done for the whole system
  81. */
  82. extern int logsys_format_set (
  83. const char *format);
  84. extern char *logsys_format_get (void);
  85. /*
  86. * per system/subsystem settings.
  87. *
  88. * NOTE: once a subsystem is created and configured, changing
  89. * the default does NOT affect the subsystems.
  90. *
  91. * Pass a NULL subsystem to change them all
  92. */
  93. extern int logsys_config_syslog_facility_set (
  94. const char *subsys,
  95. unsigned int facility);
  96. extern int logsys_config_syslog_priority_set (
  97. const char *subsys,
  98. unsigned int priority);
  99. extern int logsys_config_mode_set (
  100. const char *subsys,
  101. unsigned int mode);
  102. extern unsigned int logsys_config_mode_get (
  103. const char *subsys);
  104. void logsys_config_apply(void);
  105. /*
  106. * to close a logfile, just invoke this function with a NULL
  107. * file or if you want to change logfile, the old one will
  108. * be closed for you.
  109. */
  110. extern int logsys_config_file_set (
  111. const char *subsys,
  112. const char **error_string,
  113. const char *file);
  114. extern int logsys_config_logfile_priority_set (
  115. const char *subsys,
  116. unsigned int priority);
  117. /*
  118. * enabling debug, disable message priority filtering.
  119. * everything is sent everywhere. priority values
  120. * for file and syslog are not overwritten.
  121. */
  122. extern int logsys_config_debug_set (
  123. const char *subsys,
  124. unsigned int value);
  125. /*
  126. * External API - helpers
  127. *
  128. * convert facility/priority to/from name/values
  129. */
  130. extern int logsys_priority_id_get (
  131. const char *name);
  132. extern const char *logsys_priority_name_get (
  133. unsigned int priority);
  134. extern int _logsys_system_setup(
  135. const char *mainsystem,
  136. unsigned int mode,
  137. int syslog_facility,
  138. int syslog_priority);
  139. extern void logsys_system_fini (void);
  140. extern int _logsys_config_subsys_get (
  141. const char *subsys);
  142. extern int _logsys_subsys_create (const char *subsys, const char *filename);
  143. static int logsys_subsys_id __attribute__((unused)) = LOGSYS_MAX_SUBSYS_COUNT;
  144. #define LOGSYS_DECLARE_SYSTEM(name,mode,syslog_facility,syslog_priority)\
  145. __attribute__ ((constructor)) \
  146. static void logsys_system_init (void) \
  147. { \
  148. if (_logsys_system_setup (name,mode,syslog_facility,syslog_priority) < 0) { \
  149. fprintf (stderr, \
  150. "Unable to setup logging system: %s.\n", name); \
  151. exit (-1); \
  152. } \
  153. }
  154. #define LOGSYS_DECLARE_SUBSYS(subsys) \
  155. __attribute__ ((constructor)) \
  156. static void logsys_subsys_init (void) \
  157. { \
  158. assert(__start___verbose != __stop___verbose); \
  159. logsys_subsys_id = \
  160. _logsys_subsys_create ((subsys), __FILE__); \
  161. if (logsys_subsys_id == -1) { \
  162. fprintf (stderr, \
  163. "Unable to create logging subsystem: %s.\n", subsys); \
  164. exit (-1); \
  165. } \
  166. }
  167. #define LOGSYS_PERROR(err_num, level, fmt, args...) do { \
  168. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  169. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  170. qb_log(level, fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  171. } while(0)
  172. #define log_printf(level, format, args...) qb_log(level, format, ##args)
  173. #define ENTER qb_enter
  174. #define LEAVE qb_leave
  175. #define TRACE1(format, args...) qb_log(LOG_TRACE, "TRACE1:" #format, ##args)
  176. #define TRACE2(format, args...) qb_log(LOG_TRACE, "TRACE2:" #format, ##args)
  177. #define TRACE3(format, args...) qb_log(LOG_TRACE, "TRACE3:" #format, ##args)
  178. #define TRACE4(format, args...) qb_log(LOG_TRACE, "TRACE4:" #format, ##args)
  179. #define TRACE5(format, args...) qb_log(LOG_TRACE, "TRACE5:" #format, ##args)
  180. #define TRACE6(format, args...) qb_log(LOG_TRACE, "TRACE6:" #format, ##args)
  181. #define TRACE7(format, args...) qb_log(LOG_TRACE, "TRACE7:" #format, ##args)
  182. #define TRACE8(format, args...) qb_log(LOG_TRACE, "TRACE8:" #format, ##args)
  183. #endif /* LOGSYS_UTILS_ONLY */
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif /* LOGSYS_H_DEFINED */