Skip to content

Commit 87c44c7

Browse files
authored
Add Buffer Logs to ExecuTorchRuntime Exception flow (#14557) (#14557)
Summary: These logs provide detailed context when an error occurs in ET runtime flow to Android host apps / processes. Differential Revision: D83113293
1 parent a1daab9 commit 87c44c7

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

extension/android/executorch_android/src/main/java/org/pytorch/executorch/ExecutorchRuntimeException.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class ExecutorchRuntimeException extends RuntimeException {
7575
}
7676

7777
static class ErrorHelper {
78-
private static final boolean ENABLE_READ_LOG_BUFFER = false;
78+
private static final boolean ENABLE_READ_LOG_BUFFER_LOGS = true;
7979
// Reusable StringBuilder instance
8080
private static final StringBuilder sb = new StringBuilder();
8181

@@ -94,10 +94,12 @@ static String formatMessage(int errorCode, String details) {
9494
.append(baseMessage)
9595
.append(": ")
9696
.append(details);
97-
if (ENABLE_READ_LOG_BUFFER) {
97+
if (ENABLE_READ_LOG_BUFFER_LOGS) {
9898
try {
99-
sb.append("\nDetailed Logs:\n");
100-
String[] logEntries = readLogBuffer(); // JNI call
99+
String[] logEntries = Module.readLogBufferStatic(); // JNI call
100+
if (logEntries != null && logEntries.length > 0) {
101+
sb.append("\n Detailed logs:\n");
102+
}
101103
formatLogEntries(sb, logEntries);
102104
} catch (Exception e) {
103105
sb.append("Failed to retrieve detailed logs: ").append(e.getMessage());
@@ -108,9 +110,6 @@ static String formatMessage(int errorCode, String details) {
108110
}
109111
}
110112

111-
// Native JNI method declaration
112-
private static native String[] readLogBuffer();
113-
114113
// Append log entries to the provided StringBuilder
115114
private static void formatLogEntries(StringBuilder sb, String[] logEntries) {
116115
if (logEntries == null || logEntries.length == 0) {

extension/android/executorch_android/src/main/java/org/pytorch/executorch/Module.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ public MethodMetadata getMethodMetadata(String name) {
211211
return methodMetadata;
212212
}
213213

214+
@DoNotStrip
215+
private static native String[] readLogBufferStaticNative();
216+
217+
public static String[] readLogBufferStatic() {
218+
return readLogBufferStaticNative();
219+
}
220+
214221
/** Retrieve the in-memory log buffer, containing the most recent ExecuTorch log entries. */
215222
public String[] readLogBuffer() {
216223
return readLogBufferNative();

extension/android/jni/jni_layer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,16 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
389389

390390
facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>>
391391
readLogBuffer() {
392+
return readLogBufferUtil();
393+
}
394+
395+
static facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>>
396+
readLogBufferStatic(facebook::jni::alias_ref<jclass>) {
397+
return readLogBufferUtil();
398+
}
399+
400+
static facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>>
401+
readLogBufferUtil() {
392402
#ifdef __ANDROID__
393403

394404
facebook::jni::local_ref<facebook::jni::JArrayClass<jstring>> ret;
@@ -500,6 +510,8 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
500510
makeNativeMethod("executeNative", ExecuTorchJni::execute),
501511
makeNativeMethod("loadMethodNative", ExecuTorchJni::load_method),
502512
makeNativeMethod("readLogBufferNative", ExecuTorchJni::readLogBuffer),
513+
makeNativeMethod(
514+
"readLogBufferStaticNative", ExecuTorchJni::readLogBufferStatic),
503515
makeNativeMethod("etdump", ExecuTorchJni::etdump),
504516
makeNativeMethod("getMethods", ExecuTorchJni::getMethods),
505517
makeNativeMethod("getUsedBackends", ExecuTorchJni::getUsedBackends),

0 commit comments

Comments
 (0)