From 97b3a8638a0437f0c93257bdffd41927f5767bf1 Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 20 Nov 2024 09:53:01 -0500 Subject: [PATCH] Refactor common module: streamline includes, improve code organization, and enhance logging functionality --- src/app/core/common.cc | 7 ------- src/app/core/common.h | 45 +++++++++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/app/core/common.cc b/src/app/core/common.cc index fd2851cc..4cfe1919 100644 --- a/src/app/core/common.cc +++ b/src/app/core/common.cc @@ -4,20 +4,13 @@ #include #include -#include #include -#include #include -#include #include #include "absl/status/statusor.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" -#include "app/core/constants.h" -#include "app/gui/icons.h" -#include "imgui/imgui.h" -#include "imgui/misc/cpp/imgui_stdlib.h" namespace yaze { namespace app { diff --git a/src/app/core/common.h b/src/app/core/common.h index e58e3d21..f4f0791d 100644 --- a/src/app/core/common.h +++ b/src/app/core/common.h @@ -9,6 +9,7 @@ #include "absl/status/statusor.h" #include "absl/strings/str_format.h" +#include "absl/container/flat_hash_map.h" namespace yaze { namespace app { @@ -19,8 +20,6 @@ namespace app { */ namespace core { -constexpr std::string_view kYazeVersion = "0.2.1"; - std::string UppercaseHexByte(uint8_t byte, bool leading = false); std::string UppercaseHexWord(uint16_t word, bool leading = false); std::string UppercaseHexLong(uint32_t dword); @@ -34,7 +33,7 @@ bool StringReplace(std::string &str, const std::string &from, * @brief A class to manage experimental feature flags. */ class ExperimentFlags { - public: +public: struct Flags { // Log instructions to the GUI debugger. bool kLogInstructions = true; @@ -150,7 +149,7 @@ class ExperimentFlags { return result; } - private: +private: static std::shared_ptr flags_; }; @@ -159,9 +158,8 @@ class ExperimentFlags { * @brief A class to manage a value that can be modified and notify when it * changes. */ -template -class NotifyValue { - public: +template class NotifyValue { +public: NotifyValue() : value_(), modified_(false), temp_value_() {} NotifyValue(const T &value) : value_(value), modified_(false), temp_value_() {} @@ -194,7 +192,7 @@ class NotifyValue { bool modified() const { return modified_; } - private: +private: T value_; bool modified_; T temp_value_; @@ -213,8 +211,29 @@ static void logf(const absl::FormatSpec &format, const Args &...args) { fout << message << std::endl; } +struct StructuredLog { + std::string raw_message; + std::string category; +}; + +static absl::flat_hash_map> log_categories; + +template +static void logm(const std::string &category, + const absl::FormatSpec &format, const Args &...args) { + std::string message = absl::StrFormat(format, args...); + if (log_to_console) { + std::cout << category << ": " << message << std::endl; + } + if (log_categories.contains(category)) { + log_categories[category].push_back(message); + } else { + log_categories[category] = {message}; + } +} + class Logger { - public: +public: static void log(std::string message) { static std::ofstream fout(log_file_out, std::ios::out | std::ios::app); fout << message << std::endl; @@ -291,10 +310,12 @@ void ApplyBpsPatch(const std::vector &source, const std::vector &patch, std::vector &target); +constexpr std::string_view kYazeVersion = "0.2.1"; + absl::StatusOr CheckVersion(const char *version); -} // namespace core -} // namespace app -} // namespace yaze +} // namespace core +} // namespace app +} // namespace yaze #endif