Commit 93a51bd8 authored by Administrator's avatar Administrator

允许外部访问log文件,文本日志增加时间标记

parent a04f42e3
......@@ -12,10 +12,15 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import external.org.apache.commons.io.FileUtils;
import external.org.apache.commons.io.IOUtils;
public class FileLogger {
......@@ -25,6 +30,11 @@ public class FileLogger {
private static OutputStream outputStream = null;
private static byte[] newLine = System.getProperty("line.separator", "\n").getBytes();
private static File mLogFile = null;
public static File getLogFile() {
return mLogFile;
}
public synchronized static void startRecord(File baseDir) {
if (syncLogThread != null) {
......@@ -43,12 +53,25 @@ public class FileLogger {
File filename = new File(dir, processName + ".txt");
mLogFile = filename;
RposedBridge.log("filename:" + filename.getAbsolutePath());
try {
if (!filename.exists()) {
if (!filename.createNewFile()) {
RposedBridge.log("failed to create log file :" + filename.getAbsolutePath());
}
} else {
if (filename.length() > 20 * 1024 * 1024) {
RandomAccessFile accessFile = new RandomAccessFile(filename, "r");
long total = accessFile.length();
long index = total - 10 * 1024 * 1024;
if (index > 0) {
accessFile.seek(index);
byte[] bytes = new byte[11 * 1024 * 1024];
int read = accessFile.read(bytes);
FileUtils.writeByteArrayToFile(filename, bytes, 0, read);
}
}
}
outputStream = new FileOutputStream(filename, true);
} catch (IOException e) {
......@@ -110,7 +133,7 @@ public class FileLogger {
@Override
public void handle(OutputStream outputStream) throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
outputStreamWriter.write(message);
outputStreamWriter.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).format(new Date()) + message);
outputStreamWriter.flush();
}
......@@ -150,7 +173,8 @@ public class FileLogger {
@Override
public void handle(OutputStream outputStream) throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
outputStreamWriter.append(tag);
outputStreamWriter.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).format(new Date()))
.append(tag);
outputStreamWriter.append(message);
outputStreamWriter.flush();
}
......@@ -240,6 +264,7 @@ public class FileLogger {
public static String getTrack(Throwable e) {
StringBuilder msg = new StringBuilder("\n=============>\n");
while (e != null) {
msg.append(e.getClass().getName()).append(":").append(e.getMessage()).append("\n");
StackTraceElement[] ste = e.getStackTrace();
for (StackTraceElement stackTraceElement : ste) {
msg.append(stackTraceElement.getClassName()).append(".").append(stackTraceElement.getMethodName()).append(":").append(stackTraceElement.getLineNumber()).append("\n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment