feat: add exception handler
This commit is contained in:
parent
2666805339
commit
10e3a7b711
@ -6,6 +6,7 @@ file(GLOB src
|
||||
"src/*/*.cpp"
|
||||
"src/*/*/*.cpp"
|
||||
"src/*/*/*/*.cpp"
|
||||
"src/*/*/*/*/*.cpp"
|
||||
)
|
||||
|
||||
if (WIN32)
|
||||
|
38
src/helper/exceptions/crashhandler.cpp
Normal file
38
src/helper/exceptions/crashhandler.cpp
Normal 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();
|
||||
}
|
15
src/helper/exceptions/crashhandler.hpp
Normal file
15
src/helper/exceptions/crashhandler.hpp
Normal 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();
|
||||
};
|
35
src/helper/exceptions/linux/linux.cpp
Normal file
35
src/helper/exceptions/linux/linux.cpp
Normal 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
|
9
src/helper/exceptions/windows/windows.cpp
Normal file
9
src/helper/exceptions/windows/windows.cpp
Normal 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
|
12
src/main.cpp
12
src/main.cpp
@ -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__)
|
||||
|
Loading…
x
Reference in New Issue
Block a user