Log: add Log() and LogFormat() overloads with std::exception_ptr

Make LogError()/FormatError() wrappers for those.  Now we can log
exceptions with a lower level.
This commit is contained in:
Max Kellermann 2019-05-23 12:23:28 +02:00
parent 36e6079c57
commit e7c5a42821
2 changed files with 68 additions and 27 deletions

View File

@ -91,19 +91,19 @@ FormatError(const Domain &domain, const char *fmt, ...) noexcept
} }
void void
LogError(const std::exception &e) noexcept Log(LogLevel level, const std::exception &e) noexcept
{ {
LogError(exception_domain, GetFullMessage(e).c_str()); Log(level, exception_domain, GetFullMessage(e).c_str());
} }
void void
LogError(const std::exception &e, const char *msg) noexcept Log(LogLevel level, const std::exception &e, const char *msg) noexcept
{ {
FormatError(exception_domain, "%s: %s", msg, GetFullMessage(e).c_str()); LogFormat(level, exception_domain, "%s: %s", msg, GetFullMessage(e).c_str());
} }
void void
FormatError(const std::exception &e, const char *fmt, ...) noexcept LogFormat(LogLevel level, const std::exception &e, const char *fmt, ...) noexcept
{ {
char msg[1024]; char msg[1024];
va_list ap; va_list ap;
@ -111,24 +111,24 @@ FormatError(const std::exception &e, const char *fmt, ...) noexcept
vsnprintf(msg, sizeof(msg), fmt, ap); vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap); va_end(ap);
LogError(e, msg); Log(level, e, msg);
} }
void void
LogError(const std::exception_ptr &ep) noexcept Log(LogLevel level, const std::exception_ptr &ep) noexcept
{ {
LogError(exception_domain, GetFullMessage(ep).c_str()); Log(level, exception_domain, GetFullMessage(ep).c_str());
} }
void void
LogError(const std::exception_ptr &ep, const char *msg) noexcept Log(LogLevel level, const std::exception_ptr &ep, const char *msg) noexcept
{ {
FormatError(exception_domain, "%s: %s", msg, LogFormat(level, exception_domain, "%s: %s", msg,
GetFullMessage(ep).c_str()); GetFullMessage(ep).c_str());
} }
void void
FormatError(const std::exception_ptr &ep, const char *fmt, ...) noexcept LogFormat(LogLevel level, const std::exception_ptr &ep, const char *fmt, ...) noexcept
{ {
char msg[1024]; char msg[1024];
va_list ap; va_list ap;
@ -136,7 +136,7 @@ FormatError(const std::exception_ptr &ep, const char *fmt, ...) noexcept
vsnprintf(msg, sizeof(msg), fmt, ap); vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap); va_end(ap);
LogError(ep, msg); Log(level, ep, msg);
} }
void void

View File

@ -34,6 +34,28 @@ gcc_printf(3,4)
void void
LogFormat(LogLevel level, const Domain &domain, const char *fmt, ...) noexcept; LogFormat(LogLevel level, const Domain &domain, const char *fmt, ...) noexcept;
void
Log(LogLevel level, const std::exception &e) noexcept;
void
Log(LogLevel level, const std::exception &e, const char *msg) noexcept;
gcc_printf(3,4)
void
LogFormat(LogLevel level, const std::exception &e,
const char *fmt, ...) noexcept;
void
Log(LogLevel level, const std::exception_ptr &ep) noexcept;
void
Log(LogLevel level, const std::exception_ptr &ep, const char *msg) noexcept;
gcc_printf(3,4)
void
LogFormat(LogLevel level, const std::exception_ptr &ep,
const char *fmt, ...) noexcept;
static inline void static inline void
LogDebug(const Domain &domain, const char *msg) noexcept LogDebug(const Domain &domain, const char *msg) noexcept
{ {
@ -80,25 +102,44 @@ LogError(const Domain &domain, const char *msg) noexcept
Log(LogLevel::ERROR, domain, msg); Log(LogLevel::ERROR, domain, msg);
} }
void inline void
LogError(const std::exception &e) noexcept; LogError(const std::exception &e) noexcept
{
Log(LogLevel::ERROR, e);
}
void inline void
LogError(const std::exception &e, const char *msg) noexcept; LogError(const std::exception &e, const char *msg) noexcept
{
Log(LogLevel::ERROR, e, msg);
}
gcc_printf(2,3) template<typename... Args>
void inline void
FormatError(const std::exception &e, const char *fmt, ...) noexcept; FormatError(const std::exception &e, const char *fmt, Args&&... args) noexcept
{
LogFormat(LogLevel::ERROR, e, fmt, std::forward<Args>(args)...);
}
void inline void
LogError(const std::exception_ptr &ep) noexcept; LogError(const std::exception_ptr &ep) noexcept
{
Log(LogLevel::ERROR, ep);
}
void inline void
LogError(const std::exception_ptr &ep, const char *msg) noexcept; LogError(const std::exception_ptr &ep, const char *msg) noexcept
{
Log(LogLevel::ERROR, ep, msg);
}
gcc_printf(2,3) template<typename... Args>
void inline void
FormatError(const std::exception_ptr &ep, const char *fmt, ...) noexcept; FormatError(const std::exception_ptr &ep,
const char *fmt, Args&&... args) noexcept
{
LogFormat(LogLevel::ERROR, ep, fmt, std::forward<Args>(args)...);
}
gcc_printf(2,3) gcc_printf(2,3)
void void