1
0
mirror of synced 2024-11-28 09:30:51 +01:00

impr: More style and comment fixes in crash handler

This commit is contained in:
WerWolv 2023-08-25 23:19:13 +02:00
parent 6ef96c5533
commit bd75b70d85

View File

@ -37,7 +37,8 @@ namespace hex::crash {
// Function that decides what should happen on a crash // Function that decides what should happen on a crash
// (either sending a message or saving a crash file, depending on when the crash occurred) // (either sending a message or saving a crash file, depending on when the crash occurred)
static std::function<void(const std::string&)> crashCallback = sendNativeMessage; using CrashCallback = void (*) (const std::string&);
static CrashCallback crashCallback = sendNativeMessage;
static void saveCrashFile(const std::string& message) { static void saveCrashFile(const std::string& message) {
log::fatal(message); log::fatal(message);
@ -57,7 +58,8 @@ namespace hex::crash {
return; return;
} }
} }
log::warn("Could not write crash.json file !");
log::warn("Could not write crash.json file!");
} }
static void printStackTrace() { static void printStackTrace() {
@ -96,10 +98,13 @@ namespace hex::crash {
} }
void handleCrash(const std::string &msg) { void handleCrash(const std::string &msg) {
// Call the crash callback
crashCallback(msg); crashCallback(msg);
// Print the stacktrace to the console or log file
printStackTrace(); printStackTrace();
// Flush all streams
fflush(stdout); fflush(stdout);
fflush(stderr); fflush(stderr);
} }
@ -126,6 +131,7 @@ namespace hex::crash {
handleCrash("Uncaught exception!"); handleCrash("Uncaught exception!");
// Print the current exception info
try { try {
std::rethrow_exception(std::current_exception()); std::rethrow_exception(std::current_exception());
} catch (std::exception &ex) { } catch (std::exception &ex) {
@ -146,10 +152,9 @@ namespace hex::crash {
signalHandler(signalNumber, #name); \ signalHandler(signalNumber, #name); \
}) })
HANDLE_SIGNAL(SIGSEGV); for (auto signal : Signals) {
HANDLE_SIGNAL(SIGILL); HANDLE_SIGNAL(signal);
HANDLE_SIGNAL(SIGABRT); }
HANDLE_SIGNAL(SIGFPE);
#undef HANDLE_SIGNAL #undef HANDLE_SIGNAL
} }
@ -164,10 +169,12 @@ namespace hex::crash {
// Only do it when ImHex has finished its loading // Only do it when ImHex has finished its loading
EventManager::subscribe<EventImHexStartupFinished>([] { EventManager::subscribe<EventImHexStartupFinished>([] {
EventManager::subscribe<EventAbnormalTermination>([](int) { EventManager::subscribe<EventAbnormalTermination>([](int) {
// Save ImGui settings
auto imguiSettingsPath = hex::getImGuiSettingsPath(); auto imguiSettingsPath = hex::getImGuiSettingsPath();
if (!imguiSettingsPath.empty()) if (!imguiSettingsPath.empty())
ImGui::SaveIniSettingsToDisk(wolv::util::toUTF8String(imguiSettingsPath).c_str()); ImGui::SaveIniSettingsToDisk(wolv::util::toUTF8String(imguiSettingsPath).c_str());
// Create crash backup if any providers are open
if (ImHexApi::Provider::isValid()) { if (ImHexApi::Provider::isValid()) {
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) { for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) {
if (ProjectFile::store(path / CrashBackupFileName, false)) if (ProjectFile::store(path / CrashBackupFileName, false))
@ -177,7 +184,7 @@ namespace hex::crash {
}); });
}); });
// change the crash callback when ImHex has finished startup // Change the crash callback when ImHex has finished startup
EventManager::subscribe<EventImHexStartupFinished>([]{ EventManager::subscribe<EventImHexStartupFinished>([]{
crashCallback = saveCrashFile; crashCallback = saveCrashFile;
}); });
@ -186,6 +193,7 @@ namespace hex::crash {
void resetCrashHandlers() { void resetCrashHandlers() {
std::set_terminate(nullptr); std::set_terminate(nullptr);
for(auto signal : Signals) std::signal(signal, SIG_DFL); for (auto signal : Signals)
std::signal(signal, SIG_DFL);
} }
} }