New code style refactor

This commit is contained in:
Jack Andersen 2018-12-07 19:17:15 -10:00
parent 1b6c2ae715
commit 01e291833b
2 changed files with 511 additions and 608 deletions

View File

@ -15,8 +15,7 @@
extern "C" void logvisorBp(); extern "C" void logvisorBp();
namespace logvisor namespace logvisor {
{
void logvisorAbort(); void logvisorAbort();
@ -30,30 +29,24 @@ extern bool XtermColor;
/** /**
* @brief Severity level for log messages * @brief Severity level for log messages
*/ */
enum Level 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 */ Fatal /**< Non-recoverable error message (throws exception) */
Fatal /**< Non-recoverable error message (throws exception) */
}; };
/** /**
* @brief Backend interface for receiving app-wide log events * @brief Backend interface for receiving app-wide log events
*/ */
struct ILogger struct ILogger {
{ virtual ~ILogger() {}
virtual ~ILogger() {} virtual void report(const char* modName, Level severity, const char* format, va_list ap) = 0;
virtual void report(const char* modName, Level severity, virtual void report(const char* modName, Level severity, const wchar_t* format, va_list ap) = 0;
const char* format, va_list ap)=0; virtual void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, const char* format,
virtual void report(const char* modName, Level severity, va_list ap) = 0;
const wchar_t* format, va_list ap)=0; virtual void reportSource(const char* modName, Level severity, const char* file, unsigned linenum,
virtual void reportSource(const char* modName, Level severity, const wchar_t* format, va_list ap) = 0;
const char* file, unsigned linenum,
const char* format, va_list ap)=0;
virtual void reportSource(const char* modName, Level severity,
const char* file, unsigned linenum,
const wchar_t* format, va_list ap)=0;
}; };
/** /**
@ -97,18 +90,16 @@ extern std::atomic_uint_fast64_t FrameIndex;
* *
* Ensures logging streams aren't written concurrently * Ensures logging streams aren't written concurrently
*/ */
struct LogMutex struct LogMutex {
{ bool enabled = true;
bool enabled = true; std::recursive_mutex mutex;
std::recursive_mutex mutex; ~LogMutex() { enabled = false; }
~LogMutex() { enabled = false; } std::unique_lock<std::recursive_mutex> lock() {
std::unique_lock<std::recursive_mutex> lock() if (enabled)
{ return std::unique_lock<std::recursive_mutex>(mutex);
if (enabled) else
return std::unique_lock<std::recursive_mutex>(mutex); return std::unique_lock<std::recursive_mutex>();
else }
return std::unique_lock<std::recursive_mutex>();
}
}; };
extern LogMutex _LogMutex; extern LogMutex _LogMutex;
@ -116,10 +107,7 @@ extern LogMutex _LogMutex;
* @brief Take a centralized lock for the logging output stream(s) * @brief Take a centralized lock for the logging output stream(s)
* @return RAII mutex lock * @return RAII mutex lock
*/ */
static inline std::unique_lock<std::recursive_mutex> LockLog() static inline std::unique_lock<std::recursive_mutex> LockLog() { return _LogMutex.lock(); }
{
return _LogMutex.lock();
}
extern uint64_t _LogCounter; extern uint64_t _LogCounter;
@ -127,15 +115,12 @@ extern uint64_t _LogCounter;
* @brief Get current count of logging events * @brief Get current count of logging events
* @return Log Count * @return Log Count
*/ */
static inline uint64_t GetLogCounter() static inline uint64_t GetLogCounter() { return _LogCounter; }
{
return _LogCounter;
}
/** /**
* @brief Restore centralized logger vector to default state (silent operation) * @brief Restore centralized logger vector to default state (silent operation)
*/ */
static inline void UnregisterLoggers() {MainLoggers.clear();} static inline void UnregisterLoggers() { MainLoggers.clear(); }
/** /**
* @brief Construct and register a real-time console logger singleton * @brief Construct and register a real-time console logger singleton
@ -180,89 +165,82 @@ void RegisterFileLogger(const wchar_t* filepath);
/** /**
* @brief This is constructed per-subsystem in a locally centralized fashon * @brief This is constructed per-subsystem in a locally centralized fashon
*/ */
class Module class Module {
{ const char* m_modName;
const char* m_modName;
public: public:
Module(const char* modName) : m_modName(modName) {} Module(const char* modName) : m_modName(modName) {}
/** /**
* @brief Route new log message to centralized ILogger * @brief Route new log message to centralized ILogger
* @param severity Level of log report severity * @param severity Level of log report severity
* @param format Standard printf-style format string * @param format Standard printf-style format string
*/ */
template <typename CharType> template <typename CharType>
inline void report(Level severity, const CharType* format, ...) inline void report(Level severity, const CharType* format, ...) {
{ if (MainLoggers.empty() && severity != Level::Fatal)
if (MainLoggers.empty() && severity != Level::Fatal) return;
return; va_list ap;
va_list ap; va_start(ap, format);
va_start(ap, format); report(severity, format, ap);
report(severity, format, ap); va_end(ap);
va_end(ap); }
template <typename CharType>
inline void report(Level severity, const CharType* format, va_list ap) {
auto lk = LockLog();
++_LogCounter;
if (severity == Fatal)
RegisterConsoleLogger();
for (auto& logger : MainLoggers) {
va_list apc;
va_copy(apc, ap);
logger->report(m_modName, severity, format, apc);
va_end(apc);
}
if (severity == Error || severity == Fatal)
logvisorBp();
if (severity == Fatal)
logvisorAbort();
else if (severity == Error)
++ErrorCount;
}
/**
* @brief Route new log message with source info to centralized ILogger
* @param severity Level of log report severity
* @param file Source file name from __FILE__ macro
* @param linenum Source line number from __LINE__ macro
* @param format Standard printf-style format string
*/
template <typename CharType>
inline void reportSource(Level severity, const char* file, unsigned linenum, const CharType* format, ...) {
if (MainLoggers.empty() && severity != Level::Fatal)
return;
va_list ap;
va_start(ap, format);
reportSource(severity, file, linenum, format, ap);
va_end(ap);
}
template <typename CharType>
inline void reportSource(Level severity, const char* file, unsigned linenum, const CharType* format, va_list ap) {
auto lk = LockLog();
++_LogCounter;
if (severity == Fatal)
RegisterConsoleLogger();
for (auto& logger : MainLoggers) {
va_list apc;
va_copy(apc, ap);
logger->reportSource(m_modName, severity, file, linenum, format, apc);
va_end(apc);
} }
template <typename CharType> if (severity == Fatal)
inline void report(Level severity, const CharType* format, va_list ap) logvisorAbort();
{ else if (severity == Error)
auto lk = LockLog(); ++ErrorCount;
++_LogCounter; }
if (severity == Fatal)
RegisterConsoleLogger();
for (auto& logger : MainLoggers)
{
va_list apc;
va_copy(apc, ap);
logger->report(m_modName, severity, format, apc);
va_end(apc);
}
if (severity == Error || severity == Fatal)
logvisorBp();
if (severity == Fatal)
logvisorAbort();
else if (severity == Error)
++ErrorCount;
}
/**
* @brief Route new log message with source info to centralized ILogger
* @param severity Level of log report severity
* @param file Source file name from __FILE__ macro
* @param linenum Source line number from __LINE__ macro
* @param format Standard printf-style format string
*/
template <typename CharType>
inline void reportSource(Level severity, const char* file, unsigned linenum, const CharType* format, ...)
{
if (MainLoggers.empty() && severity != Level::Fatal)
return;
va_list ap;
va_start(ap, format);
reportSource(severity, file, linenum, format, ap);
va_end(ap);
}
template <typename CharType>
inline void reportSource(Level severity, const char* file, unsigned linenum, const CharType* format, va_list ap)
{
auto lk = LockLog();
++_LogCounter;
if (severity == Fatal)
RegisterConsoleLogger();
for (auto& logger : MainLoggers)
{
va_list apc;
va_copy(apc, ap);
logger->reportSource(m_modName, severity, file, linenum, format, apc);
va_end(apc);
}
if (severity == Fatal)
logvisorAbort();
else if (severity == Error)
++ErrorCount;
}
}; };
} } // namespace logvisor

File diff suppressed because it is too large Load Diff