From fd523003a7317852535dee17384da51a3e2bd695 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 18 May 2023 17:13:08 -0400 Subject: Added some logging --- src/ap_state.cpp | 29 ++++++++++++++++++++++++----- src/logger.cpp | 30 ++++++++++++++++++++++++++++++ src/logger.h | 8 ++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/logger.cpp create mode 100644 src/logger.h (limited to 'src') diff --git a/src/ap_state.cpp b/src/ap_state.cpp index a0d0f66..8a3aaf9 100644 --- a/src/ap_state.cpp +++ b/src/ap_state.cpp @@ -19,6 +19,7 @@ #include #include "game_data.h" +#include "logger.h" #include "tracker_frame.h" #include "tracker_state.h" @@ -62,6 +63,8 @@ struct APState { void Connect(std::string server, std::string player, std::string password) { if (!initialized) { + TrackerLog("Initializing APState..."); + std::thread([this]() { for (;;) { { @@ -79,8 +82,11 @@ struct APState { } tracker_frame->SetStatusMessage("Connecting to Archipelago server...."); + TrackerLog("Connecting to Archipelago server (" + server + ")..."); { + TrackerLog("Destroying old AP client..."); + std::lock_guard client_guard(client_mutex); if (apclient) { @@ -111,6 +117,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)); tracker_frame->SetStatusMessage( "Connected to Archipelago server. Authenticating..."); @@ -122,7 +132,7 @@ struct APState { [this](const std::list& locations) { for (const int64_t location_id : locations) { checked_locations.insert(location_id); - std::cout << "Location: " << location_id << std::endl; + TrackerLog("Location: " + std::to_string(location_id)); } RefreshTracker(); @@ -131,18 +141,22 @@ struct APState { apclient->set_slot_disconnected_handler([this]() { tracker_frame->SetStatusMessage( "Disconnected from Archipelago. Attempting to reconnect..."); + TrackerLog( + "Slot disconnected from Archipelago. Attempting to reconnect..."); }); apclient->set_socket_disconnected_handler([this]() { tracker_frame->SetStatusMessage( "Disconnected from Archipelago. Attempting to reconnect..."); + TrackerLog( + "Socket disconnected from Archipelago. Attempting to reconnect..."); }); apclient->set_items_received_handler( [this](const std::list& items) { for (const APClient::NetworkItem& item : items) { inventory[item.item]++; - std::cout << "Item: " << item.item << std::endl; + TrackerLog("Item: " + std::to_string(item.item)); } RefreshTracker(); @@ -151,6 +165,7 @@ struct APState { apclient->set_slot_connected_handler([this]( const nlohmann::json& slot_data) { tracker_frame->SetStatusMessage("Connected to Archipelago!"); + TrackerLog("Connected to Archipelago!"); door_shuffle_mode = slot_data["shuffle_doors"].get(); color_shuffle = slot_data["shuffle_colors"].get(); @@ -206,6 +221,7 @@ struct APState { } std::string full_message = hatkirby::implode(error_messages, " "); + TrackerLog(full_message); wxMessageBox(full_message, "Connection failed", wxOK | wxICON_ERROR); }); @@ -224,6 +240,7 @@ struct APState { tracker_frame->SetStatusMessage("Disconnected from Archipelago."); + TrackerLog("Timeout while connecting to Archipelago server."); wxMessageBox("Timeout while connecting to Archipelago server.", "Connection failed", wxOK | wxICON_ERROR); } @@ -241,8 +258,8 @@ struct APState { int64_t ap_id = apclient->get_location_id(location.ap_location_name); if (ap_id == APClient::INVALID_NAME_ID) { - std::cout << "Could not find AP location ID for " - << location.ap_location_name << std::endl; + TrackerLog("Could not find AP location ID for " + + location.ap_location_name); } else { ap_id_by_location_id[{map_area.id, section_id}] = ap_id; } @@ -309,6 +326,8 @@ struct APState { } void RefreshTracker() { + TrackerLog("Refreshing display..."); + RecalculateReachability(); tracker_frame->UpdateIndicators(); } @@ -316,7 +335,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) { - std::cout << "Could not find AP item ID for " << item_name << std::endl; + TrackerLog("Could not find AP item ID for " + item_name); } return ap_id; diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..ccb721a --- /dev/null +++ b/src/logger.cpp @@ -0,0 +1,30 @@ +#include "logger.h" + +#include +#include +#include + +namespace { + +class Logger { + public: + Logger() : logfile_("debug.log") {} + + void LogLine(const std::string& text) { + std::lock_guard guard(file_mutex_); + logfile_ << "[" << std::chrono::system_clock::now() << "] " << text + << std::endl; + logfile_.flush(); + } + + private: + std::ofstream logfile_; + std::mutex file_mutex_; +}; + +} // namespace + +void TrackerLog(const std::string& text) { + static Logger* instance = new Logger(); + instance->LogLine(text); +} diff --git a/src/logger.h b/src/logger.h new file mode 100644 index 0000000..db9bb49 --- /dev/null +++ b/src/logger.h @@ -0,0 +1,8 @@ +#ifndef LOGGER_H_6E7B9594 +#define LOGGER_H_6E7B9594 + +#include + +void TrackerLog(const std::string& text); + +#endif /* end of include guard: LOGGER_H_6E7B9594 */ -- cgit 1.4.1