Smoothing it out

This commit is contained in:
2024-08-13 13:40:20 -04:00
parent 96b759d54e
commit 168af78441
2 changed files with 13 additions and 38 deletions

View File

@@ -13,12 +13,12 @@ namespace jlog
public: public:
explicit Logger(const std::string& context, const AnsiColor& color = AnsiColor::FG_DEFAULT); explicit Logger(const std::string& context, const AnsiColor& color = AnsiColor::FG_DEFAULT);
public: public:
void operator () (const std::string& message); void operator () (const std::string& message, const std::source_location& location = std::source_location::current());
virtual void Log(const std::string &message); virtual void Log(const std::string &message, const std::source_location& location = std::source_location::current());
//virtual void LogTrace(const std::vector<token> tokens); //virtual void LogTrace(const std::vector<token> tokens);
//virtual void LogTrace(const std::string& context, const std::string &message); //virtual void LogTrace(const std::string& context, const std::string &message);
virtual void LogTrace(const std::string &message, const std::source_location& location = std::source_location::current()); //virtual void LogTrace(const std::string &message, const std::source_location& location = std::source_location::current());
public: public:
void SetEnabled(bool state); void SetEnabled(bool state);
void Enable(); void Enable();
@@ -38,8 +38,6 @@ namespace jlog
std::string logfile = "latest.log"; std::string logfile = "latest.log";
AnsiColor colorcode = AnsiColor::FG_DEFAULT; AnsiColor colorcode = AnsiColor::FG_DEFAULT;
std::string context; std::string context;
Logger* parent;
std::map<std::string, Logger*> children;
bool trace = true; bool trace = true;
}; };
} }

View File

@@ -8,54 +8,31 @@ namespace jlog
{ {
Logger::Logger(const std::string& context, const AnsiColor& color) { Logger::Logger(const std::string& context, const AnsiColor& color) {
this->context = context; this->context = context;
this->parent = this;
this->colorcode = color; this->colorcode = color;
} }
void Logger::Log(const std::string &message) { void Logger::Log(const std::string &message, const std::source_location& location) {
if (!enabled) if (!enabled)
return; return;
std::vector<jlog::token> fmt = log_format(this->context, message, this->colorcode); std::vector<jlog::token> fmt;
if (this == this->parent) { if (trace)
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n'); fmt = log_format(this->context, message, location.function_name(), location.file_name(), location.line(),this->colorcode);
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n'); else
} else { fmt = log_format(this->context, message, this->colorcode);
// Using toks2consoleMsg is hacky, but it's a simple solution and it's generic enough
this->parent->Log(jlog::toks2consoleMsg(fmt));
}
}
void Logger::LogTrace(const std::string &message, const std::source_location& location) { jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
if (!enabled) jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
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);
if (this == this->parent) {
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
} else {
// Using toks2consoleMsg is hacky, but it's a simple solution and it's generic enough
this->parent->Log(jlog::toks2consoleMsg(fmt));
}
} }
void Logger::SetEnabled(bool state) { void Logger::SetEnabled(bool state) {
this->enabled = state; this->enabled = state;
for (auto const& [_, logger] : this->children) {
logger->SetEnabled(state);
}
} }
bool Logger::Enabled() { return this->enabled; } bool Logger::Enabled() { return this->enabled; }
void Logger::LogFile(const std::string& f) { void Logger::LogFile(const std::string& f) {
this->logfile = f; this->logfile = f;
for (auto const& [_, logger] : this->children) {
logger->LogFile(f);
}
} }
std::string Logger::LogFile() { return this->logfile; } std::string Logger::LogFile() { return this->logfile; }
@@ -64,8 +41,8 @@ namespace jlog
std::string Logger::Context() { return this->context; } std::string Logger::Context() { return this->context; }
void Logger::operator()(const std::string &message) { void Logger::operator()(const std::string &message, const std::source_location& location) {
Log(message); Log(message, location);
} }
void Logger::SetTraceback(bool enabled) { void Logger::SetTraceback(bool enabled) {