From d9f95d1280fea241a8fa92f29812af06808f5282 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sat, 15 May 2021 00:36:47 +0800 Subject: [PATCH] Use Java writer (#579) --- core/src/main/cpp/main/src/context.cpp | 2 - core/src/main/cpp/main/src/jni/logger.cpp | 46 ------------------- core/src/main/cpp/main/src/jni/logger.h | 27 ----------- .../lspd/nativebridge/ModuleLogger.java | 26 +++++++---- .../lsposed/lspd/service/ConfigManager.java | 15 +++--- 5 files changed, 24 insertions(+), 92 deletions(-) delete mode 100644 core/src/main/cpp/main/src/jni/logger.cpp delete mode 100644 core/src/main/cpp/main/src/jni/logger.h diff --git a/core/src/main/cpp/main/src/context.cpp b/core/src/main/cpp/main/src/context.cpp index 11f3ae0a..8e9c2b2e 100644 --- a/core/src/main/cpp/main/src/context.cpp +++ b/core/src/main/cpp/main/src/context.cpp @@ -28,7 +28,6 @@ #include "jni/pending_hooks.h" #include "context.h" #include "native_hook.h" -#include "jni/logger.h" #include "jni/native_api.h" #include "service.h" #include "symbol_cache.h" @@ -116,7 +115,6 @@ namespace lspd { entry_class_ = JNI_NewGlobalRef(env, entry_class); } - RegisterLogger(env); RegisterResourcesHook(env); RegisterArtClassLinker(env); RegisterYahfa(env); diff --git a/core/src/main/cpp/main/src/jni/logger.cpp b/core/src/main/cpp/main/src/jni/logger.cpp deleted file mode 100644 index 6db42ac4..00000000 --- a/core/src/main/cpp/main/src/jni/logger.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This file is part of LSPosed. - * - * LSPosed is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * LSPosed is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with LSPosed. If not, see . - * - * Copyright (C) 2020 EdXposed Contributors - * Copyright (C) 2021 LSPosed Contributors - */ - -#include "logger.h" -#include "native_util.h" -#include "jni_helper.h" -#include - -namespace lspd { - LSP_DEF_NATIVE_METHOD(void, ModuleLogger, nativeLog, int fd, jstring jstr) { - if (UNLIKELY(fd < 0)) { - LOGE("fd is -1"); - return; - } - JUTFString str(env, jstr); - int res = write(fd, str.get(), std::strlen(str.get())); - if (res < 0) { - LOGD("Logger fail: %s", strerror(errno)); - } - } - - static JNINativeMethod gMethods[] = { - LSP_NATIVE_METHOD(ModuleLogger, nativeLog, "(ILjava/lang/String;)V") - }; - - void RegisterLogger(JNIEnv *env) { - REGISTER_LSP_NATIVE_METHODS(ModuleLogger); - } -} diff --git a/core/src/main/cpp/main/src/jni/logger.h b/core/src/main/cpp/main/src/jni/logger.h deleted file mode 100644 index 88ab745b..00000000 --- a/core/src/main/cpp/main/src/jni/logger.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * This file is part of LSPosed. - * - * LSPosed is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * LSPosed is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with LSPosed. If not, see . - * - * Copyright (C) 2020 EdXposed Contributors - * Copyright (C) 2021 LSPosed Contributors - */ - -#pragma once - -#include - -namespace lspd { - void RegisterLogger(JNIEnv*); -} diff --git a/core/src/main/java/org/lsposed/lspd/nativebridge/ModuleLogger.java b/core/src/main/java/org/lsposed/lspd/nativebridge/ModuleLogger.java index 5151c855..bd26cc03 100644 --- a/core/src/main/java/org/lsposed/lspd/nativebridge/ModuleLogger.java +++ b/core/src/main/java/org/lsposed/lspd/nativebridge/ModuleLogger.java @@ -24,28 +24,29 @@ import android.app.ActivityThread; import android.os.ParcelFileDescriptor; import android.os.Process; +import org.lsposed.lspd.util.Utils; + +import java.io.FileDescriptor; +import java.io.FileWriter; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.TimeZone; -import org.lsposed.lspd.util.Utils; - public class ModuleLogger { static SimpleDateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss", Locale.getDefault()); - static int fd = -1; + static FileDescriptor fd = null; public static void initLogger(ParcelFileDescriptor fileDescriptor) { - if (fd == -1 && fileDescriptor != null) { - fd = fileDescriptor.detachFd(); + if (fd == null && fileDescriptor != null) { + fd = fileDescriptor.getFileDescriptor(); logDateFormat.setTimeZone(TimeZone.getDefault()); } } - private static native void nativeLog(int fd, String logStr); - public static void log(String str, boolean isThrowable) { - if (fd == -1) { + if (fd == null) { Utils.logE("Logger is not initialized"); return; } @@ -65,6 +66,13 @@ public class ModuleLogger { sb.append(": "); sb.append(str); sb.append('\n'); - nativeLog(fd, sb.toString()); + try { + var log = sb.toString(); + var writer = new FileWriter(fd); + writer.write(log, 0, log.length()); + writer.flush(); + } catch (IOException e) { + Utils.logE("Unable to write to module log file", e); + } } } diff --git a/core/src/main/java/org/lsposed/lspd/service/ConfigManager.java b/core/src/main/java/org/lsposed/lspd/service/ConfigManager.java index 09068972..03dcda4f 100644 --- a/core/src/main/java/org/lsposed/lspd/service/ConfigManager.java +++ b/core/src/main/java/org/lsposed/lspd/service/ConfigManager.java @@ -49,7 +49,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.StandardOpenOption; import java.util.ArrayList; @@ -92,7 +91,8 @@ public class ConfigManager { private String miscPath = null; private static final File logPath = new File(basePath, "log"); - private static final File modulesLogPath = new File(logPath, "modules.log"); + private static final File modulesLog = new File(logPath, "modules.log"); + private static final File oldModulesLog = new File(logPath, "modules.old.log"); private static final File verboseLogPath = new File(logPath, "all.log"); private final Handler cacheHandler; @@ -577,12 +577,11 @@ public class ConfigManager { public ParcelFileDescriptor getModulesLog(int mode) { try { - if (modulesLogPath.length() > 4000 * 1024) { - try (FileChannel outChan = new FileOutputStream(modulesLogPath, true).getChannel()) { - outChan.truncate(1000 * 1024); - } + if (modulesLog.length() > 16 * 1024 * 1024) { + //noinspection ResultOfMethodCallIgnored + modulesLog.renameTo(oldModulesLog); } - return ParcelFileDescriptor.open(modulesLogPath, mode | ParcelFileDescriptor.MODE_CREATE); + return ParcelFileDescriptor.open(modulesLog, mode | ParcelFileDescriptor.MODE_CREATE); } catch (IOException e) { Log.e(TAG, Log.getStackTraceString(e)); return null; @@ -600,7 +599,7 @@ public class ConfigManager { public boolean clearLogs(boolean verbose) { try { - OutputStream os = new FileOutputStream(verbose ? verboseLogPath : modulesLogPath); + OutputStream os = new FileOutputStream(verbose ? verboseLogPath : modulesLog); os.close(); return true; } catch (IOException e) {