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/ap_state.cpp | 43 ++++++++++++++++++++-------------------- src/game_data.cpp | 59 ++++++++++++++++++++----------------------------------- src/logger.cpp | 39 ++++++++++++++++-------------------- src/logger.h | 25 +++++++++++++++++++++-- src/main.cpp | 3 +++ 5 files changed, 85 insertions(+), 84 deletions(-) (limited to 'src') diff --git a/src/ap_state.cpp b/src/ap_state.cpp index b057beb..e5ff74d 100644 --- a/src/ap_state.cpp +++ b/src/ap_state.cpp @@ -75,7 +75,7 @@ struct APState { void Connect(std::string server, std::string player, std::string password) { if (!initialized) { - TrackerLog("Initializing APState..."); + wxLogMessage("Initializing APState..."); std::thread([this]() { for (;;) { @@ -108,10 +108,10 @@ struct APState { } tracker_frame->SetStatusMessage("Connecting to Archipelago server...."); - TrackerLog("Connecting to Archipelago server (" + server + ")..."); + wxLogMessage("Connecting to Archipelago server (%s)...", server); { - TrackerLog("Destroying old AP client..."); + wxLogMessage("Destroying old AP client..."); std::lock_guard client_guard(client_mutex); @@ -155,10 +155,10 @@ struct APState { apclient->set_room_info_handler([this, player, password]() { inventory.clear(); - TrackerLog("Connected to Archipelago server. Authenticating as " + - player + - (password.empty() ? " without password" - : " with password " + password)); + wxLogMessage("Connected to Archipelago server. Authenticating as %s %s", + player, + (password.empty() ? " without password" + : " with password " + password)); tracker_frame->SetStatusMessage( "Connected to Archipelago server. Authenticating..."); @@ -170,7 +170,7 @@ struct APState { [this](const std::list& locations) { for (const int64_t location_id : locations) { checked_locations.insert(location_id); - TrackerLog("Location: " + std::to_string(location_id)); + wxLogMessage("Location: %lld", location_id); } RefreshTracker(false); @@ -179,14 +179,14 @@ struct APState { apclient->set_slot_disconnected_handler([this]() { tracker_frame->SetStatusMessage( "Disconnected from Archipelago. Attempting to reconnect..."); - TrackerLog( + wxLogMessage( "Slot disconnected from Archipelago. Attempting to reconnect..."); }); apclient->set_socket_disconnected_handler([this]() { tracker_frame->SetStatusMessage( "Disconnected from Archipelago. Attempting to reconnect..."); - TrackerLog( + wxLogMessage( "Socket disconnected from Archipelago. Attempting to reconnect..."); }); @@ -194,7 +194,7 @@ struct APState { [this](const std::list& items) { for (const APClient::NetworkItem& item : items) { inventory[item.item]++; - TrackerLog("Item: " + std::to_string(item.item)); + wxLogMessage("Item: %lld", item.item); } RefreshTracker(false); @@ -219,7 +219,7 @@ struct APState { apclient->set_slot_connected_handler([this]( const nlohmann::json& slot_data) { tracker_frame->SetStatusMessage("Connected to Archipelago!"); - TrackerLog("Connected to Archipelago!"); + wxLogMessage("Connected to Archipelago!"); data_storage_prefix = "Lingo_" + std::to_string(apclient->get_player_number()) + "_"; @@ -323,7 +323,7 @@ struct APState { } std::string full_message = hatkirby::implode(error_messages, " "); - TrackerLog(full_message); + wxLogError(wxString(full_message)); wxMessageBox(full_message, "Connection failed", wxOK | wxICON_ERROR); }); @@ -342,7 +342,7 @@ struct APState { tracker_frame->SetStatusMessage("Disconnected from Archipelago."); - TrackerLog("Timeout while connecting to Archipelago server."); + wxLogMessage("Timeout while connecting to Archipelago server."); wxMessageBox("Timeout while connecting to Archipelago server.", "Connection failed", wxOK | wxICON_ERROR); } @@ -362,12 +362,11 @@ struct APState { void HandleDataStorage(const std::string& key, const nlohmann::json& value) { if (value.is_boolean()) { data_storage[key] = value.get(); - TrackerLog("Data storage " + key + " retrieved as " + - (value.get() ? "true" : "false")); + wxLogMessage("Data storage %s retrieved as %s", key, + (value.get() ? "true" : "false")); } else if (value.is_number()) { data_storage[key] = value.get(); - TrackerLog("Data storage " + key + " retrieved as " + - std::to_string(value.get())); + wxLogMessage("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 +375,7 @@ struct APState { data_storage[key] = value.get>(); } - TrackerLog("Data storage " + key + " retrieved as dictionary"); + wxLogMessage("Data storage %s retrieved as dictionary", key); } else if (value.is_null()) { if (key.ends_with("PlayerPos")) { player_pos = std::nullopt; @@ -384,7 +383,7 @@ struct APState { data_storage.erase(key); } - TrackerLog("Data storage " + key + " retrieved as null"); + wxLogMessage("Data storage %s retrieved as null", key); } } @@ -408,7 +407,7 @@ struct APState { } void RefreshTracker(bool reset) { - TrackerLog("Refreshing display..."); + wxLogMessage("Refreshing display..."); RecalculateReachability(); @@ -422,7 +421,7 @@ struct APState { int64_t GetItemId(const std::string& item_name) { int64_t ap_id = apclient->get_item_id(item_name); if (ap_id == APClient::INVALID_NAME_ID) { - TrackerLog("Could not find AP item ID for " + item_name); + wxLogError("Could not find AP item ID for %s", item_name); } return ap_id; diff --git a/src/game_data.cpp b/src/game_data.cpp index 74f872c..7bc3134 100644 --- a/src/game_data.cpp +++ b/src/game_data.cpp @@ -31,9 +31,7 @@ LingoColor GetColorForString(const std::string &str) { } else if (str == "purple") { return LingoColor::kPurple; } else { - std::ostringstream errmsg; - errmsg << "Invalid color: " << str; - TrackerLog(errmsg.str()); + wxLogError("Invalid color: %s", str); return LingoColor::kNone; } @@ -83,9 +81,7 @@ struct GameData { ap_id_by_color_[GetColorForString(input_name)] = ids_config["special_items"][color_name].as(); } else { - std::ostringstream errmsg; - errmsg << "Missing AP item ID for color " << color_name; - TrackerLog(errmsg.str()); + wxLogError("Missing AP item ID for color %s", color_name); } }; @@ -160,8 +156,9 @@ struct GameData { } default: { // This shouldn't happen. - std::cout << "Error reading game data: " << entrance_it - << std::endl; + std::ostringstream formatted; + formatted << entrance_it; + wxLogError("Error reading game data: %s", formatted.str()); break; } } @@ -281,10 +278,8 @@ struct GameData { [panels_[panel_id].name] .as(); } else { - std::ostringstream errmsg; - errmsg << "Missing AP location ID for panel " - << rooms_[room_id].name << " - " << panels_[panel_id].name; - TrackerLog(errmsg.str()); + wxLogError("Missing AP location ID for panel %s - %s", + rooms_[room_id].name, panels_[panel_id].name); } } } @@ -347,10 +342,8 @@ struct GameData { [doors_[door_id].name]["item"] .as(); } else { - std::ostringstream errmsg; - errmsg << "Missing AP item ID for door " << rooms_[room_id].name - << " - " << doors_[door_id].name; - TrackerLog(errmsg.str()); + wxLogError("Missing AP item ID for door %s - %s", + rooms_[room_id].name, doors_[door_id].name); } } @@ -364,10 +357,8 @@ struct GameData { ids_config["door_groups"][doors_[door_id].group_name] .as(); } else { - std::ostringstream errmsg; - errmsg << "Missing AP item ID for door group " - << doors_[door_id].group_name; - TrackerLog(errmsg.str()); + wxLogError("Missing AP item ID for door group %s", + doors_[door_id].group_name); } } @@ -377,13 +368,11 @@ struct GameData { } else if (!door_it.second["skip_location"] && !door_it.second["event"]) { if (has_external_panels) { - std::ostringstream errmsg; - errmsg - << rooms_[room_id].name << " - " << doors_[door_id].name - << " has panels from other rooms but does not have an " - "explicit " - "location name and is not marked skip_location or event"; - TrackerLog(errmsg.str()); + wxLogError( + "%s - %s has panels from other rooms but does not have an " + "explicit location name and is not marked skip_location or " + "event", + rooms_[room_id].name, doors_[door_id].name); } doors_[door_id].location_name = @@ -403,10 +392,8 @@ struct GameData { [doors_[door_id].name]["location"] .as(); } else { - std::ostringstream errmsg; - errmsg << "Missing AP location ID for door " - << rooms_[room_id].name << " - " << doors_[door_id].name; - TrackerLog(errmsg.str()); + wxLogError("Missing AP location ID for door %s - %s", + rooms_[room_id].name, doors_[door_id].name); } } @@ -472,10 +459,8 @@ struct GameData { progressive_item_id = ids_config["progression"][progressive_item_name].as(); } else { - std::ostringstream errmsg; - errmsg << "Missing AP item ID for progressive item " - << progressive_item_name; - TrackerLog(errmsg.str()); + wxLogError("Missing AP item ID for progressive item %s", + progressive_item_name); } int index = 1; @@ -606,9 +591,7 @@ struct GameData { // Report errors. for (const std::string &area : malconfigured_areas_) { - std::ostringstream errstr; - errstr << "Area data not found for: " << area; - TrackerLog(errstr.str()); + wxLogError("Area data not found for: %s", area); } // Read in subway items. 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; } diff --git a/src/logger.h b/src/logger.h index db9bb49..b1a1d99 100644 --- a/src/logger.h +++ b/src/logger.h @@ -1,8 +1,29 @@ #ifndef LOGGER_H_6E7B9594 #define LOGGER_H_6E7B9594 -#include +#include -void TrackerLog(const std::string& text); +#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 fe9aceb..db7653c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,12 +4,15 @@ #include #endif +#include "logger.h" #include "tracker_config.h" #include "tracker_frame.h" class TrackerApp : public wxApp { public: virtual bool OnInit() { + wxLog::SetActiveTarget(new Logger()); + GetTrackerConfig().Load(); TrackerFrame *frame = new TrackerFrame(); -- cgit 1.4.1