crashlog.cpp

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

Generated on Wed Mar 31 22:43:22 2010 for OpenTTD by  doxygen 1.6.1