logsys.h 9.4 KB

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