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/>.
8 /** @file crashlog.h Functions to be called to log a crash */
13 #include "3rdparty/nlohmann/json.hpp"
16 * Helper class for creating crash logs.
20 /** Error message coming from #FatalError(format, ...). */
21 static std::string message
;
24 * Convert system crash reason to JSON.
26 * @param survey The JSON object.
28 virtual void SurveyCrash(nlohmann::json
&survey
) const = 0;
31 * Convert stacktrace to JSON.
33 * @param survey The JSON object.
35 virtual void SurveyStacktrace(nlohmann::json
&survey
) const = 0;
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;
49 std::string
CreateFileName(const char *ext
, bool with_dir
= true) const;
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
;
62 void PrintCrashLog() const;
65 virtual bool WriteCrashDump();
67 bool WriteScreenshot();
69 void SendSurvey() const;
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();
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 */