Rewrote logging class/interface bullshit. Anyway it took a while and there's a lot of cool shit.

This commit is contained in:
2024-08-09 16:01:48 -04:00
parent 404c3dceba
commit c17a34f0f1
3 changed files with 259 additions and 2 deletions

View File

@@ -8,6 +8,7 @@
#pragma once
#include <string>
#include <map>
#include <Event.h>
#include <mcolor.h>
#include "token.hpp"
@@ -19,6 +20,7 @@ namespace jlog {
/// @see LOG_LEVEL macro
enum class severity : uint8_t {
none, // Show no output
info, // show only info messages
fatal, // Show only fatal errors
error, // Show fatal and not-necessarily-fatal errors
warning, // Show warnings additionally
@@ -34,18 +36,65 @@ namespace jlog {
/// @note Does not append a newline to the message.
void log_to_file(const std::string &filename, const std::string &message);
class Logger {
class LoggerInterface {
public:
//Logger(const std::string& context, const mcolor::ansiColors::Colors&);
virtual void log(const std::string &message) = 0;
virtual void log_trace(const std::string &message, const std::source_location& location = std::source_location::current()) = 0;
};
class ManagerInterface : public LoggerInterface {
public:
virtual std::string Context() = 0;
virtual void SetParent(ManagerInterface* p) = 0;
virtual ManagerInterface Parent() = 0;
virtual void AddChild(ManagerInterface* c) = 0;
virtual void DelChild(const std::string& context) = 0;
virtual std::map<std::string, ManagerInterface*> Children() = 0;
};
class Logger {
public:
Logger(const std::string& context);
public:
virtual void Log(const std::string &message);
//virtual void Log(const std::string& context, const std::string &message);
virtual void LogTrace(const std::string &message, const std::source_location& location = std::source_location::current());
public:
virtual void Enabled(bool state);
virtual bool Enabled();
virtual void LogFile(const std::string& f);
virtual std::string LogFile();
// no cc no bullshit
virtual void ColorCode(mcolor::ansiColors::Colors cc = mcolor::ansiColors::Colors::FG_DEFAULT);
virtual mcolor::ansiColors::Colors ColorCode();
public:
virtual std::string Context();
// This whole parent child system is probably overkill for our use cases.
/**/
virtual void SetParent(Logger* p);
virtual Logger* Parent();
virtual void AddChild(Logger* c);
virtual void DelChild(const std::string& context);
virtual bool HasChild(const std::string& context);
virtual bool HasChild(Logger* c);
virtual Logger* GetChild(const std::string& context);
virtual std::map<std::string, Logger*> Children();
/**/
private:
bool enabled = true;
std::string logfile = "latest.log";
mcolor::ansiColors::Colors colorcode = mcolor::ansiColors::Colors::FG_DEFAULT;
private:
std::string context;
Logger* parent;
std::map<std::string, Logger*> children;
};
}
namespace jlog::GlobalLogger {
class GlobalLogger : jlog::Logger {
class GlobalLogger : jlog::LoggerInterface {
public:
GlobalLogger(const std::string& context, const mcolor::ansiColors::Colors& colorcode);
void log(const std::string &message);

View File

@@ -5,6 +5,68 @@
// Contributors: william@redacted.cc maxi@redacted.cc
// This work is dedicated to the public domain.
#include <jlog/jlog.hpp>
#include "map"
/*
namespace CoolProject {}
namespace CoolProject::Logger {
jlog::severity LEVEL = jlog::severity::debug;
class Logger : jlog::Logger {
public:
Logger(const std::string& context, const mcolor::ansiColors::Colors& colorcode, const jlog::severity& level);
void log(const std::string &message);
void log_trace(const std::string &message, const std::source_location& location = std::source_location::current());
//bool Enabled() { return enabled; };
std::string Context() { return context; };
jlog::severity Severity() { return severity; };
public:
bool enabled = true;
//bool record_console = true;
//bool record_file = true;
// Changing this is bad practice, but we're allowing it for niche cases
std::string logfile = "latest.log";
private:
std::string context;
mcolor::ansiColors::Colors colorcode;
jlog::severity severity;
};
Logger INFO{"INFO", mcolor::ansiColors::Colors::FG_GREEN, jlog::severity::info};
Logger WARNING{"WARNING", mcolor::ansiColors::Colors::FG_YELLOW, jlog::severity::warning};
Logger ERROR{"ERROR", mcolor::ansiColors::Colors::FG_RED, jlog::severity::error};
Logger FATAL{"FATAL", mcolor::ansiColors::Colors::FG_BRIGHT_RED, jlog::severity::fatal};
Logger VERBOSE{"VERBOSE", mcolor::ansiColors::Colors::FG_CYAN, jlog::severity::verbose};
Logger DEBUG{"DEBUG", mcolor::ansiColors::Colors::FG_MAGENTA, jlog::severity::debug};
Logger::Logger(const std::string &context, const mcolor::ansiColors::Colors& colorcode, const jlog::severity& level) {
this->context = context;
this->colorcode = colorcode;
this->severity = level;
}
void Logger::log(const std::string &message) {
if ((!enabled) || (severity > LEVEL))
return;
std::vector<jlog::token> fmt = jlog::log_format(this->context, message, this->colorcode);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
}
void Logger::log_trace(const std::string &message, const std::source_location &location) {
if ((!enabled) || (severity > LEVEL))
return;
//std::vector<jlog::token> fmt = log_format(this->context, message, func, file, line,this->colorcode);
std::vector<jlog::token> fmt = jlog::log_format(this->context, message, location.function_name(), location.file_name(), location.line(),this->colorcode);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
}
}
*/
int main()
{
@@ -22,6 +84,36 @@ int main()
jlog::GlobalLogger::ERROR.log_trace("This dick");
jlog::GlobalLogger::FATAL.log_trace("This dick");
//struct { std::string ass; std::string balls; } dick{"cum","cock"};
//jlog::GlobalLogger::DEBUG.log_trace(dick.ass + dick.balls);
/*
CoolProject::Logger::INFO.log_trace("This BALLS");
CoolProject::Logger::INFO.log_trace("This dick");
CoolProject::Logger::DEBUG.log_trace("This dick");
CoolProject::Logger::VERBOSE.log_trace("This dick");
CoolProject::Logger::WARNING.log_trace("This dick");
CoolProject::Logger::ERROR.log_trace("This dick");
CoolProject::Logger::FATAL.log_trace("This dick");
*/
jlog::Logger cock{"COCK"};
//jlog::Logger info{"I"};
jlog::Logger error{"E"};
//cock.AddChild(&info);
cock.AddChild(&error);
cock.Log("dick ass");
//cock.log("I" "dick ass");
error.Log("dick ass");
error.LogTrace("dick ass" " " + error.Parent()->Context());
cock.DelChild("E");
error.LogTrace("dick ass" " " + error.Parent()->Context());
return 0;
}

View File

@@ -28,6 +28,122 @@ namespace jlog
latest_log << message;
latest_log.close();
}
Logger::Logger(const std::string& context) {
this->context = context;
this->parent = this;
this->children = {};
}
void Logger::Log(const std::string &message) {
if (!enabled)
return;
std::vector<jlog::token> fmt = log_format(this->context, message, this->colorcode);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
}
/*
void Logger::Log(const std::string& context, const std::string &message) {
Logger* c = GetChild(context);
c->Log(message);
}
*/
void Logger::LogTrace(const std::string &message, const std::source_location &location) {
if (!enabled)
return;
//std::vector<jlog::token> fmt = log_format(this->context, message, func, file, line,this->colorcode);
std::vector<jlog::token> fmt = log_format(this->context, message, location.function_name(), location.file_name(), location.line(),this->colorcode);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
}
void Logger::Enabled(bool state) {
this->enabled = state;
for (auto const& [_, logger] : this->children) {
logger->Enabled(state);
}
}
bool Logger::Enabled() { return this->enabled; }
void Logger::LogFile(const std::string& f) {
this->logfile = f;
for (auto const& [_, logger] : this->children) {
logger->LogFile(f);
}
}
std::string Logger::LogFile() { return this->logfile; }
void Logger::ColorCode(mcolor::ansiColors::Colors cc) { this->colorcode = cc; }
mcolor::ansiColors::Colors Logger::ColorCode() { return this->colorcode; }
std::string Logger::Context() { return this->context; }
// DOnt be stupid
void Logger::SetParent(Logger *p) {
//this->parent->DelChild(this->context);
this->parent = p;
// We make the child inherit the parents traits.
// Is this strictly required? No.
this->Enabled(p->Enabled());
this->LogFile(p->LogFile());
//p->AddChild(this);
}
Logger* Logger::Parent() { return this->parent; }
void Logger::AddChild(Logger *c) {
c->SetParent(this);
this->children[c->Context()] = c;
}
// Child will still keep whatever it previously inherited from the parent
// C'mon you mean to tell me when your parent disowns you ya gotta give back all
// the shit you inherited from them? I guess it would be pretty cool to give back
// debt.
void Logger::DelChild(const std::string& context) {
auto c = this->children.find(context);
if (c != this->children.end()) {
this->children.erase(context);
c->second->SetParent(c->second);
} else {
throw ("Child does not exist.");
}
}
bool Logger::HasChild(const std::string& context) {
auto c = this->children.find(context);
return c != this->children.end();
}
bool Logger::HasChild(Logger* c) {
auto result = std::find_if(
this->children.begin(), this->children.end(),
[c](const auto& mo) {return mo.second == c; });
return result != this->children.end();
}
Logger* Logger::GetChild(const std::string& context) {
if (!HasChild(context)) {
throw ("Child does not exist.");
}
// Yes yes I know we're accessing the map twice making it slower
// so fuck off this is more flexible.
auto c = this->children.find(context);
return c->second;
}
std::map<std::string, Logger*> Logger::Children() {
return this->children;
}
}
namespace jlog::GlobalLogger {