From d60bdb462c093dbb10d4288e5ac27b0108e72447 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sun, 20 May 2018 14:57:16 +0200 Subject: [PATCH] Add in memory log logcat doesn't really support showing logs see https://stackoverflow.com/q/12692103/4244993 Solution is to delegate to the logcat log and save the logs in memory that it can be viewed later. --- android/app/src/main/java/de/gotify/Log.java | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 android/app/src/main/java/de/gotify/Log.java diff --git a/android/app/src/main/java/de/gotify/Log.java b/android/app/src/main/java/de/gotify/Log.java new file mode 100644 index 0000000..81a74f6 --- /dev/null +++ b/android/app/src/main/java/de/gotify/Log.java @@ -0,0 +1,44 @@ +package de.gotify; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +public class Log { + private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH); + private static final List LOG = Collections.synchronizedList(new ArrayList()); + private static final String TAG = "gotify"; + + public static void i(String message) { + i(message, null); + } + + public static List get() { + return LOG; + } + + public static void i(String message, Throwable throwable) { + log("INFO", message, throwable); + android.util.Log.i(TAG, message, throwable); + } + + public static void e(String message) { + e(message, null); + } + + public static void e(String message, Throwable throwable) { + log("ERROR", message, throwable); + android.util.Log.e("gotify", message, throwable); + } + + private static void log(String type, String message, Throwable exception) { + if (exception == null) { + LOG.add(String.format("%s: %s - %s", type, FORMAT.format(new Date()), message)); + } else { + LOG.add(String.format("%s: %s - %s%s%s", type, FORMAT.format(new Date()), message, "\n", android.util.Log.getStackTraceString(exception))); + } + } +}