Files
@ r28060:53690f1ef210
Branch filter:
Location: cpp/openttd-patchpack/source/src/crashlog.cpp - annotation
r28060:53690f1ef210
11.8 KiB
text/x-c
Feature: Base graphics can offer parameters for additional settings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r27914:bfd64f30ccdf r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r13622:200502780bca r27371:b9ad4c1bff08 r22793:0c646694741d r27914:bfd64f30ccdf r27914:bfd64f30ccdf r12947:776ad783fe07 r14233:1f976749061a r27418:86b1bec802f9 r27557:50ad62a8825a r27914:bfd64f30ccdf r22378:70a767bcd681 r21383:942c32fb8b0e r21383:942c32fb8b0e r27914:bfd64f30ccdf r12947:776ad783fe07 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r12947:776ad783fe07 r17097:4984cc3adcdd r17097:4984cc3adcdd r27432:005dbba2ae93 r17097:4984cc3adcdd r27914:bfd64f30ccdf r12947:776ad783fe07 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r26818:9df12eaf020a r12947:776ad783fe07 r12947:776ad783fe07 r17097:4984cc3adcdd r23684:14cb6d596f0d r27432:005dbba2ae93 r22793:0c646694741d r27914:bfd64f30ccdf r22793:0c646694741d r27914:bfd64f30ccdf r22793:0c646694741d r23684:14cb6d596f0d r23684:14cb6d596f0d r27229:662da0bb155f r27229:662da0bb155f r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r22793:0c646694741d r22793:0c646694741d r22793:0c646694741d r22793:0c646694741d r26170:40e0ba47161b r26170:40e0ba47161b r26170:40e0ba47161b r27433:f785ea80973e r26170:40e0ba47161b r27433:f785ea80973e r26170:40e0ba47161b r26170:40e0ba47161b r26170:40e0ba47161b r26170:40e0ba47161b r27418:86b1bec802f9 r26170:40e0ba47161b r27433:f785ea80973e r26170:40e0ba47161b r26170:40e0ba47161b r26170:40e0ba47161b r17097:4984cc3adcdd r17097:4984cc3adcdd r27914:bfd64f30ccdf r12947:776ad783fe07 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r14115:7a3ad652baf0 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r12947:776ad783fe07 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r12947:776ad783fe07 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r12947:776ad783fe07 r12947:776ad783fe07 r17097:4984cc3adcdd r17097:4984cc3adcdd r27433:f785ea80973e r17097:4984cc3adcdd r17097:4984cc3adcdd r27433:f785ea80973e r12947:776ad783fe07 r27914:bfd64f30ccdf r12947:776ad783fe07 r27433:f785ea80973e r23607:36c15679007d r12947:776ad783fe07 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r27813:6b3a5970aa1b r27813:6b3a5970aa1b r27813:6b3a5970aa1b r27813:6b3a5970aa1b r27829:4c64d91d66c7 r27813:6b3a5970aa1b r27829:4c64d91d66c7 r12947:776ad783fe07 r27829:4c64d91d66c7 r27829:4c64d91d66c7 r12947:776ad783fe07 r12947:776ad783fe07 r17097:4984cc3adcdd r17097:4984cc3adcdd r27433:f785ea80973e r17097:4984cc3adcdd r17097:4984cc3adcdd r27433:f785ea80973e r12947:776ad783fe07 r26789:79b05bbf3bd2 r12947:776ad783fe07 r26789:79b05bbf3bd2 r12947:776ad783fe07 r12947:776ad783fe07 r27223:23146c90355d r12947:776ad783fe07 r27433:f785ea80973e r12947:776ad783fe07 r13027:56b237e9910f r27433:f785ea80973e r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r17097:4984cc3adcdd r17097:4984cc3adcdd r27433:f785ea80973e r27433:f785ea80973e r17097:4984cc3adcdd r27433:f785ea80973e r13622:200502780bca r13622:200502780bca r23607:36c15679007d r13622:200502780bca r27433:f785ea80973e r26170:40e0ba47161b r27499:c532a2c27509 r13622:200502780bca r13622:200502780bca r13622:200502780bca r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27402:8e5c6e9c8a2d r27402:8e5c6e9c8a2d r27402:8e5c6e9c8a2d r27402:8e5c6e9c8a2d r27402:8e5c6e9c8a2d r27402:8e5c6e9c8a2d r27402:8e5c6e9c8a2d r17097:4984cc3adcdd r17097:4984cc3adcdd r17097:4984cc3adcdd r17097:4984cc3adcdd r17097:4984cc3adcdd r27833:7f1a953dfadf r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r27833:7f1a953dfadf r12947:776ad783fe07 r12947:776ad783fe07 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r27914:bfd64f30ccdf r12947:776ad783fe07 r27914:bfd64f30ccdf r27914:bfd64f30ccdf r12947:776ad783fe07 r27404:8b233646def8 r27833:7f1a953dfadf r27833:7f1a953dfadf r27433:f785ea80973e r12947:776ad783fe07 r27404:8b233646def8 r27833:7f1a953dfadf r12947:776ad783fe07 r12947:776ad783fe07 r27813:6b3a5970aa1b r27833:7f1a953dfadf r27833:7f1a953dfadf r27433:f785ea80973e r27813:6b3a5970aa1b r27829:4c64d91d66c7 r27833:7f1a953dfadf r12947:776ad783fe07 r12947:776ad783fe07 r27404:8b233646def8 r27833:7f1a953dfadf r27833:7f1a953dfadf r27433:f785ea80973e r12947:776ad783fe07 r27404:8b233646def8 r27833:7f1a953dfadf r12947:776ad783fe07 r12947:776ad783fe07 r27404:8b233646def8 r27833:7f1a953dfadf r27833:7f1a953dfadf r27433:f785ea80973e r13622:200502780bca r27404:8b233646def8 r27833:7f1a953dfadf r13622:200502780bca r13622:200502780bca r27829:4c64d91d66c7 r12947:776ad783fe07 r12947:776ad783fe07 r17097:4984cc3adcdd r17097:4984cc3adcdd r17097:4984cc3adcdd r17097:4984cc3adcdd r27160:192bc991c2e6 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r12947:776ad783fe07 r17097:4984cc3adcdd r17097:4984cc3adcdd r17097:4984cc3adcdd r17097:4984cc3adcdd r12947:776ad783fe07 r12947:776ad783fe07 r23607:36c15679007d r23607:36c15679007d r23607:36c15679007d r12947:776ad783fe07 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file crashlog.cpp Implementation of generic function to be called to log a crash */
#include "stdafx.h"
#include "crashlog.h"
#include "survey.h"
#include "gamelog.h"
#include "map_func.h"
#include "music/music_driver.hpp"
#include "sound/sound_driver.hpp"
#include "video/video_driver.hpp"
#include "saveload/saveload.h"
#include "screenshot.h"
#include "network/network_survey.h"
#include "news_gui.h"
#include "fileio_func.h"
#include "fileio_type.h"
#include "company_func.h"
#include "3rdparty/fmt/chrono.h"
#include "3rdparty/fmt/std.h"
#include "core/format.hpp"
#include "safeguards.h"
/* static */ std::string CrashLog::message{};
/** The version of the schema of the JSON information. */
constexpr uint8_t CRASHLOG_SURVEY_VERSION = 1;
/**
* Writes the gamelog data to the buffer.
* @param output_iterator Iterator to write the output to.
*/
static void SurveyGamelog(nlohmann::json &json)
{
json = nlohmann::json::array();
_gamelog.Print([&json](const std::string &s) {
json.push_back(s);
});
}
/**
* Writes up to 32 recent news messages to the buffer, with the most recent first.
* @param output_iterator Iterator to write the output to.
*/
static void SurveyRecentNews(nlohmann::json &json)
{
json = nlohmann::json::array();
int i = 0;
for (NewsItem *news = _latest_news; i < 32 && news != nullptr; news = news->prev, i++) {
TimerGameCalendar::YearMonthDay ymd;
TimerGameCalendar::ConvertDateToYMD(news->date, &ymd);
json.push_back(fmt::format("({}-{:02}-{:02}) StringID: {}, Type: {}, Ref1: {}, {}, Ref2: {}, {}",
ymd.year, ymd.month + 1, ymd.day, news->string_id, news->type,
news->reftype1, news->ref1, news->reftype2, news->ref2));
}
}
/**
* Create a timestamped filename.
* @param ext The extension for the filename.
* @param with_dir Whether to prepend the filename with the personal directory.
* @return The filename
*/
std::string CrashLog::CreateFileName(const char *ext, bool with_dir) const
{
static std::string crashname;
if (crashname.empty()) {
crashname = fmt::format("crash{:%Y%m%d%H%M%S}", fmt::gmtime(time(nullptr)));
}
return fmt::format("{}{}{}", with_dir ? _personal_dir : std::string{}, crashname, ext);
}
/**
* Fill the crash log buffer with all data of a crash log.
*/
void CrashLog::FillCrashLog()
{
/* Reminder: this JSON is read in an automated fashion.
* If any structural changes are applied, please bump the version. */
this->survey["schema"] = CRASHLOG_SURVEY_VERSION;
this->survey["date"] = fmt::format("{:%Y-%m-%d %H:%M:%S} (UTC)", fmt::gmtime(time(nullptr)));
/* If no internal reason was logged, it must be a crash. */
if (CrashLog::message.empty()) {
this->SurveyCrash(this->survey["crash"]);
} else {
this->survey["crash"]["reason"] = CrashLog::message;
CrashLog::message.clear();
}
if (!this->TryExecute("stacktrace", [this]() { this->SurveyStacktrace(this->survey["stacktrace"]); return true; })) {
this->survey["stacktrace"] = "crashed while gathering information";
}
{
auto &info = this->survey["info"];
if (!this->TryExecute("os", [&info]() { SurveyOS(info["os"]); return true; })) {
info["os"] = "crashed while gathering information";
}
if (!this->TryExecute("openttd", [&info]() { SurveyOpenTTD(info["openttd"]); return true; })) {
info["openttd"] = "crashed while gathering information";
}
if (!this->TryExecute("configuration", [&info]() { SurveyConfiguration(info["configuration"]); return true; })) {
info["configuration"] = "crashed while gathering information";
}
if (!this->TryExecute("font", [&info]() { SurveyFont(info["font"]); return true; })) {
info["font"] = "crashed while gathering information";
}
if (!this->TryExecute("compiler", [&info]() { SurveyCompiler(info["compiler"]); return true; })) {
info["compiler"] = "crashed while gathering information";
}
if (!this->TryExecute("libraries", [&info]() { SurveyLibraries(info["libraries"]); return true; })) {
info["libraries"] = "crashed while gathering information";
}
}
{
auto &game = this->survey["game"];
game["local_company"] = _local_company;
game["current_company"] = _current_company;
if (!this->TryExecute("timers", [&game]() { SurveyTimers(game["timers"]); return true; })) {
game["libraries"] = "crashed while gathering information";
}
if (!this->TryExecute("companies", [&game]() { SurveyCompanies(game["companies"]); return true; })) {
game["companies"] = "crashed while gathering information";
}
if (!this->TryExecute("settings", [&game]() { SurveySettings(game["settings_changed"], true); return true; })) {
game["settings"] = "crashed while gathering information";
}
if (!this->TryExecute("grfs", [&game]() { SurveyGrfs(game["grfs"]); return true; })) {
game["grfs"] = "crashed while gathering information";
}
if (!this->TryExecute("game_script", [&game]() { SurveyGameScript(game["game_script"]); return true; })) {
game["game_script"] = "crashed while gathering information";
}
if (!this->TryExecute("gamelog", [&game]() { SurveyGamelog(game["gamelog"]); return true; })) {
game["gamelog"] = "crashed while gathering information";
}
if (!this->TryExecute("news", [&game]() { SurveyRecentNews(game["news"]); return true; })) {
game["news"] = "crashed while gathering information";
}
}
}
void CrashLog::PrintCrashLog() const
{
fmt::print(" OpenTTD version:\n");
fmt::print(" Version: {}\n", this->survey["info"]["openttd"]["version"]["revision"].get<std::string>());
fmt::print(" Hash: {}\n", this->survey["info"]["openttd"]["version"]["hash"].get<std::string>());
fmt::print(" NewGRF ver: {}\n", this->survey["info"]["openttd"]["version"]["newgrf"].get<std::string>());
fmt::print(" Content ver: {}\n", this->survey["info"]["openttd"]["version"]["content"].get<std::string>());
fmt::print("\n");
fmt::print(" Crash:\n");
fmt::print(" Reason: {}\n", this->survey["crash"]["reason"].get<std::string>());
fmt::print("\n");
fmt::print(" Stacktrace:\n");
for (const auto &line : this->survey["stacktrace"]) {
fmt::print(" {}\n", line.get<std::string>());
}
fmt::print("\n");
}
/**
* Write the crash log to a file.
* @note The filename will be written to \c crashlog_filename.
* @return true when the crash log was successfully written.
*/
bool CrashLog::WriteCrashLog()
{
this->crashlog_filename = this->CreateFileName(".json.log");
FILE *file = FioFOpenFile(this->crashlog_filename, "w", NO_DIRECTORY);
if (file == nullptr) return false;
std::string survey_json = this->survey.dump(4);
size_t len = survey_json.size();
size_t written = fwrite(survey_json.data(), 1, len, file);
FioFCloseFile(file);
return len == written;
}
/**
* Write the (crash) dump to a file.
*
* @note Sets \c crashdump_filename when there is a successful return.
* @return True iff the crashdump was successfully created.
*/
/* virtual */ bool CrashLog::WriteCrashDump()
{
fmt::print("No method to create a crash.dmp available.\n");
return false;
}
/**
* Write the (crash) savegame to a file.
* @note The filename will be written to \c savegame_filename.
* @return true when the crash save was successfully made.
*/
bool CrashLog::WriteSavegame()
{
/* If the map doesn't exist, saving will fail too. If the map got
* initialised, there is a big chance the rest is initialised too. */
if (!Map::IsInitialized()) return false;
try {
_gamelog.Emergency();
this->savegame_filename = this->CreateFileName(".sav");
/* Don't do a threaded saveload. */
return SaveOrLoad(this->savegame_filename, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK;
} catch (...) {
return false;
}
}
/**
* Write the (crash) screenshot to a file.
* @note The filename will be written to \c screenshot_filename.
* @return std::nullopt when the crash screenshot could not be made, otherwise the filename.
*/
bool CrashLog::WriteScreenshot()
{
/* Don't draw when we have invalid screen size */
if (_screen.width < 1 || _screen.height < 1 || _screen.dst_ptr == nullptr) return false;
std::string filename = this->CreateFileName("", false);
bool res = MakeScreenshot(SC_CRASHLOG, filename);
if (res) this->screenshot_filename = _full_screenshot_path;
return res;
}
/**
* Send the survey result, noting it was a crash.
*/
void CrashLog::SendSurvey() const
{
if (_game_mode == GM_NORMAL) {
_survey.Transmit(NetworkSurveyHandler::Reason::CRASH, true);
}
}
/**
* Makes the crash log, writes it to a file and then subsequently tries
* to make a crash dump and crash savegame. It uses DEBUG to write
* information like paths to the console.
*/
void CrashLog::MakeCrashLog()
{
/* Don't keep looping logging crashes. */
static bool crashlogged = false;
if (crashlogged) return;
crashlogged = true;
fmt::print("Crash encountered, generating crash log...\n");
this->FillCrashLog();
fmt::print("Crash log generated.\n\n");
fmt::print("Crash in summary:\n");
this->TryExecute("crashlog", [this]() { this->PrintCrashLog(); return true; });
fmt::print("Writing crash log to disk...\n");
bool ret = this->TryExecute("crashlog", [this]() { return this->WriteCrashLog(); });
if (ret) {
fmt::print("Crash log written to {}. Please add this file to any bug reports.\n\n", this->crashlog_filename);
} else {
fmt::print("Writing crash log failed. Please attach the output above to any bug reports.\n\n");
this->crashlog_filename = "(failed to write crash log)";
}
fmt::print("Writing crash dump to disk...\n");
ret = this->TryExecute("crashdump", [this]() { return this->WriteCrashDump(); });
if (ret) {
fmt::print("Crash dump written to {}. Please add this file to any bug reports.\n\n", this->crashdump_filename);
} else {
fmt::print("Writing crash dump failed.\n\n");
this->crashdump_filename = "(failed to write crash dump)";
}
fmt::print("Writing crash savegame...\n");
ret = this->TryExecute("savegame", [this]() { return this->WriteSavegame(); });
if (ret) {
fmt::print("Crash savegame written to {}. Please add this file and the last (auto)save to any bug reports.\n\n", this->savegame_filename);
} else {
fmt::print("Writing crash savegame failed. Please attach the last (auto)save to any bug reports.\n\n");
this->savegame_filename = "(failed to write crash savegame)";
}
fmt::print("Writing crash screenshot...\n");
ret = this->TryExecute("screenshot", [this]() { return this->WriteScreenshot(); });
if (ret) {
fmt::print("Crash screenshot written to {}. Please add this file to any bug reports.\n\n", this->screenshot_filename);
} else {
fmt::print("Writing crash screenshot failed.\n\n");
this->screenshot_filename = "(failed to write crash screenshot)";
}
this->TryExecute("survey", [this]() { this->SendSurvey(); return true; });
}
/**
* Sets a message for the error message handler.
* @param message The error message of the error.
*/
/* static */ void CrashLog::SetErrorMessage(const std::string &message)
{
CrashLog::message = message;
}
/**
* Try to close the sound/video stuff so it doesn't keep lingering around
* incorrect video states or so, e.g. keeping dpmi disabled.
*/
/* static */ void CrashLog::AfterCrashLogCleanup()
{
if (MusicDriver::GetInstance() != nullptr) MusicDriver::GetInstance()->Stop();
if (SoundDriver::GetInstance() != nullptr) SoundDriver::GetInstance()->Stop();
if (VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop();
}
|