feat: add exception handler

This commit is contained in:
Curve 2021-03-01 21:47:44 +01:00
parent 2666805339
commit 10e3a7b711
No known key found for this signature in database
GPG Key ID: 460F6C466BD35813
6 changed files with 109 additions and 1 deletions

View File

@ -6,6 +6,7 @@ file(GLOB src
"src/*/*.cpp"
"src/*/*/*.cpp"
"src/*/*/*/*.cpp"
"src/*/*/*/*/*.cpp"
)
if (WIN32)

View File

@ -0,0 +1,38 @@
#include "crashhandler.hpp"
#include <exception>
#include <fancy.hpp>
#if defined(__linux__)
#include <csignal>
#endif
void CrashHandler::init()
{
std::set_terminate(terminateCallback);
#if defined(__linux__)
signal(SIGSEGV, signalHandler);
signal(SIGABRT, signalHandler);
signal(SIGFPE, signalHandler);
#endif
}
void CrashHandler::terminateCallback()
{
Fancy::fancy.logTime().failure() << "An exception crashed the program" << std::endl;
try
{
std::rethrow_exception(std::current_exception());
}
catch (const std::exception &exception)
{
Fancy::fancy.logTime().failure() << "Exception: " >> exception.what() << std::endl;
Fancy::fancy.logTime().failure() << "Exception Type: " >> typeid(exception).name() << std::endl;
}
catch (...)
{
Fancy::fancy.logTime().failure() << "Exception: " >> "unknown" << std::endl;
}
backtrace();
}

View File

@ -0,0 +1,15 @@
#pragma once
class CrashHandler
{
private:
static void terminateCallback();
static void backtrace();
#if defined(__linux__)
static void signalHandler(int);
#endif
public:
static void init();
};

View File

@ -0,0 +1,35 @@
#if defined(__linux__)
#include "../crashhandler.hpp"
#include <csignal>
#include <execinfo.h>
#include <fancy.hpp>
void CrashHandler::backtrace()
{
Fancy::fancy.logTime().success() << "Backtrace available!" << std::endl;
void *elements[20];
auto size = ::backtrace(elements, 20);
auto *stack = backtrace_symbols(elements, size);
for (int i = 0; size > i; i++)
{
Fancy::fancy.logTime() << stack[i] << std::endl;
}
free(stack);
}
void CrashHandler::signalHandler(int signal)
{
if (signal == SIGFPE)
{
Fancy::fancy.logTime().warning() << "This crash is probably related to a bad pulseaudio config!" << std::endl;
}
Fancy::fancy.logTime().failure() << "Received Signal: " << signal << std::endl;
backtrace();
exit(1); // NOLINT
}
#endif

View File

@ -0,0 +1,9 @@
#if defined(_WIN32)
#include "../crashhandler.hpp"
#include <fancy.hpp>
void CrashHandler::backtrace()
{
Fancy::fancy.logTime().failure() << "Backtrace is not available on Windows" << std::endl;
}
#endif

View File

@ -1,4 +1,5 @@
#include "core/global/globals.hpp"
#include "helper/exceptions/crashhandler.hpp"
#include "ui/impl/webview/webview.hpp"
#include <InstanceGuard.hpp>
#include <fancy.hpp>
@ -9,11 +10,20 @@ int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
int main()
#endif
{
#if defined(_WIN32)
DWORD lMode;
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(hStdout, &lMode);
SetConsoleMode(hStdout, lMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN);
#endif
CrashHandler::init();
InstanceGuard::InstanceGuard guard("soundux-guard");
if (guard.IsAnotherInstanceRunning())
{
Fancy::fancy.logTime().failure() << "Another Instance is already running!" << std::endl;
std::terminate();
return 1;
}
#if defined(__linux__)