crashlog.cpp

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

Generated on Wed Dec 23 23:27:49 2009 for OpenTTD by  doxygen 1.5.6