Log: C++ exception support

This commit is contained in:
Max Kellermann 2015-12-15 23:10:26 +01:00
parent 16218c8680
commit 55f95b3ac9
2 changed files with 46 additions and 0 deletions

View File

@ -20,12 +20,18 @@
#include "config.h" #include "config.h"
#include "LogV.hxx" #include "LogV.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx"
#include <exception>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
/** Domain for std::exception */
static constexpr Domain exception_domain("exception");
void void
LogFormatV(const Domain &domain, LogLevel level, const char *fmt, va_list ap) 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); 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 void
LogError(const Error &error) LogError(const Error &error)
{ {

View File

@ -23,6 +23,10 @@
#include "LogLevel.hxx" #include "LogLevel.hxx"
#include "Compiler.h" #include "Compiler.h"
namespace std {
class exception;
}
class Error; class Error;
class Domain; class Domain;
@ -79,6 +83,12 @@ LogError(const Domain &domain, const char *msg)
Log(domain, LogLevel::ERROR, 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) gcc_printf(2,3)
void void
FormatError(const Domain &domain, const char *fmt, ...); FormatError(const Domain &domain, const char *fmt, ...);