WoW Model Viewer
Your premiere tool for viewing, equipping and animating World of Warcraft models.
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1#include "Logger.h"
2
3#include <windows.h>
4#ifdef min
5 #undef min
6#endif
7#include <chrono>
8#include <iomanip>
9#include <sstream>
10
11using namespace WMVLog;
12
13Logger* Logger::m_instance = nullptr;
14
19
21{
22}
23
24void Logger::dispatchLog(int type, const std::string& msg)
25{
26 const std::string message = Logger::formatLog(type, msg);
27 for (const auto it : *this)
28 it->write(message);
29}
30
31std::string Logger::formatLog(int type, const std::string& msg)
32{
33 std::string msgType;
34 switch (type)
35 {
36 case INFO_LOG:
37 msgType = "INFO";
38 break;
39 case WARNING_LOG:
40 msgType = "WARN";
41 break;
42 case ERROR_LOG:
43 msgType = "ERROR";
44 break;
45 case FATAL_LOG:
46 msgType = "FATAL";
47 break;
48 default: ;
49 }
50
51 const auto now = std::chrono::system_clock::now();
52 const auto t = std::chrono::system_clock::to_time_t(now);
53 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
54 std::tm tm_info{};
55 localtime_s(&tm_info, &t);
56 std::ostringstream ts;
57 ts << std::put_time(&tm_info, "%Y-%m-%d %H:%M:%S")
58 << "." << std::setfill('0') << std::setw(3) << ms.count();
59
60 return msgType + "\t| " + ts.str() + "\t| " + msg;
61}
62
64{
65 return LogStream(*this, static_cast<int>(type));
66}
67
68// --- LogStream implementation ---
69
70LogStream::LogStream(Logger& logger, int type)
71 : m_logger(&logger), m_type(type), m_active(true)
72{
73}
74
76 : m_logger(other.m_logger), m_type(other.m_type),
77 m_stream(std::move(other.m_stream)), m_active(other.m_active)
78{
79 other.m_active = false;
80}
81
83{
84 if (m_active && m_logger)
85 {
87 }
88}
89
90LogStream& LogStream::operator<<(const std::wstring& value)
91{
92 if (m_active)
93 {
94 // Convert wstring to UTF-8 string
95 if (!value.empty())
96 {
97 int size_needed = WideCharToMultiByte(CP_UTF8, 0, value.c_str(), static_cast<int>(value.size()), nullptr, 0, nullptr, nullptr);
98 std::string utf8(size_needed, 0);
99 WideCharToMultiByte(CP_UTF8, 0, value.c_str(), static_cast<int>(value.size()), &utf8[0], size_needed, nullptr, nullptr);
100 m_stream << utf8;
101 }
102 }
103 return *this;
104}
105
106LogStream& LogStream::operator<<(const wchar_t* value)
107{
108 if (m_active && value)
109 {
110 return *this << std::wstring(value);
111 }
112 return *this;
113}
RAII stream object that collects log output and dispatches it on destruction.
Definition Logger.h:23
Logger * m_logger
Definition Logger.h:47
LogStream(Logger &logger, int type)
Definition Logger.cpp:70
LogStream & operator<<(const T &value)
Definition Logger.h:33
std::ostringstream m_stream
Definition Logger.h:49
Singleton logging facility that dispatches formatted messages to LogOutput sinks.
Definition Logger.h:58
static Logger * m_instance
Definition Logger.h:93
LogStream operator()(Logger::LogType type)
Create a LogStream for the given severity level.
Definition Logger.cpp:63
void dispatchLog(int type, const std::string &msg)
Send a pre-formatted message to all registered outputs.
Definition Logger.cpp:24
static std::string formatLog(int type, const std::string &msg)
Format a log message with a severity prefix and timestamp.
Definition Logger.cpp:31
static void init()
Initialise the logging subsystem (creates console and file outputs).
Definition Logger.cpp:20