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"
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"
27 #include "network/network.h"
30 #include "ai/ai_info.hpp"
31 #include "company_base.h"
32 #include "company_func.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: "
45 #elif defined(__ICC) && defined(__GNUC__)
46 "ICC %d (GCC %d.%d.%d mode)", __ICC
, __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
49 #elif defined(__GNUC__)
50 "GCC %d.%d.%d", __GNUC__
, __GNUC_MINOR__
, __GNUC_PATCHLEVEL__
51 #elif defined(__WATCOMC__)
52 "WatcomC %d", __WATCOMC__
57 #if defined(__VERSION__)
58 return buffer
+ seprintf(buffer
, last
, " \"" __VERSION__
"\"\n\n");
60 return buffer
+ seprintf(buffer
, last
, "\n\n");
64 /* virtual */ char *CrashLog::LogRegisters(char *buffer
, const char *last
) const
66 /* Stub implementation; not all OSes support this. */
70 /* virtual */ char *CrashLog::LogModules(char *buffer
, const char *last
) const
72 /* Stub implementation; not all OSes support this. */
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
,
91 " Build date: %s\n\n",
93 _openttd_revision_modified
,
94 _openttd_newgrf_version
,
100 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
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
,
126 " Graphics set: %s (%u)\n"
128 " Music driver: %s\n"
129 " Music set: %s (%u)\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
);
150 FOR_ALL_COMPANIES(c
) {
151 if (c
->ai_info
== NULL
) {
152 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());
156 #endif /* ENABLE_AI */
159 buffer
+= seprintf(buffer
, last
, "\n");
164 /* Include these here so it's close to where it's actually used. */
166 # include <allegro.h>
167 #endif /* WITH_ALLEGRO */
168 #ifdef WITH_FONTCONFIG
169 # include <fontconfig/fontconfig.h>
170 #endif /* WITH_FONTCONFIG */
172 /* pngconf.h, included by png.h doesn't like something in the
173 * freetype headers. As such it's not alphabetically sorted. */
175 #endif /* WITH_PNG */
177 # include <ft2build.h>
178 # include FT_FREETYPE_H
179 #endif /* WITH_FREETYPE */
181 # include <unicode/uversion.h>
182 #endif /* WITH_ICU */
187 #include <lzo/lzo1x.h>
192 #endif /* WITH_SDL */
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");
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 */
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 */
226 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
227 char buf
[4 * 3 + 3 + 1];
230 u_versionToString(ver
, buf
);
231 buffer
+= seprintf(buffer
, last
, " ICU: %s\n", buf
);
232 #endif /* WITH_ICU */
235 buffer
+= seprintf(buffer
, last
, " LZMA: %s\n", lzma_version_string());
239 buffer
+= seprintf(buffer
, last
, " LZO: %s\n", lzo_version_string());
243 buffer
+= seprintf(buffer
, last
, " PNG: %s\n", png_get_libpng_ver(NULL
));
244 #endif /* WITH_PNG */
247 #ifdef DYNAMICALLY_LOADED_SDL
248 if (SDL_CALL SDL_Linked_Version
!= NULL
) {
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 */
258 buffer
+= seprintf(buffer
, last
, " Zlib: %s\n", zlibVersion());
261 buffer
+= seprintf(buffer
, last
, "\n");
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
)));
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");
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
);
339 return len
== written
;
342 /* virtual */ int CrashLog::WriteCrashDump(char *filename
, const char *filename_last
) const
344 /* Stub implementation; not all OSes support this. */
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;
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
;
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
);
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;
405 char filename
[MAX_PATH
];
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
));
417 printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename
);
419 printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
423 /* Don't mention writing crash dumps because not all platforms support it. */
424 int dret
= this->WriteCrashDump(filename
, lastof(filename
));
426 printf("Writing crash dump failed.\n\n");
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
));
435 printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename
);
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
));
444 printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename
);
447 printf("Writing crash screenshot failed.\n\n");
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();