mirror of https://github.com/AxioDL/logvisor.git
New code style refactor
This commit is contained in:
parent
1b6c2ae715
commit
01e291833b
|
@ -15,8 +15,7 @@
|
|||
|
||||
extern "C" void logvisorBp();
|
||||
|
||||
namespace logvisor
|
||||
{
|
||||
namespace logvisor {
|
||||
|
||||
void logvisorAbort();
|
||||
|
||||
|
@ -30,30 +29,24 @@ extern bool XtermColor;
|
|||
/**
|
||||
* @brief Severity level for log messages
|
||||
*/
|
||||
enum Level
|
||||
{
|
||||
Info, /**< Non-error informative message */
|
||||
Warning, /**< Non-error warning message */
|
||||
Error, /**< Recoverable error message */
|
||||
Fatal /**< Non-recoverable error message (throws exception) */
|
||||
enum Level {
|
||||
Info, /**< Non-error informative message */
|
||||
Warning, /**< Non-error warning message */
|
||||
Error, /**< Recoverable error message */
|
||||
Fatal /**< Non-recoverable error message (throws exception) */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Backend interface for receiving app-wide log events
|
||||
*/
|
||||
struct 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,
|
||||
const wchar_t* format, va_list ap)=0;
|
||||
virtual void reportSource(const char* modName, Level severity,
|
||||
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;
|
||||
struct 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, const wchar_t* format, va_list ap) = 0;
|
||||
virtual void reportSource(const char* modName, Level severity, 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
|
||||
*/
|
||||
struct LogMutex
|
||||
{
|
||||
bool enabled = true;
|
||||
std::recursive_mutex mutex;
|
||||
~LogMutex() { enabled = false; }
|
||||
std::unique_lock<std::recursive_mutex> lock()
|
||||
{
|
||||
if (enabled)
|
||||
return std::unique_lock<std::recursive_mutex>(mutex);
|
||||
else
|
||||
return std::unique_lock<std::recursive_mutex>();
|
||||
}
|
||||
struct LogMutex {
|
||||
bool enabled = true;
|
||||
std::recursive_mutex mutex;
|
||||
~LogMutex() { enabled = false; }
|
||||
std::unique_lock<std::recursive_mutex> lock() {
|
||||
if (enabled)
|
||||
return std::unique_lock<std::recursive_mutex>(mutex);
|
||||
else
|
||||
return std::unique_lock<std::recursive_mutex>();
|
||||
}
|
||||
};
|
||||
extern LogMutex _LogMutex;
|
||||
|
||||
|
@ -116,10 +107,7 @@ extern LogMutex _LogMutex;
|
|||
* @brief Take a centralized lock for the logging output stream(s)
|
||||
* @return RAII mutex lock
|
||||
*/
|
||||
static inline std::unique_lock<std::recursive_mutex> LockLog()
|
||||
{
|
||||
return _LogMutex.lock();
|
||||
}
|
||||
static inline std::unique_lock<std::recursive_mutex> LockLog() { return _LogMutex.lock(); }
|
||||
|
||||
extern uint64_t _LogCounter;
|
||||
|
||||
|
@ -127,15 +115,12 @@ extern uint64_t _LogCounter;
|
|||
* @brief Get current count of logging events
|
||||
* @return Log Count
|
||||
*/
|
||||
static inline uint64_t GetLogCounter()
|
||||
{
|
||||
return _LogCounter;
|
||||
}
|
||||
static inline uint64_t GetLogCounter() { return _LogCounter; }
|
||||
|
||||
/**
|
||||
* @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
|
||||
|
@ -180,89 +165,82 @@ void RegisterFileLogger(const wchar_t* filepath);
|
|||
/**
|
||||
* @brief This is constructed per-subsystem in a locally centralized fashon
|
||||
*/
|
||||
class Module
|
||||
{
|
||||
const char* m_modName;
|
||||
class Module {
|
||||
const char* m_modName;
|
||||
|
||||
public:
|
||||
Module(const char* modName) : m_modName(modName) {}
|
||||
Module(const char* modName) : m_modName(modName) {}
|
||||
|
||||
/**
|
||||
* @brief Route new log message to centralized ILogger
|
||||
* @param severity Level of log report severity
|
||||
* @param format Standard printf-style format string
|
||||
*/
|
||||
template <typename CharType>
|
||||
inline void report(Level severity, const CharType* format, ...)
|
||||
{
|
||||
if (MainLoggers.empty() && severity != Level::Fatal)
|
||||
return;
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
report(severity, format, ap);
|
||||
va_end(ap);
|
||||
/**
|
||||
* @brief Route new log message to centralized ILogger
|
||||
* @param severity Level of log report severity
|
||||
* @param format Standard printf-style format string
|
||||
*/
|
||||
template <typename CharType>
|
||||
inline void report(Level severity, const CharType* format, ...) {
|
||||
if (MainLoggers.empty() && severity != Level::Fatal)
|
||||
return;
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
report(severity, format, 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>
|
||||
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);
|
||||
}
|
||||
|
||||
if (severity == Fatal)
|
||||
logvisorAbort();
|
||||
else if (severity == Error)
|
||||
++ErrorCount;
|
||||
}
|
||||
if (severity == Fatal)
|
||||
logvisorAbort();
|
||||
else if (severity == Error)
|
||||
++ErrorCount;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
} // namespace logvisor
|
||||
|
|
897
lib/logvisor.cpp
897
lib/logvisor.cpp
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue