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.cpp Implementation of generic function to be called to log a crash */
13 #include "date_func.h"
16 #include "strings_func.h"
17 #include "blitter/factory.hpp"
18 #include "base_media_base.h"
19 #include "music/music_driver.hpp"
20 #include "sound/sound_driver.hpp"
21 #include "video/video_driver.hpp"
22 #include "saveload/saveload.h"
23 #include "screenshot.h"
25 #include "network/network.h"
27 #include "fontcache.h"
30 #include "ai/ai_info.hpp"
31 #include "game/game.hpp"
32 #include "game/game_info.hpp"
33 #include "company_base.h"
34 #include "company_func.h"
35 #include "walltime_func.h"
39 #endif /* WITH_ALLEGRO */
40 #ifdef WITH_FONTCONFIG
41 # include <fontconfig/fontconfig.h>
42 #endif /* WITH_FONTCONFIG */
44 /* pngconf.h, included by png.h doesn't like something in the
45 * freetype headers. As such it's not alphabetically sorted. */
49 # include <ft2build.h>
50 # include FT_FREETYPE_H
51 #endif /* WITH_FREETYPE */
52 #if defined(WITH_ICU_LX) || defined(WITH_ICU_I18N)
53 # include <unicode/uversion.h>
54 #endif /* WITH_ICU_LX || WITH_ICU_I18N */
59 #include <lzo/lzo1x.h>
61 #if defined(WITH_SDL) || defined(WITH_SDL2)
63 #endif /* WITH_SDL || WITH_SDL2 */
68 #include "safeguards.h"
70 /* static */ const char *CrashLog::message
= nullptr;
71 /* static */ char *CrashLog::gamelog_buffer
= nullptr;
72 /* static */ const char *CrashLog::gamelog_last
= nullptr;
74 char *CrashLog::LogCompiler(char *buffer
, const char *last
) const
76 buffer
+= seprintf(buffer
, last
, " Compiler: "
79 #elif defined(__ICC) && defined(__GNUC__)
80 "ICC %d (GCC %d.%d.%d mode)", __ICC
, __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
83 #elif defined(__GNUC__)
84 "GCC %d.%d.%d", __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
85 #elif defined(__WATCOMC__)
86 "WatcomC %d", __WATCOMC__
91 #if defined(__VERSION__)
92 return buffer
+ seprintf(buffer
, last
, " \"" __VERSION__
"\"\n\n");
94 return buffer
+ seprintf(buffer
, last
, "\n\n");
98 /* virtual */ char *CrashLog::LogRegisters(char *buffer
, const char *last
) const
100 /* Stub implementation; not all OSes support this. */
104 /* virtual */ char *CrashLog::LogModules(char *buffer
, const char *last
) const
106 /* Stub implementation; not all OSes support this. */
111 * Writes OpenTTD's version to the buffer.
112 * @param buffer The begin where to write at.
113 * @param last The last position in the buffer to write to.
114 * @return the position of the \c '\0' character after the buffer.
116 char *CrashLog::LogOpenTTDVersion(char *buffer
, const char *last
) const
118 return buffer
+ seprintf(buffer
, last
,
120 " Version: %s (%d)\n"
121 " NewGRF ver: %08x\n"
125 " Build date: %s\n\n",
127 _openttd_revision_modified
,
128 _openttd_newgrf_version
,
129 #ifdef POINTER_IS_64BIT
134 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
149 * Writes the (important) configuration settings to the buffer.
150 * E.g. graphics set, sound set, blitter and AIs.
151 * @param buffer The begin where to write at.
152 * @param last The last position in the buffer to write to.
153 * @return the position of the \c '\0' character after the buffer.
155 char *CrashLog::LogConfiguration(char *buffer
, const char *last
) const
157 buffer
+= seprintf(buffer
, last
,
160 " Graphics set: %s (%u)\n"
162 " Music driver: %s\n"
163 " Music set: %s (%u)\n"
165 " Sound driver: %s\n"
166 " Sound set: %s (%u)\n"
167 " Video driver: %s\n\n",
168 BlitterFactory::GetCurrentBlitter() == nullptr ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
169 BaseGraphics::GetUsedSet() == nullptr ? "none" : BaseGraphics::GetUsedSet()->name
.c_str(),
170 BaseGraphics::GetUsedSet() == nullptr ? UINT32_MAX
: BaseGraphics::GetUsedSet()->version
,
171 _current_language
== nullptr ? "none" : _current_language
->file
,
172 MusicDriver::GetInstance() == nullptr ? "none" : MusicDriver::GetInstance()->GetName(),
173 BaseMusic::GetUsedSet() == nullptr ? "none" : BaseMusic::GetUsedSet()->name
.c_str(),
174 BaseMusic::GetUsedSet() == nullptr ? UINT32_MAX
: BaseMusic::GetUsedSet()->version
,
175 _networking
? (_network_server
? "server" : "client") : "no",
176 SoundDriver::GetInstance() == nullptr ? "none" : SoundDriver::GetInstance()->GetName(),
177 BaseSounds::GetUsedSet() == nullptr ? "none" : BaseSounds::GetUsedSet()->name
.c_str(),
178 BaseSounds::GetUsedSet() == nullptr ? UINT32_MAX
: BaseSounds::GetUsedSet()->version
,
179 VideoDriver::GetInstance() == nullptr ? "none" : VideoDriver::GetInstance()->GetName()
182 buffer
+= seprintf(buffer
, last
,
188 FontCache::Get(FS_SMALL
)->GetFontName(),
189 FontCache::Get(FS_NORMAL
)->GetFontName(),
190 FontCache::Get(FS_LARGE
)->GetFontName(),
191 FontCache::Get(FS_MONO
)->GetFontName()
194 buffer
+= seprintf(buffer
, last
, "AI Configuration (local: %i) (current: %i):\n", (int)_local_company
, (int)_current_company
);
195 for (const Company
*c
: Company::Iterate()) {
196 if (c
->ai_info
== nullptr) {
197 buffer
+= seprintf(buffer
, last
, " %2i: Human\n", (int)c
->index
);
199 buffer
+= seprintf(buffer
, last
, " %2i: %s (v%d)\n", (int)c
->index
, c
->ai_info
->GetName(), c
->ai_info
->GetVersion());
203 if (Game::GetInfo() != nullptr) {
204 buffer
+= seprintf(buffer
, last
, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
206 buffer
+= seprintf(buffer
, last
, "\n");
212 * Writes information (versions) of the used libraries.
213 * @param buffer The begin where to write at.
214 * @param last The last position in the buffer to write to.
215 * @return the position of the \c '\0' character after the buffer.
217 char *CrashLog::LogLibraries(char *buffer
, const char *last
) const
219 buffer
+= seprintf(buffer
, last
, "Libraries:\n");
222 buffer
+= seprintf(buffer
, last
, " Allegro: %s\n", allegro_id
);
223 #endif /* WITH_ALLEGRO */
225 #ifdef WITH_FONTCONFIG
226 int version
= FcGetVersion();
227 buffer
+= seprintf(buffer
, last
, " FontConfig: %d.%d.%d\n", version
/ 10000, (version
/ 100) % 100, version
% 100);
228 #endif /* WITH_FONTCONFIG */
232 int major
, minor
, patch
;
233 FT_Init_FreeType(&library
);
234 FT_Library_Version(library
, &major
, &minor
, &patch
);
235 FT_Done_FreeType(library
);
236 buffer
+= seprintf(buffer
, last
, " FreeType: %d.%d.%d\n", major
, minor
, patch
);
237 #endif /* WITH_FREETYPE */
239 #if defined(WITH_ICU_LX) || defined(WITH_ICU_I18N)
240 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
241 char buf
[4 * 3 + 3 + 1];
244 u_versionToString(ver
, buf
);
246 buffer
+= seprintf(buffer
, last
, " ICU i18n: %s\n", buf
);
249 buffer
+= seprintf(buffer
, last
, " ICU lx: %s\n", buf
);
251 #endif /* WITH_ICU_LX || WITH_ICU_I18N */
254 buffer
+= seprintf(buffer
, last
, " LZMA: %s\n", lzma_version_string());
258 buffer
+= seprintf(buffer
, last
, " LZO: %s\n", lzo_version_string());
262 buffer
+= seprintf(buffer
, last
, " PNG: %s\n", png_get_libpng_ver(nullptr));
263 #endif /* WITH_PNG */
266 const SDL_version
*sdl_v
= SDL_Linked_Version();
267 buffer
+= seprintf(buffer
, last
, " SDL1: %d.%d.%d\n", sdl_v
->major
, sdl_v
->minor
, sdl_v
->patch
);
268 #elif defined(WITH_SDL2)
270 SDL_GetVersion(&sdl2_v
);
271 buffer
+= seprintf(buffer
, last
, " SDL2: %d.%d.%d\n", sdl2_v
.major
, sdl2_v
.minor
, sdl2_v
.patch
);
275 buffer
+= seprintf(buffer
, last
, " Zlib: %s\n", zlibVersion());
278 buffer
+= seprintf(buffer
, last
, "\n");
283 * Helper function for printing the gamelog.
284 * @param s the string to print.
286 /* static */ void CrashLog::GamelogFillCrashLog(const char *s
)
288 CrashLog::gamelog_buffer
+= seprintf(CrashLog::gamelog_buffer
, CrashLog::gamelog_last
, "%s\n", s
);
292 * Writes the gamelog data to the buffer.
293 * @param buffer The begin where to write at.
294 * @param last The last position in the buffer to write to.
295 * @return the position of the \c '\0' character after the buffer.
297 char *CrashLog::LogGamelog(char *buffer
, const char *last
) const
299 CrashLog::gamelog_buffer
= buffer
;
300 CrashLog::gamelog_last
= last
;
301 GamelogPrint(&CrashLog::GamelogFillCrashLog
);
302 return CrashLog::gamelog_buffer
+ seprintf(CrashLog::gamelog_buffer
, last
, "\n");
306 * Writes up to 32 recent news messages to the buffer, with the most recent first.
307 * @param buffer The begin where to write at.
308 * @param last The last position in the buffer to write to.
309 * @return the position of the \c '\0' character after the buffer.
311 char *CrashLog::LogRecentNews(char *buffer
, const char *last
) const
313 buffer
+= seprintf(buffer
, last
, "Recent news messages:\n");
316 for (NewsItem
*news
= _latest_news
; i
< 32 && news
!= nullptr; news
= news
->prev
, i
++) {
318 ConvertDateToYMD(news
->date
, &ymd
);
319 buffer
+= seprintf(buffer
, last
, "(%i-%02i-%02i) StringID: %u, Type: %u, Ref1: %u, %u, Ref2: %u, %u\n",
320 ymd
.year
, ymd
.month
+ 1, ymd
.day
, news
->string_id
, news
->type
,
321 news
->reftype1
, news
->ref1
, news
->reftype2
, news
->ref2
);
323 buffer
+= seprintf(buffer
, last
, "\n");
328 * Fill the crash log buffer with all data of a crash log.
329 * @param buffer The begin where to write at.
330 * @param last The last position in the buffer to write to.
331 * @return the position of the \c '\0' character after the buffer.
333 char *CrashLog::FillCrashLog(char *buffer
, const char *last
) const
335 buffer
+= seprintf(buffer
, last
, "*** OpenTTD Crash Report ***\n\n");
336 buffer
+= UTCTime::Format(buffer
, last
, "Crash at: %Y-%m-%d %H:%M:%S (UTC)\n");
339 ConvertDateToYMD(_date
, &ymd
);
340 buffer
+= seprintf(buffer
, last
, "In game date: %i-%02i-%02i (%i)\n\n", ymd
.year
, ymd
.month
+ 1, ymd
.day
, _date_fract
);
342 buffer
= this->LogError(buffer
, last
, CrashLog::message
);
343 buffer
= this->LogOpenTTDVersion(buffer
, last
);
344 buffer
= this->LogRegisters(buffer
, last
);
345 buffer
= this->LogStacktrace(buffer
, last
);
346 buffer
= this->LogOSVersion(buffer
, last
);
347 buffer
= this->LogCompiler(buffer
, last
);
348 buffer
= this->LogConfiguration(buffer
, last
);
349 buffer
= this->LogLibraries(buffer
, last
);
350 buffer
= this->LogModules(buffer
, last
);
351 buffer
= this->LogGamelog(buffer
, last
);
352 buffer
= this->LogRecentNews(buffer
, last
);
354 buffer
+= seprintf(buffer
, last
, "*** End of OpenTTD Crash Report ***\n");
359 * Write the crash log to a file.
360 * @note On success the filename will be filled with the full path of the
361 * crash log file. Make sure filename is at least \c MAX_PATH big.
362 * @param buffer The begin of the buffer to write to the disk.
363 * @param filename Output for the filename of the written file.
364 * @param filename_last The last position in the filename buffer.
365 * @return true when the crash log was successfully written.
367 bool CrashLog::WriteCrashLog(const char *buffer
, char *filename
, const char *filename_last
) const
369 seprintf(filename
, filename_last
, "%scrash.log", _personal_dir
.c_str());
371 FILE *file
= FioFOpenFile(filename
, "w", NO_DIRECTORY
);
372 if (file
== nullptr) return false;
374 size_t len
= strlen(buffer
);
375 size_t written
= fwrite(buffer
, 1, len
, file
);
378 return len
== written
;
381 /* virtual */ int CrashLog::WriteCrashDump(char *filename
, const char *filename_last
) const
383 /* Stub implementation; not all OSes support this. */
388 * Write the (crash) savegame to a file.
389 * @note On success the filename will be filled with the full path of the
390 * crash save file. Make sure filename is at least \c MAX_PATH big.
391 * @param filename Output for the filename of the written file.
392 * @param filename_last The last position in the filename buffer.
393 * @return true when the crash save was successfully made.
395 bool CrashLog::WriteSavegame(char *filename
, const char *filename_last
) const
397 /* If the map array doesn't exist, saving will fail too. If the map got
398 * initialised, there is a big chance the rest is initialised too. */
399 if (_m
== nullptr) return false;
404 seprintf(filename
, filename_last
, "%scrash.sav", _personal_dir
.c_str());
406 /* Don't do a threaded saveload. */
407 return SaveOrLoad(filename
, SLO_SAVE
, DFT_GAME_FILE
, NO_DIRECTORY
, false) == SL_OK
;
414 * Write the (crash) screenshot to a file.
415 * @note On success the filename will be filled with the full path of the
416 * screenshot. Make sure filename is at least \c MAX_PATH big.
417 * @param filename Output for the filename of the written file.
418 * @param filename_last The last position in the filename buffer.
419 * @return true when the crash screenshot was successfully made.
421 bool CrashLog::WriteScreenshot(char *filename
, const char *filename_last
) const
423 /* Don't draw when we have invalid screen size */
424 if (_screen
.width
< 1 || _screen
.height
< 1 || _screen
.dst_ptr
== nullptr) return false;
426 bool res
= MakeScreenshot(SC_CRASHLOG
, "crash");
427 if (res
) strecpy(filename
, _full_screenshot_name
, filename_last
);
432 * Makes the crash log, writes it to a file and then subsequently tries
433 * to make a crash dump and crash savegame. It uses DEBUG to write
434 * information like paths to the console.
435 * @return true when everything is made successfully.
437 bool CrashLog::MakeCrashLog() const
439 /* Don't keep looping logging crashes. */
440 static bool crashlogged
= false;
441 if (crashlogged
) return false;
444 char filename
[MAX_PATH
];
448 printf("Crash encountered, generating crash log...\n");
449 this->FillCrashLog(buffer
, lastof(buffer
));
450 printf("%s\n", buffer
);
451 printf("Crash log generated.\n\n");
453 printf("Writing crash log to disk...\n");
454 bool bret
= this->WriteCrashLog(buffer
, filename
, lastof(filename
));
456 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename
);
458 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
462 /* Don't mention writing crash dumps because not all platforms support it. */
463 int dret
= this->WriteCrashDump(filename
, lastof(filename
));
465 printf("Writing crash dump failed.\n\n");
467 } else if (dret
> 0) {
468 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename
);
471 printf("Writing crash savegame...\n");
472 bret
= this->WriteSavegame(filename
, lastof(filename
));
474 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename
);
477 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
480 printf("Writing crash screenshot...\n");
481 bret
= this->WriteScreenshot(filename
, lastof(filename
));
483 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename
);
486 printf("Writing crash screenshot failed.\n\n");
493 * Sets a message for the error message handler.
494 * @param message The error message of the error.
496 /* static */ void CrashLog::SetErrorMessage(const char *message
)
498 CrashLog::message
= message
;
502 * Try to close the sound/video stuff so it doesn't keep lingering around
503 * incorrect video states or so, e.g. keeping dpmi disabled.
505 /* static */ void CrashLog::AfterCrashLogCleanup()
507 if (MusicDriver::GetInstance() != nullptr) MusicDriver::GetInstance()->Stop();
508 if (SoundDriver::GetInstance() != nullptr) SoundDriver::GetInstance()->Stop();
509 if (VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop();