changed logging concept, reworked task manager

This commit is contained in:
Blue 2023-10-08 20:29:40 -03:00
parent 6c7356598a
commit 0b268a7245
Signed by: blue
GPG key ID: 9B203B252A63EE38
19 changed files with 497 additions and 260 deletions

13
src/logger/CMakeLists.txt Normal file
View file

@ -0,0 +1,13 @@
set(SOURCES
logger.cpp
printer.cpp
accumulator.cpp
)
set(HEADERS
logger.h
printer.h
accumulator.h
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

View file

@ -0,0 +1,33 @@
#include "accumulator.h"
Accumulator::Accumulator(Severity severity):
severity(severity),
history()
{}
Logger::Severity Accumulator::getSeverity() const {
return severity;
}
void Accumulator::setSeverity(Severity severity) {
Accumulator::severity = severity;
}
void Accumulator::log(const std::list<Message>& comments, bool colored) const {
(void)(colored);
for (const Message& comment : comments)
if (comment.first >= Accumulator::severity)
history.emplace_back(comment);
}
void Accumulator::log(Severity severity, const std::string& comment, bool colored) const {
(void)(colored);
if (severity < Accumulator::severity)
return;
history.emplace_back(severity, comment);
}
std::list<Logger::Message> Accumulator::getHistory() const {
return history;
}

20
src/logger/accumulator.h Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include "logger.h"
class Accumulator : public Logger {
public:
Accumulator(Severity severity = Severity::info);
virtual void log(const std::list<Message>& comments, bool colored = true) const override;
virtual void log(Severity severity, const std::string& comment, bool colored = true) const override;
virtual Severity getSeverity() const override;
virtual void setSeverity(Severity severity) override;
std::list<Message> getHistory() const;
private:
Severity severity;
mutable std::list<Message> history;
};

53
src/logger/logger.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "logger.h"
#include <array>
#include <string_view>
#include <algorithm>
constexpr std::array<std::string_view, static_cast<int>(Logger::Severity::_sevetirySize)> levels({
"debug",
"info",
"minor",
"major",
"warning",
"error",
"fatal"
});
Logger::~Logger() {}
void Logger::debug(const std::string& comment, bool colored) const {
log(Severity::debug, comment, colored);
}
void Logger::info(const std::string& comment, bool colored) const {
log(Severity::info, comment, colored);
}
void Logger::minor(const std::string& comment, bool colored) const {
log(Severity::minor, comment, colored);
}
void Logger::major(const std::string& comment, bool colored) const {
log(Severity::major, comment, colored);
}
void Logger::warn(const std::string& comment, bool colored) const {
log(Severity::warning, comment, colored);
}
void Logger::error(const std::string& comment, bool colored) const {
log(Severity::error, comment, colored);
}
void Logger::fatal(const std::string& comment, bool colored) const {
log(Severity::fatal, comment, colored);
}
Logger::Severity Logger::stringToSeverity(const std::string& line) {
unsigned char dist = std::distance(levels.begin(), std::find(levels.begin(), levels.end(), line));
if (dist < static_cast<unsigned char>(Severity::_sevetirySize))
return static_cast<Severity>(dist);
return Severity::_sevetirySize;
}

36
src/logger/logger.h Normal file
View file

@ -0,0 +1,36 @@
#pragma once
#include <list>
#include <string>
class Logger {
public:
enum class Severity {
debug,
info,
minor,
major,
warning,
error,
fatal,
_sevetirySize
};
using Message = std::pair<Severity, std::string>;
void debug(const std::string& comment, bool colored = true) const;
void info(const std::string& comment, bool colored = true) const;
void minor(const std::string& comment, bool colored = true) const;
void major(const std::string& comment, bool colored = true) const;
void warn(const std::string& comment, bool colored = true) const;
void error(const std::string& comment, bool colored = true) const;
void fatal(const std::string& comment, bool colored = true) const;
virtual ~Logger();
virtual void log(const std::list<Message>& comments, bool colored = true) const = 0;
virtual void log(Severity severity, const std::string& comment, bool colored = true) const = 0;
virtual void setSeverity(Severity severity) = 0;
virtual Severity getSeverity() const = 0;
static Severity stringToSeverity(const std::string& line);
};

135
src/logger/printer.cpp Normal file
View file

@ -0,0 +1,135 @@
#include "printer.h"
#include <array>
#include <string_view>
#include <iostream>
constexpr const std::array<std::string_view, static_cast<int>(Logger::Severity::_sevetirySize)> 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::_sevetirySize)> 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");
constexpr const std::string_view clearLine("\e[2K\r");
Printer::Printer(Severity severity):
severity(severity),
mutex(),
status(std::nullopt)
{}
Logger::Severity Printer::getSeverity() const {
return severity;
}
void Printer::setSeverity(Severity severity) {
Printer::severity = severity;
}
void Printer::setStatusMessage(const std::string& message) {
if (status.has_value())
std::cout << clearLine;
status = message;
std::cout << message << std::flush;
}
void Printer::clearStatusMessage() {
if (status.has_value()) {
std::cout << clearLine << std::flush;
status = std::nullopt;
}
}
void Printer::printNested(
const std::string& header,
const std::vector<std::string>& lines,
const std::list<Message>& comments,
const std::optional<std::string>& status
) {
std::lock_guard lock(mutex);
if (comments.size() > 0) {
if (status.has_value())
std::cout << clearLine;
std::cout << bold << header << clearStyle << "\n";
for (const std::string& line : lines)
std::cout << line << "\n";
for (const Message& msg : comments) {
if (msg.first >= severity) {
std::cout << '\t';
printMessage(msg, true);
}
}
}
if (status.has_value())
Printer::status = status;
finishPrint(true);
}
void Printer::log(const std::list<Message>& comments, bool colored) const {
std::lock_guard lock(mutex);
bool changed = false;
for (const Message& msg : comments) {
if (msg.first >= severity) {
if (!changed && status.has_value())
std::cout << clearLine;
printMessage(msg, colored);
changed = true;
}
}
finishPrint(colored);
}
void Printer::log(Severity severity, const std::string& comment, bool colored) const {
if (severity < Printer::severity)
return;
std::lock_guard lock(mutex);
if (status.has_value())
std::cout << clearLine;
printMessage({severity, comment}, colored);
finishPrint(colored);
}
void Printer::finishPrint(bool colored) const {
if (colored)
std::cout << clearStyle;
if (status.has_value())
std::cout << '\r' << status.value();
std::cout << std::flush;
}
void Printer::printMessage(const Message& message, bool colored) const {
if (colored) {
int severity = static_cast<int>(message.first);
std::cout << logSettings[severity] << bold << logHeaders[severity] << regular;
}
std::cout << message.second << '\n';
}

38
src/logger/printer.h Normal file
View file

@ -0,0 +1,38 @@
#pragma once
#include <atomic>
#include <mutex>
#include <string>
#include <optional>
#include <vector>
#include "logger.h"
class Printer : public Logger {
public:
Printer(Severity severity = Severity::info);
virtual void log(const std::list<Message>& comments, bool colored = true) const override;
virtual void log(Severity severity, const std::string& comment, bool colored = true) const override;
virtual void setSeverity(Severity severity) override;
virtual Severity getSeverity() const override;
void setStatusMessage(const std::string& message);
void clearStatusMessage();
void printNested(
const std::string& header,
const std::vector<std::string>& lines = {},
const std::list<Message>& comments = {},
const std::optional<std::string>& status = std::nullopt
);
private:
void printMessage(const Message& message, bool colored = false) const;
void finishPrint(bool colored = false) const;
private:
std::atomic<Severity> severity;
mutable std::mutex mutex;
std::optional<std::string> status;
};