added FatalException for FatalError reports

This commit is contained in:
Jack Andersen 2015-07-05 15:34:08 -10:00
parent 47a7a95098
commit c3dc632dd0
1 changed files with 30 additions and 5 deletions

View File

@ -6,6 +6,8 @@
#include <stdio.h> #include <stdio.h>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <locale>
#include <codecvt>
namespace LogVisor namespace LogVisor
{ {
@ -14,6 +16,29 @@ namespace LogVisor
#define LOG_UCS2 1 #define LOG_UCS2 1
#endif #endif
/**
* @brief Exception thrown when FatalError is issued
*/
class FatalException : public std::exception
{
std::string m_what;
public:
FatalException(const char* format, va_list ap)
{
char buf[1024];
vsnprintf(buf, 1024, format, ap);
m_what.assign(buf);
}
FatalException(const wchar_t* format, va_list ap)
{
wchar_t buf[1024];
vswprintf(buf, 1024, format, ap);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
m_what.assign(conv.to_bytes(buf));
}
inline const char* what() const noexcept {return m_what.c_str();}
};
/** /**
* @brief Severity level for log messages * @brief Severity level for log messages
*/ */
@ -22,7 +47,7 @@ enum Level
Info, /**< Non-error informative message */ Info, /**< Non-error informative message */
Warning, /**< Non-error warning message */ Warning, /**< Non-error warning message */
Error, /**< Recoverable error message */ Error, /**< Recoverable error message */
FatalError /**< Non-recoverable error message (calls abort) */ FatalError /**< Non-recoverable error message (throws exception) */
}; };
/** /**
@ -119,9 +144,9 @@ public:
va_start(ap, format); va_start(ap, format);
for (auto& logger : MainLoggers) for (auto& logger : MainLoggers)
logger->report(m_modName, severity, format, ap); logger->report(m_modName, severity, format, ap);
va_end(ap);
if (severity == FatalError) if (severity == FatalError)
abort(); throw FatalException(format, ap);
va_end(ap);
} }
/** /**
@ -138,9 +163,9 @@ public:
va_start(ap, format); va_start(ap, format);
for (auto& logger : MainLoggers) for (auto& logger : MainLoggers)
logger->reportSource(m_modName, severity, file, linenum, format, ap); logger->reportSource(m_modName, severity, file, linenum, format, ap);
va_end(ap);
if (severity == FatalError) if (severity == FatalError)
abort(); throw FatalException(format, ap);
va_end(ap);
} }
}; };