This patch adds an output operator overload for std::exception_ptr, to
make it easy to log a caught exception. It always shows the exception's
exact type, and for some types of exceptions (subtypes of std::system_error
and std::exception) it prints more information.
For example, the code
try {
throw std::runtime_error("Hello world");
} catch (...) {
sstlog.warn("Exception: {}", std::current_exception());
}
produces the output:
Exception when deleting sstable file: std::runtime_error (Hello world)
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
94 lines
2.1 KiB
C++
94 lines
2.1 KiB
C++
/*
|
|
* Copyright (C) 2014 Cloudius Systems, Ltd.
|
|
*/
|
|
|
|
#include "log.hh"
|
|
#include <cxxabi.h>
|
|
#include <system_error>
|
|
|
|
namespace logging {
|
|
|
|
logger::logger(sstring name) : _name(std::move(name)) {
|
|
g_registry.register_logger(this);
|
|
}
|
|
|
|
logger::logger(logger&& x) : _name(std::move(x._name)), _level(x._level) {
|
|
g_registry.moved(&x, this);
|
|
}
|
|
|
|
logger::~logger() {
|
|
g_registry.unregister_logger(this);
|
|
}
|
|
|
|
void
|
|
logger::really_do_log(log_level level, const char* fmt, stringer** s, size_t n) {
|
|
const char* p = fmt;
|
|
while (*p != '\0') {
|
|
if (*p == '{' && *(p+1) == '}') {
|
|
p += 2;
|
|
if (n > 0) {
|
|
(*s++)->append(std::cout);
|
|
--n;
|
|
} else {
|
|
std::cout << "???";
|
|
}
|
|
} else {
|
|
std::cout << *p++;
|
|
}
|
|
}
|
|
std::cout << "\n";
|
|
}
|
|
|
|
void
|
|
registry::register_logger(logger* l) {
|
|
_loggers[l->name()] = l;
|
|
}
|
|
|
|
void
|
|
registry::unregister_logger(logger* l) {
|
|
_loggers.erase(l->name());
|
|
}
|
|
|
|
void
|
|
registry::moved(logger* from, logger* to) {
|
|
_loggers[from->name()] = to;
|
|
}
|
|
|
|
sstring pretty_type_name(const std::type_info& ti) {
|
|
int status;
|
|
std::unique_ptr<char[], void (*)(void*)> result(
|
|
abi::__cxa_demangle(ti.name(), 0, 0, &status), std::free);
|
|
return result.get() ? result.get() : ti.name();
|
|
}
|
|
|
|
thread_local registry g_registry;
|
|
|
|
}
|
|
|
|
std::ostream& operator<<(std::ostream&out, std::exception_ptr eptr) {
|
|
if (!eptr) {
|
|
out << "<no exception>";
|
|
return out;
|
|
}
|
|
try {
|
|
std::rethrow_exception(eptr);
|
|
} catch(...) {
|
|
auto tp = abi::__cxa_current_exception_type();
|
|
if (tp) {
|
|
out << logging::pretty_type_name(*tp);
|
|
} else {
|
|
// This case shouldn't happen...
|
|
out << "<unknown exception>";
|
|
}
|
|
// Print more information on some familiar exception types
|
|
try {
|
|
throw;
|
|
} catch(const std::system_error &e) {
|
|
out << " (error " << e.code() << ", " << e.code().message() << ")";
|
|
} catch(const std::exception& e) {
|
|
out << " (" << e.what() << ")";
|
|
}
|
|
}
|
|
return out;
|
|
}
|