Skip to content

Commit 97e134a

Browse files
psiddhfacebook-github-bot
authored andcommitted
Pack the native log tail into ExecutorchRuntimeException's message.
Summary: The fix changes the native log truncation from keeping the prefix (first 2048 characters) to keeping the suffix/tail (last 2048 characters) of the log string. This ensures that the most relevant recent log lines are preserved for diagnosing failures, rather than the older log entries at the beginning. using takeLast ensures we keep the most recent log lines which are most relevant for diagnosing failures Reviewed By: SS-JIA Differential Revision: D107196396
1 parent 3b3f621 commit 97e134a

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ constructor(
8484
}
8585
}
8686
} catch (e: Exception) {
87-
sb.append("Failed to retrieve detailed logs: ").append(e.message)
87+
return ""
8888
}
8989
return sb.toString()
9090
}
@@ -124,10 +124,28 @@ constructor(
124124

125125
@DoNotStrip
126126
@JvmStatic
127-
fun makeExecutorchException(errorCode: Int, details: String?): RuntimeException =
128-
when (errorCode) {
129-
INVALID_ARGUMENT -> ExecutorchInvalidArgumentException(details)
130-
else -> ExecutorchRuntimeException(errorCode, details)
131-
}
127+
fun makeExecutorchException(errorCode: Int, details: String?): RuntimeException {
128+
val nativeTail =
129+
try {
130+
ErrorHelper.getDetailedErrorLogs()
131+
.removePrefix("\nDetailed logs:\n")
132+
.replace(Regex("\\s+"), " ")
133+
.trim()
134+
} catch (t: Throwable) {
135+
""
136+
}
137+
val enrichedDetails =
138+
if (nativeTail.isNotBlank()) {
139+
"${details ?: "No details provided"} | nativeLog=${nativeTail.takeLast(NATIVE_LOG_TAIL_MAX_CHARS)}"
140+
} else {
141+
details
142+
}
143+
return when (errorCode) {
144+
INVALID_ARGUMENT -> ExecutorchInvalidArgumentException(enrichedDetails)
145+
else -> ExecutorchRuntimeException(errorCode, enrichedDetails)
146+
}
147+
}
148+
149+
private const val NATIVE_LOG_TAIL_MAX_CHARS = 2048
132150
}
133151
}

0 commit comments

Comments
 (0)