rewrite Logs

This commit is contained in:
luvletter2333 2023-03-28 11:56:36 +08:00
parent b93a338a7e
commit e8ab40bfe8
No known key found for this signature in database
GPG Key ID: 9EB7723F3A0ACF92
2 changed files with 44 additions and 20 deletions

View File

@ -29,6 +29,8 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.Locale;
import cn.hutool.core.util.StrUtil;
public class FileLog {
private OutputStreamWriter streamWriter = null;
private FastDateFormat dateFormat = null;
@ -47,6 +49,7 @@ public class FileLog {
private final static String mtproto_tag = "MTProto";
private static volatile FileLog Instance = null;
public static FileLog getInstance() {
FileLog localInstance = Instance;
if (localInstance == null) {
@ -93,7 +96,7 @@ public class FileLog {
long time = System.currentTimeMillis();
FileLog.getInstance().logQueue.postRunnable(() -> {
try {
String metadata = "requestMsgId=" + requestMsgId + " requestingTime=" + (System.currentTimeMillis() - startRequestTimeInMillis) + " request_token=" + requestToken;
String metadata = "requestMsgId=" + requestMsgId + " requestingTime=" + (System.currentTimeMillis() - startRequestTimeInMillis) + " request_token=" + requestToken;
FileLog.getInstance().tlStreamWriter.write(getInstance().dateFormat.format(time) + " " + metadata);
FileLog.getInstance().tlStreamWriter.write("\n");
FileLog.getInstance().tlStreamWriter.write(req);
@ -185,12 +188,11 @@ public class FileLog {
}
public void init() {
if (initied) {
return;
}
dateFormat = FastDateFormat.getInstance("dd_MM_yyyy_HH_mm_ss", Locale.US);
dateFormat = FastDateFormat.getInstance("yyyy_MM_dd-HH_mm_ss", Locale.US);
String date = dateFormat.format(System.currentTimeMillis());
try {
File dir = AndroidUtilities.getLogsDir();
@ -263,11 +265,12 @@ public class FileLog {
return;
}
ensureInitied();
String tag = mkTag();
Log.e(tag, message, exception);
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + message + "\n");
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/" + tag + ": " + message + "\n");
getInstance().streamWriter.write(exception.toString());
getInstance().streamWriter.flush();
} catch (Exception e) {
@ -282,11 +285,12 @@ public class FileLog {
return;
}
ensureInitied();
String tag = mkTag();
Log.e(tag, message);
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + message + "\n");
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/" + tag + ": " + message + "\n");
getInstance().streamWriter.flush();
} catch (Exception e) {
e.printStackTrace();
@ -321,16 +325,18 @@ public class FileLog {
}
}
}
final String tag = mkTag();
Log.e(tag, mkMessage(e));
ensureInitied();
e.printStackTrace();
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + e + "\n");
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/" + tag + ": " + e + "\n");
StackTraceElement[] stack = e.getStackTrace();
for (int a = 0; a < stack.length; a++) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + stack[a] + "\n");
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/" + tag + ": " + stack[a] + "\n");
}
getInstance().streamWriter.flush();
} catch (Exception e1) {
@ -355,13 +361,15 @@ public class FileLog {
// }
ensureInitied();
e.printStackTrace();
String tag = mkTag();
Log.e(tag, mkMessage(e));
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + e + "\n");
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " Fatal/" + tag + ": " + e + "\n");
StackTraceElement[] stack = e.getStackTrace();
for (int a = 0; a < stack.length; a++) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + stack[a] + "\n");
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " Fatal/" + tag + ": " + stack[a] + "\n");
}
getInstance().streamWriter.flush();
} catch (Exception e1) {
@ -380,6 +388,17 @@ public class FileLog {
}
}
private static String mkTag() {
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
return StrUtil.subAfter(stackTrace[4].getClassName(), ".", true);
}
private static String mkMessage(Throwable e) {
String message = e.getMessage();
if (message != null) return e.getClass().getSimpleName() + ": " + message;
return e.getClass().getSimpleName();
}
private static boolean needSent(Throwable e) {
if (e instanceof InterruptedException || e instanceof MediaCodecVideoConvertor.ConversionCanceledException || e instanceof IgnoreSentException) {
return false;
@ -392,11 +411,12 @@ public class FileLog {
return;
}
ensureInitied();
String tag = mkTag();
Log.d(tag, message);
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " D/tmessages: " + message + "\n");
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " D/" + tag + ": " + message + "\n");
getInstance().streamWriter.flush();
} catch (Exception e) {
e.printStackTrace();
@ -413,11 +433,12 @@ public class FileLog {
return;
}
ensureInitied();
String tag = mkTag();
Log.w(tag, message);
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " W/tmessages: " + message + "\n");
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " W/" + tag + ": " + message + "\n");
getInstance().streamWriter.flush();
} catch (Exception e) {
e.printStackTrace();
@ -450,7 +471,7 @@ public class FileLog {
}
}
public static class IgnoreSentException extends Exception{
public static class IgnoreSentException extends Exception {
public IgnoreSentException(String e) {
super(e);

View File

@ -3394,7 +3394,7 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
AlertDialog pro = AlertUtil.showProgress(getParentActivity());
pro.show();
UIUtil.runOnIoDispatcher(() -> {
FileUtil.delete(new File(EnvUtil.getTelegramPath(), "logs"));
FileUtil.delete(AndroidUtilities.getLogsDir());
ThreadUtil.sleep(100L);
LangsKt.uDismiss(pro);
});
@ -9039,6 +9039,14 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
return;
}
File logcatFile = new File(dir, "NekoX-" + System.currentTimeMillis() + ".log");
try {
RuntimeUtil.exec("logcat", "-df", logcatFile.getPath()).waitFor();
RuntimeUtil.exec("logcat", "-c").waitFor();
} catch (Exception e) {
AlertUtil.showToast(e);
}
File zipFile = new File(dir, "logs.zip");
if (zipFile.exists()) {
zipFile.delete();
@ -9046,18 +9054,13 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
ArrayList<File> files = new ArrayList<>();
File[] logFiles = dir.listFiles();
for (File f : logFiles) {
files.add(f);
}
files.addAll(Arrays.asList(dir.listFiles()));
File filesDir = ApplicationLoader.getFilesDirFixed();
filesDir = new File(filesDir, "malformed_database/");
if (filesDir.exists() && filesDir.isDirectory()) {
File[] malformedDatabaseFiles = filesDir.listFiles();
for (File file : malformedDatabaseFiles) {
files.add(file);
}
files.addAll(Arrays.asList(malformedDatabaseFiles));
}
boolean[] finished = new boolean[1];