Skip to content

Commit 23a900c

Browse files
Move Termux app specific logic out of CrashHandler
Create the TermuxCrashUtils class that provides the default path and app for termux instead of hardcoding it in CrashHandler. TermuxCrashUtils can be used by termux plugins as well for their own usage or they can implement the CrashHandler.CrashHandlerClient if they want to log to different files or want custom logic.
1 parent 93a7525 commit 23a900c

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

app/src/main/java/com/termux/app/TermuxApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import android.app.Application;
44

5-
import com.termux.shared.crash.CrashHandler;
5+
import com.termux.shared.crash.TermuxCrashUtils;
66
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
77
import com.termux.shared.logger.Logger;
88

@@ -12,7 +12,7 @@ public void onCreate() {
1212
super.onCreate();
1313

1414
// Set crash handler for the app
15-
CrashHandler.setCrashHandler(this);
15+
TermuxCrashUtils.setCrashHandler(this);
1616

1717
// Set log level for the app
1818
setLogLevel();

termux-shared/src/main/java/com/termux/shared/crash/CrashHandler.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import com.termux.shared.markdown.MarkdownUtils;
1010
import com.termux.shared.models.errors.Error;
1111
import com.termux.shared.termux.AndroidUtils;
12-
import com.termux.shared.termux.TermuxConstants;
13-
import com.termux.shared.termux.TermuxUtils;
1412

1513
import java.nio.charset.Charset;
1614

@@ -19,58 +17,85 @@
1917
*/
2018
public class CrashHandler implements Thread.UncaughtExceptionHandler {
2119

22-
private final Context context;
20+
private final Context mContext;
21+
private final CrashHandlerClient mCrashHandlerClient;
2322
private final Thread.UncaughtExceptionHandler defaultUEH;
2423

2524
private static final String LOG_TAG = "CrashUtils";
2625

27-
private CrashHandler(final Context context) {
28-
this.context = context;
26+
private CrashHandler(@NonNull final Context context, @NonNull final CrashHandlerClient crashHandlerClient) {
27+
this.mContext = context;
28+
this.mCrashHandlerClient = crashHandlerClient;
2929
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
3030
}
3131

3232
public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) {
33-
logCrash(context,thread, throwable);
33+
logCrash(mContext, mCrashHandlerClient, thread, throwable);
3434
defaultUEH.uncaughtException(thread, throwable);
3535
}
3636

3737
/**
3838
* Set default uncaught crash handler of current thread to {@link CrashHandler}.
3939
*/
40-
public static void setCrashHandler(final Context context) {
40+
public static void setCrashHandler(@NonNull final Context context, @NonNull final CrashHandlerClient crashHandlerClient) {
4141
if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof CrashHandler)) {
42-
Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(context));
42+
Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(context, crashHandlerClient));
4343
}
4444
}
4545

4646
/**
47-
* Log a crash in the crash log file at
48-
* {@link TermuxConstants#TERMUX_CRASH_LOG_FILE_PATH}.
47+
* Log a crash in the crash log file at {@code crashlogFilePath}.
4948
*
5049
* @param context The {@link Context} for operations.
50+
* @param crashHandlerClient The {@link CrashHandlerClient} implementation.
5151
* @param thread The {@link Thread} in which the crash happened.
5252
* @param throwable The {@link Throwable} thrown for the crash.
5353
*/
54-
public static void logCrash(final Context context, final Thread thread, final Throwable throwable) {
55-
54+
public static void logCrash(@NonNull final Context context, @NonNull final CrashHandlerClient crashHandlerClient, final Thread thread, final Throwable throwable) {
5655
StringBuilder reportString = new StringBuilder();
5756

5857
reportString.append("## Crash Details\n");
5958
reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Thread", thread.toString(), "-"));
6059
reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Timestamp", AndroidUtils.getCurrentTimeStamp(), "-"));
6160

6261
reportString.append("\n\n").append(Logger.getStackTracesMarkdownString("Stacktrace", Logger.getStackTracesStringArray(throwable)));
63-
reportString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(context, true));
62+
63+
String appInfoMarkdownString = crashHandlerClient.getAppInfoMarkdownString(context);
64+
if (appInfoMarkdownString != null && !appInfoMarkdownString.isEmpty())
65+
reportString.append("\n\n").append(appInfoMarkdownString);
66+
6467
reportString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(context));
6568

6669
// Log report string to logcat
6770
Logger.logError(reportString.toString());
6871

6972
// Write report string to crash log file
70-
Error error = FileUtils.writeStringToFile("crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportString.toString(), false);
73+
Error error = FileUtils.writeStringToFile("crash log", crashHandlerClient.getCrashLogFilePath(context),
74+
Charset.defaultCharset(), reportString.toString(), false);
7175
if (error != null) {
7276
Logger.logErrorExtended(LOG_TAG, error.toString());
7377
}
7478
}
7579

80+
public interface CrashHandlerClient {
81+
82+
/**
83+
* Get crash log file path.
84+
*
85+
* @param context The {@link Context} passed to {@link CrashHandler#CrashHandler(Context, CrashHandlerClient)}.
86+
* @return Should return the crash log file path.
87+
*/
88+
@NonNull
89+
String getCrashLogFilePath(Context context);
90+
91+
/**
92+
* Get app info markdown string to add to crash log.
93+
*
94+
* @param context The {@link Context} passed to {@link CrashHandler#CrashHandler(Context, CrashHandlerClient)}.
95+
* @return Should return app info markdown string.
96+
*/
97+
String getAppInfoMarkdownString(Context context);
98+
99+
}
100+
76101
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.termux.shared.crash;
2+
3+
import android.content.Context;
4+
5+
import androidx.annotation.NonNull;
6+
7+
import com.termux.shared.termux.TermuxConstants;
8+
import com.termux.shared.termux.TermuxUtils;
9+
10+
public class TermuxCrashUtils implements CrashHandler.CrashHandlerClient {
11+
12+
/**
13+
* Set default uncaught crash handler of current thread to {@link CrashHandler} for Termux app
14+
* and its plugin to log crashes at {@link TermuxConstants#TERMUX_CRASH_LOG_FILE_PATH}.
15+
*/
16+
public static void setCrashHandler(@NonNull final Context context) {
17+
CrashHandler.setCrashHandler(context, new TermuxCrashUtils());
18+
}
19+
20+
@NonNull
21+
@Override
22+
public String getCrashLogFilePath(Context context) {
23+
return TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH;
24+
}
25+
26+
@Override
27+
public String getAppInfoMarkdownString(Context context) {
28+
return TermuxUtils.getAppInfoMarkdownString(context, true);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)