-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
45 lines (37 loc) · 1.02 KB
/
Logger.cpp
File metadata and controls
45 lines (37 loc) · 1.02 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
42
43
44
45
#include "Logger.h"
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
Logger::Logger(const std::string& filename) {
logFile.open(filename, std::ios::app);
}
Logger::~Logger() {
if (logFile.is_open()) {
logFile.close();
}
}
void Logger::logInfo(const std::string& message) {
if (logFile.is_open()) {
logFile << "[INFO] " << getCurrentTimestamp() << " - " << message << std::endl;
}
}
void Logger::logError(const std::string& message) {
if (logFile.is_open()) {
logFile << "[ERROR] " << getCurrentTimestamp() << " - " << message << std::endl;
}
}
std::string Logger::getCurrentTimestamp() const {
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::tm tm_buf;
#ifdef _WIN32
localtime_s(&tm_buf, &now_c);
#else
localtime_r(&now_c, &tm_buf);
#endif
std::ostringstream oss;
oss << std::put_time(&tm_buf, "%Y-%m-%d %H:%M:%S");
return oss.str();
}