aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/nullwire/trace/DefaultExceptionHandler.java
blob: f9abe700ec6c7d86ee9ff7a3dec058580a06a7a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.nullwire.trace;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Random;

import android.util.Log;

public class DefaultExceptionHandler implements UncaughtExceptionHandler {

	private UncaughtExceptionHandler defaultExceptionHandler;

	private static final String TAG = "UNHANDLED_EXCEPTION";

	// constructor
	public DefaultExceptionHandler(UncaughtExceptionHandler pDefaultExceptionHandler)
	{
		defaultExceptionHandler = pDefaultExceptionHandler;
	}

	// Default exception handler
	public void uncaughtException(Thread t, Throwable e) {
		// Here you should have a more robust, permanent record of problems
		final Writer result = new StringWriter();
		final PrintWriter printWriter = new PrintWriter(result);
		e.printStackTrace(printWriter);
		try {
			// Random number to avoid duplicate files
			Random generator = new Random();
			int random = generator.nextInt(99999);
			// Embed version in stacktrace filename
			String filename = G.APP_VERSION + "-" + Integer.toString(random);
			Log.d(TAG, "Writing unhandled exception to: " + G.FILES_PATH + "/"
					+ filename + ".stacktrace");
			// Write the stacktrace to disk
			BufferedWriter bos = new BufferedWriter(new FileWriter(G.FILES_PATH
					+ "/" + filename + ".stacktrace"));
			bos.write(G.APP_VERSION + "\n");
			bos.write(G.APP_DESCRIPTION + "\n");
			bos.write(G.ANDROID_VERSION + "\n");
			bos.write(G.PHONE_MODEL + "\n");
			bos.write(result.toString());
			bos.flush();
			// Close up everything
			bos.close();
		} catch (Exception ebos) {
			// Nothing much we can do about this - the game is over
			ebos.printStackTrace();
		}
		Log.d(TAG, result.toString());
		//call original handler
		defaultExceptionHandler.uncaughtException(t, e);
	}
}