# crashlog.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "crashlog.h"
    00014 #include "gamelog.h"
    00015 #include "date_func.h"
    00016 #include "map_func.h"
    00017 #include "rev.h"
    00018 #include "strings_func.h"
    00019 #include "blitter/factory.hpp"
    00020 #include "base_media_base.h"
    00021 #include "music/music_driver.hpp"
    00022 #include "sound/sound_driver.hpp"
    00023 #include "video/video_driver.hpp"
    00024 #include "saveload/saveload.h"
    00025 #include "screenshot.h"
    00026 #include "gfx_func.h"
    00027 #include "network/network.h"
    00028 #include "language.h"
    00029
    00030 #include "ai/ai_info.hpp"
    00031 #include "game/game.hpp"
    00032 #include "game/game_info.hpp"
    00033 #include "company_base.h"
    00034 #include "company_func.h"
    00035
    00036 #include <time.h>
    00037
    00038 /* static */ const char *CrashLog::message = NULL;
    00039 /* static */ char *CrashLog::gamelog_buffer = NULL;
    00040 /* static */ const char *CrashLog::gamelog_last = NULL;
    00041
    00042 char *CrashLog::LogCompiler(char *buffer, const char *last) const
    00043 {
    00044       buffer += seprintf(buffer, last, " Compiler: "
    00045 #if defined(_MSC_VER)
    00046       "MSVC %d", _MSC_VER
    00047 #elif defined(__ICC) && defined(__GNUC__)
    00048       "ICC %d (GCC %d.%d.%d mode)", __ICC,  __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
    00049 #elif defined(__ICC)
    00050       "ICC %d", __ICC
    00051 #elif defined(__GNUC__)
    00052       "GCC %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
    00053 #elif defined(__WATCOMC__)
    00054       "WatcomC %d", __WATCOMC__
    00055 #else
    00056       "<unknown>"
    00057 #endif
    00058       );
    00059 #if defined(__VERSION__)
    00060       return buffer + seprintf(buffer, last,  " \"" __VERSION__ "\"\n\n");
    00061 #else
    00062       return buffer + seprintf(buffer, last,  "\n\n");
    00063 #endif
    00064 }
    00065
    00066 /* virtual */ char *CrashLog::LogRegisters(char *buffer, const char *last) const
    00067 {
    00068   /* Stub implementation; not all OSes support this. */
    00069   return buffer;
    00070 }
    00071
    00072 /* virtual */ char *CrashLog::LogModules(char *buffer, const char *last) const
    00073 {
    00074   /* Stub implementation; not all OSes support this. */
    00075   return buffer;
    00076 }
    00077
    00084 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
    00085 {
    00086   return buffer + seprintf(buffer, last,
    00087       "OpenTTD version:\n"
    00088       " Version:    %s (%d)\n"
    00089       " NewGRF ver: %08x\n"
    00090       " Bits:       %d\n"
    00091       " Endian:     %s\n"
    00092       " Dedicated:  %s\n"
    00093       " Build date: %s\n\n",
    00094       _openttd_revision,
    00095       _openttd_revision_modified,
    00096       _openttd_newgrf_version,
    00097 #ifdef _SQ64
    00098       64,
    00099 #else
    00100       32,
    00101 #endif
    00102 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
    00103       "little",
    00104 #else
    00105       "big",
    00106 #endif
    00107 #ifdef DEDICATED
    00108       "yes",
    00109 #else
    00110       "no",
    00111 #endif
    00112       _openttd_build_date
    00113   );
    00114 }
    00115
    00123 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
    00124 {
    00125   buffer += seprintf(buffer, last,
    00126       "Configuration:\n"
    00127       " Blitter:      %s\n"
    00128       " Graphics set: %s (%u)\n"
    00129       " Language:     %s\n"
    00130       " Music driver: %s\n"
    00131       " Music set:    %s (%u)\n"
    00132       " Network:      %s\n"
    00133       " Sound driver: %s\n"
    00134       " Sound set:    %s (%u)\n"
    00135       " Video driver: %s\n\n",
    00136       BlitterFactoryBase::GetCurrentBlitter() == NULL ? "none" : BlitterFactoryBase::GetCurrentBlitter()->GetName(),
    00137       BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
    00138       BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
    00139       _current_language == NULL ? "none" : _current_language->file,
    00140       _music_driver == NULL ? "none" : _music_driver->GetName(),
    00141       BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
    00142       BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
    00143       _networking ? (_network_server ? "server" : "client") : "no",
    00144       _sound_driver == NULL ? "none" : _sound_driver->GetName(),
    00145       BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
    00146       BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
    00147       _video_driver == NULL ? "none" : _video_driver->GetName()
    00148   );
    00149
    00150   buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
    00151   const Company *c;
    00152   FOR_ALL_COMPANIES(c) {
    00153     if (c->ai_info == NULL) {
    00154       buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
    00155     } else {
    00156       buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
    00157     }
    00158   }
    00159
    00160   if (Game::GetInfo() != NULL) {
    00161     buffer += seprintf(buffer, last, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
    00162   }
    00163   buffer += seprintf(buffer, last, "\n");
    00164
    00165   return buffer;
    00166 }
    00167
    00168 /* Include these here so it's close to where it's actually used. */
    00169 #ifdef WITH_ALLEGRO
    00170 # include <allegro.h>
    00171 #endif /* WITH_ALLEGRO */
    00172 #ifdef WITH_FONTCONFIG
    00173 # include <fontconfig/fontconfig.h>
    00174 #endif /* WITH_FONTCONFIG */
    00175 #ifdef WITH_PNG
    00176   /* pngconf.h, included by png.h doesn't like something in the
    00177    * freetype headers. As such it's not alphabetically sorted. */
    00178 # include <png.h>
    00179 #endif /* WITH_PNG */
    00180 #ifdef WITH_FREETYPE
    00181 # include <ft2build.h>
    00182 # include FT_FREETYPE_H
    00183 #endif /* WITH_FREETYPE */
    00184 #ifdef WITH_ICU
    00185 # include <unicode/uversion.h>
    00186 #endif /* WITH_ICU */
    00187 #ifdef WITH_LZMA
    00188 # include <lzma.h>
    00189 #endif
    00190 #ifdef WITH_LZO
    00191 #include <lzo/lzo1x.h>
    00192 #endif
    00193 #ifdef WITH_SDL
    00194 # include "sdl.h"
    00195 # include <SDL.h>
    00196 #endif /* WITH_SDL */
    00197 #ifdef WITH_ZLIB
    00198 # include <zlib.h>
    00199 #endif
    00200
    00207 char *CrashLog::LogLibraries(char *buffer, const char *last) const
    00208 {
    00209   buffer += seprintf(buffer, last, "Libraries:\n");
    00210
    00211 #ifdef WITH_ALLEGRO
    00212   buffer += seprintf(buffer, last, " Allegro:    %s\n", allegro_id);
    00213 #endif /* WITH_ALLEGRO */
    00214
    00215 #ifdef WITH_FONTCONFIG
    00216   int version = FcGetVersion();
    00217   buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
    00218 #endif /* WITH_FONTCONFIG */
    00219
    00220 #ifdef WITH_FREETYPE
    00221   FT_Library library;
    00222   int major, minor, patch;
    00223   FT_Init_FreeType(&library);
    00224   FT_Library_Version(library, &major, &minor, &patch);
    00225   FT_Done_FreeType(library);
    00226   buffer += seprintf(buffer, last, " FreeType:   %d.%d.%d\n", major, minor, patch);
    00227 #endif /* WITH_FREETYPE */
    00228
    00229 #ifdef WITH_ICU
    00230   /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
    00231   char buf[4 * 3 + 3 + 1];
    00232   UVersionInfo ver;
    00233   u_getVersion(ver);
    00234   u_versionToString(ver, buf);
    00235   buffer += seprintf(buffer, last, " ICU:        %s\n", buf);
    00236 #endif /* WITH_ICU */
    00237
    00238 #ifdef WITH_LZMA
    00239   buffer += seprintf(buffer, last, " LZMA:       %s\n", lzma_version_string());
    00240 #endif
    00241
    00242 #ifdef WITH_LZO
    00243   buffer += seprintf(buffer, last, " LZO:        %s\n", lzo_version_string());
    00244 #endif
    00245
    00246 #ifdef WITH_PNG
    00247   buffer += seprintf(buffer, last, " PNG:        %s\n", png_get_libpng_ver(NULL));
    00248 #endif /* WITH_PNG */
    00249
    00250 #ifdef WITH_SDL
    00251 #ifdef DYNAMICALLY_LOADED_SDL
    00252   if (SDL_CALL SDL_Linked_Version != NULL) {
    00253 #else
    00254   {
    00255 #endif
    00256     const SDL_version *v = SDL_CALL SDL_Linked_Version();
    00257     buffer += seprintf(buffer, last, " SDL:        %d.%d.%d\n", v->major, v->minor, v->patch);
    00258   }
    00259 #endif /* WITH_SDL */
    00260
    00261 #ifdef WITH_ZLIB
    00262   buffer += seprintf(buffer, last, " Zlib:       %s\n", zlibVersion());
    00263 #endif
    00264
    00265   buffer += seprintf(buffer, last, "\n");
    00266   return buffer;
    00267 }
    00268
    00273 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
    00274 {
    00275   CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
    00276 }
    00277
    00284 char *CrashLog::LogGamelog(char *buffer, const char *last) const
    00285 {
    00286   CrashLog::gamelog_buffer = buffer;
    00287   CrashLog::gamelog_last = last;
    00288   GamelogPrint(&CrashLog::GamelogFillCrashLog);
    00289   return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
    00290 }
    00291
    00298 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
    00299 {
    00300   time_t cur_time = time(NULL);
    00301   buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
    00302   buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
    00303
    00304   YearMonthDay ymd;
    00305   ConvertDateToYMD(_date, &ymd);
    00306   buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
    00307
    00308   buffer = this->LogError(buffer, last, CrashLog::message);
    00309   buffer = this->LogOpenTTDVersion(buffer, last);
    00310   buffer = this->LogRegisters(buffer, last);
    00311   buffer = this->LogStacktrace(buffer, last);
    00312   buffer = this->LogOSVersion(buffer, last);
    00313   buffer = this->LogCompiler(buffer, last);
    00314   buffer = this->LogConfiguration(buffer, last);
    00315   buffer = this->LogLibraries(buffer, last);
    00316   buffer = this->LogModules(buffer, last);
    00317   buffer = this->LogGamelog(buffer, last);
    00318
    00319   buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
    00320   return buffer;
    00321 }
    00322
    00332 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
    00333 {
    00334   seprintf(filename, filename_last, "%scrash.log", _personal_dir);
    00335
    00336   FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
    00337   if (file == NULL) return false;
    00338
    00339   size_t len = strlen(buffer);
    00340   size_t written = fwrite(buffer, 1, len, file);
    00341
    00342   FioFCloseFile(file);
    00343   return len == written;
    00344 }
    00345
    00346 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
    00347 {
    00348   /* Stub implementation; not all OSes support this. */
    00349   return 0;
    00350 }
    00351
    00360 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
    00361 {
    00362   /* If the map array doesn't exist, saving will fail too. If the map got
    00363    * initialised, there is a big chance the rest is initialised too. */
    00364   if (_m == NULL) return false;
    00365
    00366   try {
    00367     GamelogEmergency();
    00368
    00369     seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
    00370
    00371     /* Don't do a threaded saveload. */
    00372     return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK;
    00373   } catch (...) {
    00374     return false;
    00375   }
    00376 }
    00377
    00386 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
    00387 {
    00388   /* Don't draw when we have invalid screen size */
    00389   if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
    00390
    00391   bool res = MakeScreenshot(SC_CRASHLOG, "crash");
    00392   if (res) strecpy(filename, _full_screenshot_name, filename_last);
    00393   return res;
    00394 }
    00395
    00402 bool CrashLog::MakeCrashLog() const
    00403 {
    00404   /* Don't keep looping logging crashes. */
    00405   static bool crashlogged = false;
    00406   if (crashlogged) return false;
    00407   crashlogged = true;
    00408
    00409   char filename[MAX_PATH];
    00410   char buffer[65536];
    00411   bool ret = true;
    00412
    00413   printf("Crash encountered, generating crash log...\n");
    00414   this->FillCrashLog(buffer, lastof(buffer));
    00415   printf("%s\n", buffer);
    00416   printf("Crash log generated.\n\n");
    00417
    00418   printf("Writing crash log to disk...\n");
    00419   bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
    00420   if (bret) {
    00421     printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
    00422   } else {
    00423     printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
    00424     ret = false;
    00425   }
    00426
    00427   /* Don't mention writing crash dumps because not all platforms support it. */
    00428   int dret = this->WriteCrashDump(filename, lastof(filename));
    00429   if (dret < 0) {
    00430     printf("Writing crash dump failed.\n\n");
    00431     ret = false;
    00432   } else if (dret > 0) {
    00433     printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
    00434   }
    00435
    00436   printf("Writing crash savegame...\n");
    00437   bret = this->WriteSavegame(filename, lastof(filename));
    00438   if (bret) {
    00439     printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
    00440   } else {
    00441     ret = false;
    00442     printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
    00443   }
    00444
    00445   printf("Writing crash screenshot...\n");
    00446   bret = this->WriteScreenshot(filename, lastof(filename));
    00447   if (bret) {
    00448     printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
    00449   } else {
    00450     ret = false;
    00451     printf("Writing crash screenshot failed.\n\n");
    00452   }
    00453
    00454   return ret;
    00455 }
    00456
    00461 /* static */ void CrashLog::SetErrorMessage(const char *message)
    00462 {
    00463   CrashLog::message = message;
    00464 }
    00465
    00470 /* static */ void CrashLog::AfterCrashLogCleanup()
    00471 {
    00472   if (_music_driver != NULL) _music_driver->Stop();
    00473   if (_sound_driver != NULL) _sound_driver->Stop();
    00474   if (_video_driver != NULL) _video_driver->Stop();
    00475 }