refactor/logger-rework #2

Merged
illyum merged 3 commits from refactor/logger-rework into master 2024-09-14 02:42:04 -06:00
3 changed files with 93 additions and 0 deletions
Showing only changes of commit 0fa35696bb - Show all commits

8
src/FatalHandler.h Normal file
View File

@ -0,0 +1,8 @@
//
// Created by illyum on 9/13/2024.
//
#ifndef FATALHANDLER_H
#define FATALHANDLER_H
#endif //FATALHANDLER_H

View File

@ -0,0 +1,29 @@
//
// Created by illyum on 9/13/2024.
//
#include "FatalHandler.h"
#include <execinfo.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
void FatalHandler(const std::string& message) {
std::ofstream crashFile("crash.log", std::ios::app);
crashFile << "FATAL ERROR: " << message << std::endl;
void* callstack[128];
int frames = backtrace(callstack, 128);
char** symbols = backtrace_symbols(callstack, frames);
crashFile << "Stack trace:\n";
for (int i = 0; i < frames; ++i) {
crashFile << symbols[i] << std::endl;
}
free(symbols);
crashFile.close();
std::abort();
}

View File

@ -0,0 +1,56 @@
//
// Created by illyum on 9/13/2024.
//
#include "FatalHandler.h"
#include <windows.h>
#include <dbghelp.h>
#include <fstream>
#include <sstream>
#pragma comment(lib, "dbghelp.lib")
void FatalHandler(const std::string& message) {
std::ofstream crashFile("crash.log", std::ios::app);
crashFile << "FATAL ERROR: " << message << std::endl;
HANDLE process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);
CONTEXT context;
RtlCaptureContext(&context);
STACKFRAME64 stackFrame = {};
stackFrame.AddrPC.Offset = context.Rip;
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Offset = context.Rbp;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Offset = context.Rsp;
stackFrame.AddrStack.Mode = AddrModeFlat;
crashFile << "Stack trace:\n";
while (StackWalk64(IMAGE_FILE_MACHINE_AMD64, process, GetCurrentThread(), &stackFrame, &context, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL)) {
DWORD64 address = stackFrame.AddrPC.Offset;
if (address == 0) {
break;
}
char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;
DWORD64 displacement = 0;
if (SymFromAddr(process, address, &displacement, symbol)) {
crashFile << symbol->Name << " - 0x" << std::hex << symbol->Address << std::dec << "\n";
} else {
crashFile << "Unresolved symbol at address: 0x" << std::hex << address << std::dec << "\n";
}
}
SymCleanup(process);
crashFile.close();
std::abort();
}