From 33bf1f9653ed608de5554940dc8a0c3f5dc7e4ea Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 10 Jun 2024 19:13:22 -0400 Subject: Go back to old logging system Brought in libfmt to handle string formatting and replace a bunch of ostringstream uses. --- src/ap_state.cpp | 79 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 39 deletions(-) (limited to 'src/ap_state.cpp') diff --git a/src/ap_state.cpp b/src/ap_state.cpp index 8ff0ccd..e4d892b 100644 --- a/src/ap_state.cpp +++ b/src/ap_state.cpp @@ -4,6 +4,7 @@ #define _WEBSOCKETPP_CPP11_STRICT_ #pragma comment(lib, "crypt32") +#include #include #include @@ -21,6 +22,7 @@ #include #include "game_data.h" +#include "logger.h" #include "tracker_frame.h" #include "tracker_state.h" @@ -75,7 +77,7 @@ struct APState { void Connect(std::string server, std::string player, std::string password) { if (!initialized) { - wxLogVerbose("Initializing APState..."); + TrackerLog("Initializing APState..."); std::thread([this]() { for (;;) { @@ -91,14 +93,14 @@ struct APState { }).detach(); for (int panel_id : GD_GetAchievementPanels()) { - tracked_data_storage_keys.push_back( - "Achievement|" + GD_GetPanel(panel_id).achievement_name); + tracked_data_storage_keys.push_back(fmt::format( + "Achievement|{}", GD_GetPanel(panel_id).achievement_name)); } for (const MapArea& map_area : GD_GetMapAreas()) { for (const Location& location : map_area.locations) { tracked_data_storage_keys.push_back( - "Hunt|" + std::to_string(location.ap_location_id)); + fmt::format("Hunt|{}", location.ap_location_id)); } } @@ -109,10 +111,10 @@ struct APState { } tracker_frame->SetStatusMessage("Connecting to Archipelago server...."); - wxLogStatus("Connecting to Archipelago server (%s)...", server); + TrackerLog(fmt::format("Connecting to Archipelago server ({})...", server)); { - wxLogVerbose("Destroying old AP client..."); + TrackerLog("Destroying old AP client..."); std::lock_guard client_guard(client_mutex); @@ -158,10 +160,10 @@ struct APState { apclient->set_room_info_handler([this, player, password]() { inventory.clear(); - wxLogStatus("Connected to Archipelago server. Authenticating as %s %s", - player, - (password.empty() ? "without password" - : "with password " + password)); + TrackerLog(fmt::format( + "Connected to Archipelago server. Authenticating as {} {}", player, + (password.empty() ? "without password" + : "with password " + password))); tracker_frame->SetStatusMessage( "Connected to Archipelago server. Authenticating..."); @@ -173,7 +175,7 @@ struct APState { [this](const std::list& locations) { for (const int64_t location_id : locations) { checked_locations.insert(location_id); - wxLogVerbose("Location: %lld", location_id); + TrackerLog(fmt::format("Location: {}", location_id)); } RefreshTracker(false); @@ -182,14 +184,14 @@ struct APState { apclient->set_slot_disconnected_handler([this]() { tracker_frame->SetStatusMessage( "Disconnected from Archipelago. Attempting to reconnect..."); - wxLogStatus( + TrackerLog( "Slot disconnected from Archipelago. Attempting to reconnect..."); }); apclient->set_socket_disconnected_handler([this]() { tracker_frame->SetStatusMessage( "Disconnected from Archipelago. Attempting to reconnect..."); - wxLogStatus( + TrackerLog( "Socket disconnected from Archipelago. Attempting to reconnect..."); }); @@ -197,7 +199,7 @@ struct APState { [this](const std::list& items) { for (const APClient::NetworkItem& item : items) { inventory[item.item]++; - wxLogVerbose("Item: %lld", item.item); + TrackerLog(fmt::format("Item: {}", item.item)); } RefreshTracker(false); @@ -222,10 +224,10 @@ struct APState { apclient->set_slot_connected_handler([this, &connection_mutex]( const nlohmann::json& slot_data) { tracker_frame->SetStatusMessage("Connected to Archipelago!"); - wxLogStatus("Connected to Archipelago!"); + TrackerLog("Connected to Archipelago!"); data_storage_prefix = - "Lingo_" + std::to_string(apclient->get_player_number()) + "_"; + fmt::format("Lingo_{}_", apclient->get_player_number()); door_shuffle_mode = slot_data["shuffle_doors"].get(); color_shuffle = slot_data["shuffle_colors"].get() == 1; painting_shuffle = slot_data["shuffle_paintings"].get() == 1; @@ -277,18 +279,18 @@ struct APState { corrected_keys.push_back(data_storage_prefix + key); } - { - std::ostringstream vdsks; - vdsks << "_read_client_status_" << apclient->get_team_number() << "_" - << apclient->get_player_number(); - victory_data_storage_key = vdsks.str(); - } + victory_data_storage_key = + fmt::format("_read_client_status_{}_{}", apclient->get_team_number(), + apclient->get_player_number()); corrected_keys.push_back(victory_data_storage_key); apclient->Get(corrected_keys); apclient->SetNotify(corrected_keys); + ResetReachabilityRequirements(); + RefreshTracker(true); + { std::lock_guard connection_lock(connection_mutex); if (!has_connection_result) { @@ -333,7 +335,7 @@ struct APState { } std::string full_message = hatkirby::implode(error_messages, " "); - wxLogError(wxString(full_message)); + TrackerLog(full_message); wxMessageBox(full_message, "Connection failed", wxOK | wxICON_ERROR); }); @@ -355,7 +357,7 @@ struct APState { DestroyClient(); tracker_frame->SetStatusMessage("Disconnected from Archipelago."); - wxLogStatus("Timeout while connecting to Archipelago server."); + TrackerLog("Timeout while connecting to Archipelago server."); wxMessageBox("Timeout while connecting to Archipelago server.", "Connection failed", wxOK | wxICON_ERROR); @@ -374,9 +376,6 @@ struct APState { } if (connected) { - ResetReachabilityRequirements(); - RefreshTracker(true); - } else { client_active = false; } } @@ -384,11 +383,12 @@ struct APState { void HandleDataStorage(const std::string& key, const nlohmann::json& value) { if (value.is_boolean()) { data_storage[key] = value.get(); - wxLogVerbose("Data storage %s retrieved as %s", key, - (value.get() ? "true" : "false")); + TrackerLog(fmt::format("Data storage {} retrieved as {}", key, + (value.get() ? "true" : "false"))); } else if (value.is_number()) { data_storage[key] = value.get(); - wxLogVerbose("Data storage %s retrieved as %d", key, value.get()); + TrackerLog(fmt::format("Data storage {} retrieved as {}", key, + value.get())); } else if (value.is_object()) { if (key.ends_with("PlayerPos")) { auto map_value = value.get>(); @@ -397,7 +397,7 @@ struct APState { data_storage[key] = value.get>(); } - wxLogVerbose("Data storage %s retrieved as dictionary", key); + TrackerLog(fmt::format("Data storage {} retrieved as dictionary", key)); } else if (value.is_null()) { if (key.ends_with("PlayerPos")) { player_pos = std::nullopt; @@ -405,7 +405,7 @@ struct APState { data_storage.erase(key); } - wxLogVerbose("Data storage %s retrieved as null", key); + TrackerLog(fmt::format("Data storage {} retrieved as null", key)); } else if (value.is_array()) { auto list_value = value.get>(); @@ -416,8 +416,8 @@ struct APState { data_storage[key] = list_value; } - wxLogVerbose("Data storage %s retrieved as list: [%s]", key, - hatkirby::implode(list_value, ", ")); + TrackerLog(fmt::format("Data storage {} retrieved as list: [{}]", key, + hatkirby::implode(list_value, ", "))); } } @@ -427,7 +427,7 @@ struct APState { bool HasCheckedHuntPanel(int location_id) { std::string key = - data_storage_prefix + "Hunt|" + std::to_string(location_id); + fmt::format("{}Hunt|{}", data_storage_prefix, location_id); return data_storage.count(key) && std::any_cast(data_storage.at(key)); } @@ -436,12 +436,13 @@ struct APState { } bool HasAchievement(const std::string& name) { - std::string key = data_storage_prefix + "Achievement|" + name; + std::string key = + fmt::format("{}Achievement|{}", data_storage_prefix, name); return data_storage.count(key) && std::any_cast(data_storage.at(key)); } const std::set& GetCheckedPaintings() { - std::string key = data_storage_prefix + "Paintings"; + std::string key = fmt::format("{}Paintings", data_storage_prefix); if (!data_storage.count(key)) { data_storage[key] = std::set(); } @@ -458,7 +459,7 @@ struct APState { } void RefreshTracker(bool reset) { - wxLogVerbose("Refreshing display..."); + TrackerLog("Refreshing display..."); RecalculateReachability(); @@ -472,7 +473,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) { - wxLogError("Could not find AP item ID for %s", item_name); + TrackerLog(fmt::format("Could not find AP item ID for {}", item_name)); } return ap_id; -- cgit 1.4.1