Skip to content

Commit

Permalink
增加崩溃日记收集和崩溃清空缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
tohodog committed Dec 14, 2020
1 parent dfd1667 commit 2ea9a89
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
75 changes: 70 additions & 5 deletions app/src/main/java/com/qsinong/example/single/QLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -113,7 +117,26 @@ public static String getPath() {
}


private Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler;

private QLog() {
//崩溃监听,清空缓存
defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
try {
if (qLogConfig != null) {
e("Crash", Util.dumpPhoneInfo(qLogConfig.application()), e);
flush();
}
} catch (Throwable a) {
a.printStackTrace();
} finally {//崩溃事件继续流动,系统或其他程序
defaultUncaughtExceptionHandler.uncaughtException(t, e);
}
}
});
}

private QLogConfig qLogConfig;
Expand Down Expand Up @@ -482,11 +505,11 @@ public static boolean writeData(WriteData writeData, String folder, String fileN
}
File file = new File(folder);
if (!file.exists()) file.mkdirs();
// PrintWriter pw = new PrintWriter(file);
// FileOutputStream fos = new FileOutputStream(new File(file, fileName));
// OutputStreamWriter osw = new OutputStreamWriter(fos);
// fos.write(bytes);
// fos.close();
// PrintWriter pw = new PrintWriter(file);
// FileOutputStream fos = new FileOutputStream(new File(file, fileName));
// OutputStreamWriter osw = new OutputStreamWriter(fos);
// fos.write(bytes);
// fos.close();
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(new File(file, fileName), "rw");
randomFile.seek(randomFile.length());
Expand Down Expand Up @@ -558,6 +581,48 @@ public static void checkLog(final QLogConfig qLogConfig) {
}
}
}

/**
* 收集手机信息
*/
public static String dumpPhoneInfo(Context context) {
StringBuilder pw = new StringBuilder("\n");

try {
//包信息
PackageManager pm = context.getPackageManager();
PackageInfo pi = null;
pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
pw.append("App Version:");
pw.append(pi.versionName);
pw.append("(");
pw.append(pi.versionCode);
pw.append(") ");
} catch (Exception e) {
e.printStackTrace();
}

//Android版本号
pw.append("OS Version:");
pw.append(Build.VERSION.RELEASE);
pw.append("(");
pw.append(Build.VERSION.SDK_INT);
pw.append(") ");

//手机制造商
pw.append("Vendor:");
pw.append(Build.MODEL).append("(").append(Build.MANUFACTURER).append(")");
pw.append(" ");

//cpu架构
pw.append("CPU ABI:");
if (Build.VERSION.SDK_INT >= 21 && Build.SUPPORTED_ABIS != null) {
for (String ss : Build.SUPPORTED_ABIS)
pw.append(ss).append(" ");
} else
pw.append(Build.CPU_ABI);
return pw.toString();
}
}

}
10 changes: 7 additions & 3 deletions qlog/src/main/java/com/qsinong/qlog/QLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ private QLog() {
@Override
public void uncaughtException(Thread t, Throwable e) {
try {
e("Crash", Util.dumpPhoneInfo(qLogConfig.application()), e);
flush();
} finally {
if (qLogConfig != null) {
e("Crash", Util.dumpPhoneInfo(qLogConfig.application()), e);
flush();
}
} catch (Throwable a) {
a.printStackTrace();
} finally {//崩溃事件继续流动,系统或其他程序
defaultUncaughtExceptionHandler.uncaughtException(t, e);
}
}
Expand Down
4 changes: 2 additions & 2 deletions qlog/src/main/java/com/qsinong/qlog/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public static String dumpPhoneInfo(Context context) {
PackageInfo pi = null;
pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
pw.append("App Version:");
pw.append(pi == null ? "??" : pi.versionName);
pw.append(pi.versionName);
pw.append("(");
pw.append(pi == null ? "??" : pi.versionCode);
pw.append(pi.versionCode);
pw.append(") ");
} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit 2ea9a89

Please sign in to comment.