diff options
author | Max Kellermann <max@duempel.org> | 2015-12-15 23:10:26 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-12-16 00:07:51 +0100 |
commit | 55f95b3ac955dbd944ae430523e1f7f372a485ec (patch) | |
tree | 10edf0963331617d54f023397b198b42f6857d0a /src/Log.cxx | |
parent | 16218c8680e6b810c655231474a1593f9fc4654d (diff) |
Log: C++ exception support
Diffstat (limited to 'src/Log.cxx')
-rw-r--r-- | src/Log.cxx | 36 |
1 files changed, 36 insertions, 0 deletions
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 <exception> #include <assert.h> #include <stdio.h> #include <string.h> #include <errno.h> +/** Domain for std::exception */ +static constexpr Domain exception_domain("exception"); + void LogFormatV(const Domain &domain, LogLevel level, const char *fmt, va_list ap) { @@ -89,6 +95,36 @@ FormatError(const Domain &domain, const char *fmt, ...) } 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) { Log(error.GetDomain(), LogLevel::ERROR, error.GetMessage()); |