Refactor common module: streamline includes, improve code organization, and enhance logging functionality

This commit is contained in:
scawful
2024-11-20 09:53:01 -05:00
parent 2deb0050be
commit 97b3a8638a
2 changed files with 33 additions and 19 deletions

View File

@@ -4,20 +4,13 @@
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <fstream>
#include <memory> #include <memory>
#include <sstream>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "absl/strings/str_format.h" #include "absl/strings/str_format.h"
#include "absl/strings/string_view.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 yaze {
namespace app { namespace app {

View File

@@ -9,6 +9,7 @@
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "absl/strings/str_format.h" #include "absl/strings/str_format.h"
#include "absl/container/flat_hash_map.h"
namespace yaze { namespace yaze {
namespace app { namespace app {
@@ -19,8 +20,6 @@ namespace app {
*/ */
namespace core { namespace core {
constexpr std::string_view kYazeVersion = "0.2.1";
std::string UppercaseHexByte(uint8_t byte, bool leading = false); std::string UppercaseHexByte(uint8_t byte, bool leading = false);
std::string UppercaseHexWord(uint16_t word, bool leading = false); std::string UppercaseHexWord(uint16_t word, bool leading = false);
std::string UppercaseHexLong(uint32_t dword); 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. * @brief A class to manage experimental feature flags.
*/ */
class ExperimentFlags { class ExperimentFlags {
public: public:
struct Flags { struct Flags {
// Log instructions to the GUI debugger. // Log instructions to the GUI debugger.
bool kLogInstructions = true; bool kLogInstructions = true;
@@ -150,7 +149,7 @@ class ExperimentFlags {
return result; return result;
} }
private: private:
static std::shared_ptr<Flags> flags_; static std::shared_ptr<Flags> flags_;
}; };
@@ -159,9 +158,8 @@ class ExperimentFlags {
* @brief A class to manage a value that can be modified and notify when it * @brief A class to manage a value that can be modified and notify when it
* changes. * changes.
*/ */
template <typename T> template <typename T> class NotifyValue {
class NotifyValue { public:
public:
NotifyValue() : value_(), modified_(false), temp_value_() {} NotifyValue() : value_(), modified_(false), temp_value_() {}
NotifyValue(const T &value) NotifyValue(const T &value)
: value_(value), modified_(false), temp_value_() {} : value_(value), modified_(false), temp_value_() {}
@@ -194,7 +192,7 @@ class NotifyValue {
bool modified() const { return modified_; } bool modified() const { return modified_; }
private: private:
T value_; T value_;
bool modified_; bool modified_;
T temp_value_; T temp_value_;
@@ -213,8 +211,29 @@ static void logf(const absl::FormatSpec<Args...> &format, const Args &...args) {
fout << message << std::endl; fout << message << std::endl;
} }
struct StructuredLog {
std::string raw_message;
std::string category;
};
static absl::flat_hash_map<std::string, std::vector<std::string>> log_categories;
template <typename... Args>
static void logm(const std::string &category,
const absl::FormatSpec<Args...> &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 { class Logger {
public: public:
static void log(std::string message) { static void log(std::string message) {
static std::ofstream fout(log_file_out, std::ios::out | std::ios::app); static std::ofstream fout(log_file_out, std::ios::out | std::ios::app);
fout << message << std::endl; fout << message << std::endl;
@@ -291,10 +310,12 @@ void ApplyBpsPatch(const std::vector<uint8_t> &source,
const std::vector<uint8_t> &patch, const std::vector<uint8_t> &patch,
std::vector<uint8_t> &target); std::vector<uint8_t> &target);
constexpr std::string_view kYazeVersion = "0.2.1";
absl::StatusOr<std::string> CheckVersion(const char *version); absl::StatusOr<std::string> CheckVersion(const char *version);
} // namespace core } // namespace core
} // namespace app } // namespace app
} // namespace yaze } // namespace yaze
#endif #endif