From 1df510f4a2dbe0fcb8c9f8a34695abf8d33f9ddc Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 14:35:27 +0100 Subject: update logging to use C++20 format and classes --- src/crepe/util/Log.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/crepe/util/Log.cpp (limited to 'src/crepe/util/Log.cpp') diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp new file mode 100644 index 0000000..346e08e --- /dev/null +++ b/src/crepe/util/Log.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +#include "../api/Config.h" +#include "Log.h" + +using namespace crepe; +using namespace std; + +string Log::prefix(const Level & level) { + switch (level) { + case Level::TRACE: + return LogColor().fg_white().str("[TRACE]") + " "; + case Level::DEBUG: + return LogColor().fg_magenta().str("[DEBUG]") + " "; + case Level::INFO: + return LogColor().fg_blue().str("[INFO]") + " "; + case Level::WARNING: + return LogColor().fg_yellow().str("[WARN]") + " "; + case Level::ERROR: + return LogColor().fg_red().str("[ERROR]") + " "; + } + return ""; +} + +void Log::log(const Level & level, const string & msg) { + 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 + fwrite(out.c_str(), 1, out.size(), stdout); + fflush(stdout); +} + -- cgit v1.2.3 From 5e59c22f77073fdf49bf98697da6a68819eb5776 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 15:50:13 +0100 Subject: `make format` --- src/crepe/Asset.cpp | 1 - src/crepe/Exception.cpp | 5 +---- src/crepe/Exception.h | 7 +++---- src/crepe/Exception.hpp | 7 +++---- src/crepe/api/Config.h | 1 + src/crepe/api/Script.hpp | 3 ++- src/crepe/util/Log.cpp | 1 - src/crepe/util/Log.h | 17 +++++++++++------ src/crepe/util/Log.hpp | 12 ++++++------ src/example/log.cpp | 1 - src/example/proxy.cpp | 2 +- src/example/savemgr.cpp | 2 +- 12 files changed, 29 insertions(+), 30 deletions(-) (limited to 'src/crepe/util/Log.cpp') diff --git a/src/crepe/Asset.cpp b/src/crepe/Asset.cpp index 4affd58..9c41ecb 100644 --- a/src/crepe/Asset.cpp +++ b/src/crepe/Asset.cpp @@ -14,4 +14,3 @@ Asset::Asset(const std::string & src) : src(src) { istream & Asset::get_stream() { return this->file; } const string & Asset::get_canonical() const { return this->src; } - diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp index 3217169..5a24e7e 100644 --- a/src/crepe/Exception.cpp +++ b/src/crepe/Exception.cpp @@ -5,7 +5,4 @@ using namespace std; const char * Exception::what() const noexcept { return error.c_str(); } -Exception::Exception(const string & msg) { - this->error = msg; -} - +Exception::Exception(const string & msg) { this->error = msg; } diff --git a/src/crepe/Exception.h b/src/crepe/Exception.h index ed3ab15..580fc16 100644 --- a/src/crepe/Exception.h +++ b/src/crepe/Exception.h @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include namespace crepe { @@ -13,8 +13,8 @@ public: Exception(const std::string & msg); //! Exception with \c std::format message - template - Exception(std::format_string fmt, Args&&... args); + template + Exception(std::format_string fmt, Args &&... args); //! Get formatted error message const char * what() const noexcept; @@ -29,4 +29,3 @@ protected: } // namespace crepe #include "Exception.hpp" - diff --git a/src/crepe/Exception.hpp b/src/crepe/Exception.hpp index d813879..7c462a3 100644 --- a/src/crepe/Exception.hpp +++ b/src/crepe/Exception.hpp @@ -4,10 +4,9 @@ namespace crepe { -template -Exception::Exception(std::format_string fmt, Args&&... args) { +template +Exception::Exception(std::format_string fmt, Args &&... args) { this->error = std::format(fmt, std::forward(args)...); } -} - +} // namespace crepe diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index dad7e08..e3f86bf 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -7,6 +7,7 @@ namespace crepe { class Config { private: Config() = default; + public: ~Config() = default; diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index afe653f..d755fda 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -13,7 +13,8 @@ T & Script::get_component() const { std::vector> all_components = this->get_components(); if (all_components.size() < 1) - throw Exception("Script: no component found with type = %s", typeid(T).name()); + throw Exception("Script: no component found with type = %s", + typeid(T).name()); return all_components.back().get(); } diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp index 346e08e..e583734 100644 --- a/src/crepe/util/Log.cpp +++ b/src/crepe/util/Log.cpp @@ -36,4 +36,3 @@ void Log::log(const Level & level, const string & msg) { fwrite(out.c_str(), 1, out.size(), stdout); fflush(stdout); } - diff --git a/src/crepe/util/Log.h b/src/crepe/util/Log.h index 4e32e9d..01452b2 100644 --- a/src/crepe/util/Log.h +++ b/src/crepe/util/Log.h @@ -8,7 +8,12 @@ #include "LogColor.h" // utility macros -#define _crepe_logf_here(level, fmt, ...) crepe::Log::logf(level, "{}" fmt, crepe::LogColor().fg_white(false).str(std::format("{} ({}:{})", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__)), __VA_ARGS__) +#define _crepe_logf_here(level, fmt, ...) \ + crepe::Log::logf( \ + level, "{}" fmt, \ + crepe::LogColor().fg_white(false).str(std::format( \ + "{} ({}:{})", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__)), \ + __VA_ARGS__) // very illegal global function-style macros // NOLINTBEGIN @@ -53,8 +58,9 @@ public: * \param fmt Message format * \param args Format arguments */ - template - static void logf(const Level & level, std::format_string fmt, Args&&... args); + template + static void logf(const Level & level, std::format_string fmt, + Args &&... args); /** * \brief Format a message and log it (with default severity \c INFO) @@ -62,8 +68,8 @@ public: * \param fmt Message format * \param args Format arguments */ - template - static void logf(std::format_string fmt, Args&&... args); + template + static void logf(std::format_string fmt, Args &&... args); private: /** @@ -77,4 +83,3 @@ private: } // namespace crepe #include "Log.hpp" - diff --git a/src/crepe/util/Log.hpp b/src/crepe/util/Log.hpp index 58ba475..651f076 100644 --- a/src/crepe/util/Log.hpp +++ b/src/crepe/util/Log.hpp @@ -4,15 +4,15 @@ namespace crepe { -template -void Log::logf(std::format_string fmt, Args&&... args) { +template +void Log::logf(std::format_string fmt, Args &&... args) { Log::logf(Level::INFO, fmt, std::forward(args)...); } -template -void Log::logf(const Level & level, std::format_string fmt, Args&&... args) { +template +void Log::logf(const Level & level, std::format_string fmt, + Args &&... args) { Log::log(level, std::format(fmt, std::forward(args)...)); } -} - +} // namespace crepe diff --git a/src/example/log.cpp b/src/example/log.cpp index 13d592b..5baa021 100644 --- a/src/example/log.cpp +++ b/src/example/log.cpp @@ -23,7 +23,6 @@ int main() { Log::logf("info message with variable: {}", 3); Log::logf(Log::Level::WARNING, "warning"); Log::logf(Log::Level::ERROR, "error"); - return 0; } diff --git a/src/example/proxy.cpp b/src/example/proxy.cpp index ca68d9a..69451f8 100644 --- a/src/example/proxy.cpp +++ b/src/example/proxy.cpp @@ -5,8 +5,8 @@ #include #include -#include #include +#include using namespace std; using namespace crepe; diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp index 5a415b7..65c4a34 100644 --- a/src/example/savemgr.cpp +++ b/src/example/savemgr.cpp @@ -6,8 +6,8 @@ #include #include #include -#include #include +#include using namespace crepe; -- cgit v1.2.3 From 5bee4515c1089ce3499bc3b74780db94f0c02306 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 15 Nov 2024 20:26:26 +0100 Subject: process feedback on #26 --- src/crepe/CMakeLists.txt | 3 --- src/crepe/Component.h | 2 ++ src/crepe/ComponentManager.cpp | 11 +++++------ src/crepe/ComponentManager.h | 3 ++- src/crepe/ComponentManager.hpp | 1 - src/crepe/Exception.cpp | 8 -------- src/crepe/Exception.h | 31 ------------------------------- src/crepe/Exception.hpp | 12 ------------ src/crepe/api/Animator.cpp | 2 -- src/crepe/api/Animator.h | 2 -- src/crepe/api/BehaviorScript.h | 3 +-- src/crepe/api/CMakeLists.txt | 1 - src/crepe/api/Camera.h | 2 -- src/crepe/api/Config.h | 17 +++++++++++------ src/crepe/api/LoopManager.hpp | 4 ++-- src/crepe/api/LoopTimer.h | 1 - src/crepe/api/Rigidbody.h | 2 -- src/crepe/api/SceneManager.h | 3 --- src/crepe/api/Script.cpp | 3 --- src/crepe/api/Script.hpp | 8 +++----- src/crepe/api/Sprite.h | 1 - src/crepe/api/Transform.h | 8 +++++--- src/crepe/facade/DB.cpp | 24 +++++++++++++++--------- src/crepe/facade/DB.h | 7 ++++--- src/crepe/facade/SDLContext.cpp | 5 ++--- src/crepe/util/Log.cpp | 9 ++++----- src/crepe/util/Log.h | 2 ++ src/crepe/util/LogColor.cpp | 1 + src/crepe/util/LogColor.h | 24 +++++++++++++++++++++++- src/example/particles.cpp | 3 ++- 30 files changed, 84 insertions(+), 119 deletions(-) delete mode 100644 src/crepe/Exception.cpp delete mode 100644 src/crepe/Exception.h delete mode 100644 src/crepe/Exception.hpp delete mode 100644 src/crepe/api/Script.cpp (limited to 'src/crepe/util/Log.cpp') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 52a781e..3b05742 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -4,7 +4,6 @@ target_sources(crepe PUBLIC ComponentManager.cpp Component.cpp Collider.cpp - Exception.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,8 +14,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h ValueBroker.h ValueBroker.hpp - Exception.h - Exception.hpp ) add_subdirectory(api) diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 12c10cb..670446f 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -26,6 +26,8 @@ protected: Component(game_object_id_t id); //! Only the ComponentManager can create components friend class ComponentManager; +public: + virtual ~Component() = default; public: /** diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 7af0380..67c6fc7 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -6,6 +6,9 @@ using namespace crepe; using namespace std; +ComponentManager::ComponentManager() { dbg_trace(); } +ComponentManager::~ComponentManager() { dbg_trace(); } + void ComponentManager::delete_all_components_of_id(game_object_id_t id) { // Loop through all the types (in the unordered_map<>) for (auto & [type, componentArray] : this->components) { @@ -18,18 +21,14 @@ void ComponentManager::delete_all_components_of_id(game_object_id_t id) { } void ComponentManager::delete_all_components() { - // Clear the whole unordered_map<> this->components.clear(); + this->next_id = 0; } -ComponentManager::ComponentManager() { dbg_trace(); } -ComponentManager::~ComponentManager() { dbg_trace(); } - GameObject ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { - GameObject object{*this, this->next_id, name, tag, - position, rotation, scale}; + GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; return object; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 51c84a4..ca2e7c6 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -6,9 +6,10 @@ #include #include -#include "Component.h" #include "api/Vector2.h" +#include "Component.h" + namespace crepe { class GameObject; diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 98efb49..0a84468 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -40,7 +40,6 @@ T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { // Check if the vector size is not greater than get_instances_max int max_instances = instance->get_instances_max(); if (max_instances != -1 && components[type][id].size() >= max_instances) { - // TODO: Exception throw std::runtime_error( "Exceeded maximum number of instances for this component type"); } diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp deleted file mode 100644 index 5a24e7e..0000000 --- a/src/crepe/Exception.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Exception.h" - -using namespace crepe; -using namespace std; - -const char * Exception::what() const noexcept { return error.c_str(); } - -Exception::Exception(const string & msg) { this->error = msg; } diff --git a/src/crepe/Exception.h b/src/crepe/Exception.h deleted file mode 100644 index 580fc16..0000000 --- a/src/crepe/Exception.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace crepe { - -//! Exception class -class Exception : public std::exception { -public: - //! Exception with plain message - Exception(const std::string & msg); - - //! Exception with \c std::format message - template - Exception(std::format_string fmt, Args &&... args); - - //! Get formatted error message - const char * what() const noexcept; - -protected: - Exception() = default; - - //! Formatted error message - std::string error; -}; - -} // namespace crepe - -#include "Exception.hpp" diff --git a/src/crepe/Exception.hpp b/src/crepe/Exception.hpp deleted file mode 100644 index 7c462a3..0000000 --- a/src/crepe/Exception.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "Exception.h" - -namespace crepe { - -template -Exception::Exception(std::format_string fmt, Args &&... args) { - this->error = std::format(fmt, std::forward(args)...); -} - -} // namespace crepe diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 54b2ec3..cccbc67 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -1,6 +1,4 @@ -#include - #include "util/Log.h" #include "Animator.h" diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 75b8139..3573403 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,7 +1,5 @@ #pragma once -#include - #include "Component.h" #include "Sprite.h" diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 2982358..1a8910d 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -3,6 +3,7 @@ #include #include "../Component.h" + #include "GameObject.h" namespace crepe { @@ -47,8 +48,6 @@ protected: std::unique_ptr