Use Java writer (#579)
This commit is contained in:
parent
438d8a2b6e
commit
d9f95d1280
|
|
@ -28,7 +28,6 @@
|
||||||
#include "jni/pending_hooks.h"
|
#include "jni/pending_hooks.h"
|
||||||
#include "context.h"
|
#include "context.h"
|
||||||
#include "native_hook.h"
|
#include "native_hook.h"
|
||||||
#include "jni/logger.h"
|
|
||||||
#include "jni/native_api.h"
|
#include "jni/native_api.h"
|
||||||
#include "service.h"
|
#include "service.h"
|
||||||
#include "symbol_cache.h"
|
#include "symbol_cache.h"
|
||||||
|
|
@ -116,7 +115,6 @@ namespace lspd {
|
||||||
entry_class_ = JNI_NewGlobalRef(env, entry_class);
|
entry_class_ = JNI_NewGlobalRef(env, entry_class);
|
||||||
}
|
}
|
||||||
|
|
||||||
RegisterLogger(env);
|
|
||||||
RegisterResourcesHook(env);
|
RegisterResourcesHook(env);
|
||||||
RegisterArtClassLinker(env);
|
RegisterArtClassLinker(env);
|
||||||
RegisterYahfa(env);
|
RegisterYahfa(env);
|
||||||
|
|
|
||||||
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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*);
|
|
||||||
}
|
|
||||||
|
|
@ -24,28 +24,29 @@ import android.app.ActivityThread;
|
||||||
import android.os.ParcelFileDescriptor;
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.os.Process;
|
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.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.lsposed.lspd.util.Utils;
|
|
||||||
|
|
||||||
public class ModuleLogger {
|
public class ModuleLogger {
|
||||||
static SimpleDateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss", Locale.getDefault());
|
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) {
|
public static void initLogger(ParcelFileDescriptor fileDescriptor) {
|
||||||
if (fd == -1 && fileDescriptor != null) {
|
if (fd == null && fileDescriptor != null) {
|
||||||
fd = fileDescriptor.detachFd();
|
fd = fileDescriptor.getFileDescriptor();
|
||||||
logDateFormat.setTimeZone(TimeZone.getDefault());
|
logDateFormat.setTimeZone(TimeZone.getDefault());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static native void nativeLog(int fd, String logStr);
|
|
||||||
|
|
||||||
public static void log(String str, boolean isThrowable) {
|
public static void log(String str, boolean isThrowable) {
|
||||||
if (fd == -1) {
|
if (fd == null) {
|
||||||
Utils.logE("Logger is not initialized");
|
Utils.logE("Logger is not initialized");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -65,6 +66,13 @@ public class ModuleLogger {
|
||||||
sb.append(": ");
|
sb.append(": ");
|
||||||
sb.append(str);
|
sb.append(str);
|
||||||
sb.append('\n');
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,6 @@ import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.channels.FileChannel;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -92,7 +91,8 @@ public class ConfigManager {
|
||||||
private String miscPath = null;
|
private String miscPath = null;
|
||||||
|
|
||||||
private static final File logPath = new File(basePath, "log");
|
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 static final File verboseLogPath = new File(logPath, "all.log");
|
||||||
|
|
||||||
private final Handler cacheHandler;
|
private final Handler cacheHandler;
|
||||||
|
|
@ -577,12 +577,11 @@ public class ConfigManager {
|
||||||
|
|
||||||
public ParcelFileDescriptor getModulesLog(int mode) {
|
public ParcelFileDescriptor getModulesLog(int mode) {
|
||||||
try {
|
try {
|
||||||
if (modulesLogPath.length() > 4000 * 1024) {
|
if (modulesLog.length() > 16 * 1024 * 1024) {
|
||||||
try (FileChannel outChan = new FileOutputStream(modulesLogPath, true).getChannel()) {
|
//noinspection ResultOfMethodCallIgnored
|
||||||
outChan.truncate(1000 * 1024);
|
modulesLog.renameTo(oldModulesLog);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ParcelFileDescriptor.open(modulesLogPath, mode | ParcelFileDescriptor.MODE_CREATE);
|
return ParcelFileDescriptor.open(modulesLog, mode | ParcelFileDescriptor.MODE_CREATE);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, Log.getStackTraceString(e));
|
Log.e(TAG, Log.getStackTraceString(e));
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -600,7 +599,7 @@ public class ConfigManager {
|
||||||
|
|
||||||
public boolean clearLogs(boolean verbose) {
|
public boolean clearLogs(boolean verbose) {
|
||||||
try {
|
try {
|
||||||
OutputStream os = new FileOutputStream(verbose ? verboseLogPath : modulesLogPath);
|
OutputStream os = new FileOutputStream(verbose ? verboseLogPath : modulesLog);
|
||||||
os.close();
|
os.close();
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue