diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-05 16:12:47 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-05 16:12:47 +0100 |
commit | e36ea050972fcaaf3d85d672755bad4ebb2dcd80 (patch) | |
tree | 5145e0b66650eea1df301106b7d197a586be65f3 /src/crepe/util | |
parent | 333b07775be1ef20fdb5909672c1e4dcabec1b40 (diff) | |
parent | b770475741b7c33d57331f3139c55a3f237ad274 (diff) |
merge `master` into `loek/savemgr`loek/savemgr
Diffstat (limited to 'src/crepe/util')
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/crepe/util/LogColor.cpp (renamed from src/crepe/util/color.cpp) | 7 | ||||
-rw-r--r-- | src/crepe/util/LogColor.h (renamed from src/crepe/util/color.h) | 4 | ||||
-rw-r--r-- | src/crepe/util/Proxy.h | 4 | ||||
-rw-r--r-- | src/crepe/util/Proxy.hpp | 4 | ||||
-rw-r--r-- | src/crepe/util/fmt.cpp | 20 | ||||
-rw-r--r-- | src/crepe/util/fmt.h | 4 | ||||
-rw-r--r-- | src/crepe/util/log.cpp | 10 | ||||
-rw-r--r-- | src/crepe/util/log.h | 17 |
9 files changed, 38 insertions, 36 deletions
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index 01d8f22..0fa4343 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 Proxy.h diff --git a/src/crepe/util/color.cpp b/src/crepe/util/LogColor.cpp index a7bbc81..b5fe3ea 100644 --- a/src/crepe/util/color.cpp +++ b/src/crepe/util/LogColor.cpp @@ -1,16 +1,17 @@ #include <cstdarg> #include "../api/Config.h" -#include "color.h" +#include "LogColor.h" + #include "fmt.h" -using namespace crepe::util; +using namespace crepe; using namespace std; static constexpr const char * RESET_CODE = "\e[0m"; const string LogColor::str(const string & content) { - auto & cfg = api::Config::get_instance(); + auto & cfg = Config::get_instance(); string out = content; if (cfg.log.color) out = this->code + out; if (content.size() == 0) return out; diff --git a/src/crepe/util/color.h b/src/crepe/util/LogColor.h index 91e1abe..c1170cb 100644 --- a/src/crepe/util/color.h +++ b/src/crepe/util/LogColor.h @@ -2,7 +2,7 @@ #include <string> -namespace crepe::util { +namespace crepe { class LogColor { public: @@ -48,4 +48,4 @@ private: std::string final = ""; }; -} // namespace crepe::util +} // namespace crepe diff --git a/src/crepe/util/Proxy.h b/src/crepe/util/Proxy.h index 89cb3c3..65db04d 100644 --- a/src/crepe/util/Proxy.h +++ b/src/crepe/util/Proxy.h @@ -2,13 +2,13 @@ #include "ValueBroker.h" -namespace crepe::util { +namespace crepe { template <typename T> class Proxy { public: Proxy & operator = (const T &); - operator const T & () const; + operator const T & (); public: Proxy(ValueBroker<T>); diff --git a/src/crepe/util/Proxy.hpp b/src/crepe/util/Proxy.hpp index c2cae93..4aec9e9 100644 --- a/src/crepe/util/Proxy.hpp +++ b/src/crepe/util/Proxy.hpp @@ -2,7 +2,7 @@ #include "Proxy.h" -namespace crepe::util { +namespace crepe { template <typename T> Proxy<T>::Proxy(ValueBroker<T> broker) : broker(broker) { } @@ -14,7 +14,7 @@ Proxy<T> & Proxy<T>::operator = (const T & val) { } template <typename T> -Proxy<T>::operator const T & () const { +Proxy<T>::operator const T & () { return this->broker.get(); } diff --git a/src/crepe/util/fmt.cpp b/src/crepe/util/fmt.cpp index 8ef1164..4b50da8 100644 --- a/src/crepe/util/fmt.cpp +++ b/src/crepe/util/fmt.cpp @@ -6,25 +6,27 @@ 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; va_copy(args_copy, args); - - size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; - char * msg = (char *) malloc(sz); + size_t length = vsnprintf(NULL, 0, fmt, args_copy); + // resize to include terminating null byte + out.resize(length + 1); va_end(args_copy); - vsnprintf(msg, sz, fmt, args); - - string out = msg; - free(msg); + // vsnprintf adds terminating null byte + vsnprintf(out.data(), out.size(), fmt, args); + // resize to actual length + out.resize(length); va_end(args); 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 <string> -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..5a1cf00 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -3,28 +3,27 @@ // 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__) -#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, ": " fmt, __VA_ARGS__) +#define dbg_log(str) _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 +36,4 @@ enum LogLevel { void logf(const char * fmt, ...); void logf(enum LogLevel level, const char * fmt, ...); -} // namespace crepe::util +} // namespace crepe |