From 1ae346304ece18b0857fa597b74e50581dba5bda Mon Sep 17 00:00:00 2001 From: josh Date: Mon, 12 Aug 2024 14:08:15 -0400 Subject: [PATCH] Stylistic adjustments --- README.md | 2 +- include/jlog/formatter.hpp | 24 ++++----- include/jlog/jlog.hpp | 60 ++++++--------------- main.cpp | 104 +++++-------------------------------- src/jlog/jlog.cpp | 33 ++---------- 5 files changed, 46 insertions(+), 177 deletions(-) diff --git a/README.md b/README.md index 2830bce..ebfac00 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Using jlog is straightforward: #include int main() { - LOGLEVEL(jlog::severity::debug); // <- see jlog::severity for full list of log levels + LOGLEVEL(jlog::severity::debug); // <- see jlog::severity for full list of Log levels INFO("This is barely useful information."); DEBUG("Debugging Information"); diff --git a/include/jlog/formatter.hpp b/include/jlog/formatter.hpp index e094347..dad70f4 100644 --- a/include/jlog/formatter.hpp +++ b/include/jlog/formatter.hpp @@ -38,11 +38,11 @@ namespace jlog { int line, const mcolor::ansiColors::Colors &severity_cc = mcolor::ansiColors::Colors::FG_WHITE); - /// Returns a token sequence pre-formatted for the INFO log level. + /// Returns a token sequence pre-formatted for the INFO Log level. /// @param message The message to send out. std::vector info_format(const std::string &message); - /// Returns a token sequence pre-formatted for the INFO log level. + /// Returns a token sequence pre-formatted for the INFO Log level. /// @param message The message to send out. /// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant. /// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant. @@ -53,11 +53,11 @@ namespace jlog { const std::string &file, int line); - /// Returns a token sequence pre-formatted for the WARNING log level. + /// Returns a token sequence pre-formatted for the WARNING Log level. /// @param message The message to send out. std::vector warning_format(const std::string &message); - /// Returns a token sequence pre-formatted for the WARNING log level. + /// Returns a token sequence pre-formatted for the WARNING Log level. /// @param message The message to send out. /// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant. /// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant. @@ -68,11 +68,11 @@ namespace jlog { const std::string &file, int line); - /// Returns a token sequence pre-formatted for the ERROR log level. + /// Returns a token sequence pre-formatted for the ERROR Log level. /// @param message The message to send out. std::vector error_format(const std::string &message); - /// Returns a token sequence pre-formatted for the ERROR log level. + /// Returns a token sequence pre-formatted for the ERROR Log level. /// @param message The message to send out. /// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant. /// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant. @@ -83,11 +83,11 @@ namespace jlog { const std::string &file, int line); - /// Returns a token sequence pre-formatted for the FATAL log level. + /// Returns a token sequence pre-formatted for the FATAL Log level. /// @param message The message to send out. std::vector fatal_format(const std::string &message); - /// Returns a token sequence pre-formatted for the FATAL log level. + /// Returns a token sequence pre-formatted for the FATAL Log level. /// @param message The message to send out. /// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant. /// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant. @@ -98,11 +98,11 @@ namespace jlog { const std::string &file, int line); - /// Returns a token sequence pre-formatted for the VERBOSE log level. + /// Returns a token sequence pre-formatted for the VERBOSE Log level. /// @param message The message to send out. std::vector verbose_format(const std::string &message); - /// Returns a token sequence pre-formatted for the VERBOSE log level. + /// Returns a token sequence pre-formatted for the VERBOSE Log level. /// @param message The message to send out. /// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant. /// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant. @@ -113,11 +113,11 @@ namespace jlog { const std::string &file, int line); - /// Returns a token sequence pre-formatted for the DEBUG log level. + /// Returns a token sequence pre-formatted for the DEBUG Log level. /// @param message The message to send out. std::vector debug_format(const std::string &message); - /// Returns a token sequence pre-formatted for the DEBUG log level. + /// Returns a token sequence pre-formatted for the DEBUG Log level. /// @param message The message to send out. /// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant. /// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant. diff --git a/include/jlog/jlog.hpp b/include/jlog/jlog.hpp index 8983acd..6cf2a8b 100644 --- a/include/jlog/jlog.hpp +++ b/include/jlog/jlog.hpp @@ -36,28 +36,13 @@ namespace jlog { /// @note Does not append a newline to the message. void log_to_file(const std::string &filename, const std::string &message); - 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 Children() = 0; - }; class Logger { public: - Logger(const std::string& context); + Logger(const std::string& context, const mcolor::ansiColors::Colors& color = mcolor::ansiColors::Colors::FG_DEFAULT); public: + void operator () (const std::string& message); + virtual void Log(const std::string &message); //virtual void Log(const std::vector tokens); //virtual void Log(const std::string& context, const std::string &message); @@ -82,41 +67,28 @@ namespace jlog { virtual bool HasChild(Logger* c); virtual Logger* GetChild(const std::string& context); virtual std::map Children(); + void SetTraceback(bool enabled) + { + trace = enabled; + } /**/ - private: + protected: 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 children; + bool trace = true; }; } -namespace jlog::GlobalLogger { - class GlobalLogger : jlog::LoggerInterface { - public: - GlobalLogger(const std::string& context, const mcolor::ansiColors::Colors& colorcode); - void log(const std::string &message); - void log_trace(const std::string &message, const std::source_location& location = std::source_location::current()); - std::string Context() { return context; }; - 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; +namespace jlog { - }; - - GlobalLogger INFO{"INFO", mcolor::ansiColors::Colors::FG_GREEN}; - GlobalLogger WARNING{"WARNING", mcolor::ansiColors::Colors::FG_YELLOW}; - GlobalLogger ERROR{"ERROR", mcolor::ansiColors::Colors::FG_RED}; - GlobalLogger FATAL{"FATAL", mcolor::ansiColors::Colors::FG_BRIGHT_RED}; - GlobalLogger VERBOSE{"VERBOSE", mcolor::ansiColors::Colors::FG_CYAN}; - GlobalLogger DEBUG{"DEBUG", mcolor::ansiColors::Colors::FG_MAGENTA}; + Logger INFO{"INFO", mcolor::ansiColors::Colors::FG_GREEN}; + Logger WARNING{"WARNING", mcolor::ansiColors::Colors::FG_YELLOW}; + Logger ERROR{"ERROR", mcolor::ansiColors::Colors::FG_RED}; + Logger FATAL{"FATAL", mcolor::ansiColors::Colors::FG_BRIGHT_RED}; + Logger VERBOSE{"VERBOSE", mcolor::ansiColors::Colors::FG_CYAN}; + Logger DEBUG{"DEBUG", mcolor::ansiColors::Colors::FG_MAGENTA}; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 6875642..1a8a1b7 100644 --- a/main.cpp +++ b/main.cpp @@ -7,96 +7,23 @@ #include #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 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 fmt = log_format(this->context, message, func, file, line,this->colorcode); - std::vector 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() { - jlog::GlobalLogger::INFO.log("This dick"); - jlog::GlobalLogger::DEBUG.log("This dick"); - jlog::GlobalLogger::VERBOSE.log("This dick"); - jlog::GlobalLogger::WARNING.log("This dick"); - jlog::GlobalLogger::ERROR.log("This dick"); - jlog::GlobalLogger::FATAL.log("This dick"); + jlog::INFO("This dick"); + jlog::DEBUG("This dick"); + jlog::VERBOSE.Log("This dick"); + jlog::WARNING.Log("This dick"); + jlog::ERROR.Log("This dick"); + jlog::FATAL.Log("This dick"); - jlog::GlobalLogger::INFO.log_trace("This dick"); - jlog::GlobalLogger::DEBUG.log_trace("This dick"); - jlog::GlobalLogger::VERBOSE.log_trace("This dick"); - jlog::GlobalLogger::WARNING.log_trace("This dick"); - jlog::GlobalLogger::ERROR.log_trace("This dick"); - jlog::GlobalLogger::FATAL.log_trace("This dick"); + jlog::INFO("This dick"); + jlog::DEBUG.LogTrace("This dick"); + jlog::VERBOSE.LogTrace("This dick"); + jlog::WARNING.LogTrace("This dick"); + jlog::ERROR.LogTrace("This dick"); + jlog::FATAL.LogTrace("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"}; @@ -106,15 +33,10 @@ int main() cock.AddChild(&error); cock.Log("dick ass"); - //cock.log("I" "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; } diff --git a/src/jlog/jlog.cpp b/src/jlog/jlog.cpp index 52b50b5..a0488ae 100644 --- a/src/jlog/jlog.cpp +++ b/src/jlog/jlog.cpp @@ -29,10 +29,11 @@ namespace jlog latest_log.close(); } - Logger::Logger(const std::string& context) { + Logger::Logger(const std::string& context, const mcolor::ansiColors::Colors& color) { this->context = context; this->parent = this; this->children = {}; + this->colorcode = color; } void Logger::Log(const std::string &message) { @@ -155,34 +156,8 @@ namespace jlog return this->children; } -} - -namespace jlog::GlobalLogger { - GlobalLogger::GlobalLogger(const std::string &context, const mcolor::ansiColors::Colors& colorcode) { - this->context = context; - this->colorcode = colorcode; - } - - - void GlobalLogger::log(const std::string &message) { - if (!enabled) - return; - - std::vector 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 GlobalLogger::log_trace(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); - - jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n'); - jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n'); + void Logger::operator()(const std::string &message) { + Log(message); } }