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