JNILogger.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the JNI API for jAssimp */
  35. #if (defined ASSIMP_JNI_EXPORT)
  36. // include assimp
  37. #include "../../include/aiTypes.h"
  38. #include "../../include/aiMesh.h"
  39. #include "../../include/aiAnim.h"
  40. #include "../../include/aiScene.h"
  41. #include "../../include/aiAssert.h"
  42. #include "../../include/aiPostProcess.h"
  43. #include "../../include/assimp.hpp"
  44. #include "../../include/DefaultLogger.h"
  45. #include "JNIEnvironment.h"
  46. #include "JNILogger.h"
  47. using namespace Assimp;
  48. namespace Assimp {
  49. namespace JNIBridge {
  50. // ------------------------------------------------------------------------------------------------
  51. bool JNILogDispatcher::OnAttachToCurrentThread(JNIThreadData* pcData)
  52. {
  53. ai_assert(NULL != pcData);
  54. //this->AddRef(); - done at another position
  55. // there is much error handling code in this function.
  56. // However, it is not impossible that the jAssimp package
  57. // loaded by the JVM is incomplete ...
  58. JNIEnv* jvmenv = JNIEnvironment::Get()->GetThread()->m_pcEnv;
  59. // get a handle to the assimp.DefaultLogger class
  60. if (NULL == this->m_pcClass)
  61. {
  62. if( NULL == (this->m_pcClass = jvmenv->FindClass("assimp.DefaultLogger")))
  63. {
  64. return false;
  65. }
  66. }
  67. // get handles to the logging functions
  68. if (NULL == this->m_pcMethodError)
  69. {
  70. if( NULL == (this->m_pcMethodError = jvmenv->GetStaticMethodID(
  71. this->m_pcClass,"_NativeCallWriteError","(Ljava/lang/String;)V")))
  72. {
  73. return false;
  74. }
  75. }
  76. if (NULL == this->m_pcMethodWarn)
  77. {
  78. if( NULL == (this->m_pcMethodWarn = jvmenv->GetStaticMethodID(
  79. this->m_pcClass,"_NativeCallWriteWarn","(Ljava/lang/String;)V")))
  80. {
  81. return false;
  82. }
  83. }
  84. if (NULL == this->m_pcMethodInfo)
  85. {
  86. if( NULL == (this->m_pcMethodInfo = jvmenv->GetStaticMethodID(
  87. this->m_pcClass,"_NativeCallWriteInfo","(Ljava/lang/String;)V")))
  88. {
  89. return false;
  90. }
  91. }
  92. if (NULL == this->m_pcMethodDebug)
  93. {
  94. if( NULL == (this->m_pcMethodDebug = jvmenv->GetStaticMethodID(
  95. this->m_pcClass,"_NativeCallWriteDebug","(Ljava/lang/String;)V")))
  96. {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. // ------------------------------------------------------------------------------------------------
  103. bool JNILogDispatcher::OnDetachFromCurrentThread(JNIThreadData* pcData)
  104. {
  105. ai_assert(NULL != pcData);
  106. this->Release();
  107. return true;
  108. }
  109. // ------------------------------------------------------------------------------------------------
  110. void JNILogDispatcher::debug(const std::string &message)
  111. {
  112. JNIEnv* jvmenv = JNIEnvironment::Get()->GetThread()->m_pcEnv;
  113. jstring jstr = JNU_NewStringNative(jvmenv,message.c_str());
  114. jvmenv->CallStaticVoidMethod(this->m_pcClass,this->m_pcMethodDebug,jstr);
  115. jvmenv->DeleteLocalRef(jstr);
  116. }
  117. // ------------------------------------------------------------------------------------------------
  118. void JNILogDispatcher::info(const std::string &message)
  119. {
  120. JNIEnv* jvmenv = JNIEnvironment::Get()->GetThread()->m_pcEnv;
  121. jstring jstr = JNU_NewStringNative(jvmenv,message.c_str());
  122. jvmenv->CallStaticVoidMethod(this->m_pcClass,this->m_pcMethodInfo,jstr);
  123. jvmenv->DeleteLocalRef(jstr);
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. void JNILogDispatcher::warn(const std::string &message)
  127. {
  128. JNIEnv* jvmenv = JNIEnvironment::Get()->GetThread()->m_pcEnv;
  129. jstring jstr = JNU_NewStringNative(jvmenv,message.c_str());
  130. jvmenv->CallStaticVoidMethod(this->m_pcClass,this->m_pcMethodWarn,jstr);
  131. jvmenv->DeleteLocalRef(jstr);
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. void JNILogDispatcher::error(const std::string &message)
  135. {
  136. JNIEnv* jvmenv = JNIEnvironment::Get()->GetThread()->m_pcEnv;
  137. jstring jstr = JNU_NewStringNative(jvmenv,message.c_str());
  138. jvmenv->CallStaticVoidMethod(this->m_pcClass,this->m_pcMethodError,jstr);
  139. jvmenv->DeleteLocalRef(jstr);
  140. }
  141. };};
  142. #endif // jni