about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/ap_state.cpp29
-rw-r--r--src/logger.cpp30
-rw-r--r--src/logger.h8
4 files changed, 63 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e12e58e..0b68a5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -38,6 +38,7 @@ add_executable(lingo_ap_tracker
38 "src/eye_indicator.cpp" 38 "src/eye_indicator.cpp"
39 "src/tracker_state.cpp" 39 "src/tracker_state.cpp"
40 "src/tracker_config.cpp" 40 "src/tracker_config.cpp"
41 "src/logger.cpp"
41) 42)
42set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD 20) 43set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD 20)
43set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD_REQUIRED ON) 44set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD_REQUIRED ON)
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 @@
19#include <tuple> 19#include <tuple>
20 20
21#include "game_data.h" 21#include "game_data.h"
22#include "logger.h"
22#include "tracker_frame.h" 23#include "tracker_frame.h"
23#include "tracker_state.h" 24#include "tracker_state.h"
24 25
@@ -62,6 +63,8 @@ struct APState {
62 63
63 void Connect(std::string server, std::string player, std::string password) { 64 void Connect(std::string server, std::string player, std::string password) {
64 if (!initialized) { 65 if (!initialized) {
66 TrackerLog("Initializing APState...");
67
65 std::thread([this]() { 68 std::thread([this]() {
66 for (;;) { 69 for (;;) {
67 { 70 {
@@ -79,8 +82,11 @@ struct APState {
79 } 82 }
80 83
81 tracker_frame->SetStatusMessage("Connecting to Archipelago server...."); 84 tracker_frame->SetStatusMessage("Connecting to Archipelago server....");
85 TrackerLog("Connecting to Archipelago server (" + server + ")...");
82 86
83 { 87 {
88 TrackerLog("Destroying old AP client...");
89
84 std::lock_guard client_guard(client_mutex); 90 std::lock_guard client_guard(client_mutex);
85 91
86 if (apclient) { 92 if (apclient) {
@@ -111,6 +117,10 @@ struct APState {
111 apclient->set_room_info_handler([this, player, password]() { 117 apclient->set_room_info_handler([this, player, password]() {
112 inventory.clear(); 118 inventory.clear();
113 119
120 TrackerLog("Connected to Archipelago server. Authenticating as " +
121 player +
122 (password.empty() ? " without password"
123 : " with password " + password));
114 tracker_frame->SetStatusMessage( 124 tracker_frame->SetStatusMessage(
115 "Connected to Archipelago server. Authenticating..."); 125 "Connected to Archipelago server. Authenticating...");
116 126
@@ -122,7 +132,7 @@ struct APState {
122 [this](const std::list<int64_t>& locations) { 132 [this](const std::list<int64_t>& locations) {
123 for (const int64_t location_id : locations) { 133 for (const int64_t location_id : locations) {
124 checked_locations.insert(location_id); 134 checked_locations.insert(location_id);
125 std::cout << "Location: " << location_id << std::endl; 135 TrackerLog("Location: " + std::to_string(location_id));
126 } 136 }
127 137
128 RefreshTracker(); 138 RefreshTracker();
@@ -131,18 +141,22 @@ struct APState {
131 apclient->set_slot_disconnected_handler([this]() { 141 apclient->set_slot_disconnected_handler([this]() {
132 tracker_frame->SetStatusMessage( 142 tracker_frame->SetStatusMessage(
133 "Disconnected from Archipelago. Attempting to reconnect..."); 143 "Disconnected from Archipelago. Attempting to reconnect...");
144 TrackerLog(
145 "Slot disconnected from Archipelago. Attempting to reconnect...");
134 }); 146 });
135 147
136 apclient->set_socket_disconnected_handler([this]() { 148 apclient->set_socket_disconnected_handler([this]() {
137 tracker_frame->SetStatusMessage( 149 tracker_frame->SetStatusMessage(
138 "Disconnected from Archipelago. Attempting to reconnect..."); 150 "Disconnected from Archipelago. Attempting to reconnect...");
151 TrackerLog(
152 "Socket disconnected from Archipelago. Attempting to reconnect...");
139 }); 153 });
140 154
141 apclient->set_items_received_handler( 155 apclient->set_items_received_handler(
142 [this](const std::list<APClient::NetworkItem>& items) { 156 [this](const std::list<APClient::NetworkItem>& items) {
143 for (const APClient::NetworkItem& item : items) { 157 for (const APClient::NetworkItem& item : items) {
144 inventory[item.item]++; 158 inventory[item.item]++;
145 std::cout << "Item: " << item.item << std::endl; 159 TrackerLog("Item: " + std::to_string(item.item));
146 } 160 }
147 161
148 RefreshTracker(); 162 RefreshTracker();
@@ -151,6 +165,7 @@ struct APState {
151 apclient->set_slot_connected_handler([this]( 165 apclient->set_slot_connected_handler([this](
152 const nlohmann::json& slot_data) { 166 const nlohmann::json& slot_data) {
153 tracker_frame->SetStatusMessage("Connected to Archipelago!"); 167 tracker_frame->SetStatusMessage("Connected to Archipelago!");
168 TrackerLog("Connected to Archipelago!");
154 169
155 door_shuffle_mode = slot_data["shuffle_doors"].get<DoorShuffleMode>(); 170 door_shuffle_mode = slot_data["shuffle_doors"].get<DoorShuffleMode>();
156 color_shuffle = slot_data["shuffle_colors"].get<bool>(); 171 color_shuffle = slot_data["shuffle_colors"].get<bool>();
@@ -206,6 +221,7 @@ struct APState {
206 } 221 }
207 222
208 std::string full_message = hatkirby::implode(error_messages, " "); 223 std::string full_message = hatkirby::implode(error_messages, " ");
224 TrackerLog(full_message);
209 225
210 wxMessageBox(full_message, "Connection failed", wxOK | wxICON_ERROR); 226 wxMessageBox(full_message, "Connection failed", wxOK | wxICON_ERROR);
211 }); 227 });
@@ -224,6 +240,7 @@ struct APState {
224 240
225 tracker_frame->SetStatusMessage("Disconnected from Archipelago."); 241 tracker_frame->SetStatusMessage("Disconnected from Archipelago.");
226 242
243 TrackerLog("Timeout while connecting to Archipelago server.");
227 wxMessageBox("Timeout while connecting to Archipelago server.", 244 wxMessageBox("Timeout while connecting to Archipelago server.",
228 "Connection failed", wxOK | wxICON_ERROR); 245 "Connection failed", wxOK | wxICON_ERROR);
229 } 246 }
@@ -241,8 +258,8 @@ struct APState {
241 258
242 int64_t ap_id = apclient->get_location_id(location.ap_location_name); 259 int64_t ap_id = apclient->get_location_id(location.ap_location_name);
243 if (ap_id == APClient::INVALID_NAME_ID) { 260 if (ap_id == APClient::INVALID_NAME_ID) {
244 std::cout << "Could not find AP location ID for " 261 TrackerLog("Could not find AP location ID for " +
245 << location.ap_location_name << std::endl; 262 location.ap_location_name);
246 } else { 263 } else {
247 ap_id_by_location_id[{map_area.id, section_id}] = ap_id; 264 ap_id_by_location_id[{map_area.id, section_id}] = ap_id;
248 } 265 }
@@ -309,6 +326,8 @@ struct APState {
309 } 326 }
310 327
311 void RefreshTracker() { 328 void RefreshTracker() {
329 TrackerLog("Refreshing display...");
330
312 RecalculateReachability(); 331 RecalculateReachability();
313 tracker_frame->UpdateIndicators(); 332 tracker_frame->UpdateIndicators();
314 } 333 }
@@ -316,7 +335,7 @@ struct APState {
316 int64_t GetItemId(const std::string& item_name) { 335 int64_t GetItemId(const std::string& item_name) {
317 int64_t ap_id = apclient->get_item_id(item_name); 336 int64_t ap_id = apclient->get_item_id(item_name);
318 if (ap_id == APClient::INVALID_NAME_ID) { 337 if (ap_id == APClient::INVALID_NAME_ID) {
319 std::cout << "Could not find AP item ID for " << item_name << std::endl; 338 TrackerLog("Could not find AP item ID for " + item_name);
320 } 339 }
321 340
322 return ap_id; 341 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 @@
1#include "logger.h"
2
3#include <chrono>
4#include <fstream>
5#include <mutex>
6
7namespace {
8
9class Logger {
10 public:
11 Logger() : logfile_("debug.log") {}
12
13 void LogLine(const std::string& text) {
14 std::lock_guard guard(file_mutex_);
15 logfile_ << "[" << std::chrono::system_clock::now() << "] " << text
16 << std::endl;
17 logfile_.flush();
18 }
19
20 private:
21 std::ofstream logfile_;
22 std::mutex file_mutex_;
23};
24
25} // namespace
26
27void TrackerLog(const std::string& text) {
28 static Logger* instance = new Logger();
29 instance->LogLine(text);
30}
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 @@
1#ifndef LOGGER_H_6E7B9594
2#define LOGGER_H_6E7B9594
3
4#include <string>
5
6void TrackerLog(const std::string& text);
7
8#endif /* end of include guard: LOGGER_H_6E7B9594 */