From 55f95b3ac955dbd944ae430523e1f7f372a485ec Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 15 Dec 2015 23:10:26 +0100 Subject: [PATCH] Log: C++ exception support --- src/Log.cxx | 36 ++++++++++++++++++++++++++++++++++++ src/Log.hxx | 10 ++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/Log.cxx b/src/Log.cxx index 585e51f7c..65d3be824 100644 --- a/src/Log.cxx +++ b/src/Log.cxx @@ -20,12 +20,18 @@ #include "config.h" #include "LogV.hxx" #include "util/Error.hxx" +#include "util/Domain.hxx" + +#include #include #include #include #include +/** Domain for std::exception */ +static constexpr Domain exception_domain("exception"); + void LogFormatV(const Domain &domain, LogLevel level, const char *fmt, va_list ap) { @@ -88,6 +94,36 @@ FormatError(const Domain &domain, const char *fmt, ...) va_end(ap); } +void +LogError(const std::exception &e) +{ + Log(exception_domain, LogLevel::ERROR, e.what()); + + try { + std::rethrow_if_nested(e); + } catch (const std::exception &nested) { + LogError(nested, "nested"); + } catch (...) { + Log(exception_domain, LogLevel::ERROR, + "Unrecognized nested exception"); + } +} + +void +LogError(const std::exception &e, const char *msg) +{ + FormatError(exception_domain, "%s: %s", msg, e.what()); + + try { + std::rethrow_if_nested(e); + } catch (const std::exception &nested) { + LogError(nested); + } catch (...) { + Log(exception_domain, LogLevel::ERROR, + "Unrecognized nested exception"); + } +} + void LogError(const Error &error) { diff --git a/src/Log.hxx b/src/Log.hxx index 684d5c394..f6474ff1a 100644 --- a/src/Log.hxx +++ b/src/Log.hxx @@ -23,6 +23,10 @@ #include "LogLevel.hxx" #include "Compiler.h" +namespace std { + class exception; +} + class Error; class Domain; @@ -79,6 +83,12 @@ LogError(const Domain &domain, const char *msg) Log(domain, LogLevel::ERROR, msg); } +void +LogError(const std::exception &e); + +void +LogError(const std::exception &e, const char *msg); + gcc_printf(2,3) void FormatError(const Domain &domain, const char *fmt, ...);