Flushing logs when received an exception(for windows only) or signal
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "IO/AppFolders.hpp"
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <csignal>
|
||||
#include <spdlog/sinks/rotating_file_sink.h>
|
||||
#include <spdlog/sinks/null_sink.h>
|
||||
#ifndef NO_CONSOLE_LOG
|
||||
@@ -17,6 +18,10 @@
|
||||
#ifdef _MSC_VER
|
||||
#include <spdlog/sinks/msvc_sink.h>
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
# include <dbghelp.h>
|
||||
#endif
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
@@ -39,6 +44,43 @@ namespace OpenVulkano
|
||||
Logger::Ptr Logger::PERF = nullptr;
|
||||
Logger::Ptr Logger::APP = nullptr;
|
||||
|
||||
void signalHandler(int sigCode)
|
||||
{
|
||||
spdlog::error("Received signal {}", sigCode);
|
||||
spdlog::shutdown();
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
LONG WINAPI win32ExceptionHandler(EXCEPTION_POINTERS *exceptionPointers)
|
||||
{
|
||||
// NOTE(vb): We can report some of the exceptionPointers fields to the user... But this is for windows only
|
||||
spdlog::error("An exception occured");
|
||||
spdlog::shutdown();
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
BOOL WINAPI handlerRoutine(DWORD dwCtrlType)
|
||||
{
|
||||
// spdlog::error("Received CTRL-C, exiting...");
|
||||
spdlog::shutdown();
|
||||
return FALSE;
|
||||
}
|
||||
#endif
|
||||
|
||||
void setupSignalHandlers()
|
||||
{
|
||||
std::signal(SIGSEGV, signalHandler);
|
||||
std::signal(SIGABRT, signalHandler);
|
||||
std::signal(SIGFPE, signalHandler);
|
||||
std::signal(SIGILL, signalHandler);
|
||||
std::signal(SIGINT, signalHandler);
|
||||
std::signal(SIGTERM, signalHandler);
|
||||
#ifdef _WIN32
|
||||
SetConsoleCtrlHandler(handlerRoutine, TRUE);
|
||||
SetUnhandledExceptionFilter(win32ExceptionHandler);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Logger::SetupLogger(std::string logFolder, const std::string& logFile)
|
||||
{
|
||||
if (logFolder.empty()) logFolder = (AppFolders::GetAppDataHomeDir() / "logs").string();
|
||||
@@ -77,6 +119,8 @@ namespace OpenVulkano
|
||||
|
||||
spdlog::flush_every(std::chrono::seconds(5));
|
||||
|
||||
setupSignalHandlers();
|
||||
|
||||
MANAGER->info("Logger initialized");
|
||||
}
|
||||
catch (const spdlog::spdlog_ex& e)
|
||||
|
||||
Reference in New Issue
Block a user