logsys.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * Author: Steven Dake (sdake@redhat.com)
  6. * Author: Lon Hohberger (lhh@redhat.com)
  7. *
  8. * All rights reserved.
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #ifndef LOGSYS_H_DEFINED
  37. #define LOGSYS_H_DEFINED
  38. #include <stdarg.h>
  39. #include <syslog.h>
  40. #include <assert.h>
  41. /*
  42. * All of the LOG_MODE's can be ORed together for combined behavior
  43. */
  44. #define LOG_MODE_OUTPUT_FILE (1<<0)
  45. #define LOG_MODE_OUTPUT_STDERR (1<<1)
  46. #define LOG_MODE_OUTPUT_SYSLOG (1<<3)
  47. #define LOG_MODE_NOSUBSYS (1<<4)
  48. #define LOG_MODE_FORK (1<<5)
  49. #define LOG_MODE_THREADED (1<<6)
  50. /*
  51. * Log priorities, compliant with syslog and SA Forum Log spec.
  52. */
  53. #define LOG_LEVEL_EMERG LOG_EMERG
  54. #define LOG_LEVEL_ALERT LOG_ALERT
  55. #define LOG_LEVEL_CRIT LOG_CRIT
  56. #define LOG_LEVEL_ERROR LOG_ERR
  57. #define LOG_LEVEL_WARNING LOG_WARNING
  58. #define LOG_LEVEL_SECURITY LOG_WARNING // corosync specific
  59. #define LOG_LEVEL_NOTICE LOG_NOTICE
  60. #define LOG_LEVEL_INFO LOG_INFO
  61. #define LOG_LEVEL_DEBUG LOG_DEBUG
  62. /*
  63. * The tag masks are all mutually exclusive
  64. */
  65. #define LOGSYS_TAG_LOG (0xff<<28)
  66. #define LOGSYS_TAG_ENTER (1<<27)
  67. #define LOGSYS_TAG_LEAVE (1<<26)
  68. #define LOGSYS_TAG_TRACE1 (1<<25)
  69. #define LOGSYS_TAG_TRACE2 (1<<24)
  70. #define LOGSYS_TAG_TRACE3 (1<<23)
  71. #define LOGSYS_TAG_TRACE4 (1<<22)
  72. #define LOGSYS_TAG_TRACE5 (1<<21)
  73. #define LOGSYS_TAG_TRACE6 (1<<20)
  74. #define LOGSYS_TAG_TRACE7 (1<<19)
  75. #define LOGSYS_TAG_TRACE8 (1<<18)
  76. /*
  77. * External API
  78. */
  79. extern void logsys_config_mode_set (
  80. unsigned int mode);
  81. extern unsigned int logsys_config_mode_get (void);
  82. extern int logsys_config_file_set (
  83. const char **error_string,
  84. const char *file);
  85. extern void logsys_config_facility_set (
  86. const char *name,
  87. unsigned int facility);
  88. extern int logsys_format_set (
  89. const char *format);
  90. extern char *logsys_format_get (void);
  91. extern unsigned int logsys_config_subsys_set (
  92. const char *subsys,
  93. unsigned int tags,
  94. unsigned int priority);
  95. extern int logsys_config_subsys_get (
  96. const char *subsys,
  97. unsigned int *tags,
  98. unsigned int *priority);
  99. extern int logsys_facility_id_get (
  100. const char *name);
  101. extern const char *logsys_facility_name_get (
  102. unsigned int facility);
  103. extern int logsys_priority_id_get (
  104. const char *name);
  105. extern const char *logsys_priority_name_get (
  106. unsigned int priority);
  107. extern int logsys_tag_id_get (
  108. const char *name);
  109. extern const char *logsys_tag_name_get (
  110. unsigned int tag);
  111. extern void logsys_fork_completed (void);
  112. extern void logsys_flush (void);
  113. extern void logsys_atsegv (void);
  114. extern int logsys_log_rec_store (const char *filename);
  115. /*
  116. * Internal APIs that must be globally exported
  117. */
  118. extern unsigned int _logsys_subsys_create (
  119. const char *ident,
  120. unsigned int priority);
  121. extern void _logsys_nosubsys_set (void);
  122. extern int _logsys_rec_init (unsigned int size);
  123. extern void _logsys_log_printf (
  124. int subsys,
  125. const char *function_name,
  126. const char *file_name,
  127. int file_line,
  128. unsigned int level,
  129. const char *format,
  130. ...) __attribute__((format(printf, 6, 7)));
  131. extern void _logsys_log_rec (
  132. int subsys,
  133. const char *function_name,
  134. const char *file_name,
  135. int file_line,
  136. unsigned int rec_ident,
  137. ...);
  138. extern int _logsys_wthread_create (void);
  139. static unsigned int logsys_subsys_id __attribute__((unused)) = -1;
  140. /*
  141. * External definitions
  142. */
  143. extern void *logsys_rec_end;
  144. #define LOG_REC_END (&logsys_rec_end)
  145. #define LOGSYS_DECLARE_SYSTEM(name,mode,file,facility,format,rec_size) \
  146. __attribute__ ((constructor)) static void logsys_system_init (void) \
  147. { \
  148. const char *error_string; \
  149. \
  150. logsys_config_mode_set (mode); \
  151. logsys_config_file_set (&error_string, (file)); \
  152. logsys_config_facility_set (name, (facility)); \
  153. logsys_format_set (format); /* FIXME: assert success? */ \
  154. _logsys_rec_init (rec_size); \
  155. _logsys_wthread_create(); \
  156. }
  157. #define LOGSYS_DECLARE_NOSUBSYS(priority) \
  158. __attribute__ ((constructor)) static void logsys_nosubsys_init (void) \
  159. { \
  160. unsigned int pri, tags; \
  161. \
  162. logsys_subsys_id = \
  163. logsys_config_subsys_get("MAIN", &tags, &pri); \
  164. \
  165. if (logsys_subsys_id == -1) { \
  166. _logsys_nosubsys_set(); \
  167. logsys_subsys_id = \
  168. _logsys_subsys_create ("MAIN", (priority)); \
  169. } \
  170. }
  171. #define LOGSYS_DECLARE_SUBSYS(subsys,priority) \
  172. __attribute__ ((constructor)) static void logsys_subsys_init (void) \
  173. { \
  174. unsigned int pri, tags; \
  175. \
  176. logsys_subsys_id = \
  177. logsys_config_subsys_get((subsys), &tags, &pri); \
  178. \
  179. if (logsys_subsys_id == -1) \
  180. logsys_subsys_id = \
  181. _logsys_subsys_create ((subsys), (priority)); \
  182. }
  183. #define log_rec(rec_ident, args...) \
  184. do { \
  185. _logsys_log_rec (logsys_subsys_id, __FUNCTION__, \
  186. __FILE__, __LINE__, rec_ident, ##args); \
  187. } while(0)
  188. #define log_printf(lvl, format, args...) \
  189. do { \
  190. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  191. __FILE__, __LINE__, lvl, format, ##args); \
  192. } while(0)
  193. #define ENTER() do { \
  194. _logsys_log_rec (logsys_subsys_id, __FUNCTION__, \
  195. __FILE__, __LINE__, LOGSYS_TAG_ENTER, LOG_REC_END); \
  196. } while(0)
  197. #define LEAVE() do { \
  198. _logsys_log_rec (logsys_subsys_id, __FUNCTION__, \
  199. __FILE__, __LINE__, LOGSYS_TAG_LEAVE, LOG_REC_END); \
  200. } while(0)
  201. #define TRACE1(format, args...) do { \
  202. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  203. __FILE__, __LINE__, LOGSYS_TAG_TRACE1, format, ##args);\
  204. } while(0)
  205. #define TRACE2(format, args...) do { \
  206. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  207. __FILE__, __LINE__, LOGSYS_TAG_TRACE2, format, ##args);\
  208. } while(0)
  209. #define TRACE3(format, args...) do { \
  210. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  211. __FILE__, __LINE__, LOGSYS_TAG_TRACE3, format, ##args);\
  212. } while(0)
  213. #define TRACE4(format, args...) do { \
  214. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  215. __FILE__, __LINE__, LOGSYS_TAG_TRACE4, format, ##args);\
  216. } while(0)
  217. #define TRACE5(format, args...) do { \
  218. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  219. __FILE__, __LINE__, LOGSYS_TAG_TRACE5, format, ##args);\
  220. } while(0)
  221. #define TRACE6(format, args...) do { \
  222. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  223. __FILE__, __LINE__, LOGSYS_TAG_TRACE6, format, ##args);\
  224. } while(0)
  225. #define TRACE7(format, args...) do { \
  226. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  227. __FILE__, __LINE__, LOGSYS_TAG_TRACE7, format, ##args);\
  228. } while(0)
  229. #define TRACE8(format, args...) do { \
  230. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  231. __FILE__, __LINE__, LOGSYS_TAG_TRACE8, format, ##args);\
  232. } while(0)
  233. /*
  234. * For one-time programmatic initialization and configuration of logsys
  235. * instead of using the DECLARE macros. These APIs do not allow subsystems
  236. */
  237. int logsys_init (
  238. const char *name,
  239. int mode,
  240. int facility,
  241. int priority,
  242. const char *file,
  243. char *format,
  244. int rec_size);
  245. int logsys_conf (
  246. char *name,
  247. int mode,
  248. int facility,
  249. int priority,
  250. char *file);
  251. void logsys_exit (void);
  252. #endif /* LOGSYS_H_DEFINED */