Overte C++ Documentation
CrashHandler.h
1 //
2 // CrashHandler.cpp
3 //
4 //
5 // Created by Dale Glass on 25/06/2023.
6 // Copyright 2023 Overte e.V.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #pragma once
13 
14 #include <QObject>
15 #include <QCoreApplication>
16 #include <SettingHandle.h>
17 #include <atomic>
18 #include <unordered_map>
19 #include <mutex>
20 
21 
22 
23 
54 class CrashHandler : public QObject {
55  Q_OBJECT
56 
57 public:
58  static CrashHandler& getInstance();
59 
60 
61 public slots:
62 
63 
72  void setPath(const QString &path);
73 
83  bool start();
84 
85 
96  void startMonitor(QCoreApplication *app);
97 
98 
99 
112  bool isStarted() const { return _crashMonitorStarted; }
113 
114 
124  bool isEnabled() const { return _crashReportingEnabled; }
125 
135  void setEnabled(bool enabled);
136 
145  void setUrl(const QString &url);
146 
158  void setToken(const QString &token);
159 
160 
161 
174  void setAnnotation(const std::string &key, const char *value);
175 
188  void setAnnotation(const std::string &key, const QString &value);
189 
203  void setAnnotation(const std::string &key, const std::string &value);
204 
205 signals:
206 
214  void enabledChanged(bool enabled);
215 
216 private:
217  CrashHandler(QObject *parent = nullptr);
218 
219 
227  void setStarted(bool started) { _crashMonitorStarted = started; }
228 
229 
230  std::atomic<bool> _crashMonitorStarted {false};
231  std::atomic<bool> _crashReportingEnabled {false};
232  std::unordered_map<std::string, std::string> _annotations{};
233  std::mutex _annotationsMutex{};
234 
235  QString _path;
236  QString _crashUrl;
237  QString _crashToken;
238 };
239 
240 
The global object in charge of setting up and controlling crash reporting.
Definition: CrashHandler.h:54
bool isStarted() const
Whether the crash monitor has been successfully started.
Definition: CrashHandler.h:112
bool isEnabled() const
Whether the crash monitor will report crashes if they occur.
Definition: CrashHandler.h:124
void setToken(const QString &token)
Set the token for the crash reporter.
Definition: CrashHandler.cpp:101
void setUrl(const QString &url)
Set the URL where to send crash reports to.
Definition: CrashHandler.cpp:89
void setAnnotation(const std::string &key, const char *value)
Set an annotation to be added to a crash.
Definition: CrashHandler.cpp:109
bool start()
Start the crash handler.
Definition: CrashHandler.cpp:42
void startMonitor(QCoreApplication *app)
Starts the unhandled exception monitor.
Definition: CrashHandler.cpp:74
void setPath(const QString &path)
Set the directory for the crash reports.
Definition: CrashHandler.cpp:28
void enabledChanged(bool enabled)
Emitted when the enabled/disabled state of the crash handler changes.
void setEnabled(bool enabled)
Set whether we want to submit crash reports to the report server.
Definition: CrashHandler.cpp:78