60 lines
1.7 KiB
C++
60 lines
1.7 KiB
C++
|
#include "loggger.h"
|
||
|
|
||
|
constexpr const std::array<std::string_view, static_cast<int>(Logger::Severity::fatal) + 1> logSettings({
|
||
|
/*debug*/ "\e[90m",
|
||
|
/*info*/ "\e[32m",
|
||
|
/*minor*/ "\e[34m",
|
||
|
/*major*/ "\e[94m",
|
||
|
/*warning*/ "\e[33m",
|
||
|
/*error*/ "\e[31m",
|
||
|
/*fatal*/ "\e[91m"
|
||
|
});
|
||
|
|
||
|
constexpr const std::array<std::string_view, static_cast<int>(Logger::Severity::fatal) + 1> logHeaders({
|
||
|
/*debug*/ "DEBUG: ",
|
||
|
/*info*/ "INFO: ",
|
||
|
/*minor*/ "MINOR: ",
|
||
|
/*major*/ "MAJOR: ",
|
||
|
/*warning*/ "WARNING: ",
|
||
|
/*error*/ "ERROR: ",
|
||
|
/*fatal*/ "FATAL: "
|
||
|
});
|
||
|
|
||
|
constexpr const std::string_view bold("\e[1m");
|
||
|
constexpr const std::string_view regular("\e[22m");
|
||
|
constexpr const std::string_view clearStyle("\e[0m");
|
||
|
|
||
|
Logger::Logger(Logger::Severity severity):
|
||
|
currentSeverity(severity),
|
||
|
history(),
|
||
|
readMutex(),
|
||
|
writeMutex()
|
||
|
{}
|
||
|
|
||
|
Logger::~Logger()
|
||
|
{}
|
||
|
|
||
|
void Logger::printLog(bool colored) {
|
||
|
std::cout << std::endl;
|
||
|
for (const Message& message : history) {
|
||
|
if (colored) {
|
||
|
int severity = static_cast<int>(message.first);
|
||
|
std::cout << logSettings[severity] << bold << logHeaders[severity] << regular;
|
||
|
}
|
||
|
std::cout << message.second << std::endl;
|
||
|
}
|
||
|
if (colored)
|
||
|
std::cout << clearStyle << std::flush;
|
||
|
}
|
||
|
|
||
|
void Logger::log(Logger::Severity severity, const std::string& comment) const {
|
||
|
std::scoped_lock lock(readMutex, writeMutex);
|
||
|
if (severity >= currentSeverity)
|
||
|
history.emplace_back(severity, comment);
|
||
|
}
|
||
|
|
||
|
std::list<std::pair<Logger::Severity, std::string>> Logger::getLog() const {
|
||
|
std::scoped_lock lock(readMutex);
|
||
|
return history;
|
||
|
}
|