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/>.
10 /** @file crashlog.cpp Implementation of generic function to be called to log a crash */
15 #include "date_func.h"
17 #include "strings_func.h"
18 #include "blitter/factory.hpp"
19 #include "base_media_base.h"
20 #include "music/music_driver.hpp"
21 #include "sound/sound_driver.hpp"
22 #include "video/video_driver.hpp"
23 #include "saveload/saveload.h"
24 #include "screenshot.h"
26 #include "network/network.h"
29 #include "ai/ai_info.hpp"
30 #include "game/game.hpp"
31 #include "game/game_info.hpp"
32 #include "company_base.h"
33 #include "company_func.h"
37 /* static */ const char *CrashLog::message
= NULL
;
38 /* static */ char *CrashLog::gamelog_buffer
= NULL
;
39 /* static */ const char *CrashLog::gamelog_last
= NULL
;
41 char *CrashLog::LogCompiler(char *buffer
, const char *last
) const
43 buffer
+= seprintf(buffer
, last
, " Compiler: "
46 #elif defined(__ICC) && defined(__GNUC__)
47 "ICC %d (GCC %d.%d.%d mode)", __ICC
, __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
50 #elif defined(__GNUC__)
51 "GCC %d.%d.%d", __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
52 #elif defined(__WATCOMC__)
53 "WatcomC %d", __WATCOMC__
58 #if defined(__VERSION__)
59 return buffer
+ seprintf(buffer
, last
, " \"" __VERSION__
"\"\n\n");
61 return buffer
+ seprintf(buffer
, last
, "\n\n");
65 /* virtual */ char *CrashLog::LogRegisters(char *buffer
, const char *last
) const
67 /* Stub implementation; not all OSes support this. */
71 /* virtual */ char *CrashLog::LogModules(char *buffer
, const char *last
) const
73 /* Stub implementation; not all OSes support this. */
78 * Writes OpenTTD's version to the buffer.
79 * @param buffer The begin where to write at.
80 * @param last The last position in the buffer to write to.
81 * @return the position of the \c '\0' character after the buffer.
83 char *CrashLog::LogOpenTTDVersion(char *buffer
, const char *last
) const
85 return buffer
+ seprintf(buffer
, last
,
92 " Build date: %s\n\n",
94 _openttd_revision_modified
,
95 _openttd_newgrf_version
,
101 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
116 * Writes the (important) configuration settings to the buffer.
117 * E.g. graphics set, sound set, blitter and AIs.
118 * @param buffer The begin where to write at.
119 * @param last The last position in the buffer to write to.
120 * @return the position of the \c '\0' character after the buffer.
122 char *CrashLog::LogConfiguration(char *buffer
, const char *last
) const
124 buffer
+= seprintf(buffer
, last
,
127 " Graphics set: %s (%u)\n"
129 " Music driver: %s\n"
130 " Music set: %s (%u)\n"
132 " Sound driver: %s\n"
133 " Sound set: %s (%u)\n"
134 " Video driver: %s\n\n",
135 BlitterFactoryBase::GetCurrentBlitter() == NULL
? "none" : BlitterFactoryBase::GetCurrentBlitter()->GetName(),
136 BaseGraphics::GetUsedSet() == NULL
? "none" : BaseGraphics::GetUsedSet()->name
,
137 BaseGraphics::GetUsedSet() == NULL
? UINT32_MAX
: BaseGraphics::GetUsedSet()->version
,
138 _current_language
== NULL
? "none" : _current_language
->file
,
139 _music_driver
== NULL
? "none" : _music_driver
->GetName(),
140 BaseMusic::GetUsedSet() == NULL
? "none" : BaseMusic::GetUsedSet()->name
,
141 BaseMusic::GetUsedSet() == NULL
? UINT32_MAX
: BaseMusic::GetUsedSet()->version
,
142 _networking
? (_network_server
? "server" : "client") : "no",
143 _sound_driver
== NULL
? "none" : _sound_driver
->GetName(),
144 BaseSounds::GetUsedSet() == NULL
? "none" : BaseSounds::GetUsedSet()->name
,
145 BaseSounds::GetUsedSet() == NULL
? UINT32_MAX
: BaseSounds::GetUsedSet()->version
,
146 _video_driver
== NULL
? "none" : _video_driver
->GetName()
149 buffer
+= seprintf(buffer
, last
, "AI Configuration (local: %i):\n", (int)_local_company
);
151 FOR_ALL_COMPANIES(c
) {
152 if (c
->ai_info
== NULL
) {
153 buffer
+= seprintf(buffer
, last
, " %2i: Human\n", (int)c
->index
);
155 buffer
+= seprintf(buffer
, last
, " %2i: %s (v%d)\n", (int)c
->index
, c
->ai_info
->GetName(), c
->ai_info
->GetVersion());
159 if (Game::GetInfo() != NULL
) {
160 buffer
+= seprintf(buffer
, last
, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
162 buffer
+= seprintf(buffer
, last
, "\n");
167 /* Include these here so it's close to where it's actually used. */
169 # include <allegro.h>
170 #endif /* WITH_ALLEGRO */
171 #ifdef WITH_FONTCONFIG
172 # include <fontconfig/fontconfig.h>
173 #endif /* WITH_FONTCONFIG */
175 /* pngconf.h, included by png.h doesn't like something in the
176 * freetype headers. As such it's not alphabetically sorted. */
178 #endif /* WITH_PNG */
180 # include <ft2build.h>
181 # include FT_FREETYPE_H
182 #endif /* WITH_FREETYPE */
184 # include <unicode/uversion.h>
185 #endif /* WITH_ICU */
190 #include <lzo/lzo1x.h>
195 #endif /* WITH_SDL */
201 * Writes information (versions) of the used libraries.
202 * @param buffer The begin where to write at.
203 * @param last The last position in the buffer to write to.
204 * @return the position of the \c '\0' character after the buffer.
206 char *CrashLog::LogLibraries(char *buffer
, const char *last
) const
208 buffer
+= seprintf(buffer
, last
, "Libraries:\n");
211 buffer
+= seprintf(buffer
, last
, " Allegro: %s\n", allegro_id
);
212 #endif /* WITH_ALLEGRO */
214 #ifdef WITH_FONTCONFIG
215 int version
= FcGetVersion();
216 buffer
+= seprintf(buffer
, last
, " FontConfig: %d.%d.%d\n", version
/ 10000, (version
/ 100) % 100, version
% 100);
217 #endif /* WITH_FONTCONFIG */
221 int major
, minor
, patch
;
222 FT_Init_FreeType(&library
);
223 FT_Library_Version(library
, &major
, &minor
, &patch
);
224 FT_Done_FreeType(library
);
225 buffer
+= seprintf(buffer
, last
, " FreeType: %d.%d.%d\n", major
, minor
, patch
);
226 #endif /* WITH_FREETYPE */
229 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
230 char buf
[4 * 3 + 3 + 1];
233 u_versionToString(ver
, buf
);
234 buffer
+= seprintf(buffer
, last
, " ICU: %s\n", buf
);
235 #endif /* WITH_ICU */
238 buffer
+= seprintf(buffer
, last
, " LZMA: %s\n", lzma_version_string());
242 buffer
+= seprintf(buffer
, last
, " LZO: %s\n", lzo_version_string());
246 buffer
+= seprintf(buffer
, last
, " PNG: %s\n", png_get_libpng_ver(NULL
));
247 #endif /* WITH_PNG */
250 #ifdef DYNAMICALLY_LOADED_SDL
251 if (SDL_CALL SDL_Linked_Version
!= NULL
) {
255 const SDL_version
*v
= SDL_CALL
SDL_Linked_Version();
256 buffer
+= seprintf(buffer
, last
, " SDL: %d.%d.%d\n", v
->major
, v
->minor
, v
->patch
);
258 #endif /* WITH_SDL */
261 buffer
+= seprintf(buffer
, last
, " Zlib: %s\n", zlibVersion());
264 buffer
+= seprintf(buffer
, last
, "\n");
269 * Helper function for printing the gamelog.
270 * @param s the string to print.
272 /* static */ void CrashLog::GamelogFillCrashLog(const char *s
)
274 CrashLog::gamelog_buffer
+= seprintf(CrashLog::gamelog_buffer
, CrashLog::gamelog_last
, "%s\n", s
);
278 * Writes the gamelog data to the buffer.
279 * @param buffer The begin where to write at.
280 * @param last The last position in the buffer to write to.
281 * @return the position of the \c '\0' character after the buffer.
283 char *CrashLog::LogGamelog(char *buffer
, const char *last
) const
285 CrashLog::gamelog_buffer
= buffer
;
286 CrashLog::gamelog_last
= last
;
287 GamelogPrint(&CrashLog::GamelogFillCrashLog
);
288 return CrashLog::gamelog_buffer
+ seprintf(CrashLog::gamelog_buffer
, last
, "\n");
292 * Fill the crash log buffer with all data of a crash log.
293 * @param buffer The begin where to write at.
294 * @param last The last position in the buffer to write to.
295 * @return the position of the \c '\0' character after the buffer.
297 char *CrashLog::FillCrashLog(char *buffer
, const char *last
) const
299 time_t cur_time
= time(NULL
);
300 buffer
+= seprintf(buffer
, last
, "*** OpenTTD Crash Report ***\n\n");
301 buffer
+= seprintf(buffer
, last
, "Crash at: %s", asctime(gmtime(&cur_time
)));
304 ConvertDateToYMD(_date
, &ymd
);
305 buffer
+= seprintf(buffer
, last
, "In game date: %i-%02i-%02i (%i)\n\n", ymd
.year
, ymd
.month
+ 1, ymd
.day
, _date_fract
);
307 buffer
= this->LogError(buffer
, last
, CrashLog::message
);
308 buffer
= this->LogOpenTTDVersion(buffer
, last
);
309 buffer
= this->LogRegisters(buffer
, last
);
310 buffer
= this->LogStacktrace(buffer
, last
);
311 buffer
= this->LogOSVersion(buffer
, last
);
312 buffer
= this->LogCompiler(buffer
, last
);
313 buffer
= this->LogConfiguration(buffer
, last
);
314 buffer
= this->LogLibraries(buffer
, last
);
315 buffer
= this->LogModules(buffer
, last
);
316 buffer
= this->LogGamelog(buffer
, last
);
318 buffer
+= seprintf(buffer
, last
, "*** End of OpenTTD Crash Report ***\n");
323 * Write the crash log to a file.
324 * @note On success the filename will be filled with the full path of the
325 * crash log file. Make sure filename is at least \c MAX_PATH big.
326 * @param buffer The begin of the buffer to write to the disk.
327 * @param filename Output for the filename of the written file.
328 * @param filename_last The last position in the filename buffer.
329 * @return true when the crash log was successfully written.
331 bool CrashLog::WriteCrashLog(const char *buffer
, char *filename
, const char *filename_last
) const
333 seprintf(filename
, filename_last
, "%scrash.log", _personal_dir
);
335 FILE *file
= FioFOpenFile(filename
, "w", NO_DIRECTORY
);
336 if (file
== NULL
) return false;
338 size_t len
= strlen(buffer
);
339 size_t written
= fwrite(buffer
, 1, len
, file
);
342 return len
== written
;
345 /* virtual */ int CrashLog::WriteCrashDump(char *filename
, const char *filename_last
) const
347 /* Stub implementation; not all OSes support this. */
352 * Write the (crash) savegame to a file.
353 * @note On success the filename will be filled with the full path of the
354 * crash save file. Make sure filename is at least \c MAX_PATH big.
355 * @param filename Output for the filename of the written file.
356 * @param filename_last The last position in the filename buffer.
357 * @return true when the crash save was successfully made.
359 bool CrashLog::WriteSavegame(char *filename
, const char *filename_last
) const
361 /* If the map array doesn't exist, saving will fail too. If the map got
362 * initialised, there is a big chance the rest is initialised too. */
363 if (_mth
== NULL
) return false;
368 seprintf(filename
, filename_last
, "%scrash.sav", _personal_dir
);
370 /* Don't do a threaded saveload. */
371 return SaveGame(filename
, NO_DIRECTORY
, false);
378 * Write the (crash) screenshot to a file.
379 * @note On success the filename will be filled with the full path of the
380 * screenshot. Make sure filename is at least \c MAX_PATH big.
381 * @param filename Output for the filename of the written file.
382 * @param filename_last The last position in the filename buffer.
383 * @return true when the crash screenshot was successfully made.
385 bool CrashLog::WriteScreenshot(char *filename
, const char *filename_last
) const
387 /* Don't draw when we have invalid screen size */
388 if (_screen
.width
< 1 || _screen
.height
< 1 || _screen
.dst_ptr
== NULL
) return false;
390 bool res
= MakeScreenshot(SC_CRASHLOG
, "crash");
391 if (res
) strecpy(filename
, _full_screenshot_name
, filename_last
);
396 * Makes the crash log, writes it to a file and then subsequently tries
397 * to make a crash dump and crash savegame. It uses DEBUG to write
398 * information like paths to the console.
399 * @return true when everything is made successfully.
401 bool CrashLog::MakeCrashLog() const
403 /* Don't keep looping logging crashes. */
404 static bool crashlogged
= false;
405 if (crashlogged
) return false;
408 char filename
[MAX_PATH
];
412 printf("Crash encountered, generating crash log...\n");
413 this->FillCrashLog(buffer
, lastof(buffer
));
414 printf("%s\n", buffer
);
415 printf("Crash log generated.\n\n");
417 printf("Writing crash log to disk...\n");
418 bool bret
= this->WriteCrashLog(buffer
, filename
, lastof(filename
));
420 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename
);
422 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
426 /* Don't mention writing crash dumps because not all platforms support it. */
427 int dret
= this->WriteCrashDump(filename
, lastof(filename
));
429 printf("Writing crash dump failed.\n\n");
431 } else if (dret
> 0) {
432 printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename
);
435 printf("Writing crash savegame...\n");
436 bret
= this->WriteSavegame(filename
, lastof(filename
));
438 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename
);
441 printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
444 printf("Writing crash screenshot...\n");
445 bret
= this->WriteScreenshot(filename
, lastof(filename
));
447 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename
);
450 printf("Writing crash screenshot failed.\n\n");
457 * Sets a message for the error message handler.
458 * @param message The error message of the error.
460 /* static */ void CrashLog::SetErrorMessage(const char *message
)
462 CrashLog::message
= message
;
466 * Try to close the sound/video stuff so it doesn't keep lingering around
467 * incorrect video states or so, e.g. keeping dpmi disabled.
469 /* static */ void CrashLog::AfterCrashLogCleanup()
471 if (_music_driver
!= NULL
) _music_driver
->Stop();
472 if (_sound_driver
!= NULL
) _sound_driver
->Stop();
473 if (_video_driver
!= NULL
) _video_driver
->Stop();