Use Java writer (#579)

This commit is contained in:
vvb2060 2021-05-15 00:36:47 +08:00 committed by GitHub
parent 438d8a2b6e
commit d9f95d1280
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 92 deletions

View File

@ -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);

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2020 EdXposed Contributors
* Copyright (C) 2021 LSPosed Contributors
*/
#include "logger.h"
#include "native_util.h"
#include "jni_helper.h"
#include <fcntl.h>
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);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2020 EdXposed Contributors
* Copyright (C) 2021 LSPosed Contributors
*/
#pragma once
#include <jni.h>
namespace lspd {
void RegisterLogger(JNIEnv*);
}

View File

@ -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);
}
}
}

View File

@ -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) {