logsys.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #ifndef LOGSYS_UTILS_ONLY
  139. extern int _logsys_system_setup(
  140. const char *mainsystem,
  141. unsigned int mode,
  142. unsigned int debug,
  143. const char *logfile,
  144. int logfile_priority,
  145. int syslog_facility,
  146. int syslog_priority);
  147. extern int _logsys_config_subsys_get (
  148. const char *subsys);
  149. extern unsigned int _logsys_subsys_create (const char *subsys);
  150. extern int _logsys_rec_init (unsigned int size);
  151. extern void _logsys_log_vprintf (
  152. int subsysid,
  153. const char *function_name,
  154. const char *file_name,
  155. int file_line,
  156. unsigned int level,
  157. unsigned int rec_ident,
  158. const char *format,
  159. va_list ap) __attribute__((format(printf, 7, 0)));
  160. extern void _logsys_log_printf (
  161. int subsysid,
  162. const char *function_name,
  163. const char *file_name,
  164. int file_line,
  165. unsigned int level,
  166. unsigned int rec_ident,
  167. const char *format,
  168. ...) __attribute__((format(printf, 7, 8)));
  169. extern void _logsys_log_rec (
  170. int subsysid,
  171. const char *function_name,
  172. const char *file_name,
  173. int file_line,
  174. unsigned int level,
  175. unsigned int rec_ident,
  176. ...);
  177. extern int _logsys_wthread_create (void);
  178. static unsigned int logsys_subsys_id __attribute__((unused)) = -1;
  179. /*
  180. * External API - init
  181. * See below:
  182. *
  183. * LOGSYS_DECLARE_SYSTEM
  184. * LOGSYS_DECLARE_SUBSYS
  185. *
  186. */
  187. extern void logsys_fork_completed (void);
  188. extern void logsys_atexit (void);
  189. /*
  190. * External API - misc
  191. */
  192. extern void logsys_flush (void);
  193. extern int logsys_log_rec_store (const char *filename);
  194. /*
  195. * External API - configuration
  196. */
  197. /*
  198. * configuration bits that can only be done for the whole system
  199. */
  200. extern int logsys_format_set (
  201. const char *format);
  202. extern char *logsys_format_get (void);
  203. /*
  204. * per system/subsystem settings.
  205. *
  206. * NOTE: once a subsystem is created and configured, changing
  207. * the default does NOT affect the subsystems.
  208. *
  209. * Pass a NULL subsystem to change them all
  210. */
  211. extern unsigned int logsys_config_syslog_facility_set (
  212. const char *subsys,
  213. unsigned int facility);
  214. extern unsigned int logsys_config_syslog_priority_set (
  215. const char *subsys,
  216. unsigned int priority);
  217. extern unsigned int logsys_config_mode_set (
  218. const char *subsys,
  219. unsigned int mode);
  220. extern unsigned int logsys_config_mode_get (
  221. const char *subsys);
  222. /*
  223. * to close a logfile, just invoke this function with a NULL
  224. * file or if you want to change logfile, the old one will
  225. * be closed for you.
  226. */
  227. extern int logsys_config_file_set (
  228. const char *subsys,
  229. const char **error_string,
  230. const char *file);
  231. extern unsigned int logsys_config_logfile_priority_set (
  232. const char *subsys,
  233. unsigned int priority);
  234. /*
  235. * enabling debug, disable message priority filtering.
  236. * everything is sent everywhere. priority values
  237. * for file and syslog are not overwritten.
  238. */
  239. extern unsigned int logsys_config_debug_set (
  240. const char *subsys,
  241. unsigned int value);
  242. /*
  243. * External API - helpers
  244. *
  245. * convert facility/priority to/from name/values
  246. */
  247. extern int logsys_facility_id_get (
  248. const char *name);
  249. extern const char *logsys_facility_name_get (
  250. unsigned int facility);
  251. extern int logsys_priority_id_get (
  252. const char *name);
  253. extern const char *logsys_priority_name_get (
  254. unsigned int priority);
  255. extern int logsys_thread_priority_set (
  256. int policy,
  257. const struct sched_param *param,
  258. unsigned int after_log_ops_yield);
  259. /*
  260. * External definitions
  261. */
  262. extern void *logsys_rec_end;
  263. #define LOGSYS_REC_END (&logsys_rec_end)
  264. #define LOGSYS_DECLARE_SYSTEM(name,mode,debug,file,file_priority, \
  265. syslog_facility,syslog_priority,format,rec_size) \
  266. __attribute__ ((constructor)) \
  267. static void logsys_system_init (void) \
  268. { \
  269. if (_logsys_system_setup (name,mode,debug,file,file_priority, \
  270. syslog_facility,syslog_priority) < 0) { \
  271. fprintf (stderr, \
  272. "Unable to setup logging system: %s.\n", name); \
  273. exit (-1); \
  274. } \
  275. \
  276. if (logsys_format_set (format) < 0) { \
  277. fprintf (stderr, \
  278. "Unable to setup logging format.\n"); \
  279. exit (-1); \
  280. } \
  281. \
  282. if (_logsys_rec_init (rec_size) < 0) { \
  283. fprintf (stderr, \
  284. "Unable to initialize log flight recorder.\n"); \
  285. exit (-1); \
  286. } \
  287. \
  288. if (_logsys_wthread_create() < 0) { \
  289. fprintf (stderr, \
  290. "Unable to initialize logging thread.\n"); \
  291. exit (-1); \
  292. } \
  293. }
  294. #define LOGSYS_DECLARE_SUBSYS(subsys) \
  295. __attribute__ ((constructor)) \
  296. static void logsys_subsys_init (void) \
  297. { \
  298. logsys_subsys_id = \
  299. _logsys_subsys_create ((subsys)); \
  300. if (logsys_subsys_id == -1) { \
  301. fprintf (stderr, \
  302. "Unable to create logging subsystem: %s.\n", subsys); \
  303. exit (-1); \
  304. } \
  305. }
  306. #define log_rec(rec_ident, args...) \
  307. do { \
  308. _logsys_log_rec (logsys_subsys_id, __FUNCTION__, \
  309. __FILE__, __LINE__, rec_ident, 0, ##args, \
  310. LOGSYS_REC_END); \
  311. } while(0)
  312. #define log_printf(level, format, args...) \
  313. do { \
  314. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  315. __FILE__, __LINE__, level, LOGSYS_RECID_LOG, \
  316. format, ##args); \
  317. } while(0)
  318. #define ENTER() do { \
  319. _logsys_log_rec (logsys_subsys_id, __FUNCTION__, \
  320. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  321. LOGSYS_RECID_ENTER, LOGSYS_REC_END); \
  322. } while(0)
  323. #define LEAVE() do { \
  324. _logsys_log_rec (logsys_subsys_id, __FUNCTION__, \
  325. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  326. LOGSYS_RECID_LEAVE, LOGSYS_REC_END); \
  327. } while(0)
  328. #define TRACE1(format, args...) do { \
  329. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  330. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  331. LOGSYS_RECID_TRACE1, format, ##args); \
  332. } while(0)
  333. #define TRACE2(format, args...) do { \
  334. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  335. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  336. LOGSYS_RECID_TRACE2, format, ##args); \
  337. } while(0)
  338. #define TRACE3(format, args...) do { \
  339. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  340. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  341. LOGSYS_RECID_TRACE3, format, ##args); \
  342. } while(0)
  343. #define TRACE4(format, args...) do { \
  344. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  345. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  346. LOGSYS_RECID_TRACE4, format, ##args); \
  347. } while(0)
  348. #define TRACE5(format, args...) do { \
  349. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  350. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  351. LOGSYS_RECID_TRACE5, format, ##args); \
  352. } while(0)
  353. #define TRACE6(format, args...) do { \
  354. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  355. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  356. LOGSYS_RECID_TRACE6, format, ##args); \
  357. } while(0)
  358. #define TRACE7(format, args...) do { \
  359. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  360. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  361. LOGSYS_RECID_TRACE7, format, ##args); \
  362. } while(0)
  363. #define TRACE8(format, args...) do { \
  364. _logsys_log_printf (logsys_subsys_id, __FUNCTION__, \
  365. __FILE__, __LINE__, LOGSYS_LEVEL_DEBUG, \
  366. LOGSYS_RECID_TRACE8, format, ##args); \
  367. } while(0)
  368. #endif /* LOGSYS_UTILS_ONLY */
  369. #ifdef __cplusplus
  370. }
  371. #endif
  372. #endif /* LOGSYS_H_DEFINED */