|
OpenTTD Source
14.0-beta3
|
Go to the documentation of this file.
26 #include "3rdparty/fmt/chrono.h"
27 #include "3rdparty/fmt/std.h"
43 json = nlohmann::json::array();
56 json = nlohmann::json::array();
61 json.push_back(fmt::format(
"({}-{:02}-{:02}) StringID: {}, Type: {}, Ref1: {}, {}, Ref2: {}, {}",
62 ymd.year, ymd.month + 1, ymd.day, news->string_id, news->type,
63 news->reftype1, news->ref1, news->reftype2, news->ref2));
75 static std::string crashname;
77 if (crashname.empty()) {
78 crashname = fmt::format(
"crash{:%Y%m%d%H%M%S}", fmt::gmtime(time(
nullptr)));
80 return fmt::format(
"{}{}{}", with_dir ?
_personal_dir : std::string{}, crashname, ext);
91 this->survey[
"date"] = fmt::format(
"{:%Y-%m-%d %H:%M:%S} (UTC)", fmt::gmtime(time(
nullptr)));
102 this->survey[
"stacktrace"] =
"crashed while gathering information";
106 auto &info = this->survey[
"info"];
107 if (!this->
TryExecute(
"os", [&info]() { SurveyOS(info[
"os"]);
return true; })) {
108 info[
"os"] =
"crashed while gathering information";
111 info[
"openttd"] =
"crashed while gathering information";
114 info[
"configuration"] =
"crashed while gathering information";
117 info[
"font"] =
"crashed while gathering information";
120 info[
"compiler"] =
"crashed while gathering information";
123 info[
"libraries"] =
"crashed while gathering information";
126 info[
"plugins"] =
"crashed while gathering information";
131 auto &game = this->survey[
"game"];
136 game[
"libraries"] =
"crashed while gathering information";
139 game[
"companies"] =
"crashed while gathering information";
142 game[
"settings"] =
"crashed while gathering information";
145 game[
"grfs"] =
"crashed while gathering information";
148 game[
"game_script"] =
"crashed while gathering information";
151 game[
"gamelog"] =
"crashed while gathering information";
154 game[
"news"] =
"crashed while gathering information";
159 void CrashLog::PrintCrashLog()
const
161 fmt::print(
" OpenTTD version:\n");
162 fmt::print(
" Version: {}\n", this->survey[
"info"][
"openttd"][
"version"][
"revision"].get<std::string>());
163 fmt::print(
" Hash: {}\n", this->survey[
"info"][
"openttd"][
"version"][
"hash"].get<std::string>());
164 fmt::print(
" NewGRF ver: {}\n", this->survey[
"info"][
"openttd"][
"version"][
"newgrf"].get<std::string>());
165 fmt::print(
" Content ver: {}\n", this->survey[
"info"][
"openttd"][
"version"][
"content"].get<std::string>());
168 fmt::print(
" Crash:\n");
169 fmt::print(
" Reason: {}\n", this->survey[
"crash"][
"reason"].get<std::string>());
172 fmt::print(
" Stacktrace:\n");
173 for (
const auto &line : this->survey[
"stacktrace"]) {
174 fmt::print(
" {}\n", line.get<std::string>());
189 if (file ==
nullptr)
return false;
191 std::string survey_json = this->survey.dump(4);
193 size_t len = survey_json.size();
194 size_t written = fwrite(survey_json.data(), 1, len, file);
197 return len == written;
208 fmt::print(
"No method to create a crash.dmp available.\n");
243 if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr ==
nullptr)
return false;
256 if (_game_mode == GM_NORMAL) {
269 static bool crashlogged =
false;
270 if (crashlogged)
return;
273 fmt::print(
"Crash encountered, generating crash log...\n");
275 fmt::print(
"Crash log generated.\n\n");
277 fmt::print(
"Crash in summary:\n");
278 this->
TryExecute(
"crashlog", [
this]() { this->PrintCrashLog();
return true; });
280 fmt::print(
"Writing crash log to disk...\n");
283 fmt::print(
"Crash log written to {}. Please add this file to any bug reports.\n\n", this->crashlog_filename);
285 fmt::print(
"Writing crash log failed. Please attach the output above to any bug reports.\n\n");
286 this->crashlog_filename =
"(failed to write crash log)";
289 fmt::print(
"Writing crash dump to disk...\n");
292 fmt::print(
"Crash dump written to {}. Please add this file to any bug reports.\n\n", this->crashdump_filename);
294 fmt::print(
"Writing crash dump failed.\n\n");
295 this->crashdump_filename =
"(failed to write crash dump)";
298 fmt::print(
"Writing crash savegame...\n");
301 fmt::print(
"Crash savegame written to {}. Please add this file and the last (auto)save to any bug reports.\n\n", this->savegame_filename);
303 fmt::print(
"Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
304 this->savegame_filename =
"(failed to write crash savegame)";
307 fmt::print(
"Writing crash screenshot...\n");
310 fmt::print(
"Crash screenshot written to {}. Please add this file to any bug reports.\n\n", this->screenshot_filename);
312 fmt::print(
"Writing crash screenshot failed.\n\n");
313 this->screenshot_filename =
"(failed to write crash screenshot)";
void SurveyConfiguration(nlohmann::json &survey)
Convert generic game information to JSON.
void SurveyLibraries(nlohmann::json &survey)
Convert compiled libraries information to JSON.
void SurveyTimers(nlohmann::json &survey)
Convert timer information to JSON.
std::string _personal_dir
custom directory for personal settings, saves, newgrf, etc.
static void AfterCrashLogCleanup()
Try to close the sound/video stuff so it doesn't keep lingering around incorrect video states or so,...
Information about a single item of news.
Gamelog _gamelog
Gamelog instance.
bool WriteCrashLog()
Write the crash log to a file.
SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
Main Save or Load function where the high-level saveload functions are handled.
virtual void SurveyCrash(nlohmann::json &survey) const =0
Convert system crash reason to JSON.
void MakeCrashLog()
Makes the crash log, writes it to a file and then subsequently tries to make a crash dump and crash s...
static bool IsInitialized()
Check whether the map has been initialized, as to not try to save the map during crashlog when the ma...
@ DFT_GAME_FILE
Save game or scenario file.
static void SurveyGamelog(nlohmann::json &json)
Writes the gamelog data to the buffer.
void FillCrashLog()
Fill the crash log buffer with all data of a crash log.
void SurveySettings(nlohmann::json &survey, bool skip_if_default)
Convert settings to JSON.
void Emergency()
Logs a emergency savegame.
@ SLO_SAVE
File is being saved.
static MusicDriver * GetInstance()
Get the currently active instance of the music driver.
static void SurveyRecentNews(nlohmann::json &json)
Writes up to 32 recent news messages to the buffer, with the most recent first.
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
void SurveyOpenTTD(nlohmann::json &survey)
Convert generic OpenTTD information to JSON.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
void SurveyCompanies(nlohmann::json &survey)
Convert company information to JSON.
static YearMonthDay ConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
std::string CreateFileName(const char *ext, bool with_dir=true) const
Create a timestamped filename.
NewsItem * prev
Previous news item.
void SurveyGrfs(nlohmann::json &survey)
Convert GRF information to JSON.
void Transmit(Reason reason, bool blocking=false)
Transmit the survey.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
bool WriteScreenshot()
Write the (crash) screenshot to a file.
CompanyID _current_company
Company currently doing an action.
constexpr uint8_t CRASHLOG_SURVEY_VERSION
The version of the schema of the JSON information.
void SurveyCompiler(nlohmann::json &survey)
Convert compiler information to JSON.
void Print(std::function< void(const std::string &)> proc)
Prints active gamelog.
static void SetErrorMessage(const std::string &message)
Sets a message for the error message handler.
@ NO_DIRECTORY
A path without any base directory.
NewsItem * _latest_news
tail of news items queue
static SoundDriver * GetInstance()
Get the currently active instance of the sound driver.
void SendSurvey() const
Send the survey result, noting it was a crash.
static std::string message
Error message coming from #FatalError(format, ...).
@ SC_CRASHLOG
Raw screenshot from blitter buffer.
bool MakeScreenshot(ScreenshotType t, std::string name, uint32_t width, uint32_t height)
Schedule making a screenshot.
virtual bool WriteCrashDump()
Write the (crash) dump to a file.
void SurveyGameScript(nlohmann::json &survey)
Convert game-script information to JSON.
virtual bool TryExecute(std::string_view section_name, std::function< bool()> &&func)=0
Execute the func() and return its value.
virtual void Stop()=0
Stop this driver.
@ SL_OK
completed successfully
void SurveyFont(nlohmann::json &survey)
Convert font information to JSON.
void SurveyPlugins(nlohmann::json &survey)
Convert plugin information to JSON.
void FioFCloseFile(FILE *f)
Close a file in a safe way.
std::string _full_screenshot_path
Pathname of the screenshot file.
bool WriteSavegame()
Write the (crash) savegame to a file.
virtual void SurveyStacktrace(nlohmann::json &survey) const =0
Convert stacktrace to JSON.