From bee4194f9e12c9d2210a5ecba7249bdfe3f3deda Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 14 May 2024 12:53:59 -0400 Subject: Switch to wx logging --- src/logger.cpp | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'src/logger.cpp') diff --git a/src/logger.cpp b/src/logger.cpp index 4b722c8..dddcc4a 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1,32 +1,27 @@ #include "logger.h" -#include -#include -#include - #include "global.h" -namespace { +Logger::Logger() : logfile_(GetAbsolutePath("debug.log")) {} -class Logger { - public: - Logger() : logfile_(GetAbsolutePath("debug.log")) {} +void Logger::Flush() { + wxLog::Flush(); - void LogLine(const std::string& text) { - std::lock_guard guard(file_mutex_); - logfile_ << "[" << std::chrono::system_clock::now() << "] " << text - << std::endl; - logfile_.flush(); - } + std::lock_guard guard(file_mutex_); + logfile_.flush(); +} - private: - std::ofstream logfile_; - std::mutex file_mutex_; -}; +Logger::~Logger() { + std::lock_guard guard(file_mutex_); + logfile_.flush(); +} -} // namespace +void Logger::DoLogText(const wxString& msg) { +#ifdef _WIN64 + OutputDebugStringA(msg.c_str()); + OutputDebugStringA("\r\n"); +#endif -void TrackerLog(const std::string& text) { - static Logger* instance = new Logger(); - instance->LogLine(text); + std::lock_guard guard(file_mutex_); + logfile_ << msg << std::endl; } -- cgit 1.4.1 From 0c7cb22f3da56fde00f981f9fefb971c541bc8f1 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 15 May 2024 13:19:34 -0400 Subject: Logging changes --- CMakeLists.txt | 1 - src/ap_state.cpp | 56 +++++++++++++++++++++++++------------------------------ src/game_data.cpp | 7 ++++++- src/logger.cpp | 27 --------------------------- src/logger.h | 29 ---------------------------- src/main.cpp | 14 ++++++++++++-- 6 files changed, 43 insertions(+), 91 deletions(-) delete mode 100644 src/logger.cpp delete mode 100644 src/logger.h (limited to 'src/logger.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 6dc2f4c..cd62c55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,6 @@ add_executable(lingo_ap_tracker "src/connection_dialog.cpp" "src/tracker_state.cpp" "src/tracker_config.cpp" - "src/logger.cpp" "src/achievements_pane.cpp" "src/settings_dialog.cpp" "src/global.cpp" diff --git a/src/ap_state.cpp b/src/ap_state.cpp index 68a6902..4a15db0 100644 --- a/src/ap_state.cpp +++ b/src/ap_state.cpp @@ -21,7 +21,6 @@ #include #include "game_data.h" -#include "logger.h" #include "tracker_frame.h" #include "tracker_state.h" @@ -75,7 +74,7 @@ struct APState { void Connect(std::string server, std::string player, std::string password) { if (!initialized) { - wxLogMessage("Initializing APState..."); + wxLogVerbose("Initializing APState..."); std::thread([this]() { for (;;) { @@ -108,11 +107,10 @@ struct APState { initialized = true; } - tracker_frame->SetStatusMessage("Connecting to Archipelago server...."); - wxLogMessage("Connecting to Archipelago server (%s)...", server); + wxLogStatus("Connecting to Archipelago server (%s)...", server); { - wxLogMessage("Destroying old AP client..."); + wxLogVerbose("Destroying old AP client..."); std::lock_guard client_guard(client_mutex); @@ -156,12 +154,11 @@ struct APState { apclient->set_room_info_handler([this, player, password]() { inventory.clear(); - wxLogMessage("Connected to Archipelago server. Authenticating as %s %s", - player, - (password.empty() ? " without password" - : " with password " + password)); - tracker_frame->SetStatusMessage( + wxLogStatus( "Connected to Archipelago server. Authenticating..."); + wxLogVerbose("Authenticating as %s %s", player, + (password.empty() ? "without password" + : "with password " + password)); apclient->ConnectSlot(player, password, ITEM_HANDLING, {"Tracker"}, {AP_MAJOR, AP_MINOR, AP_REVISION}); @@ -171,23 +168,19 @@ struct APState { [this](const std::list& locations) { for (const int64_t location_id : locations) { checked_locations.insert(location_id); - wxLogMessage("Location: %lld", location_id); + wxLogVerbose("Location: %lld", location_id); } RefreshTracker(false); }); apclient->set_slot_disconnected_handler([this]() { - tracker_frame->SetStatusMessage( - "Disconnected from Archipelago. Attempting to reconnect..."); - wxLogMessage( + wxLogStatus( "Slot disconnected from Archipelago. Attempting to reconnect..."); }); apclient->set_socket_disconnected_handler([this]() { - tracker_frame->SetStatusMessage( - "Disconnected from Archipelago. Attempting to reconnect..."); - wxLogMessage( + wxLogStatus( "Socket disconnected from Archipelago. Attempting to reconnect..."); }); @@ -195,7 +188,7 @@ struct APState { [this](const std::list& items) { for (const APClient::NetworkItem& item : items) { inventory[item.item]++; - wxLogMessage("Item: %lld", item.item); + wxLogVerbose("Item: %lld", item.item); } RefreshTracker(false); @@ -219,8 +212,7 @@ struct APState { apclient->set_slot_connected_handler([this]( const nlohmann::json& slot_data) { - tracker_frame->SetStatusMessage("Connected to Archipelago!"); - wxLogMessage("Connected to Archipelago!"); + wxLogStatus("Connected to Archipelago!"); data_storage_prefix = "Lingo_" + std::to_string(apclient->get_player_number()) + "_"; @@ -341,9 +333,7 @@ struct APState { DestroyClient(); - tracker_frame->SetStatusMessage("Disconnected from Archipelago."); - - wxLogMessage("Timeout while connecting to Archipelago server."); + wxLogStatus("Timeout while connecting to Archipelago server."); wxMessageBox("Timeout while connecting to Archipelago server.", "Connection failed", wxOK | wxICON_ERROR); } @@ -363,11 +353,11 @@ struct APState { void HandleDataStorage(const std::string& key, const nlohmann::json& value) { if (value.is_boolean()) { data_storage[key] = value.get(); - wxLogMessage("Data storage %s retrieved as %s", key, + wxLogVerbose("Data storage %s retrieved as %s", key, (value.get() ? "true" : "false")); } else if (value.is_number()) { data_storage[key] = value.get(); - wxLogMessage("Data storage %s retrieved as %d", key, value.get()); + wxLogVerbose("Data storage %s retrieved as %d", key, value.get()); } else if (value.is_object()) { if (key.ends_with("PlayerPos")) { auto map_value = value.get>(); @@ -376,7 +366,7 @@ struct APState { data_storage[key] = value.get>(); } - wxLogMessage("Data storage %s retrieved as dictionary", key); + wxLogVerbose("Data storage %s retrieved as dictionary", key); } else if (value.is_null()) { if (key.ends_with("PlayerPos")) { player_pos = std::nullopt; @@ -384,15 +374,19 @@ struct APState { data_storage.erase(key); } - wxLogMessage("Data storage %s retrieved as null", key); + wxLogVerbose("Data storage %s retrieved as null", key); } else if (value.is_array()) { + auto list_value = value.get>(); + if (key.ends_with("Paintings")) { - data_storage[key] = value.get>(); + data_storage[key] = + std::set(list_value.begin(), list_value.end()); } else { - data_storage[key] = value.get>(); + data_storage[key] = list_value; } - wxLogMessage("Data storage %s retrieved as list", key); + wxLogVerbose("Data storage %s retrieved as list: [%s]", key, + hatkirby::implode(list_value, ", ")); } } @@ -425,7 +419,7 @@ struct APState { } void RefreshTracker(bool reset) { - wxLogMessage("Refreshing display..."); + wxLogVerbose("Refreshing display..."); RecalculateReachability(); diff --git a/src/game_data.cpp b/src/game_data.cpp index 8af57e5..de47ba4 100644 --- a/src/game_data.cpp +++ b/src/game_data.cpp @@ -1,5 +1,11 @@ #include "game_data.h" +#include + +#ifndef WX_PRECOMP +#include +#endif + #include #include @@ -7,7 +13,6 @@ #include #include "global.h" -#include "logger.h" namespace { diff --git a/src/logger.cpp b/src/logger.cpp deleted file mode 100644 index dddcc4a..0000000 --- a/src/logger.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "logger.h" - -#include "global.h" - -Logger::Logger() : logfile_(GetAbsolutePath("debug.log")) {} - -void Logger::Flush() { - wxLog::Flush(); - - std::lock_guard guard(file_mutex_); - logfile_.flush(); -} - -Logger::~Logger() { - std::lock_guard guard(file_mutex_); - logfile_.flush(); -} - -void Logger::DoLogText(const wxString& msg) { -#ifdef _WIN64 - OutputDebugStringA(msg.c_str()); - OutputDebugStringA("\r\n"); -#endif - - std::lock_guard guard(file_mutex_); - logfile_ << msg << std::endl; -} diff --git a/src/logger.h b/src/logger.h deleted file mode 100644 index b1a1d99..0000000 --- a/src/logger.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef LOGGER_H_6E7B9594 -#define LOGGER_H_6E7B9594 - -#include - -#ifndef WX_PRECOMP -#include -#endif - -#include -#include - -class Logger : public wxLog { - public: - Logger(); - - void Flush() override; - - ~Logger(); - - protected: - void DoLogText(const wxString& msg) override; - - private: - std::ofstream logfile_; - std::mutex file_mutex_; -}; - -#endif /* end of include guard: LOGGER_H_6E7B9594 */ diff --git a/src/main.cpp b/src/main.cpp index db7653c..5b036ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,14 +4,24 @@ #include #endif -#include "logger.h" +#include + +#include "global.h" #include "tracker_config.h" #include "tracker_frame.h" +static std::ofstream* logfile; + class TrackerApp : public wxApp { public: virtual bool OnInit() { - wxLog::SetActiveTarget(new Logger()); + logfile = new std::ofstream(GetAbsolutePath("debug.log")); + wxLog::SetActiveTarget(new wxLogStream(logfile)); + +#ifndef NDEBUG + wxLog::SetVerbose(true); + wxLog::SetActiveTarget(new wxLogWindow(nullptr, "Debug Log")); +#endif GetTrackerConfig().Load(); -- cgit 1.4.1