From a47b981c2254e1d49f58ebd4244c1be4624ba998 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 15:49:59 +0100 Subject: remove util and api namespaces --- src/crepe/util/CMakeLists.txt | 4 +- src/crepe/util/LogColor.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++ src/crepe/util/LogColor.h | 51 ++++++++++++++++++++++++ src/crepe/util/color.cpp | 91 ------------------------------------------ src/crepe/util/color.h | 51 ------------------------ src/crepe/util/fmt.cpp | 4 +- src/crepe/util/fmt.h | 4 +- src/crepe/util/log.cpp | 10 ++--- src/crepe/util/log.h | 16 ++++---- 9 files changed, 162 insertions(+), 161 deletions(-) create mode 100644 src/crepe/util/LogColor.cpp create mode 100644 src/crepe/util/LogColor.h delete mode 100644 src/crepe/util/color.cpp delete mode 100644 src/crepe/util/color.h (limited to 'src/crepe/util') diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index bbeaad9..3675bee 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -1,11 +1,11 @@ target_sources(crepe PUBLIC - color.cpp + LogColor.cpp log.cpp fmt.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - color.h + LogColor.h log.h fmt.h ) diff --git a/src/crepe/util/LogColor.cpp b/src/crepe/util/LogColor.cpp new file mode 100644 index 0000000..b5fe3ea --- /dev/null +++ b/src/crepe/util/LogColor.cpp @@ -0,0 +1,92 @@ +#include + +#include "../api/Config.h" +#include "LogColor.h" + +#include "fmt.h" + +using namespace crepe; +using namespace std; + +static constexpr const char * RESET_CODE = "\e[0m"; + +const string LogColor::str(const string & content) { + auto & cfg = 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/LogColor.h b/src/crepe/util/LogColor.h new file mode 100644 index 0000000..7e60ba2 --- /dev/null +++ b/src/crepe/util/LogColor.h @@ -0,0 +1,51 @@ +#pragma once + +#include + +namespace crepe { + +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 diff --git a/src/crepe/util/color.cpp b/src/crepe/util/color.cpp deleted file mode 100644 index a7bbc81..0000000 --- a/src/crepe/util/color.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include - -#include "../api/Config.h" -#include "color.h" -#include "fmt.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 deleted file mode 100644 index 91e1abe..0000000 --- a/src/crepe/util/color.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include - -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 diff --git a/src/crepe/util/fmt.cpp b/src/crepe/util/fmt.cpp index 397bf9f..4b50da8 100644 --- a/src/crepe/util/fmt.cpp +++ b/src/crepe/util/fmt.cpp @@ -6,7 +6,7 @@ using namespace std; -string crepe::util::va_stringf(va_list args, const char * fmt) { +string crepe::va_stringf(va_list args, const char * fmt) { string out; va_list args_copy; @@ -26,7 +26,7 @@ string crepe::util::va_stringf(va_list args, const char * fmt) { return out; } -string crepe::util::stringf(const char * fmt, ...) { +string crepe::stringf(const char * fmt, ...) { va_list args; va_start(args, fmt); string out = va_stringf(args, fmt); diff --git a/src/crepe/util/fmt.h b/src/crepe/util/fmt.h index 44c426f..e319e6e 100644 --- a/src/crepe/util/fmt.h +++ b/src/crepe/util/fmt.h @@ -2,9 +2,9 @@ #include -namespace crepe::util { +namespace crepe { std::string va_stringf(va_list args, const char * fmt); std::string stringf(const char * fmt, ...); -} // namespace crepe::util +} // namespace crepe diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp index 6bcc4ae..4a8f8e8 100644 --- a/src/crepe/util/log.cpp +++ b/src/crepe/util/log.cpp @@ -7,7 +7,7 @@ #include "fmt.h" #include "log.h" -using namespace crepe::util; +using namespace crepe; using namespace std; string log_prefix(LogLevel level) { @@ -27,25 +27,25 @@ string log_prefix(LogLevel level) { } static void log(LogLevel level, const string msg) { - auto & cfg = crepe::api::Config::get_instance(); + auto & cfg = Config::get_instance(); if (level < cfg.log.level) return; string out = log_prefix(level) + msg; if (!out.ends_with("\n")) out += "\n"; // TODO: also log to file or smth - printf("%s", out.c_str()); + fwrite(out.c_str(), 1, out.size(), stdout); fflush(stdout); } -void crepe::util::logf(const char * fmt, ...) { +void crepe::logf(const char * fmt, ...) { va_list args; va_start(args, fmt); log(LogLevel::DEBUG, va_stringf(args, fmt)); va_end(args); } -void crepe::util::logf(LogLevel level, const char * fmt, ...) { +void crepe::logf(LogLevel level, const char * fmt, ...) { va_list args; va_start(args, fmt); log(level, va_stringf(args, fmt)); diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index 308ba96..b13b9cc 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -3,28 +3,28 @@ // allow user to disable debug macros #ifndef CREPE_DISABLE_MACROS -#include "color.h" +#include "LogColor.h" // utility macros #define _crepe_logf_here(level, format, ...) \ - crepe::util::logf( \ + crepe::logf( \ level, "%s" format, \ - crepe::util::LogColor().fg_white(false).fmt( \ + crepe::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(crepe::util::LogLevel::DEBUG, ": " fmt, __VA_ARGS__) + _crepe_logf_here(crepe::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", "") + _crepe_logf_here(crepe::LogLevel::DEBUG, "%s: " str, "") +#define dbg_trace() _crepe_logf_here(crepe::LogLevel::TRACE, "%s", "") // NOLINTEND #endif -namespace crepe::util { +namespace crepe { enum LogLevel { TRACE, @@ -37,4 +37,4 @@ enum LogLevel { void logf(const char * fmt, ...); void logf(enum LogLevel level, const char * fmt, ...); -} // namespace crepe::util +} // namespace crepe -- cgit v1.2.3 From 74533e9fbcd0360950daec4dc297e48e30a8a2d0 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 15:51:01 +0100 Subject: `make format` --- src/crepe/api/Sprite.cpp | 2 +- src/crepe/api/Texture.cpp | 2 +- src/crepe/system/ParticleSystem.cpp | 2 +- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 4 ++-- src/crepe/system/ScriptSystem.cpp | 2 +- src/crepe/util/LogColor.h | 2 +- src/crepe/util/log.h | 3 +-- src/example/physics.cpp | 2 +- src/example/rendering.cpp | 2 +- 10 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src/crepe/util') diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 3fd6018..3dd44f2 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -3,9 +3,9 @@ #include "../util/log.h" -#include "Texture.h" #include "Component.h" #include "Sprite.h" +#include "Texture.h" using namespace std; using namespace crepe; diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index b84a3c6..b5001a6 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,7 +1,7 @@ #include -#include "../util/log.h" #include "../SDLContext.h" +#include "../util/log.h" #include "Asset.h" #include "Texture.h" diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 367c7d8..397b586 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -1,8 +1,8 @@ #include #include -#include "../api/ParticleEmitter.h" #include "../ComponentManager.h" +#include "../api/ParticleEmitter.h" #include "ParticleSystem.h" diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index efec750..cea8062 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -1,9 +1,9 @@ #include +#include "../ComponentManager.h" #include "../api/Force.h" #include "../api/Rigidbody.h" #include "../api/Transform.h" -#include "../ComponentManager.h" #include "PhysicsSystem.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index b3c53db..ee3cdf2 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,11 +1,11 @@ #include #include +#include "../ComponentManager.h" +#include "../SDLContext.h" #include "../api/Sprite.h" #include "../api/Transform.h" #include "../util/log.h" -#include "../SDLContext.h" -#include "../ComponentManager.h" #include "RenderSystem.h" diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 731adae..f1fae4d 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -2,10 +2,10 @@ #include #include +#include "../ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" #include "../util/log.h" -#include "../ComponentManager.h" #include "ScriptSystem.h" diff --git a/src/crepe/util/LogColor.h b/src/crepe/util/LogColor.h index 7e60ba2..c1170cb 100644 --- a/src/crepe/util/LogColor.h +++ b/src/crepe/util/LogColor.h @@ -48,4 +48,4 @@ private: std::string final = ""; }; -} // namespace crepe::util +} // namespace crepe diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index b13b9cc..5a1cf00 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -17,8 +17,7 @@ // NOLINTBEGIN #define dbg_logf(fmt, ...) \ _crepe_logf_here(crepe::LogLevel::DEBUG, ": " fmt, __VA_ARGS__) -#define dbg_log(str) \ - _crepe_logf_here(crepe::LogLevel::DEBUG, "%s: " str, "") +#define dbg_log(str) _crepe_logf_here(crepe::LogLevel::DEBUG, "%s: " str, "") #define dbg_trace() _crepe_logf_here(crepe::LogLevel::TRACE, "%s", "") // NOLINTEND diff --git a/src/example/physics.cpp b/src/example/physics.cpp index d3a0f32..2dbddc2 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -2,13 +2,13 @@ #include #include -#include #include #include #include #include #include #include +#include using namespace crepe; using namespace std; diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 9c8d60c..b0ab60a 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -1,6 +1,6 @@ #include -#include #include +#include #include #include -- cgit v1.2.3