[tonic] Log stack trace for unhandles exceptions (flutter/engine#19811)

Before this change, when we encounter an unhandles exception,
we see:

```
[00050.147083][37665][67367][klog] ERROR: [ERROR:flutter/shell/common/shell.cc(209)] Dart Error: Unhandled exception:
[00050.147087][37665][67367][klog] INFO: UnimplementedError
```

This doesn't help identify the root cause. Adding stack trace will help
the cause.
This commit is contained in:
Kaushik Iska 2020-07-16 12:03:02 -07:00 committed by GitHub
parent e95dfe0f02
commit 132bfaa975
3 changed files with 15 additions and 4 deletions

View File

@ -22,8 +22,9 @@ void Log(const char* format, ...) {
int result = vsnprintf(nullptr, 0, format, ap);
va_end(ap);
if (result < 0)
if (result < 0) {
return;
}
int size = result + 1;
std::unique_ptr<char[]> message(new char[size]);
@ -31,8 +32,9 @@ void Log(const char* format, ...) {
result = vsnprintf(message.get(), size, format, ap);
va_end(ap);
if (result < 0)
if (result < 0) {
return;
}
if (log_handler) {
log_handler(message.get());

View File

@ -16,6 +16,7 @@ source_set("logging") {
deps = [
"../common",
"../converter",
]
public_deps = [

View File

@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "tonic/logging/dart_error.h"
#include "tonic/converter/dart_converter.h"
#include "tonic/common/macros.h"
@ -12,11 +13,18 @@ const char kInvalidArgument[] = "Invalid argument.";
} // namespace DartError
bool LogIfError(Dart_Handle handle) {
if (Dart_IsError(handle)) {
if (Dart_IsUnhandledExceptionError(handle)) {
Dart_Handle stack_trace_handle = Dart_ErrorGetStackTrace(handle);
const std::string stack_trace =
tonic::StdStringFromDart(Dart_ToString(stack_trace_handle));
tonic::Log("Dart Unhandled Exception: %s", stack_trace.c_str());
return true;
} else if (Dart_IsError(handle)) {
tonic::Log("Dart Error: %s", Dart_GetError(handle));
return true;
} else {
return false;
}
return false;
}
DartErrorHandleType GetErrorHandleType(Dart_Handle handle) {