diff --git a/src/Log.cxx b/src/Log.cxx index 4f496eaf9..836ddcdc7 100644 --- a/src/Log.cxx +++ b/src/Log.cxx @@ -135,6 +135,48 @@ FormatError(const std::exception &e, const char *fmt, ...) LogError(e, msg); } +void +LogError(const std::exception_ptr &ep) +{ + try { + std::rethrow_exception(ep); + } catch (const std::exception &e) { + LogError(e); + } catch (const Error &e) { + LogError(e); + } catch (...) { + Log(exception_domain, LogLevel::ERROR, + "Unrecognized exception"); + } +} + +void +LogError(const std::exception_ptr &ep, const char *msg) +{ + try { + std::rethrow_exception(ep); + } catch (const std::exception &e) { + LogError(e, msg); + } catch (const Error &e) { + LogError(e, msg); + } catch (...) { + FormatError(exception_domain, + "%s: Unrecognized exception", msg); + } +} + +void +FormatError(const std::exception_ptr &ep, const char *fmt, ...) +{ + char msg[1024]; + va_list ap; + va_start(ap, fmt); + vsnprintf(msg, sizeof(msg), fmt, ap); + va_end(ap); + + LogError(ep, msg); +} + void LogError(const Error &error) { diff --git a/src/Log.hxx b/src/Log.hxx index 1cacf5c4e..78489f22d 100644 --- a/src/Log.hxx +++ b/src/Log.hxx @@ -23,9 +23,7 @@ #include "LogLevel.hxx" #include "Compiler.h" -namespace std { - class exception; -} +#include class Error; class Domain; @@ -93,6 +91,16 @@ gcc_printf(2,3) void FormatError(const std::exception &e, const char *fmt, ...); +void +LogError(const std::exception_ptr &ep); + +void +LogError(const std::exception_ptr &ep, const char *msg); + +gcc_printf(2,3) +void +FormatError(const std::exception_ptr &ep, const char *fmt, ...); + gcc_printf(2,3) void FormatError(const Domain &domain, const char *fmt, ...);