This is an automated email from the ASF dual-hosted git repository.

dongyayun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-weex.git


The following commit(s) were added to refs/heads/master by this push:
     new 4686a4b  Use Java interface to print log. (#2529)
4686a4b is described below

commit 4686a4b09b51eaf36b49c6e817c270c50afb040d
Author: YorkShen <[email protected]>
AuthorDate: Tue Jun 11 15:17:56 2019 +0800

    Use Java interface to print log. (#2529)
---
 .../java/com/taobao/weex/utils/WXLogUtils.java     | 26 ++++++---
 weex_core/Source/android/wrap/log_utils.cpp        | 61 ++++++++++++++++++++++
 weex_core/Source/android/wrap/log_utils.h          |  5 +-
 weex_core/Source/android/wrap/wx_bridge.cpp        |  3 ++
 4 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/android/sdk/src/main/java/com/taobao/weex/utils/WXLogUtils.java 
b/android/sdk/src/main/java/com/taobao/weex/utils/WXLogUtils.java
index 2a3fd62..9a7daca 100644
--- a/android/sdk/src/main/java/com/taobao/weex/utils/WXLogUtils.java
+++ b/android/sdk/src/main/java/com/taobao/weex/utils/WXLogUtils.java
@@ -93,32 +93,44 @@ public class WXLogUtils {
     }
   }
 
+  public static void v(String msg) {
+    v(WEEX_TAG,msg);
+  }
+
   public static void d(String msg) {
     d(WEEX_TAG,msg);
   }
 
+  public static void d(String tag, byte[] msg) {
+    d(tag, new String(msg));
+  }
+
   public static void i(String msg) {
     i(WEEX_TAG,msg);
   }
 
-  public static void info(String msg) {
-    i(WEEX_TAG,msg);
+  public static void i(String tag, byte[] msg) {
+    i(tag, new String(msg));
   }
 
-  public static void v(String msg) {
-    v(WEEX_TAG,msg);
+  public static void info(String msg) {
+    i(WEEX_TAG, msg);
   }
 
   public static void w(String msg) {
-    w(WEEX_TAG,msg);
+    w(WEEX_TAG, msg);
+  }
+
+  public static void w(String tag, byte[] msg) {
+    w(tag, new String(msg));
   }
 
   public static void e(String msg) {
     e(WEEX_TAG,msg);
   }
 
-  public static void d(String tag, byte[] msg) {
-    d(tag,new String(msg));
+  public static void e(String tag, byte[] msg) {
+    e(tag, new String(msg));
   }
 
   public static void wtf(String msg){
diff --git a/weex_core/Source/android/wrap/log_utils.cpp 
b/weex_core/Source/android/wrap/log_utils.cpp
index 52b2124..da6b7c0 100644
--- a/weex_core/Source/android/wrap/log_utils.cpp
+++ b/weex_core/Source/android/wrap/log_utils.cpp
@@ -17,6 +17,7 @@
  * under the License.
  */
 
+#include <sstream>
 #include "android/wrap/log_utils.h"
 
 #include "android/base/string/string_utils.h"
@@ -37,6 +38,36 @@ static void Java_WXLogUtils_d(JNIEnv* env, jstring tag, 
jbyteArray msg) {
   base::android::CheckException(env);
 }
 
+static intptr_t g_WXLogUtils_i = 0;
+static void Java_WXLogUtils_i(JNIEnv* env, jstring tag, jbyteArray msg) {
+  jmethodID method_id = base::android::GetMethod(
+      env, g_WXLogUtils_clazz, base::android::STATIC_METHOD, "i",
+      "(Ljava/lang/String;[B)V", &g_WXLogUtils_i);
+
+  env->CallStaticVoidMethod(g_WXLogUtils_clazz, method_id, tag, msg);
+  base::android::CheckException(env);
+}
+
+static intptr_t g_WXLogUtils_w = 0;
+static void Java_WXLogUtils_w(JNIEnv* env, jstring tag, jbyteArray msg) {
+  jmethodID method_id = base::android::GetMethod(
+      env, g_WXLogUtils_clazz, base::android::STATIC_METHOD, "w",
+      "(Ljava/lang/String;[B)V", &g_WXLogUtils_w);
+
+  env->CallStaticVoidMethod(g_WXLogUtils_clazz, method_id, tag, msg);
+  base::android::CheckException(env);
+}
+
+static intptr_t g_WXLogUtils_e = 0;
+static void Java_WXLogUtils_e(JNIEnv* env, jstring tag, jbyteArray msg) {
+  jmethodID method_id = base::android::GetMethod(
+      env, g_WXLogUtils_clazz, base::android::STATIC_METHOD, "e",
+      "(Ljava/lang/String;[B)V", &g_WXLogUtils_e);
+
+  env->CallStaticVoidMethod(g_WXLogUtils_clazz, method_id, tag, msg);
+  base::android::CheckException(env);
+}
+
 bool LogUtils::RegisterJNIUtils(JNIEnv* env) {
   g_WXLogUtils_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
       base::android::GetClass(env, kWXLogUtilsClassPath).Get()));
@@ -50,4 +81,34 @@ void LogUtils::NativeLog(JNIEnv* env, const char* str_array) 
{
       env, newJByteArray(env, str_array));
   Java_WXLogUtils_d(env, tag.Get(), msg.Get());
 }
+
+void LogUtils::log(LogLevel level, const char* tag,  const char* file, 
unsigned long line, const char* log){
+  JNIEnv *env = base::android::AttachCurrentThread();
+  if (env == nullptr) {
+    return;
+  }
+  else {
+    std::stringstream ss;
+    ss << file << ":" << line << "," << log;
+
+    auto tag_jstring = base::android::ScopedLocalJavaRef<jstring>(
+        env,  env->NewStringUTF(tag));
+    auto msg_jbyteArray = base::android::ScopedLocalJavaRef<jbyteArray>(
+        env, newJByteArray(env, ss.str().c_str()));
+    switch (level) {
+      case LogLevel::Debug:
+        Java_WXLogUtils_d(env, tag_jstring.Get(), msg_jbyteArray.Get());
+        break;
+      case LogLevel::Info:
+        Java_WXLogUtils_i(env, tag_jstring.Get(), msg_jbyteArray.Get());
+        break;
+      case LogLevel::Warn:
+        Java_WXLogUtils_w(env, tag_jstring.Get(), msg_jbyteArray.Get());
+        break;
+      case LogLevel::Error:
+        Java_WXLogUtils_e(env, tag_jstring.Get(), msg_jbyteArray.Get());
+        break;
+    }
+  }
+}
 }  // namespace WeexCore
diff --git a/weex_core/Source/android/wrap/log_utils.h 
b/weex_core/Source/android/wrap/log_utils.h
index e8d6652..5bdf164 100644
--- a/weex_core/Source/android/wrap/log_utils.h
+++ b/weex_core/Source/android/wrap/log_utils.h
@@ -21,12 +21,15 @@
 #define WEEX_PROJECT_LOG_UTILS_H
 
 #include <jni.h>
+#include "base/log_defines.h"
+#include "core/bridge/log_bridge.h"
 
 namespace WeexCore {
-class LogUtils {
+class LogUtils : public LogBridge {
  public:
   static bool RegisterJNIUtils(JNIEnv* env);
   static void NativeLog(JNIEnv* env, const char* str_array);
+  void log(LogLevel level, const char* tag,  const char* file, unsigned long 
line, const char* log) override;
 };
 }  // namespace WeexCore
 
diff --git a/weex_core/Source/android/wrap/wx_bridge.cpp 
b/weex_core/Source/android/wrap/wx_bridge.cpp
index eef0edf..8517bc0 100644
--- a/weex_core/Source/android/wrap/wx_bridge.cpp
+++ b/weex_core/Source/android/wrap/wx_bridge.cpp
@@ -20,6 +20,7 @@
 #include "android/wrap/wx_bridge.h"
 #include <fstream>
 
+#include "android/wrap/log_utils.h"
 #include "android/base/string/string_utils.h"
 #include "android/bridge/platform/android_bridge.h"
 #include "android/bridge/platform/android_bridge_in_multi_process.h"
@@ -349,6 +350,8 @@ static jint InitFramework(JNIEnv* env, jobject object, 
jstring script,
   auto result =
       bridge->core_side()->InitFramework(c_script.getChars(), params_vector);
   freeParams(params_vector);
+
+  WeexCoreManager::Instance()->set_log_bridge(new LogUtils());
   return result;
 }
 

Reply via email to