From 05b7e3b4e032629fcad55f927ff92b357daf9c1d Mon Sep 17 00:00:00 2001 From: emd4600 Date: Thu, 24 Oct 2024 22:19:27 +0200 Subject: [PATCH] Revert "Add a mutex to the logger so multiple threads can safely log messages (#65)" This reverts commit fce8f826c7fe25bc966373bdca6d2b35d6da8ee2. --- Spore ModAPI/SourceCode/DLL/Application.cpp | 5 ----- Spore ModAPI/SourceCode/DLL/Application.h | 2 -- Spore ModAPI/SourceCode/DLL/DllModAPI.cpp | 20 ++++++++------------ 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/Spore ModAPI/SourceCode/DLL/Application.cpp b/Spore ModAPI/SourceCode/DLL/Application.cpp index 881b0501..f6926576 100644 --- a/Spore ModAPI/SourceCode/DLL/Application.cpp +++ b/Spore ModAPI/SourceCode/DLL/Application.cpp @@ -64,7 +64,6 @@ namespace ModAPI eastl::fixed_vector disposeFunctions; eastl::fixed_map simulatorStrategies; FileStreamPtr logFile{}; - std::mutex logFileMutex; __time64_t logFileStartTime; uint32_t CRC_TABLE[256]; @@ -198,19 +197,15 @@ void CreateLogFile() { log_file_name += u".txt"; log_path.append(log_file_name); - ModAPI::logFileMutex.lock(); ModAPI::logFile = new IO::FileStream(log_path.c_str()); ModAPI::logFile->Open(IO::AccessFlags::Write, IO::CD::CreateAlways); - ModAPI::logFileMutex.unlock(); } void CloseLogFile() { - ModAPI::logFileMutex.lock(); if (ModAPI::logFile) { ModAPI::logFile->Close(); ModAPI::logFile.reset(); } - ModAPI::logFileMutex.unlock(); } int ModAPI::PreInit_detour::DETOUR(int arg_0, int arg_1) diff --git a/Spore ModAPI/SourceCode/DLL/Application.h b/Spore ModAPI/SourceCode/DLL/Application.h index 5f1dafe9..f7584c6e 100644 --- a/Spore ModAPI/SourceCode/DLL/Application.h +++ b/Spore ModAPI/SourceCode/DLL/Application.h @@ -14,7 +14,6 @@ #include #include #include -#include virtual_detour(ShaderFragments_detour, Graphics::cMaterialManager, Graphics::IMaterialManager, bool(Resource::Database*)) {}; @@ -29,7 +28,6 @@ namespace ModAPI extern FileStreamPtr logFile; extern __time64_t logFileStartTime; - extern std::mutex logFileMutex; long AttachDetour(); void DetachDetour(); diff --git a/Spore ModAPI/SourceCode/DLL/DllModAPI.cpp b/Spore ModAPI/SourceCode/DLL/DllModAPI.cpp index f43b8f36..d909e12c 100644 --- a/Spore ModAPI/SourceCode/DLL/DllModAPI.cpp +++ b/Spore ModAPI/SourceCode/DLL/DllModAPI.cpp @@ -32,7 +32,9 @@ namespace ModAPI static constexpr unsigned int SCRATCH_SIZE = 4096; char logScratch[SCRATCH_SIZE]; - void Log(const char* fmt, ...) { + void Log(const char* fmt, ...) { + unsigned int offset = 0; + __time64_t long_time; _time64(&long_time); @@ -45,31 +47,25 @@ namespace ModAPI const int mins = long_time % 60; long_time /= 60; const int hours = long_time % 24; - - logFileMutex.lock(); - sprintf_s(logScratch, SCRATCH_SIZE, format, hours,mins,secs); - unsigned int time_offset = (sizeof(formatted)-1)/sizeof(formatted[0]); + sprintf_s(logScratch + offset, SCRATCH_SIZE - offset, format, hours,mins,secs); + offset += (sizeof(formatted)-1)/sizeof(formatted[0]); va_list argList; va_start(argList, fmt); - vsnprintf(logScratch + time_offset, SCRATCH_SIZE - time_offset, fmt, argList); + vsnprintf(logScratch + offset, SCRATCH_SIZE - offset, fmt, argList); va_end(argList); // vsnprintf does not guarantee a null terminator if the formatted string exceeds the buffer size logScratch[SCRATCH_SIZE - 1] = 0; - auto log_len = strlen(logScratch); - if (logFile) { - logFile->Write(logScratch, log_len); + logFile->Write(logScratch, strlen(logScratch)); logFile->Write("\n", 1); logFile->Flush(); } - App::ConsolePrintF(logScratch + time_offset); - memset(logScratch, 0, log_len); - logFileMutex.unlock(); + App::ConsolePrintF(logScratch + offset); } bool AddSimulatorStrategy(Simulator::ISimulatorStrategy* strategy, uint32_t id) {