OpenTTD Source  20241108-master-g80f628063a
crashlog.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef CRASHLOG_H
11 #define CRASHLOG_H
12 
13 #include "3rdparty/nlohmann/json.hpp"
14 
18 class CrashLog {
19 private:
21  static std::string message;
22 
28  virtual void SurveyCrash(nlohmann::json &survey) const = 0;
29 
35  virtual void SurveyStacktrace(nlohmann::json &survey) const = 0;
36 
46  virtual bool TryExecute(std::string_view section_name, std::function<bool()> &&func) = 0;
47 
48 protected:
49  std::string CreateFileName(const char *ext, bool with_dir = true) const;
50 
51 public:
53  virtual ~CrashLog() = default;
54 
55  nlohmann::json survey;
56  std::string crashlog_filename;
57  std::string crashdump_filename;
58  std::string savegame_filename;
59  std::string screenshot_filename;
60 
61  void FillCrashLog();
62  void PrintCrashLog() const;
63 
64  bool WriteCrashLog();
65  virtual bool WriteCrashDump();
66  bool WriteSavegame();
67  bool WriteScreenshot();
68 
69  void SendSurvey() const;
70 
71  void MakeCrashLog();
72 
78  static void InitialiseCrashLog();
79 
84  static void InitThread();
85 
86  static void SetErrorMessage(const std::string &message);
87  static void AfterCrashLogCleanup();
88 };
89 
90 #endif /* CRASHLOG_H */
Helper class for creating crash logs.
Definition: crashlog.h:18
void MakeCrashLog()
Makes the crash log, writes it to a file and then subsequently tries to make a crash dump and crash s...
Definition: crashlog.cpp:270
bool WriteCrashLog()
Write the crash log to a file.
Definition: crashlog.cpp:189
virtual void SurveyStacktrace(nlohmann::json &survey) const =0
Convert stacktrace to JSON.
std::string CreateFileName(const char *ext, bool with_dir=true) const
Create a timestamped filename.
Definition: crashlog.cpp:74
static std::string message
Error message coming from #FatalError(format, ...).
Definition: crashlog.h:21
void SendSurvey() const
Send the survey result, noting it was a crash.
Definition: crashlog.cpp:258
virtual bool TryExecute(std::string_view section_name, std::function< bool()> &&func)=0
Execute the func() and return its value.
virtual bool WriteCrashDump()
Write the (crash) dump to a file.
Definition: crashlog.cpp:210
bool WriteSavegame()
Write the (crash) savegame to a file.
Definition: crashlog.cpp:221
bool WriteScreenshot()
Write the (crash) screenshot to a file.
Definition: crashlog.cpp:244
void FillCrashLog()
Fill the crash log buffer with all data of a crash log.
Definition: crashlog.cpp:87
static void InitThread()
Prepare crash log handler for a newly started thread.
static void SetErrorMessage(const std::string &message)
Sets a message for the error message handler.
Definition: crashlog.cpp:327
virtual ~CrashLog()=default
Stub destructor to silence some compilers.
virtual void SurveyCrash(nlohmann::json &survey) const =0
Convert system crash reason to JSON.
static void AfterCrashLogCleanup()
Try to close the sound/video stuff so it doesn't keep lingering around incorrect video states or so,...
Definition: crashlog.cpp:336
static void InitialiseCrashLog()
Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler ins...