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"
40 #endif /* WITH_ALLEGRO */
41 #ifdef WITH_FONTCONFIG
42 # include <fontconfig/fontconfig.h>
43 #endif /* WITH_FONTCONFIG */
45 /* pngconf.h, included by png.h doesn't like something in the
46 * freetype headers. As such it's not alphabetically sorted. */
50 # include <ft2build.h>
51 # include FT_FREETYPE_H
52 #endif /* WITH_FREETYPE */
53 #if defined(WITH_ICU_LX) || defined(WITH_ICU_I18N)
54 # include <unicode/uversion.h>
55 #endif /* WITH_ICU_LX || WITH_ICU_I18N */
60 #include <lzo/lzo1x.h>
62 #if defined(WITH_SDL) || defined(WITH_SDL2)
64 #endif /* WITH_SDL || WITH_SDL2 */
69 #include "safeguards.h"
71 /* static */ const char *CrashLog::message
= nullptr;
72 /* static */ char *CrashLog::gamelog_buffer
= nullptr;
73 /* static */ const char *CrashLog::gamelog_last
= nullptr;
75 char *CrashLog::LogCompiler(char *buffer
, const char *last
) const
77 buffer
+= seprintf(buffer
, last
, " Compiler: "
80 #elif defined(__ICC) && defined(__GNUC__)
81 "ICC %d (GCC %d.%d.%d mode)", __ICC
, __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
84 #elif defined(__GNUC__)
85 "GCC %d.%d.%d", __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
86 #elif defined(__WATCOMC__)
87 "WatcomC %d", __WATCOMC__
92 #if defined(__VERSION__)
93 return buffer
+ seprintf(buffer
, last
, " \"" __VERSION__
"\"\n\n");
95 return buffer
+ seprintf(buffer
, last
, "\n\n");
99 /* virtual */ char *CrashLog::LogRegisters(char *buffer
, const char *last
) const
101 /* Stub implementation; not all OSes support this. */
105 /* virtual */ char *CrashLog::LogModules(char *buffer
, const char *last
) const
107 /* Stub implementation; not all OSes support this. */
112 * Writes OpenTTD's version to the buffer.
113 * @param buffer The begin where to write at.
114 * @param last The last position in the buffer to write to.
115 * @return the position of the \c '\0' character after the buffer.
117 char *CrashLog::LogOpenTTDVersion(char *buffer
, const char *last
) const
119 return buffer
+ seprintf(buffer
, last
,
121 " Version: %s (%d)\n"
122 " NewGRF ver: %08x\n"
126 " Build date: %s\n\n",
128 _openttd_revision_modified
,
129 _openttd_newgrf_version
,
135 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
150 * Writes the (important) configuration settings to the buffer.
151 * E.g. graphics set, sound set, blitter and AIs.
152 * @param buffer The begin where to write at.
153 * @param last The last position in the buffer to write to.
154 * @return the position of the \c '\0' character after the buffer.
156 char *CrashLog::LogConfiguration(char *buffer
, const char *last
) const
158 buffer
+= seprintf(buffer
, last
,
161 " Graphics set: %s (%u)\n"
163 " Music driver: %s\n"
164 " Music set: %s (%u)\n"
166 " Sound driver: %s\n"
167 " Sound set: %s (%u)\n"
168 " Video driver: %s\n\n",
169 BlitterFactory::GetCurrentBlitter() == nullptr ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
170 BaseGraphics::GetUsedSet() == nullptr ? "none" : BaseGraphics::GetUsedSet()->name
.c_str(),
171 BaseGraphics::GetUsedSet() == nullptr ? UINT32_MAX
: BaseGraphics::GetUsedSet()->version
,
172 _current_language
== nullptr ? "none" : _current_language
->file
,
173 MusicDriver::GetInstance() == nullptr ? "none" : MusicDriver::GetInstance()->GetName(),
174 BaseMusic::GetUsedSet() == nullptr ? "none" : BaseMusic::GetUsedSet()->name
.c_str(),
175 BaseMusic::GetUsedSet() == nullptr ? UINT32_MAX
: BaseMusic::GetUsedSet()->version
,
176 _networking
? (_network_server
? "server" : "client") : "no",
177 SoundDriver::GetInstance() == nullptr ? "none" : SoundDriver::GetInstance()->GetName(),
178 BaseSounds::GetUsedSet() == nullptr ? "none" : BaseSounds::GetUsedSet()->name
.c_str(),
179 BaseSounds::GetUsedSet() == nullptr ? UINT32_MAX
: BaseSounds::GetUsedSet()->version
,
180 VideoDriver::GetInstance() == nullptr ? "none" : VideoDriver::GetInstance()->GetName()
183 buffer
+= seprintf(buffer
, last
,
189 FontCache::Get(FS_SMALL
)->GetFontName(),
190 FontCache::Get(FS_NORMAL
)->GetFontName(),
191 FontCache::Get(FS_LARGE
)->GetFontName(),
192 FontCache::Get(FS_MONO
)->GetFontName()
195 buffer
+= seprintf(buffer
, last
, "AI Configuration (local: %i) (current: %i):\n", (int)_local_company
, (int)_current_company
);
196 for (const Company
*c
: Company::Iterate()) {
197 if (c
->ai_info
== nullptr) {
198 buffer
+= seprintf(buffer
, last
, " %2i: Human\n", (int)c
->index
);
200 buffer
+= seprintf(buffer
, last
, " %2i: %s (v%d)\n", (int)c
->index
, c
->ai_info
->GetName(), c
->ai_info
->GetVersion());
204 if (Game::GetInfo() != nullptr) {
205 buffer
+= seprintf(buffer
, last
, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
207 buffer
+= seprintf(buffer
, last
, "\n");
213 * Writes information (versions) of the used libraries.
214 * @param buffer The begin where to write at.
215 * @param last The last position in the buffer to write to.
216 * @return the position of the \c '\0' character after the buffer.
218 char *CrashLog::LogLibraries(char *buffer
, const char *last
) const
220 buffer
+= seprintf(buffer
, last
, "Libraries:\n");
223 buffer
+= seprintf(buffer
, last
, " Allegro: %s\n", allegro_id
);
224 #endif /* WITH_ALLEGRO */
226 #ifdef WITH_FONTCONFIG
227 int version
= FcGetVersion();
228 buffer
+= seprintf(buffer
, last
, " FontConfig: %d.%d.%d\n", version
/ 10000, (version
/ 100) % 100, version
% 100);
229 #endif /* WITH_FONTCONFIG */
233 int major
, minor
, patch
;
234 FT_Init_FreeType(&library
);
235 FT_Library_Version(library
, &major
, &minor
, &patch
);
236 FT_Done_FreeType(library
);
237 buffer
+= seprintf(buffer
, last
, " FreeType: %d.%d.%d\n", major
, minor
, patch
);
238 #endif /* WITH_FREETYPE */
240 #if defined(WITH_ICU_LX) || defined(WITH_ICU_I18N)
241 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
242 char buf
[4 * 3 + 3 + 1];
245 u_versionToString(ver
, buf
);
247 buffer
+= seprintf(buffer
, last
, " ICU i18n: %s\n", buf
);
250 buffer
+= seprintf(buffer
, last
, " ICU lx: %s\n", buf
);
252 #endif /* WITH_ICU_LX || WITH_ICU_I18N */
255 buffer
+= seprintf(buffer
, last
, " LZMA: %s\n", lzma_version_string());
259 buffer
+= seprintf(buffer
, last
, " LZO: %s\n", lzo_version_string());
263 buffer
+= seprintf(buffer
, last
, " PNG: %s\n", png_get_libpng_ver(nullptr));
264 #endif /* WITH_PNG */
267 const SDL_version
*sdl_v
= SDL_Linked_Version();
268 buffer
+= seprintf(buffer
, last
, " SDL1: %d.%d.%d\n", sdl_v
->major
, sdl_v
->minor
, sdl_v
->patch
);
269 #elif defined(WITH_SDL2)
271 SDL_GetVersion(&sdl2_v
);
272 buffer
+= seprintf(buffer
, last
, " SDL2: %d.%d.%d\n", sdl2_v
.major
, sdl2_v
.minor
, sdl2_v
.patch
);
276 buffer
+= seprintf(buffer
, last
, " Zlib: %s\n", zlibVersion());
279 buffer
+= seprintf(buffer
, last
, "\n");
284 * Helper function for printing the gamelog.
285 * @param s the string to print.
287 /* static */ void CrashLog::GamelogFillCrashLog(const char *s
)
289 CrashLog::gamelog_buffer
+= seprintf(CrashLog::gamelog_buffer
, CrashLog::gamelog_last
, "%s\n", s
);
293 * Writes the gamelog data to the buffer.
294 * @param buffer The begin where to write at.
295 * @param last The last position in the buffer to write to.
296 * @return the position of the \c '\0' character after the buffer.
298 char *CrashLog::LogGamelog(char *buffer
, const char *last
) const
300 CrashLog::gamelog_buffer
= buffer
;
301 CrashLog::gamelog_last
= last
;
302 GamelogPrint(&CrashLog::GamelogFillCrashLog
);
303 return CrashLog::gamelog_buffer
+ seprintf(CrashLog::gamelog_buffer
, last
, "\n");
307 * Writes up to 32 recent news messages to the buffer, with the most recent first.
308 * @param buffer The begin where to write at.
309 * @param last The last position in the buffer to write to.
310 * @return the position of the \c '\0' character after the buffer.
312 char *CrashLog::LogRecentNews(char *buffer
, const char *last
) const
314 buffer
+= seprintf(buffer
, last
, "Recent news messages:\n");
317 for (NewsItem
*news
= _latest_news
; i
< 32 && news
!= nullptr; news
= news
->prev
, i
++) {
319 ConvertDateToYMD(news
->date
, &ymd
);
320 buffer
+= seprintf(buffer
, last
, "(%i-%02i-%02i) StringID: %u, Type: %u, Ref1: %u, %u, Ref2: %u, %u\n",
321 ymd
.year
, ymd
.month
+ 1, ymd
.day
, news
->string_id
, news
->type
,
322 news
->reftype1
, news
->ref1
, news
->reftype2
, news
->ref2
);
324 buffer
+= seprintf(buffer
, last
, "\n");
329 * Fill the crash log buffer with all data of a crash log.
330 * @param buffer The begin where to write at.
331 * @param last The last position in the buffer to write to.
332 * @return the position of the \c '\0' character after the buffer.
334 char *CrashLog::FillCrashLog(char *buffer
, const char *last
) const
336 time_t cur_time
= time(nullptr);
337 buffer
+= seprintf(buffer
, last
, "*** OpenTTD Crash Report ***\n\n");
338 buffer
+= seprintf(buffer
, last
, "Crash at: %s", asctime(gmtime(&cur_time
)));
341 ConvertDateToYMD(_date
, &ymd
);
342 buffer
+= seprintf(buffer
, last
, "In game date: %i-%02i-%02i (%i)\n\n", ymd
.year
, ymd
.month
+ 1, ymd
.day
, _date_fract
);
344 buffer
= this->LogError(buffer
, last
, CrashLog::message
);
345 buffer
= this->LogOpenTTDVersion(buffer
, last
);
346 buffer
= this->LogRegisters(buffer
, last
);
347 buffer
= this->LogStacktrace(buffer
, last
);
348 buffer
= this->LogOSVersion(buffer
, last
);
349 buffer
= this->LogCompiler(buffer
, last
);
350 buffer
= this->LogConfiguration(buffer
, last
);
351 buffer
= this->LogLibraries(buffer
, last
);
352 buffer
= this->LogModules(buffer
, last
);
353 buffer
= this->LogGamelog(buffer
, last
);
354 buffer
= this->LogRecentNews(buffer
, last
);
356 buffer
+= seprintf(buffer
, last
, "*** End of OpenTTD Crash Report ***\n");
361 * Write the crash log to a file.
362 * @note On success the filename will be filled with the full path of the
363 * crash log file. Make sure filename is at least \c MAX_PATH big.
364 * @param buffer The begin of the buffer to write to the disk.
365 * @param filename Output for the filename of the written file.
366 * @param filename_last The last position in the filename buffer.
367 * @return true when the crash log was successfully written.
369 bool CrashLog::WriteCrashLog(const char *buffer
, char *filename
, const char *filename_last
) const
371 seprintf(filename
, filename_last
, "%scrash.log", _personal_dir
.c_str());
373 FILE *file
= FioFOpenFile(filename
, "w", NO_DIRECTORY
);
374 if (file
== nullptr) return false;
376 size_t len
= strlen(buffer
);
377 size_t written
= fwrite(buffer
, 1, len
, file
);
380 return len
== written
;
383 /* virtual */ int CrashLog::WriteCrashDump(char *filename
, const char *filename_last
) const
385 /* Stub implementation; not all OSes support this. */
390 * Write the (crash) savegame to a file.
391 * @note On success the filename will be filled with the full path of the
392 * crash save file. Make sure filename is at least \c MAX_PATH big.
393 * @param filename Output for the filename of the written file.
394 * @param filename_last The last position in the filename buffer.
395 * @return true when the crash save was successfully made.
397 bool CrashLog::WriteSavegame(char *filename
, const char *filename_last
) const
399 /* If the map array doesn't exist, saving will fail too. If the map got
400 * initialised, there is a big chance the rest is initialised too. */
401 if (_m
== nullptr) return false;
406 seprintf(filename
, filename_last
, "%scrash.sav", _personal_dir
.c_str());
408 /* Don't do a threaded saveload. */
409 return SaveOrLoad(filename
, SLO_SAVE
, DFT_GAME_FILE
, NO_DIRECTORY
, false) == SL_OK
;
416 * Write the (crash) screenshot to a file.
417 * @note On success the filename will be filled with the full path of the
418 * screenshot. Make sure filename is at least \c MAX_PATH big.
419 * @param filename Output for the filename of the written file.
420 * @param filename_last The last position in the filename buffer.
421 * @return true when the crash screenshot was successfully made.
423 bool CrashLog::WriteScreenshot(char *filename
, const char *filename_last
) const
425 /* Don't draw when we have invalid screen size */
426 if (_screen
.width
< 1 || _screen
.height
< 1 || _screen
.dst_ptr
== nullptr) return false;
428 bool res
= MakeScreenshot(SC_CRASHLOG
, "crash");
429 if (res
) strecpy(filename
, _full_screenshot_name
, filename_last
);
434 * Makes the crash log, writes it to a file and then subsequently tries
435 * to make a crash dump and crash savegame. It uses DEBUG to write
436 * information like paths to the console.
437 * @return true when everything is made successfully.
439 bool CrashLog::MakeCrashLog() const
441 /* Don't keep looping logging crashes. */
442 static bool crashlogged
= false;
443 if (crashlogged
) return false;
446 char filename
[MAX_PATH
];
450 printf("Crash encountered, generating crash log...\n");
451 this->FillCrashLog(buffer
, lastof(buffer
));
452 printf("%s\n", buffer
);
453 printf("Crash log generated.\n\n");
455 printf("Writing crash log to disk...\n");
456 bool bret
= this->WriteCrashLog(buffer
, filename
, lastof(filename
));
458 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename
);
460 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
464 /* Don't mention writing crash dumps because not all platforms support it. */
465 int dret
= this->WriteCrashDump(filename
, lastof(filename
));
467 printf("Writing crash dump failed.\n\n");
469 } else if (dret
> 0) {
470 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename
);
473 printf("Writing crash savegame...\n");
474 bret
= this->WriteSavegame(filename
, lastof(filename
));
476 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename
);
479 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
482 printf("Writing crash screenshot...\n");
483 bret
= this->WriteScreenshot(filename
, lastof(filename
));
485 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename
);
488 printf("Writing crash screenshot failed.\n\n");
495 * Sets a message for the error message handler.
496 * @param message The error message of the error.
498 /* static */ void CrashLog::SetErrorMessage(const char *message
)
500 CrashLog::message
= message
;
504 * Try to close the sound/video stuff so it doesn't keep lingering around
505 * incorrect video states or so, e.g. keeping dpmi disabled.
507 /* static */ void CrashLog::AfterCrashLogCleanup()
509 if (MusicDriver::GetInstance() != nullptr) MusicDriver::GetInstance()->Stop();
510 if (SoundDriver::GetInstance() != nullptr) SoundDriver::GetInstance()->Stop();
511 if (VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop();