From 3e2ed0c7e561cf608e3bc49853493992ffe3a5cd Mon Sep 17 00:00:00 2001 From: Qi You <1425321705@qq.com> Date: Sat, 20 Mar 2021 12:04:18 +0800 Subject: [PATCH] fix potential problems --- CrashLogger.vcxproj.user | 4 ---- dllmain.cpp | 13 ++++++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) delete mode 100644 CrashLogger.vcxproj.user diff --git a/CrashLogger.vcxproj.user b/CrashLogger.vcxproj.user deleted file mode 100644 index 88a5509..0000000 --- a/CrashLogger.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/dllmain.cpp b/dllmain.cpp index 7b2db44..2bcad47 100644 --- a/dllmain.cpp +++ b/dllmain.cpp @@ -18,7 +18,7 @@ void log(const char* format, ...) va_start(args, format); vprintf(format, args); fflush(stdout); - if (fLog != NULL) + if (fLog != NULL && fLog != INVALID_HANDLE_VALUE) { vfprintf(fLog, format, args); fflush(fLog); @@ -31,6 +31,13 @@ LONG WINAPI CrashLogger(PEXCEPTION_POINTERS pe) log("[Crashed!]\n"); HANDLE hProcess = GetCurrentProcess(); HANDLE hThread = GetCurrentThread(); + CreateDirectory(L"logs", NULL); + errno_t res = fopen_s(&fLog, LOG_OUTPUT_PATH, "w"); + if (res != 0) + { + fLog = NULL; + log("[Warning] Fail to open log file! Error Code:%d\n",res); + } /// /// StackWalk @@ -93,7 +100,6 @@ BOOL APIENTRY DllMain( HMODULE hModule, { case DLL_PROCESS_ATTACH: SystemHandler = SetUnhandledExceptionFilter(CrashLogger); - fopen_s(&fLog, LOG_OUTPUT_PATH, "w"); printf("[CrashLogger] CrashLogger loaded.\n"); break; case DLL_THREAD_ATTACH: @@ -101,7 +107,8 @@ BOOL APIENTRY DllMain( HMODULE hModule, case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: - fclose(fLog); + if (fLog != NULL && fLog != INVALID_HANDLE_VALUE) + fclose(fLog); break; } return TRUE;