diff --git a/port/include/system.h b/port/include/system.h index 005dbaf48..e6b75ac3d 100644 --- a/port/include/system.h +++ b/port/include/system.h @@ -24,6 +24,7 @@ u64 sysGetMicroseconds(void); void sysFatalError(const char *fmt, ...) __attribute__((noreturn)); +void sysLogStartCrash(void); void sysLogPrintf(s32 level, const char *fmt, ...); void sysGetExecutablePath(char *outPath, const u32 outLen); diff --git a/port/src/crash.c b/port/src/crash.c index 77a8228ed..1ea2ea557 100644 --- a/port/src/crash.c +++ b/port/src/crash.c @@ -128,6 +128,9 @@ static long __stdcall crashHandler(PEXCEPTION_POINTERS exinfo) return EXCEPTION_CONTINUE_EXECUTION; } + // start writing to a crash log file + sysLogStartCrash(); + sysLogPrintf(LOG_ERROR, "FATAL: Crashed: PC=%p CODE=0x%08lx", exinfo->ExceptionRecord->ExceptionAddress, exinfo->ExceptionRecord->ExceptionCode); fflush(stderr); diff --git a/port/src/system.c b/port/src/system.c index 5261f05cd..fb24addd0 100644 --- a/port/src/system.c +++ b/port/src/system.c @@ -11,6 +11,7 @@ #include "system.h" #define LOG_FNAME "pd.log" +#define CRASHLOG_FNAME "pd.crash.log" #define USEC_IN_SEC 1000000ULL static u64 startTick = 0; @@ -19,6 +20,24 @@ static char logPath[2048]; static s32 sysArgc; static const char **sysArgv; +static inline void sysLogSetPath(const char *fname) +{ + // figure out where the log is and clear it + // try working dir first + snprintf(logPath, sizeof(logPath), "./%s", fname); + FILE *f = fopen(logPath, "wb"); + if (!f) { + // try home dir + sysGetHomePath(logPath, sizeof(logPath) - 1); + strncat(logPath, "/", sizeof(logPath) - 1); + strncat(logPath, fname, sizeof(logPath) - 1); + f = fopen(logPath, "wb"); + } + if (f) { + fclose(f); + } +} + void sysInitArgs(s32 argc, const char **argv) { sysArgc = argc; @@ -30,19 +49,7 @@ void sysInit(void) startTick = sysGetMicroseconds(); if (sysArgCheck("--log")) { - // figure out where the log is and clear it - // try working dir first - snprintf(logPath, sizeof(logPath), "./%s", LOG_FNAME); - FILE *f = fopen(logPath, "wb"); - if (!f) { - // try home dir - sysGetHomePath(logPath, sizeof(logPath) - 1); - strncat(logPath, "/" LOG_FNAME, sizeof(logPath) - 1); - f = fopen(logPath, "wb"); - } - if (f) { - fclose(f); - } + sysLogSetPath(LOG_FNAME); } #ifdef VERSION_HASH @@ -96,6 +103,13 @@ u64 sysGetMicroseconds(void) return ((u64)tv.tv_sec * USEC_IN_SEC + (u64)tv.tv_usec) - startTick; } +void sysLogStartCrash(void) +{ + if (!logPath[0]) { + sysLogSetPath(CRASHLOG_FNAME); + } +} + void sysLogPrintf(s32 level, const char *fmt, ...) { static const char *prefix[3] = {