-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.cpp
51 lines (48 loc) · 1.55 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
//
// Created by Alexandr on 2/7/2023.
//
#include <sstream>
#include <chrono>
#include <iomanip>
#include "logger.hpp"
std::array<std::string, sizeof(logger::log_level)+1u> logger::level_string = {
"TRACE", "DEBUG", "INFO", "WARNING", "ERROR"
};
void logger::log(logger::log_level level, const char* source, const std::string& message)
{
std::stringstream formatted;
formatted << "[" << level_string[level] << "]";
std::time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm tm = *std::gmtime(&tt); //GMT (UTC)
formatted << " - " << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
#ifdef DEBUG
formatted << " - " << source;
#endif
formatted << " - " << message;
std::cout << formatted.str() << std::endl;
}
void logger::log(logger::log_level level, const char* source, const std::wstring& message)
{
std::wstringstream formatted;
formatted << L"[" << level_string[level].c_str() << L"]";
std::time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm tm = *std::gmtime(&tt); //GMT (UTC)
formatted << " - " << std::put_time(&tm, L"%Y-%m-%d %H:%M:%S");
#ifdef DEBUG
formatted << " - " << source;
#endif
formatted << " - " << message;
std::wcout << formatted.str() << std::endl;
}
void logger::log(const char* source, const std::string& message)
{
log(log_level::DEBUG_LEVEL, source, message);
}
void logger::log(const char* source, const std::wstring& message)
{
log(log_level::DEBUG_LEVEL, source, message);
}
void logger::simple_out(const std::string& message)
{
std::cout << message << std::endl;
}