Skip to content

Commit e64302c

Browse files
committed
优化:DumpObjects 日志输出到指定文件中方便查看
1 parent 9eaa29d commit e64302c

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

native/cpp/quickjs_context_jni.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,26 @@ Java_com_whl_quickjs_wrapper_QuickJSContext_dumpMemoryUsage(JNIEnv *env, jclass
237237
}
238238
extern "C"
239239
JNIEXPORT void JNICALL
240-
Java_com_whl_quickjs_wrapper_QuickJSContext_dumpObjects(JNIEnv *env, jobject thiz, jlong runtime) {
240+
Java_com_whl_quickjs_wrapper_QuickJSContext_dumpObjects(JNIEnv *env, jobject thiz, jlong runtime,
241+
jstring file_name) {
241242
auto *rt = reinterpret_cast<JSRuntime*>(runtime);
242-
JS_DumpObjects(rt);
243+
244+
if (file_name == nullptr) {
245+
JS_DumpObjects(rt);
246+
} else {
247+
const char *path = env->GetStringUTFChars(file_name, JNI_FALSE);
248+
// 这里重定向打印日志到指定文件,方便查看。
249+
// todo 打印完需要再恢复到控制台打印,参考:https://cloud.tencent.com/developer/article/1544633
250+
auto file = freopen(path, "w", stdout);
251+
env->ReleaseStringUTFChars(file_name, path);
252+
if (!file) {
253+
env->ThrowNew(env->FindClass("java/lang/NullPointerException"), "File cannot be null");
254+
return;
255+
}
256+
257+
JS_DumpObjects(rt);
258+
259+
fclose(file);
260+
}
261+
243262
}

wrapper-android/src/androidTest/java/com/whl/quickjs/wrapper/QuickJSTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,21 @@ public void dumpMemoryUsageTest() {
939939
context.destroy();
940940
}
941941

942+
@Test
943+
public void dumpObjectsTest() {
944+
QuickJSContext context = createContext();
945+
context.evaluate("var testDumpObject = {data: 'I am test data'};");
946+
Context androidContext = ApplicationProvider.getApplicationContext();
947+
File file = new File(androidContext.getCacheDir(), "dump_objects.txt");
948+
try {
949+
file.createNewFile();
950+
} catch (IOException e) {
951+
e.printStackTrace();
952+
}
953+
context.dumpObjects(file);
954+
context.destroy();
955+
}
956+
942957
@Test
943958
public void testGetNativeFuncName() {
944959
QuickJSContext context = createContext();

wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSContext.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,17 @@ public void dumpMemoryUsage() {
4040
dumpMemoryUsage(runtime, null);
4141
}
4242

43+
public void dumpObjects(File target) {
44+
if (target == null || !target.exists()) {
45+
return;
46+
}
47+
48+
dumpObjects(runtime, target.getAbsolutePath());
49+
}
50+
4351
// will use stdout to print.
4452
public void dumpObjects() {
45-
dumpObjects(runtime);
53+
dumpObjects(runtime, null);
4654
}
4755

4856
private final long runtime;
@@ -316,7 +324,7 @@ public void throwJSException(String error) {
316324
private native void runGC(long runtime);
317325
private native void setMemoryLimit(long runtime, int size);
318326
private native void dumpMemoryUsage(long runtime, String fileName);
319-
private native void dumpObjects(long runtime);
327+
private native void dumpObjects(long runtime, String fileName);
320328

321329
// context
322330
private native long createContext(long runtime);

0 commit comments

Comments
 (0)