crashlog.cpp

Go to the documentation of this file.
00001 /* $Id: crashlog.cpp 21161 2010-11-13 11:25:58Z rubidium $ */
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 
00076 char *CrashLog::LogOpenTTDVersion(char *buffer, const char *last) const
00077 {
00078   return buffer + seprintf(buffer, last,
00079       "OpenTTD version:\n"
00080       " Version:    %s (%d)\n"
00081       " NewGRF ver: %08x\n"
00082       " Bits:       %d\n"
00083       " Endian:     %s\n"
00084       " Dedicated:  %s\n"
00085       " Build date: %s\n\n",
00086       _openttd_revision,
00087       _openttd_revision_modified,
00088       _openttd_newgrf_version,
00089 #ifdef _SQ64
00090       64,
00091 #else
00092       32,
00093 #endif
00094 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
00095       "little",
00096 #else
00097       "big",
00098 #endif
00099 #ifdef DEDICATED
00100       "yes",
00101 #else
00102       "no",
00103 #endif
00104       _openttd_build_date
00105   );
00106 }
00107 
00108 char *CrashLog::LogConfiguration(char *buffer, const char *last) const
00109 {
00110   buffer += seprintf(buffer, last,
00111       "Configuration:\n"
00112       " Blitter:      %s\n"
00113       " Graphics set: %s (%u)\n"
00114       " Language:     %s\n"
00115       " Music driver: %s\n"
00116       " Music set:    %s (%u)\n"
00117       " Network:      %s\n"
00118       " Sound driver: %s\n"
00119       " Sound set:    %s (%u)\n"
00120       " Video driver: %s\n\n",
00121       BlitterFactoryBase::GetCurrentBlitter() == NULL ? "none" : BlitterFactoryBase::GetCurrentBlitter()->GetName(),
00122       BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name,
00123       BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version,
00124       _current_language == NULL ? "none" : _current_language->file,
00125       _music_driver == NULL ? "none" : _music_driver->GetName(),
00126       BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name,
00127       BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version,
00128       _networking ? (_network_server ? "server" : "client") : "no",
00129       _sound_driver == NULL ? "none" : _sound_driver->GetName(),
00130       BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name,
00131       BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version,
00132       _video_driver == NULL ? "none" : _video_driver->GetName()
00133   );
00134 
00135   buffer += seprintf(buffer, last, "AI Configuration (local: %i):\n", (int)_local_company);
00136   const Company *c;
00137   FOR_ALL_COMPANIES(c) {
00138     if (c->ai_info == NULL) {
00139       buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index);
00140     } else {
00141 #ifdef ENABLE_AI
00142       buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
00143 #endif /* ENABLE_AI */
00144     }
00145   }
00146   buffer += seprintf(buffer, last, "\n");
00147 
00148   return buffer;
00149 }
00150 
00151 /* Include these here so it's close to where it's actually used. */
00152 #ifdef WITH_ALLEGRO
00153 # include <allegro.h>
00154 #endif /* WITH_ALLEGRO */
00155 #ifdef WITH_FONTCONFIG
00156 # include <fontconfig/fontconfig.h>
00157 #endif /* WITH_FONTCONFIG */
00158 #ifdef WITH_PNG
00159   /* pngconf.h, included by png.h doesn't like something in the
00160    * freetype headers. As such it's not alphabetically sorted. */
00161 # include <png.h>
00162 #endif /* WITH_PNG */
00163 #ifdef WITH_FREETYPE
00164 # include <ft2build.h>
00165 # include FT_FREETYPE_H
00166 #endif /* WITH_FREETYPE */
00167 #ifdef WITH_ICU
00168 # include <unicode/uversion.h>
00169 #endif /* WITH_ICU */
00170 #ifdef WITH_LZMA
00171 # include <lzma.h>
00172 #endif
00173 #ifdef WITH_LZO
00174 #include <lzo/lzo1x.h>
00175 #endif
00176 #ifdef WITH_SDL
00177 # include "sdl.h"
00178 # include <SDL.h>
00179 #endif /* WITH_SDL */
00180 #ifdef WITH_ZLIB
00181 # include <zlib.h>
00182 #endif
00183 
00184 char *CrashLog::LogLibraries(char *buffer, const char *last) const
00185 {
00186   buffer += seprintf(buffer, last, "Libraries:\n");
00187 
00188 #ifdef WITH_ALLEGRO
00189   buffer += seprintf(buffer, last, " Allegro:    %s\n", allegro_id);
00190 #endif /* WITH_ALLEGRO */
00191 
00192 #ifdef WITH_FONTCONFIG
00193   int version = FcGetVersion();
00194   buffer += seprintf(buffer, last, " FontConfig: %d.%d.%d\n", version / 10000, (version / 100) % 100, version % 100);
00195 #endif /* WITH_FONTCONFIG */
00196 
00197 #ifdef WITH_FREETYPE
00198   FT_Library library;
00199   int major, minor, patch;
00200   FT_Init_FreeType(&library);
00201   FT_Library_Version(library, &major, &minor, &patch);
00202   FT_Done_FreeType(library);
00203   buffer += seprintf(buffer, last, " FreeType:   %d.%d.%d\n", major, minor, patch);
00204 #endif /* WITH_FREETYPE */
00205 
00206 #ifdef WITH_ICU
00207   /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
00208   char buf[4 * 3 + 3 + 1];
00209   UVersionInfo ver;
00210   u_getVersion(ver);
00211   u_versionToString(ver, buf);
00212   buffer += seprintf(buffer, last, " ICU:        %s\n", buf);
00213 #endif /* WITH_ICU */
00214 
00215 #ifdef WITH_LZMA
00216   buffer += seprintf(buffer, last, " LZMA:       %s\n", lzma_version_string());
00217 #endif
00218 
00219 #ifdef WITH_LZO
00220   buffer += seprintf(buffer, last, " LZO:        %s\n", lzo_version_string());
00221 #endif
00222 
00223 #ifdef WITH_PNG
00224   buffer += seprintf(buffer, last, " PNG:        %s\n", png_get_libpng_ver(NULL));
00225 #endif /* WITH_PNG */
00226 
00227 #ifdef WITH_SDL
00228 #ifdef DYNAMICALLY_LOADED_SDL
00229   if (SDL_CALL SDL_Linked_Version != NULL) {
00230 #else
00231   {
00232 #endif
00233     const SDL_version *v = SDL_CALL SDL_Linked_Version();
00234     buffer += seprintf(buffer, last, " SDL:        %d.%d.%d\n", v->major, v->minor, v->patch);
00235   }
00236 #endif /* WITH_SDL */
00237 
00238 #ifdef WITH_ZLIB
00239   buffer += seprintf(buffer, last, " Zlib:       %s\n", zlibVersion());
00240 #endif
00241 
00242   buffer += seprintf(buffer, last, "\n");
00243   return buffer;
00244 }
00245 
00246 /* static */ void CrashLog::GamelogFillCrashLog(const char *s)
00247 {
00248   CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s);
00249 }
00250 
00251 char *CrashLog::LogGamelog(char *buffer, const char *last) const
00252 {
00253   CrashLog::gamelog_buffer = buffer;
00254   CrashLog::gamelog_last = last;
00255   GamelogPrint(&CrashLog::GamelogFillCrashLog);
00256   return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n");
00257 }
00258 
00259 char *CrashLog::FillCrashLog(char *buffer, const char *last) const
00260 {
00261   time_t cur_time = time(NULL);
00262   buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n");
00263   buffer += seprintf(buffer, last, "Crash at: %s", asctime(gmtime(&cur_time)));
00264 
00265   YearMonthDay ymd;
00266   ConvertDateToYMD(_date, &ymd);
00267   buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract);
00268 
00269   buffer = this->LogError(buffer, last, CrashLog::message);
00270   buffer = this->LogOpenTTDVersion(buffer, last);
00271   buffer = this->LogRegisters(buffer, last);
00272   buffer = this->LogStacktrace(buffer, last);
00273   buffer = this->LogOSVersion(buffer, last);
00274   buffer = this->LogCompiler(buffer, last);
00275   buffer = this->LogConfiguration(buffer, last);
00276   buffer = this->LogLibraries(buffer, last);
00277   buffer = this->LogModules(buffer, last);
00278   buffer = this->LogGamelog(buffer, last);
00279 
00280   buffer += seprintf(buffer, last, "*** End of OpenTTD Crash Report ***\n");
00281   return buffer;
00282 }
00283 
00284 bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const
00285 {
00286   seprintf(filename, filename_last, "%scrash.log", _personal_dir);
00287 
00288   FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY);
00289   if (file == NULL) return false;
00290 
00291   size_t len = strlen(buffer);
00292   size_t written = fwrite(buffer, 1, len, file);
00293 
00294   FioFCloseFile(file);
00295   return len == written;
00296 }
00297 
00298 /* virtual */ int CrashLog::WriteCrashDump(char *filename, const char *filename_last) const
00299 {
00300   /* Stub implementation; not all OSes support this. */
00301   return 0;
00302 }
00303 
00304 bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
00305 {
00306   /* If the map array doesn't exist, saving will fail too. If the map got
00307    * initialised, there is a big chance the rest is initialised too. */
00308   if (_m == NULL) return false;
00309 
00310   try {
00311     GamelogEmergency();
00312 
00313     seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
00314 
00315     /* Don't do a threaded saveload. */
00316     return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK;
00317   } catch (...) {
00318     return false;
00319   }
00320 }
00321 
00322 bool CrashLog::WriteScreenshot(char *filename, const char *filename_last) const
00323 {
00324   /* Don't draw when we have invalid screen size */
00325   if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == NULL) return false;
00326 
00327   bool res = MakeScreenshot(SC_RAW, "crash");
00328   if (res) strecpy(filename, _full_screenshot_name, filename_last);
00329   return res;
00330 }
00331 
00332 bool CrashLog::MakeCrashLog() const
00333 {
00334   /* Don't keep looping logging crashes. */
00335   static bool crashlogged = false;
00336   if (crashlogged) return false;
00337   crashlogged = true;
00338 
00339   char filename[MAX_PATH];
00340   char buffer[65536];
00341   bool ret = true;
00342 
00343   printf("Crash encountered, generating crash log...\n");
00344   this->FillCrashLog(buffer, lastof(buffer));
00345   printf("%s\n", buffer);
00346   printf("Crash log generated.\n\n");
00347 
00348   printf("Writing crash log to disk...\n");
00349   bool bret = this->WriteCrashLog(buffer, filename, lastof(filename));
00350   if (bret) {
00351     printf("Crash log written to %s. Please add this file to any bug reports.\n\n", filename);
00352   } else {
00353     printf("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
00354     ret = false;
00355   }
00356 
00357   /* Don't mention writing crash dumps because not all platforms support it. */
00358   int dret = this->WriteCrashDump(filename, lastof(filename));
00359   if (dret < 0) {
00360     printf("Writing crash dump failed.\n\n");
00361     ret = false;
00362   } else if (dret > 0) {
00363     printf("Crash dump written to %s. Please add this file to any bug reports.\n\n", filename);
00364   }
00365 
00366   printf("Writing crash savegame...\n");
00367   bret = this->WriteSavegame(filename, lastof(filename));
00368   if (bret) {
00369     printf("Crash savegame written to %s. Please add this file and the last (auto)save to any bug reports.\n\n", filename);
00370   } else {
00371     ret = false;
00372     printf("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
00373   }
00374 
00375   printf("Writing crash screenshot...\n");
00376   bret = this->WriteScreenshot(filename, lastof(filename));
00377   if (bret) {
00378     printf("Crash screenshot written to %s. Please add this file to any bug reports.\n\n", filename);
00379   } else {
00380     ret = false;
00381     printf("Writing crash screenshot failed.\n\n");
00382   }
00383 
00384   return ret;
00385 }
00386 
00387 /* static */ void CrashLog::SetErrorMessage(const char *message)
00388 {
00389   CrashLog::message = message;
00390 }
00391 
00392 /* static */ void CrashLog::AfterCrashLogCleanup()
00393 {
00394   if (_music_driver != NULL) _music_driver->Stop();
00395   if (_sound_driver != NULL) _sound_driver->Stop();
00396   if (_video_driver != NULL) _video_driver->Stop();
00397 }

Generated on Sun Jan 9 16:01:53 2011 for OpenTTD by  doxygen 1.6.1