Skip to content

Commit

Permalink
Add a mutex to the logger so multiple threads can safely log messages (
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarklord authored Oct 24, 2024
1 parent b4350c9 commit fce8f82
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Spore ModAPI/SourceCode/DLL/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ 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 @@ -197,15 +198,19 @@ 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: 2 additions & 0 deletions Spore ModAPI/SourceCode/DLL/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#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 @@ -28,6 +29,7 @@ namespace ModAPI

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

long AttachDetour();
void DetachDetour();
Expand Down
20 changes: 12 additions & 8 deletions Spore ModAPI/SourceCode/DLL/DllModAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ namespace ModAPI
static constexpr unsigned int SCRATCH_SIZE = 4096;
char logScratch[SCRATCH_SIZE];

void Log(const char* fmt, ...) {
unsigned int offset = 0;

void Log(const char* fmt, ...) {
__time64_t long_time;
_time64(&long_time);

Expand All @@ -47,25 +45,31 @@ namespace ModAPI
const int mins = long_time % 60;
long_time /= 60;
const int hours = long_time % 24;

logFileMutex.lock();

sprintf_s(logScratch + offset, SCRATCH_SIZE - offset, format, hours,mins,secs);
offset += (sizeof(formatted)-1)/sizeof(formatted[0]);
sprintf_s(logScratch, SCRATCH_SIZE, format, hours,mins,secs);
unsigned int time_offset = (sizeof(formatted)-1)/sizeof(formatted[0]);

va_list argList;
va_start(argList, fmt);
vsnprintf(logScratch + offset, SCRATCH_SIZE - offset, fmt, argList);
vsnprintf(logScratch + time_offset, SCRATCH_SIZE - time_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, strlen(logScratch));
logFile->Write(logScratch, log_len);
logFile->Write("\n", 1);
logFile->Flush();
}
App::ConsolePrintF(logScratch + offset);
App::ConsolePrintF(logScratch + time_offset);
memset(logScratch, 0, log_len);
logFileMutex.unlock();
}

bool AddSimulatorStrategy(Simulator::ISimulatorStrategy* strategy, uint32_t id) {
Expand Down

0 comments on commit fce8f82

Please sign in to comment.