-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogger.cpp
63 lines (50 loc) · 1.57 KB
/
Logger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "Logger.h"
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string_view>
Logger::LogLevel Logger::m_minLogLevel = Logger::LogLevel::Debug;
void Logger::log(const std::string& message, const LogLevel logLevel)
{
using namespace std::literals;
if (logLevel < m_minLogLevel)
{
return;
}
std::string_view strLogLevel = "UNKNOWN"sv;
switch (logLevel)
{
case LogLevel::Debug:
strLogLevel = "DEBUG"sv;
break;
case LogLevel::Info:
strLogLevel = "INFO"sv;
break;
case LogLevel::Warning:
strLogLevel = "WARNING"sv;
break;
case LogLevel::Error:
strLogLevel = "ERROR"sv;
break;
case LogLevel::Critical:
strLogLevel = "CRITICAL"sv;
break;
}
auto& stream = logLevel >= LogLevel::Warning ? std::cerr : std::cout;
const auto now = std::chrono::system_clock::now();
const unsigned milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() % 1000;
const std::time_t tNow = std::chrono::system_clock::to_time_t(now);
tm tmNow = {};
#if defined(_MSC_VER) || defined(__MINGW32__)
gmtime_s(&tmNow, &tNow);
#else
gmtime_r(&tNow, &tmNow);
#endif
static std::mutex protect;
std::lock_guard lock(protect);
stream
<< std::put_time(&tmNow, "%Y-%m-%d %H:%M:%S") << '.' << std::setw(3) << std::setfill('0') << std::right << milliseconds
<< ' ' << std::setw(8) << std::setfill(' ') << std::left << strLogLevel
<< ' ' << message
<< std::endl;
}