OpenTTD: src/crashlog.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

crashlog.cpp
Go to the documentation of this file.
1 /* $Id: crashlog.cpp 27367 2015-08-09 12:33:27Z rubidium $ */
2
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 */
9
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"
30
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"
36
37 #include <time.h>
38
39 #include "safeguards.h"
40
41 /* static */ const char *CrashLog::message = NULL;
42 /* static */ char *CrashLog::gamelog_buffer = NULL;
43 /* static */ const char *CrashLog::gamelog_last = NULL;
44
45 char *CrashLog::LogCompiler(char *buffer, const char *last) const
46 {
47 buffer += seprintf(buffer, last, " Compiler: "
48 #if defined(_MSC_VER)
49 "MSVC %d", _MSC_VER
50 #elif defined(__ICC) && defined(__GNUC__)
51 "ICC %d (GCC %d.%d.%d mode)", __ICC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
52 #elif defined(__ICC)
53 "ICC %d", __ICC
54 #elif defined(__GNUC__)
55 "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
56 #elif defined(__WATCOMC__)
57 "WatcomC %d", __WATCOMC__
58 #else
59 "<unknown>"
60 #endif
61 );
62 #if defined(__VERSION__)
63 return buffer + seprintf(buffer, last, " \"" __VERSION__ "\"\n\n");
64 #else
65 return buffer + seprintf(buffer, last, "\n\n");
66 #endif
67 }
68
69 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
70 {
71 /* Stub implementation; not all OSes support this. */
72 return buffer;
73 }
74
75 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
76 {
77 /* Stub implementation; not all OSes support this. */
78 return buffer;
79 }
80
87 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
88 {
89 return buffer + seprintf(buffer, last,
90 "OpenTTD version:\n"
91 " Version: %s (%d)\n"
92 " NewGRF ver: %08x\n"
93 " Bits: %d\n"
94 " Endian: %s\n"
95 " Dedicated: %s\n"
96 " Build date: %s\n\n",
97 _openttd_revision,
98 _openttd_revision_modified,
99 _openttd_newgrf_version,
100 #ifdef _SQ64
101 64,
102 #else
103 32,
104 #endif
105 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
106 "little",
107 #else
108 "big",
109 #endif
110 #ifdef DEDICATED
111 "yes",
112 #else
113 "no",
114 #endif
115 _openttd_build_date
116 );
117 }
118
126 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
127 {
128 buffer += seprintf(buffer, last,
129 "Configuration:\n"
130 " Blitter: %s\n"
131 " Graphics set: %s (%u)\n"
132 " Language: %s\n"
133 " Music driver: %s\n"
134 " Music set: %s (%u)\n"
135 " Network: %s\n"
136 " Sound driver: %s\n"
137 " Sound set: %s (%u)\n"
138 " Video driver: %s\n\n",
139 BlitterFactory::GetCurrentBlitter() == NULL ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(),
140 BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
141 BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
142 _current_language == NULL ? "none" : _current_language->file,
143 MusicDriver::GetInstance() == NULL ? "none" : MusicDriver::GetInstance()->GetName(),
144 BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
145 BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
146 _networking ? (_network_server ? "server" : "client") : "no",
147 SoundDriver::GetInstance() == NULL ? "none" : SoundDriver::GetInstance()->GetName(),
148 BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
149 BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
150 VideoDriver::GetInstance() == NULL ? "none" : VideoDriver::GetInstance()->GetName()
151 );
152
153 buffer += seprintf(buffer, last,
154 "Fonts:\n"
155 " Small: %s\n"
156 " Medium: %s\n"
157 " Large: %s\n"
158 " Mono: %s\n\n",
159 FontCache::Get(FS_SMALL)->GetFontName(),
160 FontCache::Get(FS_NORMAL)->GetFontName(),
161 FontCache::Get(FS_LARGE)->GetFontName(),
162 FontCache::Get(FS_MONO)->GetFontName()
163 );
164
165 buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
166 const Company *c;
167 FOR_ALL_COMPANIES(c) {
168 if (c->ai_info == NULL) {
169 buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
170 } else {
171 buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
172 }
173 }
174
175 if (Game::GetInfo() != NULL) {
176 buffer += seprintf(buffer, last, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
177 }
178 buffer += seprintf(buffer, last, "\n");
179
180 return buffer;
181 }
182
183 /* Include these here so it's close to where it's actually used. */
184 #ifdef WITH_ALLEGRO
185 # include <allegro.h>
186 #endif /* WITH_ALLEGRO */
187 #ifdef WITH_FONTCONFIG
188 # include <fontconfig/fontconfig.h>
189 #endif /* WITH_FONTCONFIG */
190 #ifdef WITH_PNG
191 /* pngconf.h, included by png.h doesn't like something in the
192 * freetype headers. As such it's not alphabetically sorted. */
193 # include <png.h>
194 #endif /* WITH_PNG */
195 #ifdef WITH_FREETYPE
196 # include <ft2build.h>
197 # include FT_FREETYPE_H
198 #endif /* WITH_FREETYPE */
199 #if defined(WITH_ICU_LAYOUT) || defined(WITH_ICU_SORT)
200 # include <unicode/uversion.h>
201 #endif /* WITH_ICU_SORT || WITH_ICU_LAYOUT */
202 #ifdef WITH_LZMA
203 # include <lzma.h>
204 #endif
205 #ifdef WITH_LZO
206 #include <lzo/lzo1x.h>
207 #endif
208 #ifdef WITH_SDL
209 # include "sdl.h"
210 # include <SDL.h>
211 #endif /* WITH_SDL */
212 #ifdef WITH_ZLIB
213 # include <zlib.h>
214 #endif
215
222 char *CrashLog::LogLibraries(char *buffer, const char *last) const
223 {
224 buffer += seprintf(buffer, last, "Libraries:\n");
225
226 #ifdef WITH_ALLEGRO
227 buffer += seprintf(buffer, last, " Allegro: %s\n", allegro_id);
228 #endif /* WITH_ALLEGRO */
229
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 */
234
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 */
243
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 */
257
258 #ifdef WITH_LZMA
259 buffer += seprintf(buffer, last, " LZMA: %s\n", lzma_version_string());
260 #endif
261
262 #ifdef WITH_LZO
263 buffer += seprintf(buffer, last, " LZO: %s\n", lzo_version_string());
264 #endif
265
266 #ifdef WITH_PNG
267 buffer += seprintf(buffer, last, " PNG: %s\n", png_get_libpng_ver(NULL));
268 #endif /* WITH_PNG */
269
270 #ifdef WITH_SDL
271 #ifdef DYNAMICALLY_LOADED_SDL
272 if (SDL_CALL SDL_Linked_Version != NULL) {
273 #else
274 {
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);
278 }
279 #endif /* WITH_SDL */
280
281 #ifdef WITH_ZLIB
282 buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
283 #endif
284
285 buffer += seprintf(buffer, last, "\n");
286 return buffer;
287 }
288
293 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
294 {
295 CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
296 }
297
304 char *CrashLog::LogGamelog(char *buffer, const char *last) const
305 {
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");
310 }
311
318 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
319 {
320 time_t cur_time = time(NULL);
321 buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
322 buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
323
324 YearMonthDay ymd;
325 ConvertDateToYMD(_date, &ymd);
326 buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
327
328 buffer = this->LogError(buffer, last, CrashLog::message);
329 buffer = this->LogOpenTTDVersion(buffer, last);
330 buffer = this->LogRegisters(buffer, last);
331 buffer = this->LogStacktrace(buffer, last);
332 buffer = this->LogOSVersion(buffer, last);
333 buffer = this->LogCompiler(buffer, last);
334 buffer = this->LogConfiguration(buffer, last);
335 buffer = this->LogLibraries(buffer, last);
336 buffer = this->LogModules(buffer, last);
337 buffer = this->LogGamelog(buffer, last);
338
339 buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
340 return buffer;
341 }
342
352 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
353 {
354 seprintf(filename, filename_last, "%scrash.log", _personal_dir);
355
356 FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
357 if (file == NULL) return false;
358
359 size_t len = strlen(buffer);
360 size_t written = fwrite(buffer, 1, len, file);
361
362 FioFCloseFile(file);
363 return len == written;
364 }
365
366 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
367 {
368 /* Stub implementation; not all OSes support this. */
369 return 0;
370 }
371
380 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
381 {
382 /* If the map array doesn't exist, saving will fail too. If the map got
383 * initialised, there is a big chance the rest is initialised too. */
384 if (_m == NULL) return false;
385
386 try {
387 GamelogEmergency();
388
389 seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
390
391 /* Don't do a threaded saveload. */
392 return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK;
393 } catch (...) {
394 return false;
395 }
396 }
397
406 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
407 {
408 /* Don't draw when we have invalid screen size */
409 if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
410
411 bool res = MakeScreenshot(SC_CRASHLOG, "crash");
412 if (res) strecpy(filename, _full_screenshot_name, filename_last);
413 return res;
414 }
415
422 bool CrashLog::MakeCrashLog() const
423 {
424 /* Don't keep looping logging crashes. */
425 static bool crashlogged = false;
426 if (crashlogged) return false;
427 crashlogged = true;
428
429 char filename[MAX_PATH];
430 char buffer[65536];
431 bool ret = true;
432
433 printf("Crash encountered, generating crash log...\n");
434 this->FillCrashLog(buffer, lastof(buffer));
435 printf("%s\n", buffer);
436 printf("Crash log generated.\n\n");
437
438 printf("Writing crash log to disk...\n");
439 bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
440 if (bret) {
441 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
442 } else {
443 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
444 ret = false;
445 }
446
447 /* Don't mention writing crash dumps because not all platforms support it. */
448 int dret = this->WriteCrashDump(filename, lastof(filename));
449 if (dret < 0) {
450 printf("Writing crash dump failed.\n\n");
451 ret = false;
452 } else if (dret > 0) {
453 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
454 }
455
456 printf("Writing crash savegame...\n");
457 bret = this->WriteSavegame(filename, lastof(filename));
458 if (bret) {
459 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
460 } else {
461 ret = false;
462 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
463 }
464
465 printf("Writing crash screenshot...\n");
466 bret = this->WriteScreenshot(filename, lastof(filename));
467 if (bret) {
468 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
469 } else {
470 ret = false;
471 printf("Writing crash screenshot failed.\n\n");
472 }
473
474 return ret;
475 }
476
481 /* static */ void CrashLog::SetErrorMessage(const char *message)
482 {
483 CrashLog::message = message;
484 }
485
490 /* static */ void CrashLog::AfterCrashLogCleanup()
491 {
492 if (MusicDriver::GetInstance() != NULL) MusicDriver::GetInstance()->Stop();
493 if (SoundDriver::GetInstance() != NULL) SoundDriver::GetInstance()->Stop();
494 if (VideoDriver::GetInstance() != NULL) VideoDriver::GetInstance()->Stop();
495 }