OpenTTD Source: CrashLog Class Reference

OpenTTD Source 15.0-RC2

Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions | Private Member Functions | Static Private Attributes
CrashLog Class Referenceabstract

Helper class for creating crash logs. More...

`#include <crashlog.h>`

Inheritance diagram for CrashLog:

## Public Member Functions
virtual | ~CrashLog ()=default
| Stub destructor to silence some compilers.
void | FillCrashLog ()
| Fill the crash log buffer with all data of a crash log.
void | PrintCrashLog () const
bool | WriteCrashLog ()
| Write the crash log to a file.
virtual bool | WriteCrashDump ()
| Write the (crash) dump to a file.
bool | WriteSavegame ()
| Write the (crash) savegame to a file.
bool | WriteScreenshot ()
| Write the (crash) screenshot to a file.
void | SendSurvey () const
| Send the survey result, noting it was a crash.
void | MakeCrashLog ()
| Makes the crash log, writes it to a file and then subsequently tries to make a crash dump and crash savegame.

## Static Public Member Functions
static void | InitialiseCrashLog ()
| Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler instead of returning straight to the OS.
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.
static void | AfterCrashLogCleanup ()
| Try to close the sound/video stuff so it doesn't keep lingering around incorrect video states or so, e.g.

## Data Fields
nlohmann::json | survey
std::string | crashlog_filename
std::string | crashdump_filename
std::string | savegame_filename
std::string | screenshot_filename

## Protected Member Functions
std::string | CreateFileName (std::string_view ext, bool with_dir=true) const
| Create a timestamped filename.

## Private Member Functions
virtual void | SurveyCrash (nlohmann::json &survey) const =0
| Convert system crash reason to JSON.
virtual void | SurveyStacktrace (nlohmann::json &survey) const =0
| Convert stacktrace to JSON.
virtual bool | TryExecute (std::string_view section_name, std::function< bool()> &&func)=0
| Execute the func() and return its value.

## Static Private Attributes
static std::string | message {}
| Error message coming from FatalError(format, ...).

## Detailed Description

Helper class for creating crash logs.

Definition at line 18 of file crashlog.h.

## Member Function Documentation

## ◆ AfterCrashLogCleanup()

void CrashLog::AfterCrashLogCleanup | ( |  | ) |
| static

Try to close the sound/video stuff so it doesn't keep lingering around incorrect video states or so, e.g.

keeping dpmi disabled.

Definition at line 338 of file crashlog.cpp.

References MusicDriver::GetInstance(), SoundDriver::GetInstance(), VideoDriver::GetInstance(), and Driver::Stop().

Referenced by HandleCrash(), and HandleCrash().

## ◆ CreateFileName()

std::string CrashLog::CreateFileName | ( | std::string_view | ext,
|  | bool | with_dir = `true`
| ) |  | const
| protected

Create a timestamped filename.

Parameters

  ext | The extension for the filename.
  with_dir | Whether to prepend the filename with the personal directory.

Returns
  The filename

Definition at line 76 of file crashlog.cpp.

References _personal_dir.

Referenced by WriteCrashLog(), WriteSavegame(), and WriteScreenshot().

## ◆ FillCrashLog()

void CrashLog::FillCrashLog | ( |  | ) |

Fill the crash log buffer with all data of a crash log.

Definition at line 89 of file crashlog.cpp.

References _current_company, _local_company, CRASHLOG_SURVEY_VERSION, message, SurveyCompanies(), SurveyCompiler(), SurveyConfiguration(), SurveyCrash(), SurveyFont(), SurveyGamelog(), SurveyGameScript(), SurveyGameSession(), SurveyGrfs(), SurveyLibraries(), SurveyOpenTTD(), SurveyPlugins(), SurveyRecentNews(), SurveySettings(), SurveyStacktrace(), SurveyTimers(), and TryExecute().

Referenced by MakeCrashLog().

## ◆ InitialiseCrashLog()

void CrashLog::InitialiseCrashLog | ( |  | ) |
| static

Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler instead of returning straight to the OS.

Note
  must be implemented by all implementers of CrashLog.

Definition at line 232 of file crashlog_osx.cpp.

References HandleCrash(), and SetSignals().

## ◆ InitThread()

void CrashLog::InitThread | ( |  | ) |
| static

Prepare crash log handler for a newly started thread.

Note
  must be implemented by all implementers of CrashLog.

Definition at line 237 of file crashlog_osx.cpp.

Referenced by StartNewThread().

## ◆ MakeCrashLog()

void CrashLog::MakeCrashLog | ( |  | ) |

Makes the crash log, writes it to a file and then subsequently tries to make a crash dump and crash savegame.

It uses DEBUG to write information like paths to the console.

Definition at line 272 of file crashlog.cpp.

References FillCrashLog(), SendSurvey(), TryExecute(), WriteCrashDump(), WriteCrashLog(), WriteSavegame(), and WriteScreenshot().

Referenced by HandleCrash(), and HandleCrash().

