(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / crashlog.cpp
blobbda4d674f2e6a0b53a33c05f9ac09eaeb2c37b27
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"
30 #include "ai/ai_info.hpp"
31 #include "company_base.h"
32 #include "company_func.h"
34 #include <time.h>
36 /* static */ const char *CrashLog::message = NULL;
37 /* static */ char *CrashLog::gamelog_buffer = NULL;
38 /* static */ const char *CrashLog::gamelog_last = NULL;
40 char *CrashLog::LogCompiler(char *buffer, const char *last) const
42 buffer += seprintf(buffer, last, " Compiler: "
43 #if defined(_MSC_VER)
44 "MSVC %d", _MSC_VER
45 #elif defined(__ICC) && defined(__GNUC__)
46 "ICC %d (GCC %d.%d.%d mode)", __ICC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
47 #elif defined(__ICC)
48 "ICC %d", __ICC
49 #elif defined(__GNUC__)
50 "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
51 #elif defined(__WATCOMC__)
52 "WatcomC %d", __WATCOMC__
53 #else
54 "<unknown>"
55 #endif
57 #if defined(__VERSION__)
58 return buffer + seprintf(buffer, last, " \"" __VERSION__ "\"\n\n");
59 #else
60 return buffer + seprintf(buffer, last, "\n\n");
61 #endif
64 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
66 /* Stub implementation; not all OSes support this. */
67 return buffer;
70 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
72 /* Stub implementation; not all OSes support this. */
73 return buffer;
76 /**
77 * Writes OpenTTD's version to the buffer.
78 * @param buffer The begin where to write at.
79 * @param last The last position in the buffer to write to.
80 * @return the position of the \c '\0' character after the buffer.
82 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
84 return buffer + seprintf(buffer, last,
85 "OpenTTD version:\n"
86 " Version: %s (%d)\n"
87 " NewGRF ver: %08x\n"
88 " Bits: %d\n"
89 " Endian: %s\n"
90 " Dedicated: %s\n"
91 " Build date: %s\n\n",
92 _openttd_revision,
93 _openttd_revision_modified,
94 _openttd_newgrf_version,
95 #ifdef _SQ64
96 64,
97 #else
98 32,
99 #endif
100 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
101 "little",
102 #else
103 "big",
104 #endif
105 #ifdef DEDICATED
106 "yes",
107 #else
108 "no",
109 #endif
110 _openttd_build_date
115 * Writes the (important) configuration settings to the buffer.
116 * E.g. graphics set, sound set, blitter and AIs.
117 * @param buffer The begin where to write at.
118 * @param last The last position in the buffer to write to.
119 * @return the position of the \c '\0' character after the buffer.
121 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
123 buffer += seprintf(buffer, last,
124 "Configuration:\n"
125 " Blitter: %s\n"
126 " Graphics set: %s (%u)\n"
127 " Language: %s\n"
128 " Music driver: %s\n"
129 " Music set: %s (%u)\n"
130 " Network: %s\n"
131 " Sound driver: %s\n"
132 " Sound set: %s (%u)\n"
133 " Video driver: %s\n\n",
134 BlitterFactoryBase::GetCurrentBlitter() == NULL ? "none" : BlitterFactoryBase::GetCurrentBlitter()->GetName(),
135 BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
136 BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
137 _current_language == NULL ? "none" : _current_language->file,
138 _music_driver == NULL ? "none" : _music_driver->GetName(),
139 BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
140 BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
141 _networking ? (_network_server ? "server" : "client") : "no",
142 _sound_driver == NULL ? "none" : _sound_driver->GetName(),
143 BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
144 BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
145 _video_driver == NULL ? "none" : _video_driver->GetName()
148 buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
149 const Company *c;
150 FOR_ALL_COMPANIES(c) {
151 if (c->ai_info == NULL) {
152 buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
153 } else {
154 #ifdef ENABLE_AI
155 buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
156 #endif /* ENABLE_AI */
159 buffer += seprintf(buffer, last, "\n");
161 return buffer;
164 /* Include these here so it's close to where it's actually used. */
165 #ifdef WITH_ALLEGRO
166 # include <allegro.h>
167 #endif /* WITH_ALLEGRO */
168 #ifdef WITH_FONTCONFIG
169 # include <fontconfig/fontconfig.h>
170 #endif /* WITH_FONTCONFIG */
171 #ifdef WITH_PNG
172 /* pngconf.h, included by png.h doesn't like something in the
173 * freetype headers. As such it's not alphabetically sorted. */
174 # include <png.h>
175 #endif /* WITH_PNG */
176 #ifdef WITH_FREETYPE
177 # include <ft2build.h>
178 # include FT_FREETYPE_H
179 #endif /* WITH_FREETYPE */
180 #ifdef WITH_ICU
181 # include <unicode/uversion.h>
182 #endif /* WITH_ICU */
183 #ifdef WITH_LZMA
184 # include <lzma.h>
185 #endif
186 #ifdef WITH_LZO
187 #include <lzo/lzo1x.h>
188 #endif
189 #ifdef WITH_SDL
190 # include "sdl.h"
191 # include <SDL.h>
192 #endif /* WITH_SDL */
193 #ifdef WITH_ZLIB
194 # include <zlib.h>
195 #endif
198 * Writes information (versions) of the used libraries.
199 * @param buffer The begin where to write at.
200 * @param last The last position in the buffer to write to.
201 * @return the position of the \c '\0' character after the buffer.
203 char *CrashLog::LogLibraries(char *buffer, const char *last) const
205 buffer += seprintf(buffer, last, "Libraries:\n");
207 #ifdef WITH_ALLEGRO
208 buffer += seprintf(buffer, last, " Allegro: %s\n", allegro_id);
209 #endif /* WITH_ALLEGRO */
211 #ifdef WITH_FONTCONFIG
212 int version = FcGetVersion();
213 buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
214 #endif /* WITH_FONTCONFIG */
216 #ifdef WITH_FREETYPE
217 FT_Library library;
218 int major, minor, patch;
219 FT_Init_FreeType(&library);
220 FT_Library_Version(library, &major, &minor, &patch);
221 FT_Done_FreeType(library);
222 buffer += seprintf(buffer, last, " FreeType: %d.%d.%d\n", major, minor, patch);
223 #endif /* WITH_FREETYPE */
225 #ifdef WITH_ICU
226 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
227 char buf[4 * 3 + 3 + 1];
228 UVersionInfo ver;
229 u_getVersion(ver);
230 u_versionToString(ver, buf);
231 buffer += seprintf(buffer, last, " ICU: %s\n", buf);
232 #endif /* WITH_ICU */
234 #ifdef WITH_LZMA
235 buffer += seprintf(buffer, last, " LZMA: %s\n", lzma_version_string());
236 #endif
238 #ifdef WITH_LZO
239 buffer += seprintf(buffer, last, " LZO: %s\n", lzo_version_string());
240 #endif
242 #ifdef WITH_PNG
243 buffer += seprintf(buffer, last, " PNG: %s\n", png_get_libpng_ver(NULL));
244 #endif /* WITH_PNG */
246 #ifdef WITH_SDL
247 #ifdef DYNAMICALLY_LOADED_SDL
248 if (SDL_CALL SDL_Linked_Version != NULL) {
249 #else
251 #endif
252 const SDL_version *v = SDL_CALL SDL_Linked_Version();
253 buffer += seprintf(buffer, last, " SDL: %d.%d.%d\n", v->major, v->minor, v->patch);
255 #endif /* WITH_SDL */
257 #ifdef WITH_ZLIB
258 buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
259 #endif
261 buffer += seprintf(buffer, last, "\n");
262 return buffer;
266 * Helper function for printing the gamelog.
267 * @param s the string to print.
269 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
271 CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
275 * Writes the gamelog data to the buffer.
276 * @param buffer The begin where to write at.
277 * @param last The last position in the buffer to write to.
278 * @return the position of the \c '\0' character after the buffer.
280 char *CrashLog::LogGamelog(char *buffer, const char *last) const
282 CrashLog::gamelog_buffer = buffer;
283 CrashLog::gamelog_last = last;
284 GamelogPrint(&CrashLog::GamelogFillCrashLog);
285 return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
289 * Fill the crash log buffer with all data of a crash log.
290 * @param buffer The begin where to write at.
291 * @param last The last position in the buffer to write to.
292 * @return the position of the \c '\0' character after the buffer.
294 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
296 time_t cur_time = time(NULL);
297 buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
298 buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
300 YearMonthDay ymd;
301 ConvertDateToYMD(_date, &ymd);
302 buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
304 buffer = this->LogError(buffer, last, CrashLog::message);
305 buffer = this->LogOpenTTDVersion(buffer, last);
306 buffer = this->LogRegisters(buffer, last);
307 buffer = this->LogStacktrace(buffer, last);
308 buffer = this->LogOSVersion(buffer, last);
309 buffer = this->LogCompiler(buffer, last);
310 buffer = this->LogConfiguration(buffer, last);
311 buffer = this->LogLibraries(buffer, last);
312 buffer = this->LogModules(buffer, last);
313 buffer = this->LogGamelog(buffer, last);
315 buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
316 return buffer;
320 * Write the crash log to a file.
321 * @note On success the filename will be filled with the full path of the
322 * crash log file. Make sure filename is at least \c MAX_PATH big.
323 * @param buffer The begin of the buffer to write to the disk.
324 * @param filename Output for the filename of the written file.
325 * @param filename_last The last position in the filename buffer.
326 * @return true when the crash log was successfully written.
328 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
330 seprintf(filename, filename_last, "%scrash.log", _personal_dir);
332 FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
333 if (file == NULL) return false;
335 size_t len = strlen(buffer);
336 size_t written = fwrite(buffer, 1, len, file);
338 FioFCloseFile(file);
339 return len == written;
342 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
344 /* Stub implementation; not all OSes support this. */
345 return 0;
349 * Write the (crash) savegame to a file.
350 * @note On success the filename will be filled with the full path of the
351 * crash save file. Make sure filename is at least \c MAX_PATH big.
352 * @param filename Output for the filename of the written file.
353 * @param filename_last The last position in the filename buffer.
354 * @return true when the crash save was successfully made.
356 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
358 /* If the map array doesn't exist, saving will fail too. If the map got
359 * initialised, there is a big chance the rest is initialised too. */
360 if (_m == NULL) return false;
362 try {
363 GamelogEmergency();
365 seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
367 /* Don't do a threaded saveload. */
368 return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK;
369 } catch (...) {
370 return false;
375 * Write the (crash) screenshot to a file.
376 * @note On success the filename will be filled with the full path of the
377 * screenshot. Make sure filename is at least \c MAX_PATH big.
378 * @param filename Output for the filename of the written file.
379 * @param filename_last The last position in the filename buffer.
380 * @return true when the crash screenshot was successfully made.
382 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
384 /* Don't draw when we have invalid screen size */
385 if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
387 bool res = MakeScreenshot(SC_RAW, "crash");
388 if (res) strecpy(filename, _full_screenshot_name, filename_last);
389 return res;
393 * Makes the crash log, writes it to a file and then subsequently tries
394 * to make a crash dump and crash savegame. It uses DEBUG to write
395 * information like paths to the console.
396 * @return true when everything is made successfully.
398 bool CrashLog::MakeCrashLog() const
400 /* Don't keep looping logging crashes. */
401 static bool crashlogged = false;
402 if (crashlogged) return false;
403 crashlogged = true;
405 char filename[MAX_PATH];
406 char buffer[65536];
407 bool ret = true;
409 printf("Crash encountered, generating crash log...\n");
410 this->FillCrashLog(buffer, lastof(buffer));
411 printf("%s\n", buffer);
412 printf("Crash log generated.\n\n");
414 printf("Writing crash log to disk...\n");
415 bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
416 if (bret) {
417 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
418 } else {
419 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
420 ret = false;
423 /* Don't mention writing crash dumps because not all platforms support it. */
424 int dret = this->WriteCrashDump(filename, lastof(filename));
425 if (dret < 0) {
426 printf("Writing crash dump failed.\n\n");
427 ret = false;
428 } else if (dret > 0) {
429 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
432 printf("Writing crash savegame...\n");
433 bret = this->WriteSavegame(filename, lastof(filename));
434 if (bret) {
435 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
436 } else {
437 ret = false;
438 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
441 printf("Writing crash screenshot...\n");
442 bret = this->WriteScreenshot(filename, lastof(filename));
443 if (bret) {
444 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
445 } else {
446 ret = false;
447 printf("Writing crash screenshot failed.\n\n");
450 return ret;
454 * Sets a message for the error message handler.
455 * @param message The error message of the error.
457 /* static */ void CrashLog::SetErrorMessage(const char *message)
459 CrashLog::message = message;
463 * Try to close the sound/video stuff so it doesn't keep lingering around
464 * incorrect video states or so, e.g. keeping dpmi disabled.
466 /* static */ void CrashLog::AfterCrashLogCleanup()
468 if (_music_driver != NULL) _music_driver->Stop();
469 if (_sound_driver != NULL) _sound_driver->Stop();
470 if (_video_driver != NULL) _video_driver->Stop();