-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
executable file
·41 lines (32 loc) · 1.06 KB
/
logger.cpp
File metadata and controls
executable file
·41 lines (32 loc) · 1.06 KB
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
#include "logger.h"
using namespace std;
Logger *Logger::instance_ = nullptr;
Logger::Logger() : console_output_(true), start_time_(chrono::steady_clock::now()) {}
Logger::~Logger() {
if (file_.is_open()) {
file_.close();
}
}
Logger &Logger::getInstance() {
if (instance_ == nullptr) {
instance_ = new Logger();
}
return *instance_;
}
string Logger::getTimestamp() {
const auto wall_time = chrono::system_clock::now();
const auto wall_time_t = chrono::system_clock::to_time_t(wall_time);
stringstream ss;
// Format: [Wall Clock Time] (Monotonic Time)
ss << put_time(localtime(&wall_time_t), "%Y-%m-%d %H:%M:%S");
return ss.str();
}
string Logger::getLevelString(const LogType level) {
switch (level) {
case DEBUG: return "\033[36mDEBUG\033[0m"; // Cyan text for DEBUG
case INFO: return "INFO";
case WARNING: return "\033[33mWARNING\033[0m"; // Yellow text for WARNING
case ERROR: return "\033[31mERROR\033[0m"; // Red text for ERROR
default: return "UNKNOWN";
}
}