Doc: Clarify comment that SND_05_TRAIN_THROUGH_TUNNEL is only for steam engines ...
[openttd-github.git] / src / crashlog.cpp
blobc447019fef9144af827b0586297e653ef102047b
1 /*
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/>.
6 */
8 /** @file crashlog.cpp Implementation of generic function to be called to log a crash */
10 #include "stdafx.h"
11 #include "crashlog.h"
12 #include "gamelog.h"
13 #include "date_func.h"
14 #include "map_func.h"
15 #include "rev.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"
24 #include "gfx_func.h"
25 #include "network/network.h"
26 #include "language.h"
27 #include "fontcache.h"
28 #include "news_gui.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"
36 #include <time.h>
38 #ifdef WITH_ALLEGRO
39 # include <allegro.h>
40 #endif /* WITH_ALLEGRO */
41 #ifdef WITH_FONTCONFIG
42 # include <fontconfig/fontconfig.h>
43 #endif /* WITH_FONTCONFIG */
44 #ifdef WITH_PNG
45 /* pngconf.h, included by png.h doesn't like something in the
46 * freetype headers. As such it's not alphabetically sorted. */
47 # include <png.h>
48 #endif /* WITH_PNG */
49 #ifdef WITH_FREETYPE
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 */
56 #ifdef WITH_LIBLZMA
57 # include <lzma.h>
58 #endif
59 #ifdef WITH_LZO
60 #include <lzo/lzo1x.h>
61 #endif
62 #if defined(WITH_SDL) || defined(WITH_SDL2)
63 # include <SDL.h>
64 #endif /* WITH_SDL || WITH_SDL2 */
65 #ifdef WITH_ZLIB
66 # include <zlib.h>
67 #endif
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: "
78 #if defined(_MSC_VER)
79 "MSVC %d", _MSC_VER
80 #elif defined(__ICC) && defined(__GNUC__)
81 "ICC %d (GCC %d.%d.%d mode)", __ICC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
82 #elif defined(__ICC)
83 "ICC %d", __ICC
84 #elif defined(__GNUC__)
85 "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
86 #elif defined(__WATCOMC__)
87 "WatcomC %d", __WATCOMC__
88 #else
89 "<unknown>"
90 #endif
92 #if defined(__VERSION__)
93 return buffer + seprintf(buffer, last, " \"" __VERSION__ "\"\n\n");
94 #else
95 return buffer + seprintf(buffer, last, "\n\n");
96 #endif
99 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
101 /* Stub implementation; not all OSes support this. */
102 return buffer;
105 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
107 /* Stub implementation; not all OSes support this. */
108 return buffer;
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,
120 "OpenTTD version:\n"
121 " Version: %s (%d)\n"
122 " NewGRF ver: %08x\n"
123 " Bits: %d\n"
124 " Endian: %s\n"
125 " Dedicated: %s\n"
126 " Build date: %s\n\n",
127 _openttd_revision,
128 _openttd_revision_modified,
129 _openttd_newgrf_version,
130 #ifdef _SQ64
132 #else
134 #endif
135 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
136 "little",
137 #else
138 "big",
139 #endif
140 #ifdef DEDICATED
141 "yes",
142 #else
143 "no",
144 #endif
145 _openttd_build_date
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,
159 "Configuration:\n"
160 " Blitter: %s\n"
161 " Graphics set: %s (%u)\n"
162 " Language: %s\n"
163 " Music driver: %s\n"
164 " Music set: %s (%u)\n"
165 " Network: %s\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,
184 "Fonts:\n"
185 " Small: %s\n"
186 " Medium: %s\n"
187 " Large: %s\n"
188 " Mono: %s\n\n",
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);
199 } else {
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");
209 return buffer;
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");
222 #ifdef WITH_ALLEGRO
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 */
231 #ifdef WITH_FREETYPE
232 FT_Library library;
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];
243 UVersionInfo ver;
244 u_getVersion(ver);
245 u_versionToString(ver, buf);
246 #ifdef WITH_ICU_I18N
247 buffer += seprintf(buffer, last, " ICU i18n: %s\n", buf);
248 #endif
249 #ifdef WITH_ICU_LX
250 buffer += seprintf(buffer, last, " ICU lx: %s\n", buf);
251 #endif
252 #endif /* WITH_ICU_LX || WITH_ICU_I18N */
254 #ifdef WITH_LIBLZMA
255 buffer += seprintf(buffer, last, " LZMA: %s\n", lzma_version_string());
256 #endif
258 #ifdef WITH_LZO
259 buffer += seprintf(buffer, last, " LZO: %s\n", lzo_version_string());
260 #endif
262 #ifdef WITH_PNG
263 buffer += seprintf(buffer, last, " PNG: %s\n", png_get_libpng_ver(nullptr));
264 #endif /* WITH_PNG */
266 #ifdef WITH_SDL
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)
270 SDL_version sdl2_v;
271 SDL_GetVersion(&sdl2_v);
272 buffer += seprintf(buffer, last, " SDL2: %d.%d.%d\n", sdl2_v.major, sdl2_v.minor, sdl2_v.patch);
273 #endif
275 #ifdef WITH_ZLIB
276 buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
277 #endif
279 buffer += seprintf(buffer, last, "\n");
280 return buffer;
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");
316 int i = 0;
317 for (NewsItem *news = _latest_news; i < 32 && news != nullptr; news = news->prev, i++) {
318 YearMonthDay ymd;
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");
325 return buffer;
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)));
340 YearMonthDay ymd;
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");
357 return buffer;
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);
379 FioFCloseFile(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. */
386 return 0;
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;
403 try {
404 GamelogEmergency();
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;
410 } catch (...) {
411 return false;
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);
430 return res;
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;
444 crashlogged = true;
446 char filename[MAX_PATH];
447 char buffer[65536];
448 bool ret = true;
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));
457 if (bret) {
458 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
459 } else {
460 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
461 ret = false;
464 /* Don't mention writing crash dumps because not all platforms support it. */
465 int dret = this->WriteCrashDump(filename, lastof(filename));
466 if (dret < 0) {
467 printf("Writing crash dump failed.\n\n");
468 ret = false;
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));
475 if (bret) {
476 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
477 } else {
478 ret = false;
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));
484 if (bret) {
485 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
486 } else {
487 ret = false;
488 printf("Writing crash screenshot failed.\n\n");
491 return ret;
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();