|
| 1 | +package moe.yuuta.mipushtester |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import android.os.SystemClock |
| 5 | +import com.crashlytics.android.Crashlytics |
| 6 | +import com.crashlytics.android.answers.Answers |
| 7 | +import com.elvishew.xlog.LogConfiguration |
| 8 | +import com.elvishew.xlog.XLog |
| 9 | +import com.elvishew.xlog.formatter.message.json.DefaultJsonFormatter |
| 10 | +import com.elvishew.xlog.printer.AndroidPrinter |
| 11 | +import com.elvishew.xlog.printer.file.FilePrinter |
| 12 | +import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy |
| 13 | +import com.elvishew.xlog.printer.file.naming.DateFileNameGenerator |
| 14 | +import com.xiaomi.channel.commonutils.logger.LoggerInterface |
| 15 | +import com.xiaomi.mipush.sdk.Logger |
| 16 | +import io.fabric.sdk.android.Fabric |
| 17 | +import moe.yuuta.mipushtester.log.LogUtils |
| 18 | + |
| 19 | +class App : Application() { |
| 20 | + @Override |
| 21 | + override fun onCreate() { |
| 22 | + super.onCreate() |
| 23 | + val logConfiguration = LogConfiguration.Builder() |
| 24 | + .tag("MiPushTester") |
| 25 | + .jsonFormatter(DefaultJsonFormatter()) |
| 26 | + .build() |
| 27 | + val androidPrinter = AndroidPrinter() |
| 28 | + val filePrinter = FilePrinter.Builder(LogUtils.getLogFolder(this)) |
| 29 | + .fileNameGenerator(DateFileNameGenerator()) |
| 30 | + .cleanStrategy(FileLastModifiedCleanStrategy(1000 * 60 * 60 * 24 * 5)) |
| 31 | + .build() |
| 32 | + XLog.init(logConfiguration, androidPrinter, filePrinter) |
| 33 | + |
| 34 | + if (!BuildConfig.DEBUG && !BuildConfig.FABRIC_KEY.equals("disabled")) { |
| 35 | + Fabric.with(this, Crashlytics(), Answers()) |
| 36 | + } |
| 37 | + val currentHandler = Thread.getDefaultUncaughtExceptionHandler() |
| 38 | + Thread.setDefaultUncaughtExceptionHandler(object : Thread.UncaughtExceptionHandler { |
| 39 | + override fun uncaughtException(t: Thread?, e: Throwable?) { |
| 40 | + val logger = XLog.tag("Crash").build() |
| 41 | + logger.e("App crashed", e) |
| 42 | + SystemClock.sleep(100) |
| 43 | + if (currentHandler != null) currentHandler.uncaughtException(t, e) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + val newLogger = object: LoggerInterface { |
| 48 | + private var logger: com.elvishew.xlog.Logger = |
| 49 | + XLog.tag("XMPush").build() |
| 50 | + |
| 51 | + @Override |
| 52 | + override fun setTag(tag: String) { |
| 53 | + logger = XLog.tag("XMPush-$tag").build() |
| 54 | + } |
| 55 | + @Override |
| 56 | + override fun log(content: String, t: Throwable) { |
| 57 | + logger.d(content, t) |
| 58 | + } |
| 59 | + @Override |
| 60 | + override fun log(content: String) { |
| 61 | + logger.d(content) |
| 62 | + } |
| 63 | + } |
| 64 | + Logger.setLogger(this, newLogger) |
| 65 | + } |
| 66 | +} |
0 commit comments