res_pjsip_log_forwarder.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Bridge PJSIP logging to Asterisk logging.
  21. * \author David M. Lee, II <dlee@digium.com>
  22. *
  23. * PJSIP logging doesn't exactly match Asterisk logging, but mapping the two is
  24. * not too bad. PJSIP log levels are identified by a single int. Limits are
  25. * not specified by PJSIP, but their implementation used 1 through 6.
  26. *
  27. * The mapping is as follows:
  28. * - 0: LOG_ERROR
  29. * - 1: LOG_ERROR
  30. * - 2: LOG_WARNING
  31. * - 3 and above: equivalent to ast_debug(level, ...) for res_pjsip.so
  32. */
  33. /*** MODULEINFO
  34. <depend>pjproject</depend>
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include <pjsip.h>
  40. #include <pj/log.h>
  41. #include "asterisk/logger.h"
  42. #include "asterisk/module.h"
  43. static pj_log_func *log_cb_orig;
  44. static unsigned decor_orig;
  45. static void log_cb(int level, const char *data, int len)
  46. {
  47. int ast_level;
  48. /* PJSIP doesn't provide much in the way of source info */
  49. const char * log_source = "pjsip";
  50. int log_line = 0;
  51. const char *log_func = "<?>";
  52. int mod_level;
  53. /* Lower number indicates higher importance */
  54. switch (level) {
  55. case 0: /* level zero indicates fatal error, according to docs */
  56. case 1: /* 1 seems to be used for errors */
  57. ast_level = __LOG_ERROR;
  58. break;
  59. case 2: /* 2 seems to be used for warnings and errors */
  60. ast_level = __LOG_WARNING;
  61. break;
  62. default:
  63. ast_level = __LOG_DEBUG;
  64. /* For levels 3 and up, obey the debug level for res_pjsip */
  65. mod_level = ast_opt_dbg_module ?
  66. ast_debug_get_by_module("res_pjsip") : 0;
  67. if (option_debug < level && mod_level < level) {
  68. return;
  69. }
  70. break;
  71. }
  72. /* PJSIP uses indention to indicate function call depth. We'll prepend
  73. * log statements with a tab so they'll have a better shot at lining
  74. * up */
  75. ast_log(ast_level, log_source, log_line, log_func, "\t%s\n", data);
  76. }
  77. static int load_module(void)
  78. {
  79. pj_init();
  80. decor_orig = pj_log_get_decor();
  81. log_cb_orig = pj_log_get_log_func();
  82. ast_debug(3, "Forwarding PJSIP logger to Asterisk logger\n");
  83. /* SENDER prepends the source to the log message. This could be a
  84. * filename, object reference, or simply a string
  85. *
  86. * INDENT is assumed to be on by most log statements in PJSIP itself.
  87. */
  88. pj_log_set_decor(PJ_LOG_HAS_SENDER | PJ_LOG_HAS_INDENT);
  89. pj_log_set_log_func(log_cb);
  90. return AST_MODULE_LOAD_SUCCESS;
  91. }
  92. static int unload_module(void)
  93. {
  94. pj_log_set_log_func(log_cb_orig);
  95. pj_log_set_decor(decor_orig);
  96. pj_shutdown();
  97. return 0;
  98. }
  99. /* While we don't really export global symbols, we want to load before other
  100. * modules that do */
  101. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP Log Forwarder",
  102. .support_level = AST_MODULE_SUPPORT_CORE,
  103. .load = load_module,
  104. .unload = unload_module,
  105. .load_pri = AST_MODPRI_CHANNEL_DEPEND - 6,
  106. );