127 lines
3.9 KiB
C++
127 lines
3.9 KiB
C++
#include "MetaCoreEditor/MetaCoreEditorApp.h"
|
|
|
|
#include <csignal>
|
|
#include <cstdio>
|
|
#include <crtdbg.h>
|
|
#include <cstdlib>
|
|
#include <exception>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <new>
|
|
|
|
namespace {
|
|
|
|
void MetaCoreWriteCrashLog(const std::string& message) {
|
|
std::ofstream stream("editor.crash.log", std::ios::app);
|
|
if (!stream.is_open()) {
|
|
return;
|
|
}
|
|
stream << message << '\n';
|
|
}
|
|
|
|
void MetaCoreWriteCrashLogRaw(const char* message) {
|
|
if (message == nullptr) {
|
|
return;
|
|
}
|
|
FILE* file = nullptr;
|
|
if (fopen_s(&file, "editor.crash.log", "a") == 0 && file != nullptr) {
|
|
std::fputs(message, file);
|
|
std::fputs("\n", file);
|
|
std::fclose(file);
|
|
}
|
|
}
|
|
|
|
void MetaCoreInvalidParameterHandler(
|
|
const wchar_t* expression,
|
|
const wchar_t* functionName,
|
|
const wchar_t* fileName,
|
|
unsigned int line,
|
|
uintptr_t reserved
|
|
) {
|
|
(void)reserved;
|
|
MetaCoreWriteCrashLog("invalid_parameter triggered");
|
|
if (expression != nullptr) {
|
|
MetaCoreWriteCrashLog("expression: <non-null>");
|
|
}
|
|
if (functionName != nullptr) {
|
|
MetaCoreWriteCrashLog("function: <non-null>");
|
|
}
|
|
if (fileName != nullptr) {
|
|
MetaCoreWriteCrashLog("file: <non-null>");
|
|
}
|
|
MetaCoreWriteCrashLog("line: " + std::to_string(line));
|
|
}
|
|
|
|
void MetaCorePureCallHandler() {
|
|
MetaCoreWriteCrashLog("purecall handler triggered");
|
|
}
|
|
|
|
void MetaCoreSignalHandler(int signalValue) {
|
|
MetaCoreWriteCrashLog("signal: " + std::to_string(signalValue));
|
|
}
|
|
|
|
void MetaCoreTerminateHandler() {
|
|
MetaCoreWriteCrashLog("std::terminate triggered");
|
|
if (std::exception_ptr exceptionPtr = std::current_exception(); exceptionPtr != nullptr) {
|
|
try {
|
|
std::rethrow_exception(exceptionPtr);
|
|
} catch (const std::exception& exceptionObject) {
|
|
MetaCoreWriteCrashLog(std::string("exception: ") + exceptionObject.what());
|
|
} catch (...) {
|
|
MetaCoreWriteCrashLog("exception: <non-std exception>");
|
|
}
|
|
} else {
|
|
MetaCoreWriteCrashLog("exception: <none>");
|
|
}
|
|
std::_Exit(3);
|
|
}
|
|
|
|
void MetaCoreInstallDebugCrashHooks() {
|
|
#if defined(_DEBUG)
|
|
_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
|
|
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
|
|
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
|
|
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
|
|
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
|
|
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
|
|
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
|
|
|
|
_set_invalid_parameter_handler(&MetaCoreInvalidParameterHandler);
|
|
_set_purecall_handler(&MetaCorePureCallHandler);
|
|
std::set_terminate(&MetaCoreTerminateHandler);
|
|
std::signal(SIGABRT, &MetaCoreSignalHandler);
|
|
MetaCoreWriteCrashLog("debug crash hooks installed");
|
|
#endif
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char* argv[]) {
|
|
MetaCoreInstallDebugCrashHooks();
|
|
MetaCoreWriteCrashLogRaw("main: hooks installed");
|
|
if (argc > 0) {
|
|
MetaCoreWriteCrashLogRaw("main: set current_path begin");
|
|
std::filesystem::current_path(std::filesystem::path(argv[0]).parent_path());
|
|
MetaCoreWriteCrashLogRaw("main: set current_path done");
|
|
}
|
|
|
|
MetaCoreWriteCrashLogRaw("main: construct app begin");
|
|
MetaCore::MetaCoreEditorApp editorApp;
|
|
MetaCoreWriteCrashLogRaw("main: construct app done");
|
|
MetaCoreWriteCrashLogRaw("main: initialize begin");
|
|
if (!editorApp.Initialize()) {
|
|
MetaCoreWriteCrashLogRaw("main: initialize failed");
|
|
std::cerr << "MetaCoreEditorApp initialize failed\n";
|
|
return 1;
|
|
}
|
|
MetaCoreWriteCrashLogRaw("main: initialize done");
|
|
|
|
MetaCoreWriteCrashLogRaw("main: run begin");
|
|
const int exitCode = editorApp.Run();
|
|
MetaCoreWriteCrashLogRaw("main: run done");
|
|
editorApp.Shutdown();
|
|
MetaCoreWriteCrashLogRaw("main: shutdown done");
|
|
return exitCode;
|
|
}
|