1
0

CrashHandler.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /******************************************************************************
  2. Copyright (C) 2025 by Patrick Heyer <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <utility/RemoteTextThread.hpp>
  16. #include <util/base.h>
  17. #include <QObject>
  18. #include <QThread>
  19. #include <QUuid>
  20. #include <chrono>
  21. #include <filesystem>
  22. namespace OBS {
  23. enum class PlatformType { InvalidPlatform, Windows, macOS, Linux, FreeBSD };
  24. using Clock = std::chrono::system_clock;
  25. using TimePoint = std::chrono::time_point<Clock>;
  26. class CrashHandler : public QObject {
  27. Q_OBJECT
  28. private:
  29. QUuid appLaunchUUID_;
  30. std::filesystem::path crashSentinelFile_;
  31. TimePoint lastCrashUploadTime_;
  32. std::filesystem::path lastCrashLogFile_;
  33. std::string lastCrashLogFileName_;
  34. std::string lastCrashLogURL_;
  35. bool isSentinelPresent_ = false;
  36. bool isActiveCrashHandler_ = false;
  37. std::unique_ptr<RemoteTextThread> crashLogUploadThread_;
  38. public:
  39. CrashHandler() = default;
  40. CrashHandler(QUuid appLaunchUUID);
  41. CrashHandler(const CrashHandler &other) = delete;
  42. CrashHandler &operator=(const CrashHandler &other) = delete;
  43. CrashHandler(CrashHandler &&other) noexcept;
  44. CrashHandler &operator=(CrashHandler &&other) noexcept;
  45. ~CrashHandler();
  46. bool hasUncleanShutdown();
  47. bool hasNewCrashLog();
  48. std::filesystem::path getCrashLogDirectory() const;
  49. void uploadLastCrashLog();
  50. enum class CrashLogUpdateResult { InvalidResult, NotAvailable, NotUpdated, Updated };
  51. private:
  52. void checkCrashState();
  53. void setupSentinel();
  54. std::filesystem::path findLastCrashLog() const;
  55. CrashLogUpdateResult updateLocalCrashLogState();
  56. void updateCrashLogFromConfig();
  57. void saveCrashLogToConfig();
  58. void uploadCrashLogToServer();
  59. void handleExistingCrashLogUpload();
  60. PlatformType getPlatformType() const;
  61. private slots:
  62. void crashLogUploadResultHandler(const QString &uploadResult, const QString &error);
  63. // FIXME: Turn into private slot once OBSBasic does not handle application shutdown duties anymore.
  64. public slots:
  65. void applicationShutdownHandler() noexcept;
  66. signals:
  67. void crashLogUploadStarted() const;
  68. void crashLogUploadFailed(const QString &errorMessage) const;
  69. void crashLogUploadFinished(const QString &crashLogURL) const;
  70. };
  71. } // namespace OBS