From 38a269ae1cc4a14111aa682f2c7e8c42a2214180 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Sat, 22 Oct 2022 13:23:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8D=95=E8=8E=B7=E5=B9=B6?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=BC=82=E5=B8=B8=E6=97=A5=E5=BF=97=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 3 + .../java/com/shayu/phonelive/AppContext.java | 4 +- .../com/shayu/phonelive/NeverCrashUtils.java | 11 ++- .../phonelive/activity/LauncherActivity.java | 3 + .../com/yunbao/common/CommonAppConfig.java | 2 + .../com/yunbao/common/utils/FileUtil.java | 22 ++++++ .../java/com/yunbao/common/utils/LogUtil.java | 70 +++++++++++++++++++ config.gradle | 4 +- 8 files changed, 114 insertions(+), 5 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d3f6b665f..f178f1635 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -148,6 +148,9 @@ + reference : activities) { - if(reference.get()==activity){ + if (reference.get() == activity) { activities.remove(reference); return; } @@ -297,7 +297,7 @@ public class AppContext extends CommonAppContext { e.printStackTrace(); //闪退后finish所有Activity并且杀死进程 for (WeakReference activity : activities) { - if (activity != null) { + if (activity != null && activity.get() != null) { activity.get().finish(); } } diff --git a/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java b/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java index fa761d83c..c5f677c5f 100644 --- a/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java +++ b/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java @@ -6,6 +6,8 @@ import android.os.Looper; import android.util.Log; import android.widget.Toast; import com.yunbao.common.utils.FileUtil; +import com.yunbao.common.utils.SpUtil; + import java.io.File; import java.io.PrintWriter; import java.io.StringWriter; @@ -94,7 +96,7 @@ public class NeverCrashUtils { } e.printStackTrace(); Toast.makeText(application, "发生闪退", Toast.LENGTH_SHORT).show(); - // FileUtil.saveStringToFile(new File(application.getDir("files", Context.MODE_PRIVATE).getAbsolutePath()),throwableToString(e),"error.log"); + FileUtil.saveStringToFile(new File(application.getDir("files", Context.MODE_PRIVATE).getAbsolutePath()),throwableToString(e),"error.log"); getMainCrashHandler().mainException(Looper.getMainLooper().getThread(), e); } @@ -124,8 +126,13 @@ public class NeverCrashUtils { } private static String throwableToString(Throwable e) { StringWriter writer=new StringWriter(); + writer.write(SpUtil.getInstance().getStringValue(SpUtil.USER_INFO)); + writer.write("\n"); + writer.write("time="+System.currentTimeMillis()); + writer.write("\n"); + writer.write("[ERROR]"); PrintWriter printWriter=new PrintWriter(writer); e.printStackTrace(printWriter); - return "[ERROR]"+writer.toString(); + return writer.toString(); } } \ No newline at end of file diff --git a/app/src/main/java/com/shayu/phonelive/activity/LauncherActivity.java b/app/src/main/java/com/shayu/phonelive/activity/LauncherActivity.java index 963dab50f..b67bb3bb4 100644 --- a/app/src/main/java/com/shayu/phonelive/activity/LauncherActivity.java +++ b/app/src/main/java/com/shayu/phonelive/activity/LauncherActivity.java @@ -27,6 +27,7 @@ import com.adjust.sdk.AdjustEvent; import com.alibaba.android.arouter.facade.annotation.Route; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.shayu.phonelive.AppContext; import com.tencent.rtmp.ITXLivePlayListener; import com.tencent.rtmp.TXLiveBase; import com.tencent.rtmp.TXLiveConstants; @@ -48,6 +49,7 @@ import com.yunbao.common.manager.IMLoginManager; import com.yunbao.common.manager.imrongcloud.RongcloudIMManager; import com.yunbao.common.utils.DownloadUtil; import com.yunbao.common.utils.L; +import com.yunbao.common.utils.LogUtil; import com.yunbao.common.utils.MD5Util; import com.yunbao.common.utils.RouteUtil; import com.yunbao.common.utils.SpUtil; @@ -137,6 +139,7 @@ public class LauncherActivity extends AppCompatActivity implements View.OnClickL } }; mHandler.sendEmptyMessageDelayed(WHAT_GET_CONFIG, 1000); + LogUtil.pushError(AppContext.sInstance); } diff --git a/common/src/main/java/com/yunbao/common/CommonAppConfig.java b/common/src/main/java/com/yunbao/common/CommonAppConfig.java index edbe22569..f514fc808 100644 --- a/common/src/main/java/com/yunbao/common/CommonAppConfig.java +++ b/common/src/main/java/com/yunbao/common/CommonAppConfig.java @@ -33,6 +33,8 @@ public class CommonAppConfig { public static final String HOST = getMetaDataString("SERVER_HOST"); //是否使用谷歌支付 public static final boolean IS_GOOGLE_PLAY = getMetaDataBoolean("IS_GOOGLE_PLAY"); + //是否开启上报错误日志功能 + public static final boolean IS_UPLOAD_ERROR_LOG = getMetaDataBoolean("IS_UPLOAD_ERROR_LOG"); //外部sd卡 public static final String DCMI_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath(); diff --git a/common/src/main/java/com/yunbao/common/utils/FileUtil.java b/common/src/main/java/com/yunbao/common/utils/FileUtil.java index 7e18855b0..6563406fc 100644 --- a/common/src/main/java/com/yunbao/common/utils/FileUtil.java +++ b/common/src/main/java/com/yunbao/common/utils/FileUtil.java @@ -2,9 +2,11 @@ package com.yunbao.common.utils; import android.media.MediaMetadataRetriever; +import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.PrintWriter; /** @@ -32,6 +34,26 @@ public class FileUtil { } } + /** + * 从文件读取字符串 + */ + public static String loadStringToFile(File file){ + if(file.exists()){ + try { + BufferedReader reader=new BufferedReader(new FileReader(file)); + StringBuilder builder=new StringBuilder(); + String tmp; + while ((tmp=reader.readLine())!=null){ + builder.append(tmp).append("\n"); + } + return builder.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return ""; + } + /** * 获取 视频 或 音频 时长 * @param path 视频 或 音频 文件路径 diff --git a/common/src/main/java/com/yunbao/common/utils/LogUtil.java b/common/src/main/java/com/yunbao/common/utils/LogUtil.java index 61d49238f..8c52d9a82 100644 --- a/common/src/main/java/com/yunbao/common/utils/LogUtil.java +++ b/common/src/main/java/com/yunbao/common/utils/LogUtil.java @@ -1,9 +1,24 @@ package com.yunbao.common.utils; +import android.content.Context; import android.text.TextUtils; +import android.util.Log; + +import com.lzy.okgo.OkGo; +import com.lzy.okgo.callback.StringCallback; +import com.lzy.okgo.model.Progress; +import com.lzy.okgo.model.Response; +import com.lzy.okgo.request.PostRequest; +import com.lzy.okserver.OkUpload; +import com.lzy.okserver.upload.UploadListener; +import com.lzy.okserver.upload.UploadTask; +import com.yunbao.common.CommonAppConfig; import java.io.File; import java.io.FileWriter; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; /** * Created by cxf on 2019/6/20. @@ -31,4 +46,59 @@ public class LogUtil { } } } + public static void pushError(Context context){ + if(!CommonAppConfig.IS_UPLOAD_ERROR_LOG){ + return; + } + File file = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath()+File.separator+"error.log"); + if(file.exists()){ + Log.i("异常上传", "pushError: 准备上传文件"); + String err = FileUtil.loadStringToFile(file); + PostRequest request = OkGo.post("https://newht.yaoulive.com/index.php/api/error_log/report") + .params("log", err) + .params("uid", CommonAppConfig.getInstance().getUid()) + .params("time", new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss", Locale.CHINA).format(new Date())) + .params("isGoogle",CommonAppConfig.IS_GOOGLE_PLAY) + .params("appVersion",CommonAppConfig.getInstance().getVersion()) + .tag("errorLog") + .converter(new StringCallback() { + String TAG = "异常上传"; + + @Override + public void onSuccess(Response response) { + Log.i(TAG, "onSuccess: " + response.message()); + file.delete(); + } + }); + UploadTask task = OkUpload.request("errorLog", request) + .save() + .register(new UploadListener("errorLog") { + + @Override + public void onStart(Progress progress) { + + } + + @Override + public void onProgress(Progress progress) { + + } + + @Override + public void onError(Progress progress) { + } + + @Override + public void onFinish(String s, Progress progress) { + file.delete(); + } + + @Override + public void onRemove(Progress progress) { + + } + }); + task.start(); + } + } } diff --git a/config.gradle b/config.gradle index f0557bc1e..9e347c98c 100644 --- a/config.gradle +++ b/config.gradle @@ -25,6 +25,8 @@ ext { baiduAppSecretKey: "nEVSgmuGpU0pjPr6VleEGGAl0hzGW52S", // true表示谷歌支付 false - isGooglePlay : false + isGooglePlay : false, + //是否上报异常日志 + isUploadLog : false ] } \ No newline at end of file