Browse Source

Log format errors as critical and call app_fatal to terminate

pull/1673/head
Jonathan Mercier-Ganady 5 years ago committed by GitHub
parent
commit
4274cc6f1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 50
      Source/utils/log.hpp

50
Source/utils/log.hpp

@ -10,6 +10,9 @@
namespace devilution {
// Local definition to fix compilation issue due to header conflict.
void app_fatal(const char *pszFmt, ...);
enum class LogCategory {
Application = SDL_LOG_CATEGORY_APPLICATION,
Error = SDL_LOG_CATEGORY_ERROR,
@ -25,25 +28,44 @@ enum class LogCategory {
constexpr auto defaultCategory = LogCategory::Application;
enum class LogPriority {
Verbose = SDL_LOG_PRIORITY_VERBOSE,
Debug = SDL_LOG_PRIORITY_DEBUG,
Info = SDL_LOG_PRIORITY_INFO,
Warn = SDL_LOG_PRIORITY_WARN,
Error = SDL_LOG_PRIORITY_ERROR,
Critical = SDL_LOG_PRIORITY_CRITICAL,
Verbose = SDL_LOG_PRIORITY_VERBOSE,
Debug = SDL_LOG_PRIORITY_DEBUG,
Info = SDL_LOG_PRIORITY_INFO,
Warn = SDL_LOG_PRIORITY_WARN,
Error = SDL_LOG_PRIORITY_ERROR,
Critical = SDL_LOG_PRIORITY_CRITICAL,
};
namespace detail {
template <typename... Args>
std::string format(const char *fmt, Args &&... args)
{
FMT_TRY
{
return fmt::format(fmt, std::forward<Args>(args)...);
}
FMT_CATCH(const fmt::format_error &e)
{
auto error = fmt::format("Format error, fmt: {}, error: {}", fmt ? fmt : "nullptr", e.what());
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", error.c_str());
app_fatal("%s", error.c_str());
}
}
} // namespace detail
template <typename... Args>
void Log(const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_Log("%s", str.c_str());
}
template <typename... Args>
void LogVerbose(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogVerbose(static_cast<int>(category), "%s", str.c_str());
}
@ -56,7 +78,7 @@ void LogVerbose(const char *fmt, Args &&... args)
template <typename... Args>
void LogDebug(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogDebug(static_cast<int>(category), "%s", str.c_str());
}
@ -69,7 +91,7 @@ void LogDebug(const char *fmt, Args &&... args)
template <typename... Args>
void LogInfo(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogInfo(static_cast<int>(category), "%s", str.c_str());
}
@ -82,7 +104,7 @@ void LogInfo(const char *fmt, Args &&... args)
template <typename... Args>
void LogWarn(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogWarn(static_cast<int>(category), "%s", str.c_str());
}
@ -95,7 +117,7 @@ void LogWarn(const char *fmt, Args &&... args)
template <typename... Args>
void LogError(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogError(static_cast<int>(category), "%s", str.c_str());
}
@ -108,7 +130,7 @@ void LogError(const char *fmt, Args &&... args)
template <typename... Args>
void LogCritical(LogCategory category, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogCritical(static_cast<int>(category), "%s", str.c_str());
}
@ -121,7 +143,7 @@ void LogCritical(const char *fmt, Args &&... args)
template <typename... Args>
void LogMessageV(LogCategory category, LogPriority priority, const char *fmt, Args &&... args)
{
auto str = fmt::format(fmt, std::forward<Args>(args)...);
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogMessageV(static_cast<int>(category), static_cast<SDL_LogPriority>(priority), "%s", str.c_str());
}

Loading…
Cancel
Save