JNI exception properties

This commit is contained in:
M66B 2023-12-12 07:40:24 +01:00
parent f690e58c28
commit 420f6c7f36
3 changed files with 43 additions and 6 deletions

View File

@ -1838,7 +1838,7 @@ public class Log {
try (FileWriter out = new FileWriter(file, true)) { try (FileWriter out = new FileWriter(file, true)) {
out.write(BuildConfig.VERSION_NAME + BuildConfig.REVISION + " " + new Date() + "\r\n"); out.write(BuildConfig.VERSION_NAME + BuildConfig.REVISION + " " + new Date() + "\r\n");
out.write(ex + "\r\n" + new ThrowableWrapper(ex).getStackTraceString() + "\r\n"); out.write(ex + "\r\n" + new ThrowableWrapper(ex).getSafeStackTraceString() + "\r\n");
} catch (IOException e) { } catch (IOException e) {
Log.e(e); Log.e(e);
} }

View File

@ -28,6 +28,16 @@ public class ThrowableWrapper extends Throwable {
private final Throwable ex; private final Throwable ex;
private final String msg; private final String msg;
public static native String jni_get_safe_message(Throwable ex);
public static native String jni_to_safe_string(Throwable ex);
public static native String jni_get_safe_stack_trace_string(Throwable ex);
static {
System.loadLibrary("fairemail");
}
ThrowableWrapper(String msg) { ThrowableWrapper(String msg) {
this.ex = new Throwable(); this.ex = new Throwable();
this.msg = msg; this.msg = msg;
@ -39,7 +49,7 @@ public class ThrowableWrapper extends Throwable {
} }
public String getSafeMessage() { public String getSafeMessage() {
return (TextUtils.isEmpty(msg) ? super.getMessage() : msg); return (TextUtils.isEmpty(msg) ? jni_get_safe_message(ex) : msg);
} }
public String getSafeMessageOrName() { public String getSafeMessageOrName() {
@ -47,12 +57,12 @@ public class ThrowableWrapper extends Throwable {
return (msg == null ? ex.getClass().getName() : msg); return (msg == null ? ex.getClass().getName() : msg);
} }
public String getStackTraceString() { public String getSafeStackTraceString() {
return android.util.Log.getStackTraceString(ex); return jni_get_safe_stack_trace_string(ex);
} }
public String toSafeString() { public String toSafeString() {
return ex.toString(); return jni_to_safe_string(ex);
} }
@Nullable @Nullable
@ -64,7 +74,7 @@ public class ThrowableWrapper extends Throwable {
@Nullable @Nullable
@Override @Override
public String getLocalizedMessage() { public String getLocalizedMessage() {
return getMessage(); return ex.getLocalizedMessage();
} }
@NonNull @NonNull

View File

@ -23,6 +23,33 @@ void log_android(int prio, const char *fmt, ...) {
} }
} }
extern "C"
JNIEXPORT jstring JNICALL
Java_eu_faircode_email_ThrowableWrapper_jni_1get_1safe_1message(
JNIEnv *env, jclass clazz, jthrowable ex) {
jclass cls = env->FindClass("java/lang/Throwable");
jmethodID mid = env->GetMethodID(cls, "getMessage", "()Ljava/lang/String;");
return (jstring) env->CallObjectMethod(ex, mid);
}
extern "C"
JNIEXPORT jstring JNICALL
Java_eu_faircode_email_ThrowableWrapper_jni_1to_1safe_1string(
JNIEnv *env, jclass clazz, jthrowable ex) {
jclass cls = env->FindClass("java/lang/Throwable");
jmethodID mid = env->GetMethodID(cls, "toString", "()Ljava/lang/String;");
return (jstring) env->CallObjectMethod(ex, mid);
}
extern "C"
JNIEXPORT jstring JNICALL
Java_eu_faircode_email_ThrowableWrapper_jni_1get_1safe_1stack_1trace_1string(
JNIEnv *env, jclass clazz, jthrowable ex) {
jclass cls = env->FindClass("android/util/Log");
jmethodID mid = env->GetStaticMethodID(cls, "getStackTraceString",
"(Ljava/lang/Throwable;)Ljava/lang/String;");
return (jstring) env->CallStaticObjectMethod(cls, mid, ex);
}
extern "C" extern "C"
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL