Skip to content

Commit

Permalink
Add error (crash) log
Browse files Browse the repository at this point in the history
  • Loading branch information
renyuneyun committed May 25, 2019
1 parent a67175c commit 78dfe49
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ android {
}

dependencies {
def acraVersion = '5.1.3'

implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':plugin')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Expand All @@ -57,6 +59,8 @@ dependencies {
implementation 'de.blox:graphview:0.6.0'
implementation 'com.zeugmasolutions.localehelper:locale-helper-android:1.0.2'
implementation 'com.github.daniel-stoneuk:material-about-library:2.4.2'
implementation "ch.acra:acra-core:$acraVersion"
implementation "ch.acra:acra-toast:$acraVersion"
testImplementation 'junit:junit:4.12'
testImplementation 'org.json:json:20160810'
androidTestImplementation 'androidx.test:runner:1.1.1'
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/ryey/easer/EaserApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Environment;
import android.preference.PreferenceManager;

import androidx.core.content.ContextCompat;
Expand All @@ -34,10 +35,21 @@
import com.orhanobut.logger.Logger;
import com.zeugmasolutions.localehelper.LocaleHelperApplicationDelegate;

import org.acra.ACRA;
import org.acra.annotation.AcraCore;
import org.acra.annotation.AcraToast;

import java.io.File;

import ryey.easer.core.log.ActivityLogService;

@AcraCore(buildConfigClass = BuildConfig.class,
reportSenderFactoryClasses = ErrorSenderFactory.class)
@AcraToast(resText=R.string.prompt_error_logged)
public class EaserApplication extends Application {

static final String LOG_DIR = new File(Environment.getExternalStorageDirectory(), "/logger/error").getAbsolutePath();

private final LocaleHelperApplicationDelegate localeAppDelegate = new LocaleHelperApplicationDelegate();

@Override
Expand Down Expand Up @@ -71,5 +83,6 @@ public void onConfigurationChanged(Configuration newConfig) {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(localeAppDelegate.attachBaseContext(base));
ACRA.init(this);
}
}
63 changes: 63 additions & 0 deletions app/src/main/java/ryey/easer/ErrorSender.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2016 - 2019 Rui Zhao <renyuneyun@gmail.com>
*
* This file is part of Easer.
*
* Easer 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.
*
* Easer 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 Easer. If not, see <http://www.gnu.org/licenses/>.
*/

package ryey.easer

import android.content.Context
import org.acra.ReportField
import org.acra.data.CrashReportData
import org.acra.sender.ReportSender
import java.io.File
import java.io.FileWriter
import java.text.SimpleDateFormat
import java.util.*

class ErrorSender : ReportSender {
override fun send(context: Context, errorContent: CrashReportData) {
if (SettingsHelper.logging(context)) {
val dir = File(EaserApplication.LOG_DIR)
if (!dir.exists()) {
dir.mkdirs()
}
val dateFormat = SimpleDateFormat("yyyy_MM_dd_HH_mm_ss")
val date = Date()
val filename = dateFormat.format(date) + ".log"
val reportFile = File(dir, filename)
FileWriter(reportFile).use {
for (elem in FIELDS) {
it.append("%s: %s\n".format(elem, errorContent.getString(elem)))
}
}

}
}

companion object {
val FIELDS = arrayOf(
ReportField.BUILD_CONFIG,
ReportField.APP_VERSION_CODE,
ReportField.USER_CRASH_DATE,
ReportField.ANDROID_VERSION,
ReportField.BRAND,
ReportField.PHONE_MODEL,
ReportField.PRODUCT,
ReportField.STACK_TRACE
)
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/ryey/easer/ErrorSenderFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2016 - 2019 Rui Zhao <renyuneyun@gmail.com>
*
* This file is part of Easer.
*
* Easer 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.
*
* Easer 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 Easer. If not, see <http://www.gnu.org/licenses/>.
*/

package ryey.easer

import android.content.Context
import org.acra.config.CoreConfiguration
import org.acra.sender.ReportSender
import org.acra.sender.ReportSenderFactory

class ErrorSenderFactory : ReportSenderFactory {
override fun create(context: Context, config: CoreConfiguration): ReportSender {
return ErrorSender()
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,6 @@
<string name="title_donate">捐助</string>
<string name="title_homepage">個人主頁</string>
<string name="title_source">源代碼</string>
<string name="prompt_error_logged">Easer記錄了一次錯誤。請查看日誌目錄下的error子目錄。</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<string name="prompt_delete_failed">Deletion failed. Check if this item is being used.</string>
<string name="prompt_data_illegal">Prevented saving data with invalid field(s). Please recheck your choices / inputs.</string>
<string name="prompt_prevented_for_permission">Grant Easer &lt;%s&gt; permission for this to work..</string>
<string name="prompt_error_logged">Easer just recorded an error. See the "error" subdirectory of log directory.</string>

<string name="text_app_description">Ease your life by automating routine actions on your device(s).
\nChain different events and let their corresponding actions performed. No longer need to keep them in mind!
Expand Down

0 comments on commit 78dfe49

Please sign in to comment.