OpenTTD Source  20241121-master-g67a0fccfad
CrashLog Class Referenceabstract

Helper class for creating crash logs. More...

#include <crashlog.h>

Inheritance diagram for CrashLog:
CrashLogOSX CrashLogUnix CrashLogWindows

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. More...
 
virtual bool WriteCrashDump ()
 Write the (crash) dump to a file. More...
 
bool WriteSavegame ()
 Write the (crash) savegame to a file. More...
 
bool WriteScreenshot ()
 Write the (crash) screenshot to a file. More...
 
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. More...
 

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. More...
 
static void InitThread ()
 Prepare crash log handler for a newly started thread. More...
 
static void SetErrorMessage (const std::string &message)
 Sets a message for the error message handler. More...
 
static void AfterCrashLogCleanup ()
 Try to close the sound/video stuff so it doesn't keep lingering around incorrect video states or so, e.g. More...
 

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 (const char *ext, bool with_dir=true) const
 Create a timestamped filename. More...
 

Private Member Functions

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

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 336 of file crashlog.cpp.

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

Referenced by HandleCrash().

◆ CreateFileName()

std::string CrashLog::CreateFileName ( const char *  ext,
bool  with_dir = true 
) const
protected

Create a timestamped filename.

Parameters
extThe extension for the filename.
with_dirWhether to prepend the filename with the personal directory.
Returns
The filename

Definition at line 74 of file crashlog.cpp.

References _personal_dir.

Referenced by WriteCrashLog(), and WriteSavegame().

◆ 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 233 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 238 of file crashlog_osx.cpp.

◆ 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 270 of file crashlog.cpp.

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

Referenced by HandleCrash().

◆ SetErrorMessage()

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

Sets a message for the error message handler.

Parameters
messageThe error message of the error.

Definition at line 327 of file crashlog.cpp.

References message.

◆ SurveyCrash()

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

Convert system crash reason to JSON.

Parameters
surveyThe JSON object.

Implemented in CrashLogWindows, CrashLogUnix, and CrashLogOSX.

Referenced by FillCrashLog().

◆ SurveyStacktrace()

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

Convert stacktrace to JSON.

Parameters
surveyThe JSON object.

Implemented in CrashLogWindows, and CrashLogOSX.

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_nameThe name of the section to be executed. Printed when a crash happens.
funcThe function to call.
Returns
true iff the function returned true.

Implemented in CrashLogWindows, CrashLogUnix, and CrashLogOSX.

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 210 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 189 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 221 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 244 of file crashlog.cpp.

Referenced by MakeCrashLog().


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