-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cpp
47 lines (40 loc) · 1 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
#include "Logger.h"
Logger::Logger() {
openLogFile("logs.txt");
}
Logger::~Logger() {
closeLogFile();
}
void Logger::logMsg(std::string_view message, const char* file, int line, const char* function)
{
std::string timestamp = getTimestamp();
std::cout << "[" << timestamp << "] " << message << std::endl;
if (fileStream.is_open()) {
fileStream << "[" << timestamp << "] | "
<< "File: "
<< file << "("
<< line << ") `"
<< function << "`: "
<< message
<< std::endl;
fileStream.flush();
}
}
void Logger::openLogFile(const std::string& filename) {
if (fileStream.is_open()) {
fileStream.close();
}
fileStream.open(filename, std::ios::out | std::ios::app);
}
void Logger::closeLogFile() {
if (fileStream.is_open()) {
fileStream.close();
}
}
std::string Logger::getTimestamp() {
auto now = std::chrono::system_clock::now();
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
std::string timestamp = std::ctime(¤tTime);
timestamp.pop_back();
return timestamp;
}