Make it possible to stop road vehicles from slowing down in curves so "diagonal"...
[openttd-joker.git] / src / crashlog.cpp
blob60b25f8811554d3ea909cc7343b0c6db5c6fa11b
1 /* $Id: crashlog.cpp 26209 2014-01-02 22:41:58Z rubidium $ */
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 "command_func.h"
35 #include "company_base.h"
36 #include "company_func.h"
38 #include <time.h>
40 #ifdef WITH_ALLEGRO
41 # include <allegro.h>
42 #endif /* WITH_ALLEGRO */
43 #ifdef WITH_FONTCONFIG
44 # include <fontconfig/fontconfig.h>
45 #endif /* WITH_FONTCONFIG */
46 #ifdef WITH_PNG
47 /* pngconf.h, included by png.h doesn't like something in the
48 * freetype headers. As such it's not alphabetically sorted. */
49 # include <png.h>
50 #endif /* WITH_PNG */
51 #ifdef WITH_FREETYPE
52 # include <ft2build.h>
53 # include FT_FREETYPE_H
54 #endif /* WITH_FREETYPE */
55 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
56 # include <unicode/uversion.h>
57 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
58 #ifdef WITH_LZMA
59 # include <lzma.h>
60 #endif
61 #ifdef WITH_LZO
62 #include <lzo/lzo1x.h>
63 #endif
64 #ifdef WITH_SDL
65 # include "sdl.h"
66 # include <SDL.h>
67 #endif /* WITH_SDL */
68 #ifdef WITH_ZLIB
69 # include <zlib.h>
70 #endif
72 #include "safeguards.h"
74 /* static */ const char *CrashLog::message = nullptr;
75 /* static */ char *CrashLog::gamelog_buffer = nullptr;
76 /* static */ const char *CrashLog::gamelog_last = nullptr;
78 char *CrashLog::LogCompiler(char *buffer, const char *last) const
80 buffer += seprintf(buffer, last, " Compiler: "
81 #if defined(_MSC_VER)
82 "MSVC %d", _MSC_VER
83 #elif defined(__ICC) && defined(__GNUC__)
84 "ICC %d (GCC %d.%d.%d mode)", __ICC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
85 #elif defined(__ICC)
86 "ICC %d", __ICC
87 #elif defined(__GNUC__)
88 "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
89 #elif defined(__WATCOMC__)
90 "WatcomC %d", __WATCOMC__
91 #else
92 "<unknown>"
93 #endif
95 #if defined(__VERSION__)
96 return buffer + seprintf(buffer, last, " \"" __VERSION__ "\"\n\n");
97 #else
98 return buffer + seprintf(buffer, last, "\n\n");
99 #endif
102 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
104 /* Stub implementation; not all OSes support this. */
105 return buffer;
108 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
110 /* Stub implementation; not all OSes support this. */
111 return buffer;
115 * Writes OpenTTD's version to the buffer.
116 * @param buffer The begin where to write at.
117 * @param last The last position in the buffer to write to.
118 * @return the position of the \c '\0' character after the buffer.
120 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
122 return buffer + seprintf(buffer, last,
123 "OpenTTD version:\n"
124 " Version: %s (%d)\n"
125 " NewGRF ver: %08x\n"
126 " Bits: %d\n"
127 " Endian: %s\n"
128 " Dedicated: %s\n"
129 " Build date: %s\n\n",
130 _openttd_revision,
131 _openttd_revision_modified,
132 _openttd_newgrf_version,
133 #ifdef _SQ64
135 #else
137 #endif
138 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
139 "little",
140 #else
141 "big",
142 #endif
143 #ifdef DEDICATED
144 "yes",
145 #else
146 "no",
147 #endif
148 _openttd_build_date
153 * Writes the (important) configuration settings to the buffer.
154 * E.g. graphics set, sound set, blitter and AIs.
155 * @param buffer The begin where to write at.
156 * @param last The last position in the buffer to write to.
157 * @return the position of the \c '\0' character after the buffer.
159 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
161 buffer += seprintf(buffer, last,
162 "Configuration:\n"
163 " Blitter: %s\n"
164 " Graphics set: %s (%u)\n"
165 " Language: %s\n"
166 " Music driver: %s\n"
167 " Music set: %s (%u)\n"
168 " Network: %s\n"
169 " Sound driver: %s\n"
170 " Sound set: %s (%u)\n"
171 " Video driver: %s\n\n",
172 BlitterFactory::GetCurrentBlitter() == nullptr ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
173 BaseGraphics::GetUsedSet() == nullptr ? "none" : BaseGraphics::GetUsedSet()->name,
174 BaseGraphics::GetUsedSet() == nullptr ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
175 _current_language == nullptr ? "none" : _current_language->file,
176 MusicDriver::GetInstance() == nullptr ? "none" : MusicDriver::GetInstance()->GetName(),
177 BaseMusic::GetUsedSet() == nullptr ? "none" : BaseMusic::GetUsedSet()->name,
178 BaseMusic::GetUsedSet() == nullptr ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
179 _networking ? (_network_server ? "server" : "client") : "no",
180 SoundDriver::GetInstance() == nullptr ? "none" : SoundDriver::GetInstance()->GetName(),
181 BaseSounds::GetUsedSet() == nullptr ? "none" : BaseSounds::GetUsedSet()->name,
182 BaseSounds::GetUsedSet() == nullptr ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
183 VideoDriver::GetInstance() == nullptr ? "none" : VideoDriver::GetInstance()->GetName()
186 buffer += seprintf(buffer, last,
187 "Fonts:\n"
188 " Small: %s\n"
189 " Medium: %s\n"
190 " Large: %s\n"
191 " Mono: %s\n\n",
192 FontCache::Get(FS_SMALL)->GetFontName(),
193 FontCache::Get(FS_NORMAL)->GetFontName(),
194 FontCache::Get(FS_LARGE)->GetFontName(),
195 FontCache::Get(FS_MONO)->GetFontName()
198 buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
199 const Company *c;
200 FOR_ALL_COMPANIES(c) {
201 if (c->ai_info == nullptr) {
202 buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
203 } else {
204 buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
208 if (Game::GetInfo() != nullptr) {
209 buffer += seprintf(buffer, last, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
211 buffer += seprintf(buffer, last, "\n");
213 return buffer;
217 * Writes information (versions) of the used libraries.
218 * @param buffer The begin where to write at.
219 * @param last The last position in the buffer to write to.
220 * @return the position of the \c '\0' character after the buffer.
222 char *CrashLog::LogLibraries(char *buffer, const char *last) const
224 buffer += seprintf(buffer, last, "Libraries:\n");
226 #ifdef WITH_ALLEGRO
227 buffer += seprintf(buffer, last, " Allegro: %s\n", allegro_id);
228 #endif /* WITH_ALLEGRO */
230 #ifdef WITH_FONTCONFIG
231 int version = FcGetVersion();
232 buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
233 #endif /* WITH_FONTCONFIG */
235 #ifdef WITH_FREETYPE
236 FT_Library library;
237 int major, minor, patch;
238 FT_Init_FreeType(&library);
239 FT_Library_Version(library, &major, &minor, &patch);
240 FT_Done_FreeType(library);
241 buffer += seprintf(buffer, last, " FreeType: %d.%d.%d\n", major, minor, patch);
242 #endif /* WITH_FREETYPE */
244 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
245 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
246 char buf[4 * 3 + 3 + 1];
247 UVersionInfo ver;
248 u_getVersion(ver);
249 u_versionToString(ver, buf);
250 #ifdef WITH_ICU_SORT
251 buffer += seprintf(buffer, last, " ICU i18n: %s\n", buf);
252 #endif
253 #ifdef WITH_ICU_LAYOUT
254 buffer += seprintf(buffer, last, " ICU lx: %s\n", buf);
255 #endif
256 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
258 #ifdef WITH_LZMA
259 buffer += seprintf(buffer, last, " LZMA: %s\n", lzma_version_string());
260 #endif
262 #ifdef WITH_LZO
263 buffer += seprintf(buffer, last, " LZO: %s\n", lzo_version_string());
264 #endif
266 #ifdef WITH_PNG
267 buffer += seprintf(buffer, last, " PNG: %s\n", png_get_libpng_ver(nullptr));
268 #endif /* WITH_PNG */
270 #ifdef WITH_SDL
271 #ifdef DYNAMICALLY_LOADED_SDL
272 if (SDL_CALL SDL_Linked_Version != nullptr) {
273 #else
275 #endif
276 const SDL_version *v = SDL_CALL SDL_Linked_Version();
277 buffer += seprintf(buffer, last, " SDL: %d.%d.%d\n", v->major, v->minor, v->patch);
279 #endif /* WITH_SDL */
281 #ifdef WITH_ZLIB
282 buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
283 #endif
285 buffer += seprintf(buffer, last, "\n");
286 return buffer;
290 * Helper function for printing the gamelog.
291 * @param s the string to print.
293 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
295 CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
299 * Writes the gamelog data to the buffer.
300 * @param buffer The begin where to write at.
301 * @param last The last position in the buffer to write to.
302 * @return the position of the \c '\0' character after the buffer.
304 char *CrashLog::LogGamelog(char *buffer, const char *last) const
306 CrashLog::gamelog_buffer = buffer;
307 CrashLog::gamelog_last = last;
308 GamelogPrint(&CrashLog::GamelogFillCrashLog);
309 return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
313 * Writes the command log data to the buffer.
314 * @param buffer The begin where to write at.
315 * @param last The last position in the buffer to write to.
316 * @return the position of the \c '\0' character after the buffer.
318 char *CrashLog::LogCommandLog(char *buffer, const char *last) const
320 buffer = DumpCommandLog(buffer, last);
321 buffer += seprintf(buffer, last, "\n");
322 return buffer;
326 * Fill the crash log buffer with all data of a crash log.
327 * @param buffer The begin where to write at.
328 * @param last The last position in the buffer to write to.
329 * @return the position of the \c '\0' character after the buffer.
331 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
333 time_t cur_time = time(nullptr);
334 buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
336 if (GamelogTestEmergency()) {
337 buffer += seprintf(buffer, last, "-=-=- As you loaded an emergency savegame no crash information would ordinarily be generated. -=-=-\n\n");
339 if (SaveloadCrashWithMissingNewGRFs()) {
340 buffer += seprintf(buffer, last, "-=-=- As you loaded a savegame for which you do not have the required NewGRFs no crash information would ordinarily be generated. -=-=-\n\n");
343 buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
345 buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", _cur_date_ymd.year, _cur_date_ymd.month + 1, _cur_date_ymd.day, _date_fract);
347 buffer = this->LogError(buffer, last, CrashLog::message);
348 buffer = this->LogOpenTTDVersion(buffer, last);
349 buffer = this->LogRegisters(buffer, last);
350 buffer = this->LogStacktrace(buffer, last);
351 buffer = this->LogOSVersion(buffer, last);
352 buffer = this->LogCompiler(buffer, last);
353 buffer = this->LogConfiguration(buffer, last);
354 buffer = this->LogLibraries(buffer, last);
355 buffer = this->LogModules(buffer, last);
356 buffer = this->LogGamelog(buffer, last);
357 buffer = this->LogCommandLog(buffer, last);
359 buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
360 return buffer;
364 * Write the crash log to a file.
365 * @note On success the filename will be filled with the full path of the
366 * crash log file. Make sure filename is at least \c MAX_PATH big.
367 * @param buffer The begin of the buffer to write to the disk.
368 * @param filename Output for the filename of the written file.
369 * @param filename_last The last position in the filename buffer.
370 * @return true when the crash log was successfully written.
372 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
374 seprintf(filename, filename_last, "%scrash.log", _personal_dir);
376 FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
377 if (file == nullptr) return false;
379 size_t len = strlen(buffer);
380 size_t written = fwrite(buffer, 1, len, file);
382 FioFCloseFile(file);
383 return len == written;
386 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
388 /* Stub implementation; not all OSes support this. */
389 return 0;
393 * Write the (crash) savegame to a file.
394 * @note On success the filename will be filled with the full path of the
395 * crash save file. Make sure filename is at least \c MAX_PATH big.
396 * @param filename Output for the filename of the written file.
397 * @param filename_last The last position in the filename buffer.
398 * @return true when the crash save was successfully made.
400 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
402 /* If the map array doesn't exist, saving will fail too. If the map got
403 * initialised, there is a big chance the rest is initialised too. */
404 if (_m == nullptr) return false;
406 try {
407 GamelogEmergency();
409 seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
411 /* Don't do a threaded saveload. */
412 return SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK;
413 } catch (...) {
414 return false;
419 * Write the (crash) screenshot to a file.
420 * @note On success the filename will be filled with the full path of the
421 * screenshot. Make sure filename is at least \c MAX_PATH big.
422 * @param filename Output for the filename of the written file.
423 * @param filename_last The last position in the filename buffer.
424 * @return true when the crash screenshot was successfully made.
426 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
428 /* Don't draw when we have invalid screen size */
429 if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == nullptr) return false;
431 bool res = MakeScreenshot(SC_CRASHLOG, "crash");
432 if (res) strecpy(filename, _full_screenshot_name, filename_last);
433 return res;
437 * Makes the crash log, writes it to a file and then subsequently tries
438 * to make a crash dump and crash savegame. It uses DEBUG to write
439 * information like paths to the console.
440 * @return true when everything is made successfully.
442 bool CrashLog::MakeCrashLog() const
444 /* Don't keep looping logging crashes. */
445 static bool crashlogged = false;
446 if (crashlogged) return false;
447 crashlogged = true;
449 char filename[MAX_PATH];
450 char buffer[65536 * 4];
451 bool ret = true;
453 printf("Crash encountered, generating crash log...\n");
454 this->FillCrashLog(buffer, lastof(buffer));
455 printf("%s\n", buffer);
456 printf("Crash log generated.\n\n");
458 printf("Writing crash log to disk...\n");
459 bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
460 if (bret) {
461 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
462 } else {
463 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
464 ret = false;
467 /* Don't mention writing crash dumps because not all platforms support it. */
468 int dret = this->WriteCrashDump(filename, lastof(filename));
469 if (dret < 0) {
470 printf("Writing crash dump failed.\n\n");
471 ret = false;
472 } else if (dret > 0) {
473 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
476 printf("Writing crash savegame...\n");
477 bret = this->WriteSavegame(filename, lastof(filename));
478 if (bret) {
479 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
480 } else {
481 ret = false;
482 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
485 printf("Writing crash screenshot...\n");
486 bret = this->WriteScreenshot(filename, lastof(filename));
487 if (bret) {
488 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
489 } else {
490 ret = false;
491 printf("Writing crash screenshot failed.\n\n");
494 return ret;
498 * Sets a message for the error message handler.
499 * @param message The error message of the error.
501 /* static */ void CrashLog::SetErrorMessage(const char *message)
503 CrashLog::message = message;
507 * Try to close the sound/video stuff so it doesn't keep lingering around
508 * incorrect video states or so, e.g. keeping dpmi disabled.
510 /* static */ void CrashLog::AfterCrashLogCleanup()
512 if (MusicDriver::GetInstance() != nullptr) MusicDriver::GetInstance()->Stop();
513 if (SoundDriver::GetInstance() != nullptr) SoundDriver::GetInstance()->Stop();
514 if (VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop();
517 /* static */ const char *CrashLog::GetAbortCrashlogReason()
519 if (_settings_client.gui.developer > 0) return nullptr;
521 if (GamelogTestEmergency()) {
522 return "As you loaded an emergency savegame no crash information will be generated.\n";
525 if (SaveloadCrashWithMissingNewGRFs()) {
526 return "As you loaded an savegame for which you do not have the required NewGRFs\n" \
527 "no crash information will be generated.\n";
530 return nullptr;