From e58c98e224afcf4a28078344e0cd5f8c0e10961f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 21 Oct 2024 11:17:23 +0200 Subject: add logging example + finish log color config integration --- src/crepe/api/Config.h | 13 +++++++++++++ src/crepe/util/color.cpp | 13 ++++++++++++- src/crepe/util/color.h | 5 +++++ src/crepe/util/log.cpp | 25 ++++++++++++++----------- src/crepe/util/log.h | 12 ++++-------- 5 files changed, 48 insertions(+), 20 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index d6ee8e8..f47f45b 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -11,14 +11,27 @@ public: ~Config() = default; public: + //! Retrieve handle to global Config instance static Config & get_instance() { static Config instance; return instance; } public: + //! Logging-related settings struct { + /** + * \brief Log level + * + * Only messages with equal or higher priority than this value will be + * logged. + */ util::log_level level = util::log_level::INFO; + /** + * \brief Colored log output + * + * Enables log coloring using ANSI escape codes. + */ bool color = true; } log; }; diff --git a/src/crepe/util/color.cpp b/src/crepe/util/color.cpp index 3fec157..bdf5809 100644 --- a/src/crepe/util/color.cpp +++ b/src/crepe/util/color.cpp @@ -1,4 +1,7 @@ +#include + #include "color.h" +#include "fmt.h" #include "../api/Config.h" using namespace crepe::util; @@ -16,10 +19,18 @@ const string LogColor::str(const string & content) { } const char * LogColor::c_str(const char * content) { - this->final = this->str(content); + this->final = this->str(content == NULL ? "" : content); return this->final.c_str(); } +const char * LogColor::fmt(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + string content = va_stringf(args, fmt); + va_end(args); + return this->c_str(content.c_str()); +} + LogColor & LogColor::add_code(unsigned int code) { this->code += stringf("\e[%dm", code); return *this; diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h index 0b7c1e6..66f3a28 100644 --- a/src/crepe/util/color.h +++ b/src/crepe/util/color.h @@ -9,10 +9,15 @@ public: LogColor() = default; public: + //! get color code as c-style string (or color content string) const char * c_str(const char * content = NULL); + //! color printf-style format string + const char * fmt(const char * fmt, ...); + //! get color code as stl string (or color content string) const std::string str(const std::string & content = ""); public: + //! reset color to default foreground and background color LogColor & reset(); public: diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp index e3e9f1f..a2672a3 100644 --- a/src/crepe/util/log.cpp +++ b/src/crepe/util/log.cpp @@ -8,21 +8,24 @@ #include "../api/Config.h" using namespace crepe::util; +using namespace std; + +string log_prefix(log_level level) { + switch (level) { + case log_level::TRACE: return LogColor().fg_white().str("[TRACE]") + " "; + case log_level::DEBUG: return LogColor().fg_magenta().str("[DEBUG]") + " "; + case log_level::INFO: return LogColor().fg_blue().str("[INFO]") + " "; + case log_level::WARNING: return LogColor().fg_yellow().str("[WARN]") + " "; + case log_level::ERROR: return LogColor().fg_red().str("[ERROR]") + " "; + } + return ""; +} -static const char * const LOG_PREFIX[] = { - [log_level::TRACE] = "[TRACE] ", - [log_level::DEBUG] = "[DEBUG] ", - [log_level::INFO] = "[INFO] ", - [log_level::WARNING] = "[WARN] ", - [log_level::ERROR] = "[ERROR] ", -}; - -static void log(enum log_level level, const std::string msg) { +static void log(enum log_level level, const string msg) { auto & cfg = crepe::api::Config::get_instance(); if (level < cfg.log.level) return; - using namespace std; - string final = string(LOG_PREFIX[level]) + msg; + string final = log_prefix(level) + msg; if (!final.ends_with("\n")) final += "\n"; // TODO: also log to file or smth diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index c5d8665..bcc06a5 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -6,17 +6,13 @@ #include "color.h" // utility macros -#define _crepe_logf_here(lvl, fmt, ...) \ - crepe::util::logf(lvl, "%s%s (%s:%d)%s" fmt "\n", \ - crepe::util::color::FG_WHITE, __PRETTY_FUNCTION__, \ - __FILE_NAME__, __LINE__, crepe::util::color::RESET, \ - __VA_ARGS__) +#define _crepe_logf_here(level, format, ...) crepe::util::logf(level, "%s" format, crepe::util::LogColor().fg_white(false).fmt("%s (%s:%d)", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__), __VA_ARGS__) // very illegal global function-style macros // NOLINTBEGIN -#define dbg_logf(fmt, ...) _crepe_logf_here(util::log_level::DEBUG, ": " fmt, __VA_ARGS__) -#define dbg_log(str) _crepe_logf_here(util::log_level::DEBUG, "%s: " str, "") -#define dbg_trace() _crepe_logf_here(util::log_level::TRACE, "%s", "") +#define dbg_logf(fmt, ...) _crepe_logf_here(crepe::util::log_level::DEBUG, ": " fmt, __VA_ARGS__) +#define dbg_log(str) _crepe_logf_here(crepe::util::log_level::DEBUG, "%s: " str, "") +#define dbg_trace() _crepe_logf_here(crepe::util::log_level::TRACE, "%s", "") // NOLINTEND #endif -- cgit v1.2.3