From 2f068fbcd60ac8d54e993d22e3369e07e73b467f Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Fri, 2 Nov 2018 13:55:03 +0100 Subject: [PATCH] Add log utils --- .../java/com/github/gotify/log/Format.java | 25 +++++++++ .../main/java/com/github/gotify/log/Log.java | 52 +++++++++++++++++++ .../gotify/log/UncaughtExceptionHandler.java | 7 +++ 3 files changed, 84 insertions(+) create mode 100644 app/src/main/java/com/github/gotify/log/Format.java create mode 100644 app/src/main/java/com/github/gotify/log/Log.java create mode 100644 app/src/main/java/com/github/gotify/log/UncaughtExceptionHandler.java diff --git a/app/src/main/java/com/github/gotify/log/Format.java b/app/src/main/java/com/github/gotify/log/Format.java new file mode 100644 index 0000000..e9d825f --- /dev/null +++ b/app/src/main/java/com/github/gotify/log/Format.java @@ -0,0 +1,25 @@ +package com.github.gotify.log; + +import android.content.Context; + +import com.hypertrack.hyperlog.LogFormat; + +import java.util.Locale; + +public class Format extends LogFormat { + Format(Context context) { + super(context); + } + + @Override + public String getFormattedLogMessage( + String logLevelName, + String tag, + String message, + String timeStamp, + String senderName, + String osVersion, + String deviceUUID) { + return String.format(Locale.ENGLISH, "%s %s: %s", timeStamp, logLevelName, message); + } +} diff --git a/app/src/main/java/com/github/gotify/log/Log.java b/app/src/main/java/com/github/gotify/log/Log.java new file mode 100644 index 0000000..305d651 --- /dev/null +++ b/app/src/main/java/com/github/gotify/log/Log.java @@ -0,0 +1,52 @@ +package com.github.gotify.log; + +import android.content.Context; +import android.text.TextUtils; + +import com.hypertrack.hyperlog.HyperLog; + +import java.util.Collections; +import java.util.List; + +public class Log { + private static String TAG = "gotify"; + + public static void init(Context content) { + HyperLog.initialize(content, new Format(content)); + HyperLog.setLogLevel(android.util.Log.INFO); // TODO configurable + } + + public static String get() { + List logs = HyperLog.getDeviceLogsAsStringList(false); + Collections.reverse(logs); + return TextUtils.join("\n", logs.subList(0, Math.min(200, logs.size()))); + } + + public static void e(String message) { + HyperLog.e(TAG, message); + } + + public static void e(String message, Throwable e) { + HyperLog.e(TAG, message + '\n' + android.util.Log.getStackTraceString(e)); + } + + public static void i(String message) { + HyperLog.i(TAG, message); + } + + public static void i(String message, Throwable e) { + HyperLog.i(TAG, message + '\n' + android.util.Log.getStackTraceString(e)); + } + + public static void w(String message) { + HyperLog.w(TAG, message); + } + + public static void w(String message, Throwable e) { + HyperLog.w(TAG, message + '\n' + android.util.Log.getStackTraceString(e)); + } + + public static void clear() { + HyperLog.deleteLogs(); + } +} diff --git a/app/src/main/java/com/github/gotify/log/UncaughtExceptionHandler.java b/app/src/main/java/com/github/gotify/log/UncaughtExceptionHandler.java new file mode 100644 index 0000000..134f276 --- /dev/null +++ b/app/src/main/java/com/github/gotify/log/UncaughtExceptionHandler.java @@ -0,0 +1,7 @@ +package com.github.gotify.log; + +public class UncaughtExceptionHandler { + public static void registerCurrentThread() { + Thread.setDefaultUncaughtExceptionHandler((t, e) -> Log.e("uncaught exception", e)); + } +}