logsys.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * Author: Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * All rights reserved.
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef LOGSYS_H_DEFINED
  38. #define LOGSYS_H_DEFINED
  39. #include <stdarg.h>
  40. #include <stdlib.h>
  41. #include <syslog.h>
  42. #include <pthread.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /*
  47. * All of the LOGSYS_MODE's can be ORed together for combined behavior
  48. *
  49. * FORK and THREADED are ignored for SUBSYSTEMS
  50. */
  51. #define LOGSYS_MODE_OUTPUT_FILE (1<<0)
  52. #define LOGSYS_MODE_OUTPUT_STDERR (1<<1)
  53. #define LOGSYS_MODE_OUTPUT_SYSLOG (1<<2)
  54. #define LOGSYS_MODE_FORK (1<<3)
  55. #define LOGSYS_MODE_THREADED (1<<4)
  56. /*
  57. * Log priorities, compliant with syslog and SA Forum Log spec.
  58. * LOGSYS_LEVEL_SECURITY is corosync/openais specific
  59. */
  60. #define LOGSYS_LEVEL_EMERG LOG_EMERG
  61. #define LOGSYS_LEVEL_ALERT LOG_ALERT
  62. #define LOGSYS_LEVEL_CRIT LOG_CRIT
  63. #define LOGSYS_LEVEL_ERROR LOG_ERR
  64. #define LOGSYS_LEVEL_WARNING LOG_WARNING
  65. #define LOGSYS_LEVEL_SECURITY LOG_WARNING
  66. #define LOGSYS_LEVEL_NOTICE LOG_NOTICE
  67. #define LOGSYS_LEVEL_INFO LOG_INFO
  68. #define LOGSYS_LEVEL_DEBUG LOG_DEBUG
  69. /*
  70. * All of the LOGSYS_TAG's can be ORed together for combined behavior
  71. *
  72. * TAG_LOG controls the behaviour for tracing and it's mostly for internal
  73. * use only. Enable it to see tracing messages in normal log output.
  74. * Disable to record tracing only in the flight recorder.
  75. * logging via log_printf will set TAG_LOG automatically.
  76. *
  77. * ENTER/LEAVE/TRACE* will not set TAG_LOG.
  78. *
  79. * All tracing messages have priority set to LOGSYS_LEVEL_DEBUG and cannot
  80. * be changed.
  81. *
  82. * Enabling output for debug will not enable logging of tracing messages
  83. * automatically. Tracing has to be enabled explicitly.
  84. */
  85. #define LOGSYS_TAG_LOG (1<<0)
  86. #define LOGSYS_TAG_ENTER (1<<1)
  87. #define LOGSYS_TAG_LEAVE (1<<2)
  88. #define LOGSYS_TAG_TRACE1 (1<<3)
  89. #define LOGSYS_TAG_TRACE2 (1<<4)
  90. #define LOGSYS_TAG_TRACE3 (1<<5)
  91. #define LOGSYS_TAG_TRACE4 (1<<6)
  92. #define LOGSYS_TAG_TRACE5 (1<<7)
  93. #define LOGSYS_TAG_TRACE6 (1<<8)
  94. #define LOGSYS_TAG_TRACE7 (1<<9)
  95. #define LOGSYS_TAG_TRACE8 (1<<10)
  96. /*
  97. * Internal APIs that must be globally exported
  98. * (External API below)
  99. */
  100. /*
  101. * logsys_logger bits
  102. *
  103. * SUBSYS_COUNT defines the maximum number of subsystems
  104. * SUBSYS_NAMELEN defines the maximum len of a subsystem name
  105. */
  106. #define LOGSYS_MAX_SUBSYS_COUNT 64
  107. #define LOGSYS_MAX_SUBSYS_NAMELEN 64
  108. extern int _logsys_system_setup(
  109. const char *mainsystem,
  110. unsigned int mode,
  111. unsigned int debug,
  112. const char *logfile,
  113. int logfile_priority,
  114. int syslog_facility,
  115. int syslog_priority,
  116. unsigned int tags);
  117. extern int _logsys_config_subsys_get (
  118. const char *subsys);
  119. extern unsigned int _logsys_subsys_create (const char *subsys);
  120. extern int _logsys_rec_init (unsigned int size);
  121. extern void _logsys_log_vprintf (
  122. int subsysid,
  123. const char *function_name,
  124. const char *file_name,
  125. int file_line,
  126. unsigned int level,
  127. unsigned int tag,
  128. const char *format,
  129. va_list ap) __attribute__((format(printf, 7, 0)));
  130. extern void _logsys_log_printf (
  131. int subsysid,
  132. const char *function_name,
  133. const char *file_name,
  134. int file_line,
  135. unsigned int level,
  136. unsigned int tag,
  137. const char *format,
  138. ...) __attribute__((format(printf, 7, 8)));
  139. extern void _logsys_log_rec (
  140. int subsysid,
  141. const char *function_name,
  142. const char *file_name,
  143. int file_line,
  144. unsigned int rec_ident,
  145. unsigned int tag,
  146. ...);
  147. extern int _logsys_wthread_create (void);
  148. static unsigned int logsys_subsys_id __attribute__((unused)) = -1;
  149. /*
  150. * External API - init
  151. * See below:
  152. *
  153. * LOGSYS_DECLARE_SYSTEM
  154. * LOGSYS_DECLARE_SUBSYS
  155. *
  156. */
  157. extern void logsys_fork_completed (void);
  158. extern void logsys_atexit (void);
  159. /*
  160. * External API - misc
  161. */
  162. extern void logsys_flush (void);
  163. extern int logsys_log_rec_store (const char *filename);
  164. /*
  165. * External API - configuration
  166. */
  167. /*
  168. * configuration bits that can only be done for the whole system
  169. */
  170. extern int logsys_format_set (
  171. const char *format);
  172. extern char *logsys_format_get (void);
  173. /*
  174. * per system/subsystem settings.
  175. *
  176. * NOTE: once a subsystem is created and configured, changing
  177. * the default does NOT affect the subsystems.
  178. *
  179. * Pass a NULL subsystem to change them all
  180. */
  181. extern unsigned int logsys_config_syslog_facility_set (
  182. const char *subsys,
  183. unsigned int facility);
  184. extern unsigned int logsys_config_syslog_priority_set (
  185. const char *subsys,
  186. unsigned int priority);
  187. extern unsigned int logsys_config_mode_set (
  188. const char *subsys,
  189. unsigned int mode);
  190. extern unsigned int logsys_config_mode_get (
  191. const char *subsys);
  192. extern unsigned int logsys_config_tags_set (
  193. const char *subsys,
  194. unsigned int tags);
  195. extern unsigned int logsys_config_tags_get (
  196. const char *subsys);
  197. /*
  198. * to close a logfile, just invoke this function with a NULL
  199. * file or if you want to change logfile, the old one will
  200. * be closed for you.
  201. */
  202. extern int logsys_config_file_set (
  203. const char *subsys,
  204. const char **error_string,
  205. const char *file);
  206. extern unsigned int logsys_config_logfile_priority_set (
  207. const char *subsys,
  208. unsigned int priority);
  209. /*
  210. * enabling debug, disable message priority filtering.
  211. * everything is sent everywhere. priority values
  212. * for file and syslog are not overwritten.
  213. */
  214. extern unsigned int logsys_config_debug_set (
  215. const char *subsys,
  216. unsigned int value);
  217. /*
  218. * External API - helpers
  219. *
  220. * convert facility/priority/tag to/from name/values
  221. */
  222. extern int logsys_facility_id_get (
  223. const char *name);
  224. extern const char *logsys_facility_name_get (
  225. unsigned int facility);
  226. extern int logsys_priority_id_get (
  227. const char *name);
  228. extern const char *logsys_priority_name_get (
  229. unsigned int priority);
  230. extern int logsys_tag_id_get (
  231. const char *name);
  232. extern const char *logsys_tag_name_get (
  233. unsigned int tag);
  234. extern int logsys_thread_priority_set (
  235. int policy,
  236. const struct sched_param *param,
  237. unsigned int after_log_ops_yield);
  238. /*
  239. * External definitions
  240. */
  241. extern void *logsys_rec_end;
  242. #define LOGSYS_REC_END (&logsys_rec_end)
  243. #define LOGSYS_DECLARE_SYSTEM(name,mode,debug,file,file_priority, \
  244. syslog_facility,syslog_priority,tags,format,rec_size) \
  245. __attribute__ ((constructor)) \
  246. static void logsys_system_init (void) \
  247. { \
  248. if (_logsys_system_setup (name,mode,debug,file,file_priority, \
  249. syslog_facility,syslog_priority,tags) < 0) { \
  250. fprintf (stderr, \
  251. "Unable to setup logging system: %s.\n", name); \
  252. exit (-1); \
  253. } \
  254. \
  255. if (logsys_format_set (format) < 0) { \
  256. fprintf (stderr, \
  257. "Unable to setup logging format.\n"); \
  258. exit (-1); \
  259. } \
  260. \
  261. if (_logsys_rec_init (rec_size) < 0) { \
  262. fprintf (stderr, \
  263. "Unable to initialize log flight recorder.\n"); \
  264. exit (-1); \
  265. } \
  266. \
  267. if (_logsys_wthread_create() < 0) { \
  268. fprintf (stderr, \
  269. "Unable to initialize logging thread.\n"); \
  270. exit (-1); \
  271. } \
  272. }
  273. #define LOGSYS_DECLARE_SUBSYS(subsys) \
  274. __attribute__ ((constructor)) \
  275. static void logsys_subsys_init (void) \
  276. { \
  277. logsys_subsys_id = \
  278. _logsys_subsys_create ((subsys)); \
  279. if (logsys_subsys_id == -1) { \
  280. fprintf (stderr, \
  281. "Unable to create logging subsystem: %s.\n", subsys); \
  282. exit (-1); \
  283. } \
  284. }
  285. #define log_rec(rec_ident, args...) \
  286. do { \
  287. _logsys_log_rec (logsys_subsys_id, __FUNCTION__, \
  288. __FILE__, __LINE__, rec_ident, 0, ##args); \
  289. } while(0)
  290. #define log_printf(lvl, format, args...) \
  291. do { \
  292. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  293. __FILE__, __LINE__, lvl, 0, format, ##args); \
  294. } while(0)
  295. #define ENTER() do { \
  296. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  297. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  298. LOGSYS_TAG_ENTER, "ENTERING function [%s] line [%d]\n", \
  299. __FUNCTION__, __LINE__); \
  300. } while(0)
  301. #define LEAVE() do { \
  302. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  303. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  304. LOGSYS_TAG_LEAVE, "LEAVING function [%s] line [%d]\n", \
  305. __FUNCTION__, __LINE__); \
  306. } while(0)
  307. #define TRACE1(format, args...) do { \
  308. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  309. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  310. LOGSYS_TAG_TRACE1, format, ##args); \
  311. } while(0)
  312. #define TRACE2(format, args...) do { \
  313. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  314. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  315. LOGSYS_TAG_TRACE2, format, ##args); \
  316. } while(0)
  317. #define TRACE3(format, args...) do { \
  318. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  319. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  320. LOGSYS_TAG_TRACE3, format, ##args); \
  321. } while(0)
  322. #define TRACE4(format, args...) do { \
  323. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  324. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  325. LOGSYS_TAG_TRACE4, format, ##args); \
  326. } while(0)
  327. #define TRACE5(format, args...) do { \
  328. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  329. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  330. LOGSYS_TAG_TRACE5, format, ##args); \
  331. } while(0)
  332. #define TRACE6(format, args...) do { \
  333. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  334. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  335. LOGSYS_TAG_TRACE6, format, ##args); \
  336. } while(0)
  337. #define TRACE7(format, args...) do { \
  338. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  339. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  340. LOGSYS_TAG_TRACE7, format, ##args); \
  341. } while(0)
  342. #define TRACE8(format, args...) do { \
  343. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  344. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  345. LOGSYS_TAG_TRACE8, format, ##args); \
  346. } while(0)
  347. #ifdef __cplusplus
  348. }
  349. #endif
  350. #endif /* LOGSYS_H_DEFINED */