Skip to content

Commit

Permalink
Revert "Add a mutex to the logger so multiple threads can safely log …
Browse files Browse the repository at this point in the history
…messages (#65)"

This reverts commit fce8f82.
  • Loading branch information
emd4600 committed Oct 24, 2024
1 parent fce8f82 commit 05b7e3b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
5 changes: 0 additions & 5 deletions Spore ModAPI/SourceCode/DLL/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ namespace ModAPI
eastl::fixed_vector<InitFunction, MAX_MODS> disposeFunctions;
eastl::fixed_map<uint32_t, ISimulatorStrategyPtr, MAX_MODS> simulatorStrategies;
FileStreamPtr logFile{};
std::mutex logFileMutex;
__time64_t logFileStartTime;

uint32_t CRC_TABLE[256];
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions Spore ModAPI/SourceCode/DLL/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <Spore\Cheats.h>
#include <Spore\IO.h>
#include <ctime>
#include <mutex>

virtual_detour(ShaderFragments_detour, Graphics::cMaterialManager, Graphics::IMaterialManager, bool(Resource::Database*)) {};

Expand All @@ -29,7 +28,6 @@ namespace ModAPI

extern FileStreamPtr logFile;
extern __time64_t logFileStartTime;
extern std::mutex logFileMutex;

long AttachDetour();
void DetachDetour();
Expand Down
20 changes: 8 additions & 12 deletions Spore ModAPI/SourceCode/DLL/DllModAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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) {
Expand Down

0 comments on commit 05b7e3b

Please sign in to comment.