(svn r27724) -Cleanup: Remove pointless usage of IsOpenTTDBaseGRF. System GRFs are...
[openttd.git] / src / crashlog.cpp
blob53a85e442cd272c5f2de24bc333a02dece365e53
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file crashlog.cpp Implementation of generic function to be called to log a crash */
12 #include "stdafx.h"
13 #include "crashlog.h"
14 #include "gamelog.h"
15 #include "date_func.h"
16 #include "map_func.h"
17 #include "rev.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"
26 #include "gfx_func.h"
27 #include "network/network.h"
28 #include "language.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"
37 #include <time.h>
39 #ifdef WITH_ALLEGRO
40 # include <allegro.h>
41 #endif /* WITH_ALLEGRO */
42 #ifdef WITH_FONTCONFIG
43 # include <fontconfig/fontconfig.h>
44 #endif /* WITH_FONTCONFIG */
45 #ifdef WITH_PNG
46 /* pngconf.h, included by png.h doesn't like something in the
47 * freetype headers. As such it's not alphabetically sorted. */
48 # include <png.h>
49 #endif /* WITH_PNG */
50 #ifdef WITH_FREETYPE
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 */
57 #ifdef WITH_LZMA
58 # include <lzma.h>
59 #endif
60 #ifdef WITH_LZO
61 #include <lzo/lzo1x.h>
62 #endif
63 #ifdef WITH_SDL
64 # include "sdl.h"
65 # include <SDL.h>
66 #endif /* WITH_SDL */
67 #ifdef WITH_ZLIB
68 # include <zlib.h>
69 #endif
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: "
80 #if defined(_MSC_VER)
81 "MSVC %d", _MSC_VER
82 #elif defined(__ICC) && defined(__GNUC__)
83 "ICC %d (GCC %d.%d.%d mode)", __ICC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
84 #elif defined(__ICC)
85 "ICC %d", __ICC
86 #elif defined(__GNUC__)
87 "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
88 #elif defined(__WATCOMC__)
89 "WatcomC %d", __WATCOMC__
90 #else
91 "<unknown>"
92 #endif
94 #if defined(__VERSION__)
95 return buffer + seprintf(buffer, last, " \"" __VERSION__ "\"\n\n");
96 #else
97 return buffer + seprintf(buffer, last, "\n\n");
98 #endif
101 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
103 /* Stub implementation; not all OSes support this. */
104 return buffer;
107 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
109 /* Stub implementation; not all OSes support this. */
110 return buffer;
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,
122 "OpenTTD version:\n"
123 " Version: %s (%d)\n"
124 " NewGRF ver: %08x\n"
125 " Bits: %d\n"
126 " Endian: %s\n"
127 " Dedicated: %s\n"
128 " Build date: %s\n\n",
129 _openttd_revision,
130 _openttd_revision_modified,
131 _openttd_newgrf_version,
132 #ifdef _SQ64
134 #else
136 #endif
137 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
138 "little",
139 #else
140 "big",
141 #endif
142 #ifdef DEDICATED
143 "yes",
144 #else
145 "no",
146 #endif
147 _openttd_build_date
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,
161 "Configuration:\n"
162 " Blitter: %s\n"
163 " Graphics set: %s (%u)\n"
164 " Language: %s\n"
165 " Music driver: %s\n"
166 " Music set: %s (%u)\n"
167 " Network: %s\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,
186 "Fonts:\n"
187 " Small: %s\n"
188 " Medium: %s\n"
189 " Large: %s\n"
190 " Mono: %s\n\n",
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);
198 const Company *c;
199 FOR_ALL_COMPANIES(c) {
200 if (c->ai_info == NULL) {
201 buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
202 } else {
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");
212 return buffer;
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");
225 #ifdef WITH_ALLEGRO
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 */
234 #ifdef WITH_FREETYPE
235 FT_Library library;
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];
246 UVersionInfo ver;
247 u_getVersion(ver);
248 u_versionToString(ver, buf);
249 #ifdef WITH_ICU_SORT
250 buffer += seprintf(buffer, last, " ICU i18n: %s\n", buf);
251 #endif
252 #ifdef WITH_ICU_LAYOUT
253 buffer += seprintf(buffer, last, " ICU lx: %s\n", buf);
254 #endif
255 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
257 #ifdef WITH_LZMA
258 buffer += seprintf(buffer, last, " LZMA: %s\n", lzma_version_string());
259 #endif
261 #ifdef WITH_LZO
262 buffer += seprintf(buffer, last, " LZO: %s\n", lzo_version_string());
263 #endif
265 #ifdef WITH_PNG
266 buffer += seprintf(buffer, last, " PNG: %s\n", png_get_libpng_ver(NULL));
267 #endif /* WITH_PNG */
269 #ifdef WITH_SDL
270 #ifdef DYNAMICALLY_LOADED_SDL
271 if (SDL_CALL SDL_Linked_Version != NULL) {
272 #else
274 #endif
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 */
280 #ifdef WITH_ZLIB
281 buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
282 #endif
284 buffer += seprintf(buffer, last, "\n");
285 return buffer;
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)));
323 YearMonthDay ymd;
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");
339 return buffer;
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);
361 FioFCloseFile(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. */
368 return 0;
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;
385 try {
386 GamelogEmergency();
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;
392 } catch (...) {
393 return false;
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);
412 return res;
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;
426 crashlogged = true;
428 char filename[MAX_PATH];
429 char buffer[65536];
430 bool ret = true;
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));
439 if (bret) {
440 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
441 } else {
442 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
443 ret = false;
446 /* Don't mention writing crash dumps because not all platforms support it. */
447 int dret = this->WriteCrashDump(filename, lastof(filename));
448 if (dret < 0) {
449 printf("Writing crash dump failed.\n\n");
450 ret = false;
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));
457 if (bret) {
458 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
459 } else {
460 ret = false;
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));
466 if (bret) {
467 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
468 } else {
469 ret = false;
470 printf("Writing crash screenshot failed.\n\n");
473 return ret;
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();