2015-12-04 07:40:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
2015-12-14 16:30:32 +08:00
|
|
|
#include "utils/auto_scope.hpp"
|
2016-12-16 20:56:36 +08:00
|
|
|
#include "utils/stacktrace.hpp"
|
2015-12-14 16:30:32 +08:00
|
|
|
|
2015-12-04 07:40:12 +08:00
|
|
|
#include <execinfo.h>
|
2016-12-16 20:56:36 +08:00
|
|
|
#include <iostream>
|
2015-12-04 07:40:12 +08:00
|
|
|
|
|
|
|
// TODO: log to local file or remote database
|
2016-12-16 20:56:36 +08:00
|
|
|
void stacktrace(std::ostream& stream) noexcept {
|
|
|
|
Stacktrace stacktrace;
|
2015-12-14 16:30:32 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
std::string message;
|
2015-12-14 16:30:32 +08:00
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
for (int i = 0; i < stacktrace.size(); i++) {
|
|
|
|
message.append(fmt::format("\n at {} ({})", stacktrace[i].function,
|
|
|
|
stacktrace[i].location));
|
|
|
|
}
|
|
|
|
stream << message << std::endl;
|
2015-12-04 07:40:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: log to local file or remote database
|
2016-12-16 20:56:36 +08:00
|
|
|
void terminate_handler(std::ostream& stream) noexcept {
|
|
|
|
if (auto exc = std::current_exception()) {
|
|
|
|
try {
|
|
|
|
std::rethrow_exception(exc);
|
|
|
|
} catch (std::exception& ex) {
|
|
|
|
stream << ex.what() << std::endl << std::endl;
|
|
|
|
stacktrace(stream);
|
2015-12-04 07:40:12 +08:00
|
|
|
}
|
2016-12-16 20:56:36 +08:00
|
|
|
}
|
|
|
|
std::abort();
|
2015-12-14 16:30:32 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 20:56:36 +08:00
|
|
|
void terminate_handler() noexcept { terminate_handler(std::cout); }
|