diff --git a/include/jlog/logger.hpp b/include/jlog/logger.hpp index 2bcb740..3e27045 100644 --- a/include/jlog/logger.hpp +++ b/include/jlog/logger.hpp @@ -13,12 +13,12 @@ namespace jlog public: explicit Logger(const std::string& context, const AnsiColor& color = AnsiColor::FG_DEFAULT); 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 tokens); //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: void SetEnabled(bool state); void Enable(); @@ -38,8 +38,6 @@ namespace jlog std::string logfile = "latest.log"; AnsiColor colorcode = AnsiColor::FG_DEFAULT; std::string context; - Logger* parent; - std::map children; bool trace = true; }; } \ No newline at end of file diff --git a/src/jlog/logger.cpp b/src/jlog/logger.cpp index bd18520..c7f16bb 100644 --- a/src/jlog/logger.cpp +++ b/src/jlog/logger.cpp @@ -8,54 +8,31 @@ namespace jlog { Logger::Logger(const std::string& context, const AnsiColor& color) { this->context = context; - this->parent = this; this->colorcode = color; } - void Logger::Log(const std::string &message) { + void Logger::Log(const std::string &message, const std::source_location& location) { if (!enabled) return; - std::vector fmt = log_format(this->context, message, this->colorcode); + std::vector fmt; - 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)); - } - } + if (trace) + fmt = log_format(this->context, message, location.function_name(), location.file_name(), location.line(),this->colorcode); + else + fmt = log_format(this->context, message, this->colorcode); - void Logger::LogTrace(const std::string &message, const std::source_location& location) { - if (!enabled) - return; - - //std::vector fmt = log_format(this->context, message, func, file, line,this->colorcode); - std::vector 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)); - } + jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n'); + jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n'); } void Logger::SetEnabled(bool state) { this->enabled = state; - for (auto const& [_, logger] : this->children) { - logger->SetEnabled(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; } @@ -64,8 +41,8 @@ namespace jlog std::string Logger::Context() { return this->context; } - void Logger::operator()(const std::string &message) { - Log(message); + void Logger::operator()(const std::string &message, const std::source_location& location) { + Log(message, location); } void Logger::SetTraceback(bool enabled) {