4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file crashlog.cpp Implementation of generic function to be called to log a crash */
15 #include "date_func.h"
18 #include "strings_func.h"
19 #include "blitter/factory.hpp"
20 #include "base_media_base.h"
21 #include "music/music_driver.hpp"
22 #include "sound/sound_driver.hpp"
23 #include "video/video_driver.hpp"
24 #include "saveload/saveload.h"
25 #include "screenshot.h"
27 #include "network/network.h"
29 #include "fontcache.h"
31 #include "ai/ai_info.hpp"
32 #include "game/game.hpp"
33 #include "game/game_info.hpp"
34 #include "company_base.h"
35 #include "company_func.h"
41 #endif /* WITH_ALLEGRO */
42 #ifdef WITH_FONTCONFIG
43 # include <fontconfig/fontconfig.h>
44 #endif /* WITH_FONTCONFIG */
46 /* pngconf.h, included by png.h doesn't like something in the
47 * freetype headers. As such it's not alphabetically sorted. */
51 # include <ft2build.h>
52 # include FT_FREETYPE_H
53 #endif /* WITH_FREETYPE */
54 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
55 # include <unicode/uversion.h>
56 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
61 #include <lzo/lzo1x.h>
71 #include "safeguards.h"
73 /* static */ const char *CrashLog::message
= NULL
;
74 /* static */ char *CrashLog::gamelog_buffer
= NULL
;
75 /* static */ const char *CrashLog::gamelog_last
= NULL
;
77 char *CrashLog::LogCompiler(char *buffer
, const char *last
) const
79 buffer
+= seprintf(buffer
, last
, " Compiler: "
82 #elif defined(__ICC) && defined(__GNUC__)
83 "ICC %d (GCC %d.%d.%d mode)", __ICC
, __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
86 #elif defined(__GNUC__)
87 "GCC %d.%d.%d", __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
88 #elif defined(__WATCOMC__)
89 "WatcomC %d", __WATCOMC__
94 #if defined(__VERSION__)
95 return buffer
+ seprintf(buffer
, last
, " \"" __VERSION__
"\"\n\n");
97 return buffer
+ seprintf(buffer
, last
, "\n\n");
101 /* virtual */ char *CrashLog::LogRegisters(char *buffer
, const char *last
) const
103 /* Stub implementation; not all OSes support this. */
107 /* virtual */ char *CrashLog::LogModules(char *buffer
, const char *last
) const
109 /* Stub implementation; not all OSes support this. */
114 * Writes OpenTTD's version to the buffer.
115 * @param buffer The begin where to write at.
116 * @param last The last position in the buffer to write to.
117 * @return the position of the \c '\0' character after the buffer.
119 char *CrashLog::LogOpenTTDVersion(char *buffer
, const char *last
) const
121 return buffer
+ seprintf(buffer
, last
,
123 " Version: %s (%d)\n"
124 " NewGRF ver: %08x\n"
128 " Build date: %s\n\n",
130 _openttd_revision_modified
,
131 _openttd_newgrf_version
,
137 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
152 * Writes the (important) configuration settings to the buffer.
153 * E.g. graphics set, sound set, blitter and AIs.
154 * @param buffer The begin where to write at.
155 * @param last The last position in the buffer to write to.
156 * @return the position of the \c '\0' character after the buffer.
158 char *CrashLog::LogConfiguration(char *buffer
, const char *last
) const
160 buffer
+= seprintf(buffer
, last
,
163 " Graphics set: %s (%u)\n"
165 " Music driver: %s\n"
166 " Music set: %s (%u)\n"
168 " Sound driver: %s\n"
169 " Sound set: %s (%u)\n"
170 " Video driver: %s\n\n",
171 BlitterFactory::GetCurrentBlitter() == NULL
? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
172 BaseGraphics::GetUsedSet() == NULL
? "none" : BaseGraphics::GetUsedSet()->name
,
173 BaseGraphics::GetUsedSet() == NULL
? UINT32_MAX
: BaseGraphics::GetUsedSet()->version
,
174 _current_language
== NULL
? "none" : _current_language
->file
,
175 MusicDriver::GetInstance() == NULL
? "none" : MusicDriver::GetInstance()->GetName(),
176 BaseMusic::GetUsedSet() == NULL
? "none" : BaseMusic::GetUsedSet()->name
,
177 BaseMusic::GetUsedSet() == NULL
? UINT32_MAX
: BaseMusic::GetUsedSet()->version
,
178 _networking
? (_network_server
? "server" : "client") : "no",
179 SoundDriver::GetInstance() == NULL
? "none" : SoundDriver::GetInstance()->GetName(),
180 BaseSounds::GetUsedSet() == NULL
? "none" : BaseSounds::GetUsedSet()->name
,
181 BaseSounds::GetUsedSet() == NULL
? UINT32_MAX
: BaseSounds::GetUsedSet()->version
,
182 VideoDriver::GetInstance() == NULL
? "none" : VideoDriver::GetInstance()->GetName()
185 buffer
+= seprintf(buffer
, last
,
191 FontCache::Get(FS_SMALL
)->GetFontName(),
192 FontCache::Get(FS_NORMAL
)->GetFontName(),
193 FontCache::Get(FS_LARGE
)->GetFontName(),
194 FontCache::Get(FS_MONO
)->GetFontName()
197 buffer
+= seprintf(buffer
, last
, "AI Configuration (local: %i):\n", (int)_local_company
);
199 FOR_ALL_COMPANIES(c
) {
200 if (c
->ai_info
== NULL
) {
201 buffer
+= seprintf(buffer
, last
, " %2i: Human\n", (int)c
->index
);
203 buffer
+= seprintf(buffer
, last
, " %2i: %s (v%d)\n", (int)c
->index
, c
->ai_info
->GetName(), c
->ai_info
->GetVersion());
207 if (Game::GetInfo() != NULL
) {
208 buffer
+= seprintf(buffer
, last
, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
210 buffer
+= seprintf(buffer
, last
, "\n");
216 * Writes information (versions) of the used libraries.
217 * @param buffer The begin where to write at.
218 * @param last The last position in the buffer to write to.
219 * @return the position of the \c '\0' character after the buffer.
221 char *CrashLog::LogLibraries(char *buffer
, const char *last
) const
223 buffer
+= seprintf(buffer
, last
, "Libraries:\n");
226 buffer
+= seprintf(buffer
, last
, " Allegro: %s\n", allegro_id
);
227 #endif /* WITH_ALLEGRO */
229 #ifdef WITH_FONTCONFIG
230 int version
= FcGetVersion();
231 buffer
+= seprintf(buffer
, last
, " FontConfig: %d.%d.%d\n", version
/ 10000, (version
/ 100) % 100, version
% 100);
232 #endif /* WITH_FONTCONFIG */
236 int major
, minor
, patch
;
237 FT_Init_FreeType(&library
);
238 FT_Library_Version(library
, &major
, &minor
, &patch
);
239 FT_Done_FreeType(library
);
240 buffer
+= seprintf(buffer
, last
, " FreeType: %d.%d.%d\n", major
, minor
, patch
);
241 #endif /* WITH_FREETYPE */
243 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
244 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
245 char buf
[4 * 3 + 3 + 1];
248 u_versionToString(ver
, buf
);
250 buffer
+= seprintf(buffer
, last
, " ICU i18n: %s\n", buf
);
252 #ifdef WITH_ICU_LAYOUT
253 buffer
+= seprintf(buffer
, last
, " ICU lx: %s\n", buf
);
255 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
258 buffer
+= seprintf(buffer
, last
, " LZMA: %s\n", lzma_version_string());
262 buffer
+= seprintf(buffer
, last
, " LZO: %s\n", lzo_version_string());
266 buffer
+= seprintf(buffer
, last
, " PNG: %s\n", png_get_libpng_ver(NULL
));
267 #endif /* WITH_PNG */
270 #ifdef DYNAMICALLY_LOADED_SDL
271 if (SDL_CALL SDL_Linked_Version
!= NULL
) {
275 const SDL_version
*v
= SDL_CALL
SDL_Linked_Version();
276 buffer
+= seprintf(buffer
, last
, " SDL: %d.%d.%d\n", v
->major
, v
->minor
, v
->patch
);
278 #endif /* WITH_SDL */
281 buffer
+= seprintf(buffer
, last
, " Zlib: %s\n", zlibVersion());
284 buffer
+= seprintf(buffer
, last
, "\n");
289 * Helper function for printing the gamelog.
290 * @param s the string to print.
292 /* static */ void CrashLog::GamelogFillCrashLog(const char *s
)
294 CrashLog::gamelog_buffer
+= seprintf(CrashLog::gamelog_buffer
, CrashLog::gamelog_last
, "%s\n", s
);
298 * Writes the gamelog data to the buffer.
299 * @param buffer The begin where to write at.
300 * @param last The last position in the buffer to write to.
301 * @return the position of the \c '\0' character after the buffer.
303 char *CrashLog::LogGamelog(char *buffer
, const char *last
) const
305 CrashLog::gamelog_buffer
= buffer
;
306 CrashLog::gamelog_last
= last
;
307 GamelogPrint(&CrashLog::GamelogFillCrashLog
);
308 return CrashLog::gamelog_buffer
+ seprintf(CrashLog::gamelog_buffer
, last
, "\n");
312 * Fill the crash log buffer with all data of a crash log.
313 * @param buffer The begin where to write at.
314 * @param last The last position in the buffer to write to.
315 * @return the position of the \c '\0' character after the buffer.
317 char *CrashLog::FillCrashLog(char *buffer
, const char *last
) const
319 time_t cur_time
= time(NULL
);
320 buffer
+= seprintf(buffer
, last
, "*** OpenTTD Crash Report ***\n\n");
321 buffer
+= seprintf(buffer
, last
, "Crash at: %s", asctime(gmtime(&cur_time
)));
324 ConvertDateToYMD(_date
, &ymd
);
325 buffer
+= seprintf(buffer
, last
, "In game date: %i-%02i-%02i (%i)\n\n", ymd
.year
, ymd
.month
+ 1, ymd
.day
, _date_fract
);
327 buffer
= this->LogError(buffer
, last
, CrashLog::message
);
328 buffer
= this->LogOpenTTDVersion(buffer
, last
);
329 buffer
= this->LogRegisters(buffer
, last
);
330 buffer
= this->LogStacktrace(buffer
, last
);
331 buffer
= this->LogOSVersion(buffer
, last
);
332 buffer
= this->LogCompiler(buffer
, last
);
333 buffer
= this->LogConfiguration(buffer
, last
);
334 buffer
= this->LogLibraries(buffer
, last
);
335 buffer
= this->LogModules(buffer
, last
);
336 buffer
= this->LogGamelog(buffer
, last
);
338 buffer
+= seprintf(buffer
, last
, "*** End of OpenTTD Crash Report ***\n");
343 * Write the crash log to a file.
344 * @note On success the filename will be filled with the full path of the
345 * crash log file. Make sure filename is at least \c MAX_PATH big.
346 * @param buffer The begin of the buffer to write to the disk.
347 * @param filename Output for the filename of the written file.
348 * @param filename_last The last position in the filename buffer.
349 * @return true when the crash log was successfully written.
351 bool CrashLog::WriteCrashLog(const char *buffer
, char *filename
, const char *filename_last
) const
353 seprintf(filename
, filename_last
, "%scrash.log", _personal_dir
);
355 FILE *file
= FioFOpenFile(filename
, "w", NO_DIRECTORY
);
356 if (file
== NULL
) return false;
358 size_t len
= strlen(buffer
);
359 size_t written
= fwrite(buffer
, 1, len
, file
);
362 return len
== written
;
365 /* virtual */ int CrashLog::WriteCrashDump(char *filename
, const char *filename_last
) const
367 /* Stub implementation; not all OSes support this. */
372 * Write the (crash) savegame to a file.
373 * @note On success the filename will be filled with the full path of the
374 * crash save file. Make sure filename is at least \c MAX_PATH big.
375 * @param filename Output for the filename of the written file.
376 * @param filename_last The last position in the filename buffer.
377 * @return true when the crash save was successfully made.
379 bool CrashLog::WriteSavegame(char *filename
, const char *filename_last
) const
381 /* If the map array doesn't exist, saving will fail too. If the map got
382 * initialised, there is a big chance the rest is initialised too. */
383 if (_m
== NULL
) return false;
388 seprintf(filename
, filename_last
, "%scrash.sav", _personal_dir
);
390 /* Don't do a threaded saveload. */
391 return SaveOrLoad(filename
, SLO_SAVE
, DFT_GAME_FILE
, NO_DIRECTORY
, false) == SL_OK
;
398 * Write the (crash) screenshot to a file.
399 * @note On success the filename will be filled with the full path of the
400 * screenshot. Make sure filename is at least \c MAX_PATH big.
401 * @param filename Output for the filename of the written file.
402 * @param filename_last The last position in the filename buffer.
403 * @return true when the crash screenshot was successfully made.
405 bool CrashLog::WriteScreenshot(char *filename
, const char *filename_last
) const
407 /* Don't draw when we have invalid screen size */
408 if (_screen
.width
< 1 || _screen
.height
< 1 || _screen
.dst_ptr
== NULL
) return false;
410 bool res
= MakeScreenshot(SC_CRASHLOG
, "crash");
411 if (res
) strecpy(filename
, _full_screenshot_name
, filename_last
);
416 * Makes the crash log, writes it to a file and then subsequently tries
417 * to make a crash dump and crash savegame. It uses DEBUG to write
418 * information like paths to the console.
419 * @return true when everything is made successfully.
421 bool CrashLog::MakeCrashLog() const
423 /* Don't keep looping logging crashes. */
424 static bool crashlogged
= false;
425 if (crashlogged
) return false;
428 char filename
[MAX_PATH
];
432 printf("Crash encountered, generating crash log...\n");
433 this->FillCrashLog(buffer
, lastof(buffer
));
434 printf("%s\n", buffer
);
435 printf("Crash log generated.\n\n");
437 printf("Writing crash log to disk...\n");
438 bool bret
= this->WriteCrashLog(buffer
, filename
, lastof(filename
));
440 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename
);
442 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
446 /* Don't mention writing crash dumps because not all platforms support it. */
447 int dret
= this->WriteCrashDump(filename
, lastof(filename
));
449 printf("Writing crash dump failed.\n\n");
451 } else if (dret
> 0) {
452 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename
);
455 printf("Writing crash savegame...\n");
456 bret
= this->WriteSavegame(filename
, lastof(filename
));
458 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename
);
461 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
464 printf("Writing crash screenshot...\n");
465 bret
= this->WriteScreenshot(filename
, lastof(filename
));
467 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename
);
470 printf("Writing crash screenshot failed.\n\n");
477 * Sets a message for the error message handler.
478 * @param message The error message of the error.
480 /* static */ void CrashLog::SetErrorMessage(const char *message
)
482 CrashLog::message
= message
;
486 * Try to close the sound/video stuff so it doesn't keep lingering around
487 * incorrect video states or so, e.g. keeping dpmi disabled.
489 /* static */ void CrashLog::AfterCrashLogCleanup()
491 if (MusicDriver::GetInstance() != NULL
) MusicDriver::GetInstance()->Stop();
492 if (SoundDriver::GetInstance() != NULL
) SoundDriver::GetInstance()->Stop();
493 if (VideoDriver::GetInstance() != NULL
) VideoDriver::GetInstance()->Stop();