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"
1820#include " CommonFramework/Options/Environment/ThemeSelectorOption.h"
19- #include " CommonFramework/Tools/ConsoleHandle .h"
21+ #include " CommonFramework/Recording/StreamHistorySession .h"
2022#include " ProgramDumper.h"
2123#include " ErrorReports.h"
2224
@@ -112,7 +114,7 @@ SendableErrorReport::SendableErrorReport(
112114 std::string title,
113115 std::vector<std::pair<std::string, std::string>> messages,
114116 const ImageViewRGB32& image,
115- ConsoleHandle* console
117+ const StreamHistorySession* stream_history
116118)
117119 : SendableErrorReport()
118120{
@@ -145,8 +147,8 @@ SendableErrorReport::SendableErrorReport(
145147 file.flush ();
146148 m_logs_name = ERROR_LOGS_NAME;
147149 }
148- if (console ){
149- if (console-> save_stream_history (m_directory + " Video.mp4" )){
150+ if (stream_history ){
151+ if (stream_history-> save (m_directory + " Video.mp4" )){
150152 m_video_name = " Video.mp4" ;
151153 }
152154 }
@@ -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){
@@ -270,23 +279,22 @@ void SendableErrorReport::save(Logger* logger) const{
270279 report.dump (m_directory + " Report.json" );
271280}
272281
273- #ifndef PA_OFFICIAL
274- bool SendableErrorReport::send (Logger& logger){
275- return false ;
276- }
277- #endif
278-
279282void SendableErrorReport::move_to_sent (){
280283// cout << "move_to_sent()" << endl;
281284 QDir ().mkdir (QString::fromStdString (ERROR_PATH_SENT));
282285
283286 std::string new_directory = ERROR_PATH_SENT + " /" + m_timestamp + " /" ;
284287// cout << "old: " << m_directory << endl;
285288// cout << "new: " << new_directory << endl;
286- QDir ().rename (
289+ bool success = QDir ().rename (
287290 QString::fromStdString (m_directory),
288291 QString::fromStdString (new_directory)
289292 );
293+ if (success){
294+ global_logger_tagged ().log (" Moved error report " + m_timestamp + " ." );
295+ }else {
296+ global_logger_tagged ().log (" Unable to move error report " + m_timestamp + " ." , COLOR_RED);
297+ }
290298 m_directory = std::move (new_directory);
291299}
292300
@@ -306,37 +314,46 @@ std::vector<std::string> SendableErrorReport::get_pending_reports(){
306314 return ret;
307315}
308316
317+ #ifndef PA_OFFICIAL
318+ void SendableErrorReport::send (Logger& logger, std::shared_ptr<SendableErrorReport> report){}
319+ #endif
320+
309321
310322
323+ // Send all the reports. This function will return early and all the reports
324+ // will be sent asynchronously in the background.
311325void send_reports (Logger& logger, const std::vector<std::string>& reports){
312326 for (const std::string& path : reports){
313327 try {
314- SendableErrorReport report (path);
315- report.send (logger);
328+ // static int c = 0;
329+ // cout << "Sending... " << c++ << endl;
330+ // std:::shared_ptr because it needs to take ownership for
331+ // destruction at a later time.
332+ SendableErrorReport::send (logger, std::make_shared<SendableErrorReport>(path));
316333 }catch (Exception& e){
317334 logger.log (" Unable to send report: " + path + " , Message: " + e.to_str (), COLOR_RED);
318335 }catch (...){
319336 logger.log (" Unable to send report: " + path, COLOR_RED);
320337 }
321338 }
322339}
323- void send_all_unsent_reports (Logger& logger, bool allow_prompt){
340+ std::unique_ptr<AsyncTask> send_all_unsent_reports (Logger& logger, bool allow_prompt){
324341#ifdef PA_OFFICIAL
325342 ErrorReportSendMode mode = GlobalSettings::instance ().ERROR_REPORTS ->SEND_MODE ;
326343 if (mode == ErrorReportSendMode::NEVER_SEND_ANYTHING){
327- return ;
344+ return nullptr ;
328345 }
329346
330347 std::vector<std::string> reports = SendableErrorReport::get_pending_reports ();
331348 global_logger_tagged ().log (" Found " + std::to_string (reports.size ()) + " unsent error reports." , COLOR_PURPLE);
332349
333350 if (reports.empty ()){
334- return ;
351+ return nullptr ;
335352 }
336353
337354 if (mode == ErrorReportSendMode::PROMPT_WHEN_CONVENIENT){
338355 if (!allow_prompt){
339- return ;
356+ return nullptr ;
340357 }
341358 QMessageBox box;
342359 QMessageBox::StandardButton button = box.information (
@@ -356,13 +373,17 @@ void send_all_unsent_reports(Logger& logger, bool allow_prompt){
356373 QMessageBox::StandardButton::Yes
357374 );
358375 if (button != QMessageBox::StandardButton::Yes){
359- return ;
376+ return nullptr ;
360377 }
361378 }
362379
363380 global_logger_tagged ().log (" Attempting to send " + std::to_string (reports.size ()) + " error reports." , COLOR_PURPLE);
364- send_reports (global_logger_tagged (), reports);
365381
382+ return global_async_dispatcher ().dispatch ([reports = std::move (reports)]{
383+ send_reports (global_logger_tagged (), reports);
384+ });
385+ #else
386+ return nullptr ;
366387#endif
367388}
368389
@@ -372,7 +393,7 @@ void report_error(
372393 std::string title,
373394 std::vector<std::pair<std::string, std::string>> messages,
374395 const ImageViewRGB32& image,
375- ConsoleHandle* console ,
396+ const StreamHistorySession* stream_history ,
376397 const std::vector<std::string>& files
377398){
378399 if (logger == nullptr ){
@@ -386,7 +407,7 @@ void report_error(
386407 std::move (title),
387408 std::move (messages),
388409 image,
389- console
410+ stream_history
390411 );
391412
392413 std::vector<std::string> full_file_paths;
0 commit comments