logsys.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. #include <limits.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /*
  48. * All of the LOGSYS_MODE's can be ORed together for combined behavior
  49. *
  50. * FORK and THREADED are ignored for SUBSYSTEMS
  51. */
  52. #define LOGSYS_MODE_OUTPUT_FILE (1<<0)
  53. #define LOGSYS_MODE_OUTPUT_STDERR (1<<1)
  54. #define LOGSYS_MODE_OUTPUT_SYSLOG (1<<2)
  55. #define LOGSYS_MODE_FORK (1<<3)
  56. #define LOGSYS_MODE_THREADED (1<<4)
  57. /*
  58. * Log priorities, compliant with syslog and SA Forum Log spec.
  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_NOTICE LOG_NOTICE
  66. #define LOGSYS_LEVEL_INFO LOG_INFO
  67. #define LOGSYS_LEVEL_DEBUG LOG_DEBUG
  68. /*
  69. * All of the LOGSYS_RECID's are mutually exclusive. Only one RECID at any time
  70. * can be specified.
  71. *
  72. * RECID_LOG indicates a message that should be sent to log. Anything else
  73. * is stored only in the flight recorder.
  74. */
  75. #define LOGSYS_RECID_MAX ((UINT_MAX) >> LOGSYS_SUBSYSID_END)
  76. #define LOGSYS_RECID_LOG (LOGSYS_RECID_MAX - 1)
  77. #define LOGSYS_RECID_ENTER (LOGSYS_RECID_MAX - 2)
  78. #define LOGSYS_RECID_LEAVE (LOGSYS_RECID_MAX - 3)
  79. #define LOGSYS_RECID_TRACE1 (LOGSYS_RECID_MAX - 4)
  80. #define LOGSYS_RECID_TRACE2 (LOGSYS_RECID_MAX - 5)
  81. #define LOGSYS_RECID_TRACE3 (LOGSYS_RECID_MAX - 6)
  82. #define LOGSYS_RECID_TRACE4 (LOGSYS_RECID_MAX - 7)
  83. #define LOGSYS_RECID_TRACE5 (LOGSYS_RECID_MAX - 8)
  84. #define LOGSYS_RECID_TRACE6 (LOGSYS_RECID_MAX - 9)
  85. #define LOGSYS_RECID_TRACE7 (LOGSYS_RECID_MAX - 10)
  86. #define LOGSYS_RECID_TRACE8 (LOGSYS_RECID_MAX - 11)
  87. /*
  88. * Internal APIs that must be globally exported
  89. * (External API below)
  90. */
  91. /*
  92. * logsys_logger bits
  93. *
  94. * SUBSYS_COUNT defines the maximum number of subsystems
  95. * SUBSYS_NAMELEN defines the maximum len of a subsystem name
  96. */
  97. #define LOGSYS_MAX_SUBSYS_COUNT 64
  98. #define LOGSYS_MAX_SUBSYS_NAMELEN 64
  99. /*
  100. * rec_ident explained:
  101. *
  102. * rec_ident is an unsigned int and carries bitfields information
  103. * on subsys_id, log priority (level) and type of message (RECID).
  104. *
  105. * level values are imported from syslog.h.
  106. * At the time of writing it's a 3 bits value (0 to 7).
  107. *
  108. * subsys_id is any value between 0 and 64 (LOGSYS_MAX_SUBSYS_COUNT)
  109. *
  110. * RECID identifies the type of message. A set of predefined values
  111. * are available via logsys, but other custom values can be defined
  112. * by users.
  113. *
  114. * ----
  115. * bitfields:
  116. *
  117. * 0 - 2 level
  118. * 3 - 9 subsysid
  119. * 10 - n RECID
  120. */
  121. #define LOGSYS_LEVEL_END (3)
  122. #define LOGSYS_SUBSYSID_END (LOGSYS_LEVEL_END + 7)
  123. #define LOGSYS_RECID_LEVEL_MASK (LOG_PRIMASK)
  124. #define LOGSYS_RECID_SUBSYSID_MASK ((2 << (LOGSYS_SUBSYSID_END - 1)) - \
  125. (LOG_PRIMASK + 1))
  126. #define LOGSYS_RECID_RECID_MASK (UINT_MAX - \
  127. (LOGSYS_RECID_SUBSYSID_MASK + LOG_PRIMASK))
  128. #define LOGSYS_ENCODE_RECID(level,subsysid,recid) \
  129. (((recid) << LOGSYS_SUBSYSID_END) | \
  130. ((subsysid) << LOGSYS_LEVEL_END) | \
  131. (level))
  132. #define LOGSYS_DECODE_LEVEL(rec_ident) \
  133. ((rec_ident) & LOGSYS_RECID_LEVEL_MASK)
  134. #define LOGSYS_DECODE_SUBSYSID(rec_ident) \
  135. (((rec_ident) & LOGSYS_RECID_SUBSYSID_MASK) >> LOGSYS_LEVEL_END)
  136. #define LOGSYS_DECODE_RECID(rec_ident) \
  137. (((rec_ident) & LOGSYS_RECID_RECID_MASK) >> LOGSYS_SUBSYSID_END)
  138. #define LOGSYS_MAX_PERROR_MSG_LEN 128
  139. #ifdef COROSYNC_LINUX
  140. /* The GNU version of strerror_r returns a (char*) that *must* be used */
  141. #define LOGSYS_STRERROR_R(out_ptr, err_num, buffer, sizeof_buffer) \
  142. out_ptr = strerror_r(err_num, buffer, sizeof_buffer);
  143. #else
  144. /* The XSI-compliant strerror_r() return 0 or -1 (in case the buffer is full) */
  145. #define LOGSYS_STRERROR_R(out_ptr, err_num, buffer, sizeof_buffer) do { \
  146. if ( strerror_r(err_num, buffer, sizeof_buffer) == 0 ) { \
  147. out_ptr = buffer; \
  148. } else { \
  149. out_ptr = ""; \
  150. } \
  151. } while(0)
  152. #endif
  153. #define LOGSYS_PERROR(err_num, level, fmt, args...) do { \
  154. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  155. const char *_error_ptr; \
  156. LOGSYS_STRERROR_R(_error_ptr, err_num, _error_str, sizeof(_error_str)); \
  157. log_printf(level, fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
  158. } while(0)
  159. #ifndef LOGSYS_UTILS_ONLY
  160. extern int _logsys_system_setup(
  161. const char *mainsystem,
  162. unsigned int mode,
  163. unsigned int debug,
  164. const char *logfile,
  165. int logfile_priority,
  166. int syslog_facility,
  167. int syslog_priority);
  168. extern int _logsys_config_subsys_get (
  169. const char *subsys);
  170. extern int _logsys_subsys_create (const char *subsys);
  171. extern int _logsys_rec_init (unsigned int size);
  172. extern void _logsys_log_vprintf (
  173. unsigned int rec_ident,
  174. const char *function_name,
  175. const char *file_name,
  176. int file_line,
  177. const char *format,
  178. va_list ap) __attribute__((format(printf, 5, 0)));
  179. extern void _logsys_log_printf (
  180. unsigned int rec_ident,
  181. const char *function_name,
  182. const char *file_name,
  183. int file_line,
  184. const char *format,
  185. ...) __attribute__((format(printf, 5, 6)));
  186. extern void _logsys_log_rec (
  187. unsigned int rec_ident,
  188. const char *function_name,
  189. const char *file_name,
  190. int file_line,
  191. ...);
  192. extern int _logsys_wthread_create (void);
  193. static int logsys_subsys_id __attribute__((unused)) = LOGSYS_MAX_SUBSYS_COUNT;
  194. /*
  195. * External API - init
  196. * See below:
  197. *
  198. * LOGSYS_DECLARE_SYSTEM
  199. * LOGSYS_DECLARE_SUBSYS
  200. *
  201. */
  202. extern void logsys_fork_completed (void);
  203. extern void logsys_atexit (void);
  204. /*
  205. * External API - misc
  206. */
  207. extern void logsys_flush (void);
  208. extern int logsys_log_rec_store (const char *filename);
  209. /*
  210. * External API - configuration
  211. */
  212. /*
  213. * configuration bits that can only be done for the whole system
  214. */
  215. extern int logsys_format_set (
  216. const char *format);
  217. extern char *logsys_format_get (void);
  218. /*
  219. * per system/subsystem settings.
  220. *
  221. * NOTE: once a subsystem is created and configured, changing
  222. * the default does NOT affect the subsystems.
  223. *
  224. * Pass a NULL subsystem to change them all
  225. */
  226. extern int logsys_config_syslog_facility_set (
  227. const char *subsys,
  228. unsigned int facility);
  229. extern int logsys_config_syslog_priority_set (
  230. const char *subsys,
  231. unsigned int priority);
  232. extern int logsys_config_mode_set (
  233. const char *subsys,
  234. unsigned int mode);
  235. extern unsigned int logsys_config_mode_get (
  236. const char *subsys);
  237. /*
  238. * to close a logfile, just invoke this function with a NULL
  239. * file or if you want to change logfile, the old one will
  240. * be closed for you.
  241. */
  242. extern int logsys_config_file_set (
  243. const char *subsys,
  244. const char **error_string,
  245. const char *file);
  246. extern int logsys_config_logfile_priority_set (
  247. const char *subsys,
  248. unsigned int priority);
  249. /*
  250. * enabling debug, disable message priority filtering.
  251. * everything is sent everywhere. priority values
  252. * for file and syslog are not overwritten.
  253. */
  254. extern int logsys_config_debug_set (
  255. const char *subsys,
  256. unsigned int value);
  257. /*
  258. * External API - helpers
  259. *
  260. * convert facility/priority to/from name/values
  261. */
  262. extern int logsys_facility_id_get (
  263. const char *name);
  264. extern const char *logsys_facility_name_get (
  265. unsigned int facility);
  266. extern int logsys_priority_id_get (
  267. const char *name);
  268. extern const char *logsys_priority_name_get (
  269. unsigned int priority);
  270. extern int logsys_thread_priority_set (
  271. int policy,
  272. const struct sched_param *param,
  273. unsigned int after_log_ops_yield);
  274. /*
  275. * External definitions
  276. */
  277. extern void *logsys_rec_end;
  278. #define LOGSYS_REC_END (&logsys_rec_end)
  279. #define LOGSYS_DECLARE_SYSTEM(name,mode,debug,file,file_priority, \
  280. syslog_facility,syslog_priority,format,fltsize) \
  281. __attribute__ ((constructor)) \
  282. static void logsys_system_init (void) \
  283. { \
  284. if (_logsys_system_setup (name,mode,debug,file,file_priority, \
  285. syslog_facility,syslog_priority) < 0) { \
  286. fprintf (stderr, \
  287. "Unable to setup logging system: %s.\n", name); \
  288. exit (-1); \
  289. } \
  290. \
  291. if (logsys_format_set (format) == -1) { \
  292. fprintf (stderr, \
  293. "Unable to setup logging format.\n"); \
  294. exit (-1); \
  295. } \
  296. \
  297. if (_logsys_rec_init (fltsize) < 0) { \
  298. fprintf (stderr, \
  299. "Unable to initialize log flight recorder.\n"); \
  300. exit (-1); \
  301. } \
  302. \
  303. if (_logsys_wthread_create() < 0) { \
  304. fprintf (stderr, \
  305. "Unable to initialize logging thread.\n"); \
  306. exit (-1); \
  307. } \
  308. }
  309. #define LOGSYS_DECLARE_SUBSYS(subsys) \
  310. __attribute__ ((constructor)) \
  311. static void logsys_subsys_init (void) \
  312. { \
  313. logsys_subsys_id = \
  314. _logsys_subsys_create ((subsys)); \
  315. if (logsys_subsys_id == -1) { \
  316. fprintf (stderr, \
  317. "Unable to create logging subsystem: %s.\n", subsys); \
  318. exit (-1); \
  319. } \
  320. }
  321. #define log_rec(rec_ident, args...) \
  322. do { \
  323. _logsys_log_rec (rec_ident, __FUNCTION__, \
  324. __FILE__, __LINE__, ##args, \
  325. LOGSYS_REC_END); \
  326. } while(0)
  327. #define log_printf(level, format, args...) \
  328. do { \
  329. _logsys_log_printf ( \
  330. LOGSYS_ENCODE_RECID(level, \
  331. logsys_subsys_id, \
  332. LOGSYS_RECID_LOG), \
  333. __FUNCTION__, __FILE__, __LINE__, \
  334. format, ##args); \
  335. } while(0)
  336. #define ENTER() do { \
  337. _logsys_log_rec ( \
  338. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_DEBUG, \
  339. logsys_subsys_id, \
  340. LOGSYS_RECID_ENTER), \
  341. __FUNCTION__, __FILE__, __LINE__, LOGSYS_REC_END); \
  342. } while(0)
  343. #define LEAVE() do { \
  344. _logsys_log_rec ( \
  345. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_DEBUG, \
  346. logsys_subsys_id, \
  347. LOGSYS_RECID_LEAVE), \
  348. __FUNCTION__, __FILE__, __LINE__, LOGSYS_REC_END); \
  349. } while(0)
  350. #define TRACE(recid, format, args...) do { \
  351. _logsys_log_printf ( \
  352. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_DEBUG, \
  353. logsys_subsys_id, \
  354. recid), \
  355. __FUNCTION__, __FILE__, __LINE__, \
  356. format, ##args); \
  357. } while(0)
  358. #define TRACE1(format, args...) do { \
  359. TRACE(LOGSYS_RECID_TRACE1, format, ##args); \
  360. } while(0)
  361. #define TRACE2(format, args...) do { \
  362. TRACE(LOGSYS_RECID_TRACE2, format, ##args); \
  363. } while(0)
  364. #define TRACE3(format, args...) do { \
  365. TRACE(LOGSYS_RECID_TRACE3, format, ##args); \
  366. } while(0)
  367. #define TRACE4(format, args...) do { \
  368. TRACE(LOGSYS_RECID_TRACE4, format, ##args); \
  369. } while(0)
  370. #define TRACE5(format, args...) do { \
  371. TRACE(LOGSYS_RECID_TRACE5, format, ##args); \
  372. } while(0)
  373. #define TRACE6(format, args...) do { \
  374. TRACE(LOGSYS_RECID_TRACE6, format, ##args); \
  375. } while(0)
  376. #define TRACE7(format, args...) do { \
  377. TRACE(LOGSYS_RECID_TRACE7, format, ##args); \
  378. } while(0)
  379. #define TRACE8(format, args...) do { \
  380. TRACE(LOGSYS_RECID_TRACE8, format, ##args); \
  381. } while(0)
  382. #endif /* LOGSYS_UTILS_ONLY */
  383. #ifdef __cplusplus
  384. }
  385. #endif
  386. #endif /* LOGSYS_H_DEFINED */