diff options
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 40 | ||||
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/util/color.cpp | 60 | ||||
-rw-r--r-- | src/crepe/util/color.h | 84 | ||||
-rw-r--r-- | src/crepe/util/log.cpp | 27 | ||||
-rw-r--r-- | src/crepe/util/log.h | 13 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/example/log.cpp | 25 | ||||
-rw-r--r-- | src/example/script.cpp | 4 |
10 files changed, 201 insertions, 55 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index e74bb41..f262019 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -16,6 +16,7 @@ target_sources(crepe PUBLIC target_sources(crepe PUBLIC FILE_SET HEADERS FILES # AudioSource.h BehaviorScript.h + Config.h Script.h GameObject.h GameObject.hpp diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h new file mode 100644 index 0000000..24b5529 --- /dev/null +++ b/src/crepe/api/Config.h @@ -0,0 +1,40 @@ +#pragma once + +#include "../util/log.h" + +namespace crepe::api { + +class Config { +private: + Config() = default; +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::LogLevel level = util::LogLevel::INFO; + /** + * \brief Colored log output + * + * Enables log coloring using ANSI escape codes. + */ + bool color = true; + } log; +}; + +} + diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index e2cffaf..bbeaad9 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -1,4 +1,5 @@ target_sources(crepe PUBLIC + color.cpp log.cpp fmt.cpp ) diff --git a/src/crepe/util/color.cpp b/src/crepe/util/color.cpp new file mode 100644 index 0000000..bdf5809 --- /dev/null +++ b/src/crepe/util/color.cpp @@ -0,0 +1,60 @@ +#include <cstdarg> + +#include "color.h" +#include "fmt.h" +#include "../api/Config.h" + +using namespace crepe::util; +using namespace std; + +static constexpr const char * RESET_CODE = "\e[0m"; + +const string LogColor::str(const string & content) { + auto & cfg = api::Config::get_instance(); + string out = content; + if (cfg.log.color) out = this->code + out; + if (content.size() == 0) return out; + if (cfg.log.color) out = out + RESET_CODE; + return out; +} + +const char * LogColor::c_str(const char * 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; +} + +LogColor & LogColor::reset() { + this->code = RESET_CODE; + return *this; +} + +LogColor & LogColor::fg_black(bool bright) { return this->add_code(bright ? 90 : 30); } +LogColor & LogColor::fg_red(bool bright) { return this->add_code(bright ? 91 : 31); } +LogColor & LogColor::fg_green(bool bright) { return this->add_code(bright ? 92 : 32); } +LogColor & LogColor::fg_yellow(bool bright) { return this->add_code(bright ? 93 : 33); } +LogColor & LogColor::fg_blue(bool bright) { return this->add_code(bright ? 94 : 34); } +LogColor & LogColor::fg_magenta(bool bright) { return this->add_code(bright ? 95 : 35); } +LogColor & LogColor::fg_cyan(bool bright) { return this->add_code(bright ? 96 : 36); } +LogColor & LogColor::fg_white(bool bright) { return this->add_code(bright ? 97 : 37); } +LogColor & LogColor::bg_black(bool bright) { return this->add_code(bright ? 100 : 40); } +LogColor & LogColor::bg_red(bool bright) { return this->add_code(bright ? 101 : 41); } +LogColor & LogColor::bg_green(bool bright) { return this->add_code(bright ? 102 : 42); } +LogColor & LogColor::bg_yellow(bool bright) { return this->add_code(bright ? 103 : 43); } +LogColor & LogColor::bg_blue(bool bright) { return this->add_code(bright ? 104 : 44); } +LogColor & LogColor::bg_magenta(bool bright) { return this->add_code(bright ? 105 : 45); } +LogColor & LogColor::bg_cyan(bool bright) { return this->add_code(bright ? 106 : 46); } +LogColor & LogColor::bg_white(bool bright) { return this->add_code(bright ? 107 : 47); } + diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h index 066c9d3..66f3a28 100644 --- a/src/crepe/util/color.h +++ b/src/crepe/util/color.h @@ -1,41 +1,51 @@ #pragma once -namespace crepe::util::color { - -constexpr const char * RESET = "\e[0m"; - -constexpr const char * FG_BLACK = "\e[30m"; -constexpr const char * FG_RED = "\e[31m"; -constexpr const char * FG_GREEN = "\e[32m"; -constexpr const char * FG_YELLOW = "\e[33m"; -constexpr const char * FG_BLUE = "\e[34m"; -constexpr const char * FG_MAGENTA = "\e[35m"; -constexpr const char * FG_CYAN = "\e[36m"; -constexpr const char * FG_WHITE = "\e[37m"; -constexpr const char * BG_BLACK = "\e[40m"; -constexpr const char * BG_RED = "\e[41m"; -constexpr const char * BG_GREEN = "\e[42m"; -constexpr const char * BG_YELLOW = "\e[43m"; -constexpr const char * BG_BLUE = "\e[44m"; -constexpr const char * BG_MAGENTA = "\e[45m"; -constexpr const char * BG_CYAN = "\e[46m"; -constexpr const char * BG_WHITE = "\e[47m"; - -constexpr const char * FG_BLACK_BRIGHT = "\e[90m"; -constexpr const char * FG_RED_BRIGHT = "\e[91m"; -constexpr const char * FG_GREEN_BRIGHT = "\e[92m"; -constexpr const char * FG_YELLOW_BRIGHT = "\e[93m"; -constexpr const char * FG_BLUE_BRIGHT = "\e[94m"; -constexpr const char * FG_MAGENTA_BRIGHT = "\e[95m"; -constexpr const char * FG_CYAN_BRIGHT = "\e[96m"; -constexpr const char * FG_WHITE_BRIGHT = "\e[97m"; -constexpr const char * BG_BLACK_BRIGHT = "\e[100m"; -constexpr const char * BG_RED_BRIGHT = "\e[101m"; -constexpr const char * BG_GREEN_BRIGHT = "\e[102m"; -constexpr const char * BG_YELLOW_BRIGHT = "\e[103m"; -constexpr const char * BG_BLUE_BRIGHT = "\e[104m"; -constexpr const char * BG_MAGENTA_BRIGHT = "\e[105m"; -constexpr const char * BG_CYAN_BRIGHT = "\e[106m"; -constexpr const char * BG_WHITE_BRIGHT = "\e[107m"; +#include <string> + +namespace crepe::util { + +class LogColor { +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: + LogColor & fg_black(bool bright = false); + LogColor & fg_red(bool bright = false); + LogColor & fg_green(bool bright = false); + LogColor & fg_yellow(bool bright = false); + LogColor & fg_blue(bool bright = false); + LogColor & fg_magenta(bool bright = false); + LogColor & fg_cyan(bool bright = false); + LogColor & fg_white(bool bright = false); + +public: + LogColor & bg_black(bool bright = false); + LogColor & bg_red(bool bright = false); + LogColor & bg_green(bool bright = false); + LogColor & bg_yellow(bool bright = false); + LogColor & bg_blue(bool bright = false); + LogColor & bg_magenta(bool bright = false); + LogColor & bg_cyan(bool bright = false); + LogColor & bg_white(bool bright = false); + +private: + LogColor & add_code(unsigned int code); + +private: + std::string code = ""; + std::string final = ""; +}; } // namespace crepe::util::color diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp index b869aad..5ff2ad4 100644 --- a/src/crepe/util/log.cpp +++ b/src/crepe/util/log.cpp @@ -5,19 +5,27 @@ #include "fmt.h" #include "log.h" +#include "../api/Config.h" using namespace crepe::util; +using namespace std; -static const char * const LOG_PREFIX[] = { - [LogLevel::DEBUG] = "[DBG] ", - [LogLevel::INFO] = "[INFO] ", - [LogLevel::WARNING] = "[WARN] ", - [LogLevel::ERROR] = "[ERR] ", -}; +string log_prefix(LogLevel level) { + switch (level) { + case LogLevel::TRACE: return LogColor().fg_white().str("[TRACE]") + " "; + case LogLevel::DEBUG: return LogColor().fg_magenta().str("[DEBUG]") + " "; + case LogLevel::INFO: return LogColor().fg_blue().str("[INFO]") + " "; + case LogLevel::WARNING: return LogColor().fg_yellow().str("[WARN]") + " "; + case LogLevel::ERROR: return LogColor().fg_red().str("[ERROR]") + " "; + } + return ""; +} + +static void log(LogLevel level, const string msg) { + auto & cfg = crepe::api::Config::get_instance(); + if (level < cfg.log.level) return; -static void log(LogLevel level, const std::string & msg) { - using namespace std; - string out = string(LOG_PREFIX[level]) + msg; + string out = log_prefix(level) + msg; if (!out.ends_with("\n")) out += "\n"; // TODO: also log to file or smth @@ -38,3 +46,4 @@ void crepe::util::logf(LogLevel level, const char * fmt, ...) { log(level, va_stringf(args, fmt)); va_end(args); } + diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index f8e4f00..5ee67eb 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(fmt, ...) \ - crepe::util::logf(util::LogLevel::DEBUG, "%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(": " fmt, __VA_ARGS__) -#define dbg_log(str) _crepe_logf_here("%s: " str, "") -#define dbg_trace() _crepe_logf_here("%s", "") +#define dbg_logf(fmt, ...) _crepe_logf_here(crepe::util::LogLevel::DEBUG, ": " fmt, __VA_ARGS__) +#define dbg_log(str) _crepe_logf_here(crepe::util::LogLevel::DEBUG, "%s: " str, "") +#define dbg_trace() _crepe_logf_here(crepe::util::LogLevel::TRACE, "%s", "") // NOLINTEND #endif @@ -24,6 +20,7 @@ namespace crepe::util { enum LogLevel { + TRACE, DEBUG, INFO, WARNING, diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 587a7bd..fea6f60 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -19,6 +19,7 @@ endfunction() add_example(audio_internal) # add_example(components_internal) add_example(script) +add_example(log) add_example(rendering) add_example(asset_manager) add_example(particle) diff --git a/src/example/log.cpp b/src/example/log.cpp new file mode 100644 index 0000000..86ef193 --- /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::LogLevel::TRACE; + + dbg_trace(); + dbg_logf("cfg.log.color is equal to %d", cfg.log.color); + logf(LogLevel::INFO, "info message!"); + logf(LogLevel::WARNING, "very scary warning"); + logf(LogLevel::ERROR, "fatal error!!!"); + + return 0; +} + diff --git a/src/example/script.cpp b/src/example/script.cpp index 6e5563c..f737a90 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -7,6 +7,7 @@ #include <crepe/ScriptSystem.h> #include <crepe/util/log.h> +#include <crepe/api/Config.h> #include <crepe/api/BehaviorScript.h> #include <crepe/api/GameObject.h> #include <crepe/api/Script.h> @@ -20,7 +21,8 @@ class MyScript : public Script { }; int main() { - dbg_trace(); + auto & cfg = api::Config::get_instance(); + cfg.log.level = util::LogLevel::TRACE; auto obj = GameObject(0, "name", "tag", 0); obj.add_component<BehaviorScript>().set_script<MyScript>(); |