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)) {
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) {
Log.e(e);
}

View File

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