Kaushik Iska 1cb875156f
[tonic] Log stack trace for unhandles exceptions (#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.
2020-07-16 12:03:02 -07:00

51 lines
950 B
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "tonic/common/log.h"
#include <cstdarg>
#include <cstdio>
#include <memory>
namespace tonic {
namespace {
std::function<void(const char*)> log_handler;
} // namespace
void Log(const char* format, ...) {
va_list ap;
va_start(ap, format);
int result = vsnprintf(nullptr, 0, format, ap);
va_end(ap);
if (result < 0) {
return;
}
int size = result + 1;
std::unique_ptr<char[]> message(new char[size]);
va_start(ap, format);
result = vsnprintf(message.get(), size, format, ap);
va_end(ap);
if (result < 0) {
return;
}
if (log_handler) {
log_handler(message.get());
} else {
printf("%s\n", message.get());
}
}
void SetLogHandler(std::function<void(const char*)> handler) {
log_handler = handler;
}
} // namespace tonic