diff options
-rw-r--r-- | src/crepe/api/Config.h | 13 | ||||
-rw-r--r-- | src/crepe/util/color.cpp | 13 | ||||
-rw-r--r-- | src/crepe/util/color.h | 5 | ||||
-rw-r--r-- | src/crepe/util/log.cpp | 25 | ||||
-rw-r--r-- | src/crepe/util/log.h | 12 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/example/log.cpp | 25 | ||||
-rw-r--r-- | src/example/script.cpp | 2 |
8 files changed, 74 insertions, 22 deletions
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 <cstdarg> + #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 diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 6df4ce7..4ef1cd1 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -15,4 +15,5 @@ endfunction() add_example(audio_internal) add_example(components_internal) add_example(script) +add_example(log) diff --git a/src/example/log.cpp b/src/example/log.cpp new file mode 100644 index 0000000..2d693d1 --- /dev/null +++ b/src/example/log.cpp @@ -0,0 +1,25 @@ +/** \file + * + * Standalone example for usage of the logging functions + */ + +#include <crepe/util/log.h> +#include <crepe/api/Config.h> + +using namespace crepe; +using namespace crepe::util; + +int main() { + auto & cfg = api::Config::get_instance(); + // make sure all log messages get printed + cfg.log.level = util::log_level::TRACE; + + dbg_trace(); + dbg_logf("cfg.log.color is equal to %d", cfg.log.color); + logf(log_level::INFO, "info message!"); + logf(log_level::WARNING, "very scary warning"); + logf(log_level::ERROR, "fatal error!!!"); + + return 0; +} + diff --git a/src/example/script.cpp b/src/example/script.cpp index 91e4713..97e99b3 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -24,8 +24,6 @@ int main() { auto & cfg = api::Config::get_instance(); cfg.log.level = util::log_level::TRACE; - dbg_trace(); - auto obj = GameObject(0, "name", "tag", 0); obj.add_component<BehaviorScript>().set_script<MyScript>(); |