Skip to content

Commit fce8f82

Browse files
authored
Add a mutex to the logger so multiple threads can safely log messages (#65)
1 parent b4350c9 commit fce8f82

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

Spore ModAPI/SourceCode/DLL/Application.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace ModAPI
6464
eastl::fixed_vector<InitFunction, MAX_MODS> disposeFunctions;
6565
eastl::fixed_map<uint32_t, ISimulatorStrategyPtr, MAX_MODS> simulatorStrategies;
6666
FileStreamPtr logFile{};
67+
std::mutex logFileMutex;
6768
__time64_t logFileStartTime;
6869

6970
uint32_t CRC_TABLE[256];
@@ -197,15 +198,19 @@ void CreateLogFile() {
197198
log_file_name += u".txt";
198199
log_path.append(log_file_name);
199200

201+
ModAPI::logFileMutex.lock();
200202
ModAPI::logFile = new IO::FileStream(log_path.c_str());
201203
ModAPI::logFile->Open(IO::AccessFlags::Write, IO::CD::CreateAlways);
204+
ModAPI::logFileMutex.unlock();
202205
}
203206

204207
void CloseLogFile() {
208+
ModAPI::logFileMutex.lock();
205209
if (ModAPI::logFile) {
206210
ModAPI::logFile->Close();
207211
ModAPI::logFile.reset();
208212
}
213+
ModAPI::logFileMutex.unlock();
209214
}
210215

211216
int ModAPI::PreInit_detour::DETOUR(int arg_0, int arg_1)

Spore ModAPI/SourceCode/DLL/Application.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <Spore\Cheats.h>
1515
#include <Spore\IO.h>
1616
#include <ctime>
17+
#include <mutex>
1718

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

@@ -28,6 +29,7 @@ namespace ModAPI
2829

2930
extern FileStreamPtr logFile;
3031
extern __time64_t logFileStartTime;
32+
extern std::mutex logFileMutex;
3133

3234
long AttachDetour();
3335
void DetachDetour();

Spore ModAPI/SourceCode/DLL/DllModAPI.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ namespace ModAPI
3232
static constexpr unsigned int SCRATCH_SIZE = 4096;
3333
char logScratch[SCRATCH_SIZE];
3434

35-
void Log(const char* fmt, ...) {
36-
unsigned int offset = 0;
37-
35+
void Log(const char* fmt, ...) {
3836
__time64_t long_time;
3937
_time64(&long_time);
4038

@@ -47,25 +45,31 @@ namespace ModAPI
4745
const int mins = long_time % 60;
4846
long_time /= 60;
4947
const int hours = long_time % 24;
48+
49+
logFileMutex.lock();
5050

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

5454
va_list argList;
5555
va_start(argList, fmt);
56-
vsnprintf(logScratch + offset, SCRATCH_SIZE - offset, fmt, argList);
56+
vsnprintf(logScratch + time_offset, SCRATCH_SIZE - time_offset, fmt, argList);
5757
va_end(argList);
5858

5959
// vsnprintf does not guarantee a null terminator if the formatted string exceeds the buffer size
6060
logScratch[SCRATCH_SIZE - 1] = 0;
6161

62+
auto log_len = strlen(logScratch);
63+
6264
if (logFile)
6365
{
64-
logFile->Write(logScratch, strlen(logScratch));
66+
logFile->Write(logScratch, log_len);
6567
logFile->Write("\n", 1);
6668
logFile->Flush();
6769
}
68-
App::ConsolePrintF(logScratch + offset);
70+
App::ConsolePrintF(logScratch + time_offset);
71+
memset(logScratch, 0, log_len);
72+
logFileMutex.unlock();
6973
}
7074

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

0 commit comments

Comments
 (0)