Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Use Java interface to print log. (#2529)
Browse files Browse the repository at this point in the history
  • Loading branch information
YorkShen authored and Darin726 committed Jun 11, 2019
1 parent d857f37 commit 4686a4b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
26 changes: 19 additions & 7 deletions android/sdk/src/main/java/com/taobao/weex/utils/WXLogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,44 @@ private static void log(String tag, String msg, LogLevel level){
}
}

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){
Expand Down
61 changes: 61 additions & 0 deletions weex_core/Source/android/wrap/log_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

#include <sstream>
#include "android/wrap/log_utils.h"

#include "android/base/string/string_utils.h"
Expand All @@ -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()));
Expand All @@ -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
5 changes: 4 additions & 1 deletion weex_core/Source/android/wrap/log_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions weex_core/Source/android/wrap/wx_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 4686a4b

Please sign in to comment.