log.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_LOG_H__
  20. #define __PJ_LOG_H__
  21. /**
  22. * @file log.h
  23. * @brief Logging Utility.
  24. */
  25. #include <pj/types.h>
  26. #include <stdarg.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup PJ_MISC Miscelaneous
  30. */
  31. /**
  32. * @defgroup PJ_LOG Logging Facility
  33. * @ingroup PJ_MISC
  34. * @{
  35. *
  36. * The PJLIB logging facility is a configurable, flexible, and convenient
  37. * way to write logging or trace information.
  38. *
  39. * To write to the log, one uses construct like below:
  40. *
  41. * <pre>
  42. * ...
  43. * PJ_LOG(3, ("main.c", "Starting hello..."));
  44. * ...
  45. * PJ_LOG(3, ("main.c", "Hello world from process %d", pj_getpid()));
  46. * ...
  47. * </pre>
  48. *
  49. * In the above example, the number @b 3 controls the verbosity level of
  50. * the information (which means "information", by convention). The string
  51. * "main.c" specifies the source or sender of the message.
  52. *
  53. *
  54. * \section pj_log_quick_sample_sec Examples
  55. *
  56. * For examples, see:
  57. * - Simple Log sample: \src{pjlib/src/pjlib-samples/log.c}
  58. *
  59. */
  60. /**
  61. * Log decoration flag, to be specified with #pj_log_set_decor().
  62. */
  63. enum pj_log_decoration
  64. {
  65. PJ_LOG_HAS_DAY_NAME = 1, /**< Include day name [default: no] */
  66. PJ_LOG_HAS_YEAR = 2, /**< Include year digit [no] */
  67. PJ_LOG_HAS_MONTH = 4, /**< Include month [no] */
  68. PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [no] */
  69. PJ_LOG_HAS_TIME = 16, /**< Include time [yes] */
  70. PJ_LOG_HAS_MICRO_SEC = 32, /**< Include microseconds [yes] */
  71. PJ_LOG_HAS_SENDER = 64, /**< Include sender in the log [yes] */
  72. PJ_LOG_HAS_NEWLINE = 128, /**< Terminate each call with newline [yes] */
  73. PJ_LOG_HAS_CR = 256, /**< Include carriage return [no] */
  74. PJ_LOG_HAS_SPACE = 512, /**< Include two spaces before log [yes] */
  75. PJ_LOG_HAS_COLOR = 1024, /**< Colorize logs [yes on win32] */
  76. PJ_LOG_HAS_LEVEL_TEXT = 2048, /**< Include level text string [no] */
  77. PJ_LOG_HAS_THREAD_ID = 4096, /**< Include thread identification [no] */
  78. PJ_LOG_HAS_THREAD_SWC = 8192, /**< Add mark when thread has switched [yes]*/
  79. PJ_LOG_HAS_INDENT =16384 /**< Indentation. Say yes! [yes] */
  80. };
  81. /**
  82. * Write log message.
  83. * This is the main macro used to write text to the logging backend.
  84. *
  85. * @param level The logging verbosity level. Lower number indicates higher
  86. * importance, with level zero indicates fatal error. Only
  87. * numeral argument is permitted (e.g. not variable).
  88. * @param arg Enclosed 'printf' like arguments, with the first
  89. * argument is the sender, the second argument is format
  90. * string and the following arguments are variable number of
  91. * arguments suitable for the format string.
  92. *
  93. * Sample:
  94. * \verbatim
  95. PJ_LOG(2, (__FILE__, "current value is %d", value));
  96. \endverbatim
  97. * @hideinitializer
  98. */
  99. #define PJ_LOG(level,arg) do { \
  100. if (level <= pj_log_get_level()) { \
  101. pj_log_wrapper_##level(arg); \
  102. } \
  103. } while (0)
  104. /**
  105. * Signature for function to be registered to the logging subsystem to
  106. * write the actual log message to some output device.
  107. *
  108. * @param level Log level.
  109. * @param data Log message, which will be NULL terminated.
  110. * @param len Message length.
  111. */
  112. typedef void pj_log_func(int level, const char *data, int len);
  113. /**
  114. * Default logging writer function used by front end logger function.
  115. * This function will print the log message to stdout only.
  116. * Application normally should NOT need to call this function, but
  117. * rather use the PJ_LOG macro.
  118. *
  119. * @param level Log level.
  120. * @param buffer Log message.
  121. * @param len Message length.
  122. */
  123. PJ_DECL(void) pj_log_write(int level, const char *buffer, int len);
  124. #if PJ_LOG_MAX_LEVEL >= 1
  125. /**
  126. * Write to log.
  127. *
  128. * @param sender Source of the message.
  129. * @param level Verbosity level.
  130. * @param format Format.
  131. * @param marker Marker.
  132. */
  133. PJ_DECL(void) pj_log(const char *sender, int level,
  134. const char *format, va_list marker);
  135. /**
  136. * Change log output function. The front-end logging functions will call
  137. * this function to write the actual message to the desired device.
  138. * By default, the front-end functions use pj_log_write() to write
  139. * the messages, unless it's changed by calling this function.
  140. *
  141. * @param func The function that will be called to write the log
  142. * messages to the desired device.
  143. */
  144. PJ_DECL(void) pj_log_set_log_func( pj_log_func *func );
  145. /**
  146. * Get the current log output function that is used to write log messages.
  147. *
  148. * @return Current log output function.
  149. */
  150. PJ_DECL(pj_log_func*) pj_log_get_log_func(void);
  151. /**
  152. * Set maximum log level. Application can call this function to set
  153. * the desired level of verbosity of the logging messages. The bigger the
  154. * value, the more verbose the logging messages will be printed. However,
  155. * the maximum level of verbosity can not exceed compile time value of
  156. * PJ_LOG_MAX_LEVEL.
  157. *
  158. * @param level The maximum level of verbosity of the logging
  159. * messages (6=very detailed..1=error only, 0=disabled)
  160. */
  161. PJ_DECL(void) pj_log_set_level(int level);
  162. /**
  163. * Get current maximum log verbositylevel.
  164. *
  165. * @return Current log maximum level.
  166. */
  167. #if 1
  168. PJ_DECL(int) pj_log_get_level(void);
  169. #else
  170. PJ_DECL_DATA(int) pj_log_max_level;
  171. #define pj_log_get_level() pj_log_max_level
  172. #endif
  173. /**
  174. * Set log decoration. The log decoration flag controls what are printed
  175. * to output device alongside the actual message. For example, application
  176. * can specify that date/time information should be displayed with each
  177. * log message.
  178. *
  179. * @param decor Bitmask combination of #pj_log_decoration to control
  180. * the layout of the log message.
  181. */
  182. PJ_DECL(void) pj_log_set_decor(unsigned decor);
  183. /**
  184. * Get current log decoration flag.
  185. *
  186. * @return Log decoration flag.
  187. */
  188. PJ_DECL(unsigned) pj_log_get_decor(void);
  189. /**
  190. * Add indentation to log message. Indentation will add PJ_LOG_INDENT_CHAR
  191. * before the message, and is useful to show the depth of function calls.
  192. *
  193. * @param indent The indentation to add or substract. Positive value
  194. * adds current indent, negative value subtracts current
  195. * indent.
  196. */
  197. PJ_DECL(void) pj_log_add_indent(int indent);
  198. /**
  199. * Set indentation to specific value.
  200. *
  201. * @param indent The indentation value.
  202. */
  203. PJ_DECL(void) pj_log_set_indent(int indent);
  204. /**
  205. * Get current indentation value.
  206. *
  207. * @return Current indentation value.
  208. */
  209. PJ_DECL(int) pj_log_get_indent(void);
  210. /**
  211. * Push indentation to the right by default value (PJ_LOG_INDENT_SIZE).
  212. */
  213. PJ_DECL(void) pj_log_push_indent(void);
  214. /**
  215. * Pop indentation (to the left) by default value (PJ_LOG_INDENT_SIZE).
  216. */
  217. PJ_DECL(void) pj_log_pop_indent(void);
  218. /**
  219. * Set color of log messages.
  220. *
  221. * @param level Log level which color will be changed.
  222. * @param color Desired color.
  223. */
  224. PJ_DECL(void) pj_log_set_color(int level, pj_color_t color);
  225. /**
  226. * Get color of log messages.
  227. *
  228. * @param level Log level which color will be returned.
  229. * @return Log color.
  230. */
  231. PJ_DECL(pj_color_t) pj_log_get_color(int level);
  232. /**
  233. * Internal function to be called by pj_init()
  234. */
  235. pj_status_t pj_log_init(void);
  236. #else /* #if PJ_LOG_MAX_LEVEL >= 1 */
  237. /**
  238. * Change log output function. The front-end logging functions will call
  239. * this function to write the actual message to the desired device.
  240. * By default, the front-end functions use pj_log_write() to write
  241. * the messages, unless it's changed by calling this function.
  242. *
  243. * @param func The function that will be called to write the log
  244. * messages to the desired device.
  245. */
  246. # define pj_log_set_log_func(func)
  247. /**
  248. * Write to log.
  249. *
  250. * @param sender Source of the message.
  251. * @param level Verbosity level.
  252. * @param format Format.
  253. * @param marker Marker.
  254. */
  255. # define pj_log(sender, level, format, marker)
  256. /**
  257. * Set maximum log level. Application can call this function to set
  258. * the desired level of verbosity of the logging messages. The bigger the
  259. * value, the more verbose the logging messages will be printed. However,
  260. * the maximum level of verbosity can not exceed compile time value of
  261. * PJ_LOG_MAX_LEVEL.
  262. *
  263. * @param level The maximum level of verbosity of the logging
  264. * messages (6=very detailed..1=error only, 0=disabled)
  265. */
  266. # define pj_log_set_level(level)
  267. /**
  268. * Set log decoration. The log decoration flag controls what are printed
  269. * to output device alongside the actual message. For example, application
  270. * can specify that date/time information should be displayed with each
  271. * log message.
  272. *
  273. * @param decor Bitmask combination of #pj_log_decoration to control
  274. * the layout of the log message.
  275. */
  276. # define pj_log_set_decor(decor)
  277. /**
  278. * Add indentation to log message. Indentation will add PJ_LOG_INDENT_CHAR
  279. * before the message, and is useful to show the depth of function calls.
  280. *
  281. * @param indent The indentation to add or substract. Positive value
  282. * adds current indent, negative value subtracts current
  283. * indent.
  284. */
  285. # define pj_log_add_indent(indent)
  286. /**
  287. * Set indentation to specific value.
  288. *
  289. * @param indent The indentation value.
  290. */
  291. # define pj_log_set_indent(indent)
  292. /**
  293. * Get current indentation value.
  294. *
  295. * @return Current indentation value.
  296. */
  297. # define pj_log_get_indent() 0
  298. /**
  299. * Push indentation to the right by default value (PJ_LOG_INDENT_SIZE).
  300. */
  301. # define pj_log_push_indent()
  302. /**
  303. * Pop indentation (to the left) by default value (PJ_LOG_INDENT_SIZE).
  304. */
  305. # define pj_log_pop_indent()
  306. /**
  307. * Set color of log messages.
  308. *
  309. * @param level Log level which color will be changed.
  310. * @param color Desired color.
  311. */
  312. # define pj_log_set_color(level, color)
  313. /**
  314. * Get current maximum log verbositylevel.
  315. *
  316. * @return Current log maximum level.
  317. */
  318. # define pj_log_get_level() 0
  319. /**
  320. * Get current log decoration flag.
  321. *
  322. * @return Log decoration flag.
  323. */
  324. # define pj_log_get_decor() 0
  325. /**
  326. * Get color of log messages.
  327. *
  328. * @param level Log level which color will be returned.
  329. * @return Log color.
  330. */
  331. # define pj_log_get_color(level) 0
  332. /**
  333. * Internal.
  334. */
  335. # define pj_log_init() PJ_SUCCESS
  336. #endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
  337. /**
  338. * @}
  339. */
  340. /* **************************************************************************/
  341. /*
  342. * Log functions implementation prototypes.
  343. * These functions are called by PJ_LOG macros according to verbosity
  344. * level specified when calling the macro. Applications should not normally
  345. * need to call these functions directly.
  346. */
  347. /**
  348. * @def pj_log_wrapper_1(arg)
  349. * Internal function to write log with verbosity 1. Will evaluate to
  350. * empty expression if PJ_LOG_MAX_LEVEL is below 1.
  351. * @param arg Log expression.
  352. */
  353. #if PJ_LOG_MAX_LEVEL >= 1
  354. #define pj_log_wrapper_1(arg) pj_log_1 arg
  355. /** Internal function. */
  356. PJ_DECL(void) pj_log_1(const char *src, const char *format, ...)
  357. PJ_PRINT_FUNC_DECOR(2);
  358. #else
  359. #define pj_log_wrapper_1(arg)
  360. #endif
  361. /**
  362. * @def pj_log_wrapper_2(arg)
  363. * Internal function to write log with verbosity 2. Will evaluate to
  364. * empty expression if PJ_LOG_MAX_LEVEL is below 2.
  365. * @param arg Log expression.
  366. */
  367. #if PJ_LOG_MAX_LEVEL >= 2
  368. #define pj_log_wrapper_2(arg) pj_log_2 arg
  369. /** Internal function. */
  370. PJ_DECL(void) pj_log_2(const char *src, const char *format, ...)
  371. PJ_PRINT_FUNC_DECOR(2);
  372. #else
  373. #define pj_log_wrapper_2(arg)
  374. #endif
  375. /**
  376. * @def pj_log_wrapper_3(arg)
  377. * Internal function to write log with verbosity 3. Will evaluate to
  378. * empty expression if PJ_LOG_MAX_LEVEL is below 3.
  379. * @param arg Log expression.
  380. */
  381. #if PJ_LOG_MAX_LEVEL >= 3
  382. #define pj_log_wrapper_3(arg) pj_log_3 arg
  383. /** Internal function. */
  384. PJ_DECL(void) pj_log_3(const char *src, const char *format, ...)
  385. PJ_PRINT_FUNC_DECOR(2);
  386. #else
  387. #define pj_log_wrapper_3(arg)
  388. #endif
  389. /**
  390. * @def pj_log_wrapper_4(arg)
  391. * Internal function to write log with verbosity 4. Will evaluate to
  392. * empty expression if PJ_LOG_MAX_LEVEL is below 4.
  393. * @param arg Log expression.
  394. */
  395. #if PJ_LOG_MAX_LEVEL >= 4
  396. #define pj_log_wrapper_4(arg) pj_log_4 arg
  397. /** Internal function. */
  398. PJ_DECL(void) pj_log_4(const char *src, const char *format, ...)
  399. PJ_PRINT_FUNC_DECOR(2);
  400. #else
  401. #define pj_log_wrapper_4(arg)
  402. #endif
  403. /**
  404. * @def pj_log_wrapper_5(arg)
  405. * Internal function to write log with verbosity 5. Will evaluate to
  406. * empty expression if PJ_LOG_MAX_LEVEL is below 5.
  407. * @param arg Log expression.
  408. */
  409. #if PJ_LOG_MAX_LEVEL >= 5
  410. #define pj_log_wrapper_5(arg) pj_log_5 arg
  411. /** Internal function. */
  412. PJ_DECL(void) pj_log_5(const char *src, const char *format, ...)
  413. PJ_PRINT_FUNC_DECOR(2);
  414. #else
  415. #define pj_log_wrapper_5(arg)
  416. #endif
  417. /**
  418. * @def pj_log_wrapper_6(arg)
  419. * Internal function to write log with verbosity 6. Will evaluate to
  420. * empty expression if PJ_LOG_MAX_LEVEL is below 6.
  421. * @param arg Log expression.
  422. */
  423. #if PJ_LOG_MAX_LEVEL >= 6
  424. #define pj_log_wrapper_6(arg) pj_log_6 arg
  425. /** Internal function. */
  426. PJ_DECL(void) pj_log_6(const char *src, const char *format, ...)
  427. PJ_PRINT_FUNC_DECOR(2);
  428. #else
  429. #define pj_log_wrapper_6(arg)
  430. #endif
  431. PJ_END_DECL
  432. #endif /* __PJ_LOG_H__ */