Skip to content

Commit

Permalink
port: write crash info to log file even without --log
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Jan 7, 2024
1 parent c487e42 commit b1b17e2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions port/include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions port/src/crash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 27 additions & 13 deletions port/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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] = {
Expand Down

0 comments on commit b1b17e2

Please sign in to comment.