logsys.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 int logsys_config_file_set (
  93. char **error_string,
  94. char *file);
  95. extern void logsys_config_facility_set (
  96. char *name,
  97. unsigned int facility);
  98. extern unsigned int logsys_config_subsys_set (
  99. const char *subsys,
  100. unsigned int tags,
  101. unsigned int priority);
  102. extern int logsys_config_subsys_get (
  103. const char *subsys,
  104. unsigned int *tags,
  105. unsigned int *priority);
  106. extern int logsys_facility_id_get (
  107. const char *name);
  108. extern char *logsys_facility_name_get (
  109. unsigned int facility);
  110. extern int logsys_priority_id_get (
  111. const char *name);
  112. extern char *logsys_priority_name_get (
  113. unsigned int priority);
  114. extern void logsys_flush (void);
  115. extern void logsys_atsegv (void);
  116. /*
  117. * Internal APIs that must be globally exported
  118. */
  119. extern unsigned int _logsys_subsys_create (const char *ident,
  120. unsigned int priority);
  121. extern void _logsys_nosubsys_set (void);
  122. extern int _logsys_wthread_create (void);
  123. extern void logsys_log_printf (char *file, int line, int priority,
  124. char *format, ...) __attribute__((format(printf, 4, 5)));
  125. extern void _logsys_log_printf2 (char *file, int line, int priority,
  126. int id, char *format, ...) __attribute__((format(printf, 5, 6)));
  127. extern void _logsys_trace (char *file, int line, int tag, int id,
  128. char *format, ...) __attribute__((format(printf, 5, 6)));
  129. /*
  130. * External definitions
  131. */
  132. #define LOGSYS_DECLARE_SYSTEM(name,mode,file,facility) \
  133. __attribute__ ((constructor)) static void logsys_system_init (void) \
  134. { \
  135. char *error_string; \
  136. \
  137. logsys_config_mode_set (mode); \
  138. logsys_config_file_set (&error_string, (file)); \
  139. logsys_config_facility_set (name, (facility)); \
  140. if (((mode) & LOG_MODE_BUFFER_BEFORE_CONFIG) == 0) { \
  141. _logsys_wthread_create (); \
  142. } \
  143. }
  144. #define LOGSYS_DECLARE_NOSUBSYS(priority) \
  145. static unsigned int logsys_subsys_id __attribute__((unused)); \
  146. __attribute__ ((constructor)) static void logsys_nosubsys_init (void) \
  147. { \
  148. _logsys_nosubsys_set(); \
  149. logsys_subsys_id = \
  150. _logsys_subsys_create ("MAIN", (priority)); \
  151. }
  152. #define LOGSYS_DECLARE_SUBSYS(subsys,priority) \
  153. static unsigned int logsys_subsys_id __attribute__((unused)); \
  154. __attribute__ ((constructor)) static void logsys_subsys_init (void) \
  155. { \
  156. logsys_subsys_id = \
  157. _logsys_subsys_create ((subsys), (priority)); \
  158. }
  159. #define log_printf(lvl, format, args...) do { \
  160. if ((lvl) <= logsys_loggers[logsys_subsys_id].priority) { \
  161. _logsys_log_printf2 (__FILE__, __LINE__, lvl, \
  162. logsys_subsys_id, (format), ##args); \
  163. } \
  164. } while(0)
  165. #define dprintf(format, args...) do { \
  166. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  167. _logsys_log_printf2 (__FILE__, __LINE__, LOG_DEBUG, \
  168. logsys_subsys_id, (format), ##args); \
  169. } \
  170. } while(0)
  171. #define ENTER_VOID() do { \
  172. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  173. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_ENTER, \
  174. logsys_subsys_id, ">%s\n", __FUNCTION__); \
  175. } \
  176. } while(0)
  177. #define ENTER(format, args...) do { \
  178. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  179. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_ENTER, \
  180. logsys_subsys_id, ">%s: " format, __FUNCTION__, \
  181. ##args); \
  182. } \
  183. } while(0)
  184. #define LEAVE_VOID() do { \
  185. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  186. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_LEAVE, \
  187. logsys_subsys_id, "<%s\n", __FUNCTION__); \
  188. } \
  189. } while(0)
  190. #define LEAVE(format, args...) do { \
  191. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  192. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_LEAVE, \
  193. logsys_subsys_id, "<%s: " format, \
  194. __FUNCTION__, ##args); \
  195. } \
  196. } while(0)
  197. #define TRACE1(format, args...) do { \
  198. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  199. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE1, \
  200. logsys_subsys_id, (format), ##args); \
  201. } \
  202. } while(0)
  203. #define TRACE2(format, args...) do { \
  204. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  205. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE2, \
  206. logsys_subsys_id, (format), ##args); \
  207. } \
  208. } while(0)
  209. #define TRACE3(format, args...) do { \
  210. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  211. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE3, \
  212. logsys_subsys_id, (format), ##args); \
  213. } \
  214. } while(0)
  215. #define TRACE4(format, args...) do { \
  216. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  217. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE4, \
  218. logsys_subsys_id, (format), ##args); \
  219. } \
  220. } while(0)
  221. #define TRACE5(format, args...) do { \
  222. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  223. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE5, \
  224. logsys_subsys_id, (format), ##args); \
  225. } \
  226. } while(0)
  227. #define TRACE6(format, args...) do { \
  228. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  229. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE6, \
  230. logsys_subsys_id, (format), ##args); \
  231. } \
  232. } while(0)
  233. #define TRACE7(format, args...) do { \
  234. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  235. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE7, \
  236. logsys_subsys_id, (format), ##args); \
  237. } \
  238. } while(0)
  239. #define TRACE8(format, args...) do { \
  240. if (LOG_LEVEL_DEBUG <= logsys_loggers[logsys_subsys_id].priority) { \
  241. _logsys_trace (__FILE__, __LINE__, LOGSYS_TAG_TRACE8, \
  242. logsys_subsys_id, (format), ##args); \
  243. } \
  244. } while(0)
  245. extern void _logsys_config_priority_set (unsigned int id, unsigned int priority);
  246. #define logsys_config_priority_set(priority) do { \
  247. _logsys_config_priority_set (logsys_subsys_id, priority); \
  248. } while(0)
  249. #endif /* LOGSYS_H_DEFINED */