Also scroll tile separators in the train depot
[openttd/fttd.git] / src / crashlog.cpp
blobf5fa7b96143cd5a2ed8c8741712a0dd076964d33
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"
14 #ifdef WITH_ALLEGRO
15 # include <allegro.h>
16 #endif /* WITH_ALLEGRO */
17 #ifdef WITH_FONTCONFIG
18 # include <fontconfig/fontconfig.h>
19 #endif /* WITH_FONTCONFIG */
20 #ifdef WITH_PNG
21 /* pngconf.h, included by png.h doesn't like something in the
22 * freetype headers. As such it's not alphabetically sorted. */
23 # include <png.h>
24 #endif /* WITH_PNG */
25 #ifdef WITH_FREETYPE
26 # include <ft2build.h>
27 # include FT_FREETYPE_H
28 #endif /* WITH_FREETYPE */
29 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
30 # include <unicode/uversion.h>
31 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
32 #ifdef WITH_LZMA
33 # include <lzma.h>
34 #endif
35 #ifdef WITH_LZO
36 #include <lzo/lzo1x.h>
37 #endif
38 #ifdef WITH_SDL
39 # include "sdl.h"
40 # include <SDL.h>
41 #endif /* WITH_SDL */
42 #ifdef WITH_ZLIB
43 # include <zlib.h>
44 #endif
46 #include "crashlog.h"
47 #include "gamelog.h"
48 #include "date_func.h"
49 #include "rev.h"
50 #include "strings_func.h"
51 #include "blitter/blitter.h"
52 #include "base_media_base.h"
53 #include "music/music_driver.hpp"
54 #include "sound/sound_driver.hpp"
55 #include "video/video_driver.hpp"
56 #include "saveload/saveload.h"
57 #include "screenshot.h"
58 #include "gfx_func.h"
59 #include "network/network.h"
60 #include "language.h"
61 #include "font.h"
63 #include "ai/ai_info.hpp"
64 #include "game/game.hpp"
65 #include "game/game_info.hpp"
66 #include "company_base.h"
67 #include "company_func.h"
69 #include <time.h>
71 /* static */ const char *CrashLog::message = NULL;
73 /**
74 * Writes compiler (and its version, if available) to the buffer.
75 * @param buffer The string where to write.
77 static void LogCompiler (stringb *buffer)
79 buffer->append_fmt (" 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 buffer->append (" \"" __VERSION__ "\"\n\n");
96 #else
97 buffer->append ("\n\n");
98 #endif
101 /* virtual */ void CrashLog::LogRegisters (stringb *buffer) const
103 /* Stub implementation; not all OSes support this. */
106 /* virtual */ void CrashLog::LogModules (stringb *buffer) const
108 /* Stub implementation; not all OSes support this. */
112 * Writes version and compilation information to the buffer.
113 * @param buffer The string where to write.
115 static void LogVersion (stringb *buffer)
117 buffer->append_fmt (
118 "Binary:\n"
119 " Version: %s (%d)\n"
120 " NewGRF ver: %08x\n"
121 " Build date: %s\n"
122 " Flags: "
123 #ifdef _SQ64
124 " 64-bit"
125 #else
126 " 32-bit"
127 #endif
128 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
129 " little-endian"
130 #else
131 " big-endian"
132 #endif
133 #ifdef DEDICATED
134 " dedicated"
135 #endif
136 "\n\n",
137 _openttd_revision, _openttd_revision_modified,
138 _openttd_newgrf_version,
139 _openttd_build_date
144 * Writes the (important) configuration settings to the buffer.
145 * E.g. graphics set, sound set, blitter and AIs.
146 * @param buffer The string where to write.
148 static void LogConfiguration (stringb *buffer)
150 buffer->append_fmt (
151 "Configuration:\n"
152 " Blitter: %s\n"
153 " Graphics set: %s (%u)\n"
154 " Language: %s\n"
155 " Music driver: %s\n"
156 " Music set: %s (%u)\n"
157 " Network: %s\n"
158 " Sound driver: %s\n"
159 " Sound set: %s (%u)\n"
160 " Video driver: %s\n\n",
161 Blitter::get() == NULL ? "none" : Blitter::get()->name,
162 BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->get_name(),
163 BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
164 _current_language == NULL ? "none" : _current_language->file,
165 MusicDriver::GetActiveDriver() == NULL ? "none" : MusicDriver::GetActiveDriverName(),
166 BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->get_name(),
167 BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
168 _networking ? (_network_server ? "server" : "client") : "no",
169 SoundDriver::GetActiveDriver() == NULL ? "none" : SoundDriver::GetActiveDriverName(),
170 BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->get_name(),
171 BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
172 VideoDriver::GetActiveDriver() == NULL ? "none" : VideoDriver::GetActiveDriverName()
175 buffer->append_fmt (
176 "Fonts:\n"
177 " Small: %s\n"
178 " Medium: %s\n"
179 " Large: %s\n"
180 " Mono: %s\n\n",
181 FontCache::Get(FS_SMALL)->GetFontName(),
182 FontCache::Get(FS_NORMAL)->GetFontName(),
183 FontCache::Get(FS_LARGE)->GetFontName(),
184 FontCache::Get(FS_MONO)->GetFontName()
187 buffer->append_fmt ("AI Configuration (local: %i):\n", (int)_local_company);
188 const Company *c;
189 FOR_ALL_COMPANIES(c) {
190 if (c->ai_info == NULL) {
191 buffer->append_fmt (" %2i: Human\n", (int)c->index);
192 } else {
193 buffer->append_fmt (" %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
197 if (Game::GetInfo() != NULL) {
198 buffer->append_fmt (" GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
200 buffer->append ('\n');
204 * Writes information (versions) of the used libraries.
205 * @param buffer The string where to write.
207 static void LogLibraries (stringb *buffer)
209 buffer->append ("Libraries:\n");
211 #ifdef WITH_ALLEGRO
212 buffer->append_fmt (" Allegro: %s\n", allegro_id);
213 #endif /* WITH_ALLEGRO */
215 #ifdef WITH_FONTCONFIG
216 int version = FcGetVersion();
217 buffer->append_fmt (" FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
218 #endif /* WITH_FONTCONFIG */
220 #ifdef WITH_FREETYPE
221 FT_Library library;
222 int major, minor, patch;
223 FT_Init_FreeType(&library);
224 FT_Library_Version(library, &major, &minor, &patch);
225 FT_Done_FreeType(library);
226 buffer->append_fmt (" FreeType: %d.%d.%d\n", major, minor, patch);
227 #endif /* WITH_FREETYPE */
229 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
230 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
231 char buf[4 * 3 + 3 + 1];
232 UVersionInfo ver;
233 u_getVersion(ver);
234 u_versionToString(ver, buf);
235 #ifdef WITH_ICU_SORT
236 buffer->append_fmt (" ICU i18n: %s\n", buf);
237 #endif
238 #ifdef WITH_ICU_LAYOUT
239 buffer->append_fmt (" ICU lx: %s\n", buf);
240 #endif
241 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
243 #ifdef WITH_LZMA
244 buffer->append_fmt (" LZMA: %s\n", lzma_version_string());
245 #endif
247 #ifdef WITH_LZO
248 buffer->append_fmt (" LZO: %s\n", lzo_version_string());
249 #endif
251 #ifdef WITH_PNG
252 buffer->append_fmt (" PNG: %s\n", png_get_libpng_ver(NULL));
253 #endif /* WITH_PNG */
255 #ifdef WITH_SDL
256 #ifdef DYNAMICALLY_LOADED_SDL
257 if (SDL_CALL SDL_Linked_Version != NULL) {
258 #else
260 #endif
261 const SDL_version *v = SDL_CALL SDL_Linked_Version();
262 buffer->append_fmt (" SDL: %d.%d.%d\n", v->major, v->minor, v->patch);
264 #endif /* WITH_SDL */
266 #ifdef WITH_ZLIB
267 buffer->append_fmt (" Zlib: %s\n", zlibVersion());
268 #endif
270 buffer->append ('\n');
274 * Helper function for printing the gamelog.
275 * @param s the string to print.
276 * @param buffer the buffer where to print.
278 static void GamelogFillCrashLog (const char *s, void *buffer)
280 ((stringb*)buffer)->append_fmt ("%s\n", s);
284 * Fill the crash log buffer with all data of a crash log.
285 * @param buffer The string where to write.
287 void CrashLog::FillCrashLog (stringb *buffer) const
289 time_t cur_time = time(NULL);
290 buffer->append ("*** OpenTTD Crash Report ***\n\n");
291 buffer->append_fmt ("Crash at: %s", asctime(gmtime(&cur_time)));
293 YearMonthDay ymd;
294 ConvertDateToYMD(_date, &ymd);
295 buffer->append_fmt ("In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
297 this->LogError(buffer, CrashLog::message);
298 LogVersion(buffer);
299 this->LogRegisters(buffer);
300 this->LogStacktrace(buffer);
301 this->LogOSVersion(buffer);
302 LogCompiler(buffer);
303 LogConfiguration(buffer);
304 LogLibraries(buffer);
305 this->LogModules(buffer);
307 /* Write the gamelog data to the buffer. */
308 GamelogPrint (&GamelogFillCrashLog, buffer);
310 buffer->append ("\n*** End of OpenTTD Crash Report ***\n");
314 * Write the crash log to a file.
315 * @note On success the filename will be filled with the full path of the
316 * crash log file. Make sure filename is at least \c MAX_PATH big.
317 * @param buffer The begin of the buffer to write to the disk.
318 * @param filename Output for the filename of the written file.
319 * @return true when the crash log was successfully written.
321 bool CrashLog::WriteCrashLog (const char *buffer, stringb *filename) const
323 filename->fmt ("%scrash.log", _personal_dir);
325 FILE *file = FioFOpenFile (filename->c_str(), "w", NO_DIRECTORY);
326 if (file == NULL) return false;
328 size_t len = strlen(buffer);
329 size_t written = fwrite(buffer, 1, len, file);
331 FioFCloseFile(file);
332 return len == written;
335 /* virtual */ int CrashLog::WriteCrashDump (stringb *filename) const
337 /* Stub implementation; not all OSes support this. */
338 return 0;
342 * Write the (crash) savegame to a file.
343 * @note On success the filename will be filled with the full path of the
344 * crash save file. Make sure filename is at least \c MAX_PATH big.
345 * @param filename Output for the filename of the written file.
346 * @return true when the crash save was successfully made.
348 bool CrashLog::WriteSavegame (stringb *filename) const
350 /* If the map array doesn't exist, saving will fail too. If the map got
351 * initialised, there is a big chance the rest is initialised too. */
352 if (_mth == NULL) return false;
354 try {
355 GamelogEmergency();
357 filename->fmt ("%scrash.sav", _personal_dir);
359 /* Don't do a threaded saveload. */
360 return SaveGame(filename->c_str(), NO_DIRECTORY, false);
361 } catch (...) {
362 return false;
367 * Write the (crash) screenshot to a file.
368 * @note On success the filename will be filled with the full path of the
369 * screenshot. Make sure filename is at least \c MAX_PATH big.
370 * @param filename Output for the filename of the written file.
371 * @return true when the crash screenshot was successfully made.
373 bool CrashLog::WriteScreenshot (stringb *filename) const
375 /* Don't draw when we have invalid screen size */
376 if (_screen_width < 1 || _screen_height < 1 || !_screen_surface) return false;
378 bool res = MakeScreenshot(SC_CRASHLOG, "crash");
379 if (res) filename->copy (_full_screenshot_name);
380 return res;
384 * Makes the crash log, writes it to a file and then subsequently tries
385 * to make a crash dump and crash savegame. It uses DEBUG to write
386 * information like paths to the console.
387 * @return true when everything is made successfully.
389 bool CrashLog::MakeCrashLog() const
391 /* Don't keep looping logging crashes. */
392 static bool crashlogged = false;
393 if (crashlogged) return false;
394 crashlogged = true;
396 sstring<MAX_PATH> filename;
397 sstring<65536> buffer;
398 bool ret = true;
400 printf("Crash encountered, generating crash log...\n");
401 this->FillCrashLog (&buffer);
402 printf("%s\n", buffer.c_str());
403 printf("Crash log generated.\n\n");
405 printf("Writing crash log to disk...\n");
406 bool bret = this->WriteCrashLog (buffer.c_str(), &filename);
407 if (bret) {
408 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename.c_str());
409 } else {
410 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
411 ret = false;
414 /* Don't mention writing crash dumps because not all platforms support it. */
415 int dret = this->WriteCrashDump (&filename);
416 if (dret < 0) {
417 printf("Writing crash dump failed.\n\n");
418 ret = false;
419 } else if (dret > 0) {
420 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename.c_str());
423 printf("Writing crash savegame...\n");
424 bret = this->WriteSavegame (&filename);
425 if (bret) {
426 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename.c_str());
427 } else {
428 ret = false;
429 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
432 printf("Writing crash screenshot...\n");
433 bret = this->WriteScreenshot (&filename);
434 if (bret) {
435 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename.c_str());
436 } else {
437 ret = false;
438 printf("Writing crash screenshot failed.\n\n");
441 return ret;
445 * Sets a message for the error message handler.
446 * @param message The error message of the error.
448 /* static */ void CrashLog::SetErrorMessage(const char *message)
450 CrashLog::message = message;
454 * Try to close the sound/video stuff so it doesn't keep lingering around
455 * incorrect video states or so, e.g. keeping dpmi disabled.
457 /* static */ void CrashLog::AfterCrashLogCleanup()
459 if (MusicDriver::GetActiveDriver() != NULL) MusicDriver::GetActiveDriver()->Stop();
460 if (SoundDriver::GetActiveDriver() != NULL) SoundDriver::GetActiveDriver()->Stop();
461 if (VideoDriver::GetActiveDriver() != NULL) VideoDriver::GetActiveDriver()->Stop();