Potato Engine
Loading...
Searching...
No Matches
Debug.cpp
Go to the documentation of this file.
1
2
3#include <chrono>
4#include <iomanip>
5
6#include "fmt/chrono.h"
7#include "Debug.hpp"
8
10
11static const std::unordered_map<LogType, std::string> LogColorMap = {
12 {LogType::INFO, "\e[0;37m"},
13 {LogType::WARNING, "\e[0;33m"},
14 {LogType::ERROR, "\e[0;31m"},
15 {LogType::VITAL, "\e[1;35m"},
16 {LogType::DEBUG, "\e[0;34m"}
17};
18
19#define X(name) { LogType::name, std::string(#name) },
20static const std::unordered_map<LogType, std::string> LogNameMap = {
21 logtypes
22};
23#undef X
24
25void Logger::init(const char* path){
26 LogFile.open(path, std::ios::app);
27 LogFile << "\n\n\n"; // delimit on every binding
28}
29
30std::string getTimestampUTC() {
31 using namespace std::chrono;
32
33 auto now = system_clock::now();
34 auto s = time_point_cast<seconds>(now);
35 auto ms = duration_cast<milliseconds>( now - s ).count();
36
37 // threadsafe (linux specific)
38 std::time_t t = system_clock::to_time_t(now);
39 std::tm tm{};
40 gmtime_r(&t, &tm);
41
42 std::string timestamp = fmt::format("{:%H:%M:%S}.{:03}", tm, ms);
43
44 return timestamp;
45}
46
47bool Logger::operator () (LogType type, const std::string& message) {
48 std::string timestamp = getTimestampUTC(); // minimize latency
49
50 if (!LogFile.is_open()) { return false; }
51
52 std::string colorMod = LogColorMap.at(type);
53
54 LogFile << timestamp << " - ";
55 LogFile << colorMod << LogNameMap.at(type) << "\e[0m";
56 LogFile << " | ";
57 LogFile << colorMod << message << "\e[0m" << std::endl;
58
59 return true;
60}
61
62Logger::~Logger() {
63 if (LogFile.is_open()) {
64 LogFile.close();
65 }
66}
Logger LOG_DEFAULT
Default log object.
Definition Debug.cpp:9
Logging functionality.
Definition Debug.hpp:22