logsys.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007 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. /*
  41. * MODE_OUTPUT_SYSLOG_* modes are mutually exclusive
  42. */
  43. #define LOG_MODE_OUTPUT_FILE (1<<0)
  44. #define LOG_MODE_OUTPUT_STDERR (1<<1)
  45. #define LOG_MODE_OUTPUT_SYSLOG_THREADED (1<<2)
  46. #define LOG_MODE_OUTPUT_SYSLOG_LOSSY (1<<3)
  47. #define LOG_MODE_OUTPUT_SYSLOG_BLOCKING (1<<4)
  48. #define LOG_MODE_DISPLAY_PRIORITY (1<<5)
  49. #define LOG_MODE_DISPLAY_FILELINE (1<<6)
  50. #define LOG_MODE_DISPLAY_TIMESTAMP (1<<7)
  51. #define LOG_MODE_DISPLAY_DEBUG (1<<8)
  52. #define LOG_MODE_BUFFER_BEFORE_CONFIG (1<<9)
  53. #define LOG_MODE_FLUSH_AFTER_CONFIG (1<<10)
  54. /*
  55. * Log priorities, compliant with syslog and SA Forum Log spec.
  56. */
  57. #define LOG_LEVEL_EMERG LOG_EMERG
  58. #define LOG_LEVEL_ALERT LOG_ALERT
  59. #define LOG_LEVEL_CRIT LOG_CRIT
  60. #define LOG_LEVEL_ERROR LOG_ERR
  61. #define LOG_LEVEL_WARNING LOG_WARNING
  62. #define LOG_LEVEL_SECURITY LOG_WARNING // openais specific
  63. #define LOG_LEVEL_NOTICE LOG_NOTICE
  64. #define LOG_LEVEL_INFO LOG_INFO
  65. #define LOG_LEVEL_DEBUG LOG_DEBUG
  66. /*
  67. ** Log tags, used by _logsys_trace macros, uses 32 bits => 32 different tags
  68. */
  69. #define LOGSYS_TAG_LOG (1<<0)
  70. #define LOGSYS_TAG_ENTER (1<<1)
  71. #define LOGSYS_TAG_LEAVE (1<<2)
  72. #define LOGSYS_TAG_TRACE1 (1<<3)
  73. #define LOGSYS_TAG_TRACE2 (1<<4)
  74. #define LOGSYS_TAG_TRACE3 (1<<5)
  75. #define LOGSYS_TAG_TRACE4 (1<<6)
  76. #define LOGSYS_TAG_TRACE5 (1<<7)
  77. #define LOGSYS_TAG_TRACE6 (1<<8)
  78. #define LOGSYS_TAG_TRACE7 (1<<9)
  79. #define LOGSYS_TAG_TRACE8 (1<<10)
  80. /*
  81. * External API
  82. */
  83. struct logsys_logger {
  84. char subsys[6];
  85. unsigned int priority;
  86. unsigned int tags;
  87. unsigned int mode;
  88. };
  89. extern struct logsys_logger logsys_loggers[];
  90. extern inline int logsys_mkpri (int priority, int id);
  91. extern void logsys_config_mode_set (
  92. unsigned int mode);
  93. extern int logsys_config_file_set (
  94. char **error_string,
  95. char *file);
  96. extern void logsys_config_facility_set (
  97. char *name,
  98. unsigned int facility);
  99. extern void logsys_config_priority_set (
  100. unsigned int priority);
  101. extern unsigned int logsys_config_subsys_set (
  102. const char *subsys,
  103. unsigned int tags,
  104. unsigned int priority);
  105. extern int logsys_config_subsys_get (
  106. const char *subsys,
  107. unsigned int *tags,
  108. unsigned int *priority);
  109. extern int logsys_facility_id_get (
  110. const char *name);
  111. extern int logsys_priority_id_get (
  112. const char *name);
  113. extern void logsys_flush (void);
  114. extern void logsys_atsegv (void);
  115. /*
  116. * Internal APIs that must be globally exported
  117. */
  118. extern unsigned int _logsys_subsys_create (const char *ident,
  119. unsigned int priority);
  120. extern void _logsys_nosubsys_set (void);
  121. extern int _logsys_wthread_create (void);
  122. extern void logsys_log_printf (char *file, int line, int priority,
  123. char *format, ...) __attribute__((format(printf, 4, 5)));
  124. extern void _logsys_log_printf2 (char *file, int line, int priority,
  125. int id, char *format, ...) __attribute__((format(printf, 5, 6)));
  126. extern void _logsys_trace (char *file, int line, int tag, int id,
  127. char *format, ...) __attribute__((format(printf, 5, 6)));
  128. /*
  129. * External definitions
  130. */
  131. #define LOGSYS_DECLARE_SYSTEM(name,mode,file,facility) \
  132. __attribute__ ((constructor)) static void logsys_system_init (void) \
  133. { \
  134. char *error_string; \
  135. \
  136. logsys_config_mode_set (mode); \
  137. logsys_config_file_set (&error_string, (file)); \
  138. logsys_config_facility_set (name, (facility)); \
  139. if (((mode) & LOG_MODE_BUFFER_BEFORE_CONFIG) == 0) { \
  140. _logsys_wthread_create (); \
  141. } \
  142. }
  143. #define LOGSYS_DECLARE_NOSUBSYS(priority) \
  144. static unsigned int logsys_subsys_id __attribute__((unused)); \
  145. __attribute__ ((constructor)) static void logsys_nosubsys_init (void) \
  146. { \
  147. _logsys_nosubsys_set(); \
  148. logsys_subsys_id = \
  149. _logsys_subsys_create ("MAIN", (priority)); \
  150. }
  151. #define LOGSYS_DECLARE_SUBSYS(subsys,priority) \
  152. static unsigned int logsys_subsys_id __attribute__((unused)); \
  153. __attribute__ ((constructor)) static void logsys_subsys_init (void) \
  154. { \
  155. logsys_subsys_id = \
  156. _logsys_subsys_create ((subsys), (priority)); \
  157. }
  158. #define log_printf(lvl, format, args...) do { \
  159. if ((lvl) <= logsys_loggers[logsys_subsys_id].priority) { \
  160. _logsys_log_printf2 (__FILE__, __LINE__, lvl, \
  161. logsys_subsys_id, (format), ##args); \
  162. } \
  163. } while(0)
  164. #define dprintf(format, args...) do { \
  165. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  166. _logsys_log_printf2 (__FILE__, __LINE__, LOG_DEBUG, \
  167. logsys_subsys_id, (format), ##args); \
  168. } \
  169. } while(0)
  170. #define ENTER_VOID() do { \
  171. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  172. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_ENTER, \
  173. logsys_subsys_id, ">%s\n", __FUNCTION__); \
  174. } \
  175. } while(0)
  176. #define ENTER(format, args...) do { \
  177. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  178. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_ENTER, \
  179. logsys_subsys_id, ">%s: " format, __FUNCTION__, \
  180. ##args); \
  181. } \
  182. } while(0)
  183. #define LEAVE_VOID() do { \
  184. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  185. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_LEAVE, \
  186. logsys_subsys_id, "<%s\n", __FUNCTION__); \
  187. } \
  188. } while(0)
  189. #define LEAVE(format, args...) do { \
  190. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  191. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_LEAVE, \
  192. logsys_subsys_id, "<%s: " format, \
  193. __FUNCTION__, ##args); \
  194. } \
  195. } while(0)
  196. #define TRACE1(format, args...) do { \
  197. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  198. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE1, \
  199. logsys_subsys_id, (format), ##args); \
  200. } \
  201. } while(0)
  202. #define TRACE2(format, args...) do { \
  203. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  204. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE2, \
  205. logsys_subsys_id, (format), ##args); \
  206. } \
  207. } while(0)
  208. #define TRACE3(format, args...) do { \
  209. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  210. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE3, \
  211. logsys_subsys_id, (format), ##args); \
  212. } \
  213. } while(0)
  214. #define TRACE4(format, args...) do { \
  215. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  216. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE4, \
  217. logsys_subsys_id, (format), ##args); \
  218. } \
  219. } while(0)
  220. #define TRACE5(format, args...) do { \
  221. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  222. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE5, \
  223. logsys_subsys_id, (format), ##args); \
  224. } \
  225. } while(0)
  226. #define TRACE6(format, args...) do { \
  227. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  228. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE6, \
  229. logsys_subsys_id, (format), ##args); \
  230. } \
  231. } while(0)
  232. #define TRACE7(format, args...) do { \
  233. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  234. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE7, \
  235. logsys_subsys_id, (format), ##args); \
  236. } \
  237. } while(0)
  238. #define TRACE8(format, args...) do { \
  239. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  240. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE8, \
  241. logsys_subsys_id, (format), ##args); \
  242. } \
  243. } while(0)
  244. #endif /* LOGSYS_H_DEFINED */