DDebug.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #include "DDebug.h"
  23. #include "SipStack.h"
  24. #include "Common.h"
  25. #if defined(__ANDROID__) || defined(ANDROID) /* callbacks will fail with jni */
  26. # include <android/log.h>
  27. # define ANDROID_DEBUG_TAG "tinyWRAP"
  28. #endif
  29. /* Very Important ==> never call functions which could raise debug callbacks into callback functions
  30. * Callbacks should not be used with Android (JNI).
  31. */
  32. enum cb_type {
  33. cb_info,
  34. cb_warn,
  35. cb_error,
  36. cb_fatal
  37. };
  38. int debug_xxx_cb(const void* arg, const char* fmt, enum cb_type type, va_list *app)
  39. {
  40. int ret = -1;
  41. if(!arg) {
  42. return -1;
  43. }
  44. const SipStack* stack = dyn_cast<const SipStack*>((const SipStack*)arg);
  45. if(stack && stack->getDebugCallback()) {
  46. char* message = tsk_null;
  47. tsk_sprintf_2(&message, fmt, app);
  48. switch(type) {
  49. case cb_info:
  50. ret=
  51. #if defined(__ANDROID__) || defined(ANDROID)
  52. __android_log_write(ANDROID_LOG_INFO, ANDROID_DEBUG_TAG, message);
  53. #else
  54. stack->getDebugCallback()-> OnDebugInfo(message);
  55. #endif
  56. break;
  57. case cb_warn:
  58. ret=
  59. #if defined(__ANDROID__) || defined(ANDROID)
  60. __android_log_write(ANDROID_LOG_WARN, ANDROID_DEBUG_TAG, message);
  61. #else
  62. stack->getDebugCallback()-> OnDebugWarn(message);
  63. #endif
  64. break;
  65. case cb_error:
  66. ret=
  67. #if defined(__ANDROID__) || defined(ANDROID)
  68. __android_log_write(ANDROID_LOG_ERROR, ANDROID_DEBUG_TAG, message);
  69. #else
  70. stack->getDebugCallback()-> OnDebugError(message);
  71. #endif
  72. break;
  73. case cb_fatal:
  74. ret=
  75. #if defined(__ANDROID__) || defined(ANDROID)
  76. __android_log_write(ANDROID_LOG_FATAL, ANDROID_DEBUG_TAG, message);
  77. #else
  78. stack->getDebugCallback()-> OnDebugFatal(message);
  79. #endif
  80. break;
  81. }
  82. TSK_FREE(message);
  83. }
  84. return ret;
  85. }
  86. int DDebugCallback::debug_info_cb(const void* arg, const char* fmt, ...)
  87. {
  88. va_list ap;
  89. int ret;
  90. va_start(ap, fmt);
  91. ret = debug_xxx_cb(arg, fmt, cb_info, &ap);
  92. va_end(ap);
  93. return ret;
  94. }
  95. int DDebugCallback::debug_warn_cb(const void* arg, const char* fmt, ...)
  96. {
  97. va_list ap;
  98. int ret;
  99. va_start(ap, fmt);
  100. ret = debug_xxx_cb(arg, fmt, cb_warn, &ap);
  101. va_end(ap);
  102. return ret;
  103. }
  104. int DDebugCallback::debug_error_cb(const void* arg, const char* fmt, ...)
  105. {
  106. va_list ap;
  107. int ret;
  108. va_start(ap, fmt);
  109. ret = debug_xxx_cb(arg, fmt, cb_error, &ap);
  110. va_end(ap);
  111. return ret;
  112. }
  113. int DDebugCallback::debug_fatal_cb(const void* arg, const char* fmt, ...)
  114. {
  115. va_list ap;
  116. int ret;
  117. va_start(ap, fmt);
  118. ret = debug_xxx_cb(arg, fmt, cb_fatal, &ap);
  119. va_end(ap);
  120. return ret;
  121. }