Codechange: Avoid unnecessary re-reads/seeks in RandomAccessFile::ReadBlock
[openttd-github.git] / src / crashlog.h
blob836bef50822b1051445301a84976d3793f697622
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 */
8 /** @file crashlog.h Functions to be called to log a crash */
10 #ifndef CRASHLOG_H
11 #define CRASHLOG_H
13 #include "3rdparty/nlohmann/json.hpp"
15 /**
16 * Helper class for creating crash logs.
18 class CrashLog {
19 private:
20 /** Error message coming from #FatalError(format, ...). */
21 static std::string message;
23 /**
24 * Convert system crash reason to JSON.
26 * @param survey The JSON object.
28 virtual void SurveyCrash(nlohmann::json &survey) const = 0;
30 /**
31 * Convert stacktrace to JSON.
33 * @param survey The JSON object.
35 virtual void SurveyStacktrace(nlohmann::json &survey) const = 0;
37 /**
38 * Execute the func() and return its value. If any exception / signal / crash happens,
39 * catch it and return false. This function should, in theory, never not return, even
40 * in the worst conditions.
42 * @param section_name The name of the section to be executed. Printed when a crash happens.
43 * @param func The function to call.
44 * @return true iff the function returned true.
46 virtual bool TryExecute(std::string_view section_name, std::function<bool()> &&func) = 0;
48 protected:
49 std::string CreateFileName(const char *ext, bool with_dir = true) const;
51 public:
52 /** Stub destructor to silence some compilers. */
53 virtual ~CrashLog() = default;
55 nlohmann::json survey;
56 std::string crashlog_filename;
57 std::string crashdump_filename;
58 std::string savegame_filename;
59 std::string screenshot_filename;
61 void FillCrashLog();
62 void PrintCrashLog() const;
64 bool WriteCrashLog();
65 virtual bool WriteCrashDump();
66 bool WriteSavegame();
67 bool WriteScreenshot();
69 void SendSurvey() const;
71 void MakeCrashLog();
73 /**
74 * Initialiser for crash logs; do the appropriate things so crashes are
75 * handled by our crash handler instead of returning straight to the OS.
76 * @note must be implemented by all implementers of CrashLog.
78 static void InitialiseCrashLog();
80 /**
81 * Prepare crash log handler for a newly started thread.
82 * @note must be implemented by all implementers of CrashLog.
84 static void InitThread();
86 static void SetErrorMessage(const std::string &message);
87 static void AfterCrashLogCleanup();
90 #endif /* CRASHLOG_H */