Skip to content

Commit

Permalink
Handle uncaught exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ma1co committed Apr 25, 2016
1 parent 983cf0d commit c1fcf3e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 12 additions & 3 deletions app/src/main/java/com/github/ma1co/openmemories/tweak/Logger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.ma1co.openmemories.tweak;

import java.io.PrintWriter;
import java.io.StringWriter;

public class Logger {
private static final StringBuffer buffer = new StringBuffer();

Expand All @@ -23,11 +26,17 @@ public static void error(String tag, String msg) {
log("ERROR", tag, msg);
}

public static void error(String tag, String msg, Exception exp) {
error(tag, (msg.isEmpty() ? "" : msg + ": ") + exp.getClass().getSimpleName() + " (" + exp.getMessage() + ")");
public static void error(String tag, String msg, Throwable exp) {
StringWriter sw = new StringWriter();
if (!msg.isEmpty()) {
sw.append(msg);
sw.append(": ");
}
exp.printStackTrace(new PrintWriter(sw));
error(tag, sw.toString().trim());
}

public static void error(String tag, Exception exp) {
public static void error(String tag, Throwable exp) {
error(tag, "", exp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable exp) {
Logger.error("UncaughtExceptionHandler", exp);
saveLog();
System.exit(0);
}
});

addTab("video", "Video", android.R.drawable.ic_menu_camera, VideoActivity.class);
addTab("lang", "Languages", android.R.drawable.ic_menu_mapmode, LanguageActivity.class);
addTab("protection", "Protection", android.R.drawable.ic_lock_lock, ProtectionActivity.class);
Expand All @@ -37,6 +47,10 @@ protected void onResume() {
protected void onPause() {
super.onPause();
Logger.info("MainActivity", "application end");
saveLog();
}

protected void saveLog() {
try {
FileWriter writer = new FileWriter(LOG_FILE, true);
writer.write(Logger.getLogs() + "\n");
Expand Down

0 comments on commit c1fcf3e

Please sign in to comment.