logsys.h 11 KB

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