1010#include " Common/Cpp/PrettyPrint.h"
1111#include " Common/Cpp/Json/JsonArray.h"
1212#include " Common/Cpp/Json/JsonObject.h"
13+ #include " Common/Cpp/Concurrency/AsyncDispatcher.h"
1314#include " CommonFramework/Globals.h"
1415#include " CommonFramework/GlobalSettingsPanel.h"
16+ #include " CommonFramework/GlobalServices.h"
1517#include " CommonFramework/Logging/Logger.h"
1618#include " CommonFramework/Notifications/ProgramNotifications.h"
1719#include " CommonFramework/Environment/Environment.h"
@@ -165,56 +167,63 @@ SendableErrorReport::SendableErrorReport(std::string directory)
165167 JsonValue json = load_json_file (m_directory + " Report.json" );
166168 const JsonObject& obj = json.to_object_throw ();
167169 m_timestamp = obj.get_string_throw (" Timestamp" );
168- m_processor = obj.get_string_throw (" Processor" );
169- m_program = obj.get_string_throw (" Program" );
170- m_program_id = obj.get_string_throw (" ProgramID" );
171- m_program_runtime_millis = obj.get_integer_throw (" ElapsedTimeMillis" );
172- m_title = obj.get_string_throw (" Title" );
173- {
174- const JsonArray& messages = obj.get_array_throw (" Messages" );
175- for (const JsonValue& message : messages){
176- const JsonArray& item = message.to_array_throw ();
177- if (item.size () != 2 ){
178- throw ParseException (" Expected 2 values for message." );
170+
171+ // If we error from this point on, we'll just move it to the sent folder.
172+ try {
173+ m_processor = obj.get_string_throw (" Processor" );
174+ m_program = obj.get_string_throw (" Program" );
175+ m_program_id = obj.get_string_throw (" ProgramID" );
176+ m_program_runtime_millis = obj.get_integer_throw (" ElapsedTimeMillis" );
177+ m_title = obj.get_string_throw (" Title" );
178+ {
179+ const JsonArray& messages = obj.get_array_throw (" Messages" );
180+ for (const JsonValue& message : messages){
181+ const JsonArray& item = message.to_array_throw ();
182+ if (item.size () != 2 ){
183+ throw ParseException (" Expected 2 values for message." );
184+ }
185+ m_messages.emplace_back (
186+ item[0 ].to_string_throw (),
187+ item[1 ].to_string_throw ()
188+ );
179189 }
180- m_messages.emplace_back (
181- item[0 ].to_string_throw (),
182- item[1 ].to_string_throw ()
183- );
184190 }
185- }
186- {
187- const std::string* image_name = obj. get_string ( " Screenshot " );
188- if (image_name) {
189- try {
190- m_image_owner = ImageRGB32 (m_directory + *image_name) ;
191- m_image = m_image_owner;
192- }catch (FileException&){}
191+ {
192+ const std::string* image_name = obj. get_string ( " Screenshot " );
193+ if (image_name){
194+ try {
195+ m_image_owner = ImageRGB32 (m_directory + *image_name);
196+ m_image = m_image_owner ;
197+ } catch (FileException&){}
198+ }
193199 }
194- }
195- {
196- const std::string* video_name = obj. get_string ( " Video " );
197- if ( video_name){
198- m_video_name = *video_name;
200+ {
201+ const std::string* video_name = obj. get_string ( " Video " );
202+ if (video_name){
203+ m_video_name = * video_name;
204+ }
199205 }
200- }
201- {
202- const std::string* dump_name = obj. get_string ( " Dump " );
203- if ( dump_name){
204- m_dump_name = *dump_name;
206+ {
207+ const std::string* dump_name = obj. get_string ( " Dump " );
208+ if (dump_name){
209+ m_dump_name = * dump_name;
210+ }
205211 }
206- }
207- {
208- const std::string* logs_name = obj. get_string ( " Logs " );
209- if ( logs_name){
210- m_logs_name = *logs_name;
212+ {
213+ const std::string* logs_name = obj. get_string ( " Logs " );
214+ if (logs_name){
215+ m_logs_name = * logs_name;
216+ }
211217 }
212- }
213- {
214- const JsonArray& files = obj. get_array_throw ( " Files " );
215- for ( const JsonValue& file : files){
216- m_files. emplace_back (file. to_string_throw ());
218+ {
219+ const JsonArray& files = obj. get_array_throw ( " Files " );
220+ for ( const JsonValue& file : files){
221+ m_files. emplace_back (file. to_string_throw ());
222+ }
217223 }
224+ }catch (...){
225+ move_to_sent ();
226+ throw ;
218227 }
219228}
220229void SendableErrorReport::add_file (std::string filename){
@@ -283,10 +292,15 @@ void SendableErrorReport::move_to_sent(){
283292 std::string new_directory = ERROR_PATH_SENT + " /" + m_timestamp + " /" ;
284293// cout << "old: " << m_directory << endl;
285294// cout << "new: " << new_directory << endl;
286- QDir ().rename (
295+ bool success = QDir ().rename (
287296 QString::fromStdString (m_directory),
288297 QString::fromStdString (new_directory)
289298 );
299+ if (success){
300+ global_logger_tagged ().log (" Moved error report " + m_timestamp + " ." );
301+ }else {
302+ global_logger_tagged ().log (" Unable to move error report " + m_timestamp + " ." , COLOR_RED);
303+ }
290304 m_directory = std::move (new_directory);
291305}
292306
@@ -311,6 +325,8 @@ std::vector<std::string> SendableErrorReport::get_pending_reports(){
311325void send_reports (Logger& logger, const std::vector<std::string>& reports){
312326 for (const std::string& path : reports){
313327 try {
328+ // static int c = 0;
329+ // cout << "Sending... " << c++ << endl;
314330 SendableErrorReport report (path);
315331 report.send (logger);
316332 }catch (Exception& e){
@@ -320,23 +336,23 @@ void send_reports(Logger& logger, const std::vector<std::string>& reports){
320336 }
321337 }
322338}
323- void send_all_unsent_reports (Logger& logger, bool allow_prompt){
339+ std::unique_ptr<AsyncTask> send_all_unsent_reports (Logger& logger, bool allow_prompt){
324340#ifdef PA_OFFICIAL
325341 ErrorReportSendMode mode = GlobalSettings::instance ().ERROR_REPORTS ->SEND_MODE ;
326342 if (mode == ErrorReportSendMode::NEVER_SEND_ANYTHING){
327- return ;
343+ return nullptr ;
328344 }
329345
330346 std::vector<std::string> reports = SendableErrorReport::get_pending_reports ();
331347 global_logger_tagged ().log (" Found " + std::to_string (reports.size ()) + " unsent error reports." , COLOR_PURPLE);
332348
333349 if (reports.empty ()){
334- return ;
350+ return nullptr ;
335351 }
336352
337353 if (mode == ErrorReportSendMode::PROMPT_WHEN_CONVENIENT){
338354 if (!allow_prompt){
339- return ;
355+ return nullptr ;
340356 }
341357 QMessageBox box;
342358 QMessageBox::StandardButton button = box.information (
@@ -356,12 +372,15 @@ void send_all_unsent_reports(Logger& logger, bool allow_prompt){
356372 QMessageBox::StandardButton::Yes
357373 );
358374 if (button != QMessageBox::StandardButton::Yes){
359- return ;
375+ return nullptr ;
360376 }
361377 }
362378
363379 global_logger_tagged ().log (" Attempting to send " + std::to_string (reports.size ()) + " error reports." , COLOR_PURPLE);
364- send_reports (global_logger_tagged (), reports);
380+
381+ return global_async_dispatcher ().dispatch ([reports = std::move (reports)]{
382+ send_reports (global_logger_tagged (), reports);
383+ });
365384
366385#endif
367386}
0 commit comments