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       buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
00155     }
00156   }
00157   buffer += seprintf(buffer, last, "\n");
00158 
00159   return buffer;
00160 }
00161 
00162 /* Include these here so it's close to where it's actually used. */
00163 #ifdef WITH_ALLEGRO
00164 # include <allegro.h>
00165 #endif /* WITH_ALLEGRO */
00166 #ifdef WITH_FONTCONFIG
00167 # include <fontconfig/fontconfig.h>
00168 #endif /* WITH_FONTCONFIG */
00169 #ifdef WITH_PNG
00170   /* pngconf.h, included by png.h doesn't like something in the
00171    * freetype headers. As such it's not alphabetically sorted. */
00172 # include <png.h>
00173 #endif /* WITH_PNG */
00174 #ifdef WITH_FREETYPE
00175 # include <ft2build.h>
00176 # include FT_FREETYPE_H
00177 #endif /* WITH_FREETYPE */
00178 #ifdef WITH_ICU
00179 # include <unicode/uversion.h>
00180 #endif /* WITH_ICU */
00181 #ifdef WITH_LZMA
00182 # include <lzma.h>
00183 #endif
00184 #ifdef WITH_LZO
00185 #include <lzo/lzo1x.h>
00186 #endif
00187 #ifdef WITH_SDL
00188 # include "sdl.h"
00189 # include <SDL.h>
00190 #endif /* WITH_SDL */
00191 #ifdef WITH_ZLIB
00192 # include <zlib.h>
00193 #endif
00194 
00201 char *CrashLog::LogLibraries(char *buffer, const char *last) const
00202 {
00203   buffer += seprintf(buffer, last, "Libraries:\n");
00204 
00205 #ifdef WITH_ALLEGRO
00206   buffer += seprintf(buffer, last, " Allegro:    %s\n", allegro_id);
00207 #endif /* WITH_ALLEGRO */
00208 
00209 #ifdef WITH_FONTCONFIG
00210   int version = FcGetVersion();
00211   buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
00212 #endif /* WITH_FONTCONFIG */
00213 
00214 #ifdef WITH_FREETYPE
00215   FT_Library library;
00216   int major, minor, patch;
00217   FT_Init_FreeType(&library);
00218   FT_Library_Version(library, &major, &minor, &patch);
00219   FT_Done_FreeType(library);
00220   buffer += seprintf(buffer, last, " FreeType:   %d.%d.%d\n", major, minor, patch);
00221 #endif /* WITH_FREETYPE */
00222 
00223 #ifdef WITH_ICU
00224   /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
00225   char buf[4 * 3 + 3 + 1];
00226   UVersionInfo ver;
00227   u_getVersion(ver);
00228   u_versionToString(ver, buf);
00229   buffer += seprintf(buffer, last, " ICU:        %s\n", buf);
00230 #endif /* WITH_ICU */
00231 
00232 #ifdef WITH_LZMA
00233   buffer += seprintf(buffer, last, " LZMA:       %s\n", lzma_version_string());
00234 #endif
00235 
00236 #ifdef WITH_LZO
00237   buffer += seprintf(buffer, last, " LZO:        %s\n", lzo_version_string());
00238 #endif
00239 
00240 #ifdef WITH_PNG
00241   buffer += seprintf(buffer, last, " PNG:        %s\n", png_get_libpng_ver(NULL));
00242 #endif /* WITH_PNG */
00243 
00244 #ifdef WITH_SDL
00245 #ifdef DYNAMICALLY_LOADED_SDL
00246   if (SDL_CALL SDL_Linked_Version != NULL) {
00247 #else
00248   {
00249 #endif
00250     const SDL_version *v = SDL_CALL SDL_Linked_Version();
00251     buffer += seprintf(buffer, last, " SDL:        %d.%d.%d\n", v->major, v->minor, v->patch);
00252   }
00253 #endif /* WITH_SDL */
00254 
00255 #ifdef WITH_ZLIB
00256   buffer += seprintf(buffer, last, " Zlib:       %s\n", zlibVersion());
00257 #endif
00258 
00259   buffer += seprintf(buffer, last, "\n");
00260   return buffer;
00261 }
00262 
00267 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
00268 {
00269   CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
00270 }
00271 
00278 char *CrashLog::LogGamelog(char *buffer, const char *last) const
00279 {
00280   CrashLog::gamelog_buffer = buffer;
00281   CrashLog::gamelog_last = last;
00282   GamelogPrint(&CrashLog::GamelogFillCrashLog);
00283   return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
00284 }
00285 
00292 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
00293 {
00294   time_t cur_time = time(NULL);
00295   buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
00296   buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
00297 
00298   YearMonthDay ymd;
00299   ConvertDateToYMD(_date, &ymd);
00300   buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
00301 
00302   buffer = this->LogError(buffer, last, CrashLog::message);
00303   buffer = this->LogOpenTTDVersion(buffer, last);
00304   buffer = this->LogRegisters(buffer, last);
00305   buffer = this->LogStacktrace(buffer, last);
00306   buffer = this->LogOSVersion(buffer, last);
00307   buffer = this->LogCompiler(buffer, last);
00308   buffer = this->LogConfiguration(buffer, last);
00309   buffer = this->LogLibraries(buffer, last);
00310   buffer = this->LogModules(buffer, last);
00311   buffer = this->LogGamelog(buffer, last);
00312 
00313   buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
00314   return buffer;
00315 }
00316 
00326 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
00327 {
00328   seprintf(filename, filename_last, "%scrash.log", _personal_dir);
00329 
00330   FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
00331   if (file == NULL) return false;
00332 
00333   size_t len = strlen(buffer);
00334   size_t written = fwrite(buffer, 1, len, file);
00335 
00336   FioFCloseFile(file);
00337   return len == written;
00338 }
00339 
00340 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
00341 {
00342   /* Stub implementation; not all OSes support this. */
00343   return 0;
00344 }
00345 
00354 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
00355 {
00356   /* If the map array doesn't exist, saving will fail too. If the map got
00357    * initialised, there is a big chance the rest is initialised too. */
00358   if (_m == NULL) return false;
00359 
00360   try {
00361     GamelogEmergency();
00362 
00363     seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
00364 
00365     /* Don't do a threaded saveload. */
00366     return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK;
00367   } catch (...) {
00368     return false;
00369   }
00370 }
00371 
00380 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
00381 {
00382   /* Don't draw when we have invalid screen size */
00383   if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
00384 
00385   bool res = MakeScreenshot(SC_RAW, "crash");
00386   if (res) strecpy(filename, _full_screenshot_name, filename_last);
00387   return res;
00388 }
00389 
00396 bool CrashLog::MakeCrashLog() const
00397 {
00398   /* Don't keep looping logging crashes. */
00399   static bool crashlogged = false;
00400   if (crashlogged) return false;
00401   crashlogged = true;
00402 
00403   char filename[MAX_PATH];
00404   char buffer[65536];
00405   bool ret = true;
00406 
00407   printf("Crash encountered, generating crash log...\n");
00408   this->FillCrashLog(buffer, lastof(buffer));
00409   printf("%s\n", buffer);
00410   printf("Crash log generated.\n\n");
00411 
00412   printf("Writing crash log to disk...\n");
00413   bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
00414   if (bret) {
00415     printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
00416   } else {
00417     printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
00418     ret = false;
00419   }
00420 
00421   /* Don't mention writing crash dumps because not all platforms support it. */
00422   int dret = this->WriteCrashDump(filename, lastof(filename));
00423   if (dret < 0) {
00424     printf("Writing crash dump failed.\n\n");
00425     ret = false;
00426   } else if (dret > 0) {
00427     printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
00428   }
00429 
00430   printf("Writing crash savegame...\n");
00431   bret = this->WriteSavegame(filename, lastof(filename));
00432   if (bret) {
00433     printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
00434   } else {
00435     ret = false;
00436     printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
00437   }
00438 
00439   printf("Writing crash screenshot...\n");
00440   bret = this->WriteScreenshot(filename, lastof(filename));
00441   if (bret) {
00442     printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
00443   } else {
00444     ret = false;
00445     printf("Writing crash screenshot failed.\n\n");
00446   }
00447 
00448   return ret;
00449 }
00450 
00455 /* static */ void CrashLog::SetErrorMessage(const char *message)
00456 {
00457   CrashLog::message = message;
00458 }
00459 
00464 /* static */ void CrashLog::AfterCrashLogCleanup()
00465 {
00466   if (_music_driver != NULL) _music_driver->Stop();
00467   if (_sound_driver != NULL) _sound_driver->Stop();
00468   if (_video_driver != NULL) _video_driver->Stop();
00469 }