## ◆ PrintCrashLog()

void CrashLog::PrintCrashLog | ( |  | ) | const

Definition at line 166 of file crashlog.cpp.

## ◆ SendSurvey()

void CrashLog::SendSurvey | ( |  | ) | const

Send the survey result, noting it was a crash.

Definition at line 260 of file crashlog.cpp.

References NetworkSurveyHandler::CRASH, and NetworkSurveyHandler::Transmit().

Referenced by MakeCrashLog().

## ◆ SetErrorMessage()

void CrashLog::SetErrorMessage | ( | const std::string & | message | ) |
| static

Sets a message for the error message handler.

Parameters

  message | The error message of the error.

Definition at line 329 of file crashlog.cpp.

References message.

Referenced by FatalErrorI().

## ◆ SurveyCrash()

virtual void CrashLog::SurveyCrash | ( | nlohmann::json & | survey | ) | const
| privatepure virtual

Convert system crash reason to JSON.

Parameters

  survey | The JSON object.

Implemented in CrashLogOSX, CrashLogUnix, and CrashLogWindows.

Referenced by FillCrashLog().

## ◆ SurveyStacktrace()

virtual void CrashLog::SurveyStacktrace | ( | nlohmann::json & | survey | ) | const
| privatepure virtual

Convert stacktrace to JSON.

Parameters

  survey | The JSON object.

Implemented in CrashLogOSX, CrashLogUnix, and CrashLogWindows.

Referenced by FillCrashLog().

## ◆ TryExecute()

virtual bool CrashLog::TryExecute | ( | std::string_view | section_name,
|  | std::function< bool()> && | func
| ) |  |
| privatepure virtual

Execute the func() and return its value.

If any exception / signal / crash happens, catch it and return false. This function should, in theory, never not return, even in the worst conditions.

Parameters

  section_name | The name of the section to be executed. Printed when a crash happens.
  func | The function to call.

Returns
  true iff the function returned true.

Implemented in CrashLogOSX, CrashLogUnix, and CrashLogWindows.

Referenced by FillCrashLog(), and MakeCrashLog().

## ◆ WriteCrashDump()

bool CrashLog::WriteCrashDump | ( |  | ) |
| virtual

Write the (crash) dump to a file.

Note
  Sets `crashdump_filename` when there is a successful return.

Returns
  True iff the crashdump was successfully created.

Definition at line 212 of file crashlog.cpp.

Referenced by MakeCrashLog().

## ◆ WriteCrashLog()

bool CrashLog::WriteCrashLog | ( |  | ) |

Write the crash log to a file.

Note
  The filename will be written to `crashlog_filename`.

Returns
  true when the crash log was successfully written.

Definition at line 191 of file crashlog.cpp.

References CreateFileName(), FioFOpenFile(), and NO_DIRECTORY.

Referenced by MakeCrashLog().

## ◆ WriteSavegame()

bool CrashLog::WriteSavegame | ( |  | ) |

Write the (crash) savegame to a file.

Note
  The filename will be written to `savegame_filename`.

Returns
  true when the crash save was successfully made.

Definition at line 223 of file crashlog.cpp.

References _gamelog, CreateFileName(), DFT_GAME_FILE, Gamelog::Emergency(), Map::IsInitialized(), NO_DIRECTORY, SaveOrLoad(), SL_OK, and SLO_SAVE.

Referenced by MakeCrashLog().

## ◆ WriteScreenshot()

bool CrashLog::WriteScreenshot | ( |  | ) |

Write the (crash) screenshot to a file.

Note
  The filename will be written to `screenshot_filename`.

Returns
  std::nullopt when the crash screenshot could not be made, otherwise the filename.

Definition at line 246 of file crashlog.cpp.

References _full_screenshot_path, CreateFileName(), MakeScreenshot(), and SC_CRASHLOG.

Referenced by MakeCrashLog().

## Field Documentation

## ◆ crashdump_filename

std::string CrashLog::crashdump_filename

Definition at line 57 of file crashlog.h.

## ◆ crashlog_filename

std::string CrashLog::crashlog_filename

Definition at line 56 of file crashlog.h.

## ◆ message

std::string CrashLog::message {}
| staticprivate

Error message coming from FatalError(format, ...).

Definition at line 33 of file crashlog.h.

Referenced by CrashLogOSX::DisplayCrashDialog(), FillCrashLog(), and SetErrorMessage().

## ◆ savegame_filename

std::string CrashLog::savegame_filename

Definition at line 58 of file crashlog.h.

## ◆ screenshot_filename

std::string CrashLog::screenshot_filename

Definition at line 59 of file crashlog.h.

## ◆ survey

nlohmann::json CrashLog::survey

Definition at line 55 of file crashlog.h.

The documentation for this class was generated from the following files:

- src/crashlog.h
- src/crashlog.cpp
- src/os/macosx/crashlog_osx.cpp
- src/os/unix/crashlog_unix.cpp
- src/os/windows/crashlog_win.cpp