mirror of
https://github.com/glest/glest-source.git
synced 2025-08-17 21:51:17 +02:00
Added mutex guards around logging
This commit is contained in:
@@ -42,7 +42,7 @@ public:
|
||||
void signalQuit();
|
||||
bool getQuitStatus();
|
||||
bool getRunningStatus();
|
||||
static void shutdownAndWait(BaseThread *pThread);
|
||||
static void shutdownAndWait(BaseThread *ppThread);
|
||||
void shutdownAndWait();
|
||||
};
|
||||
|
||||
|
@@ -15,8 +15,10 @@
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include "thread.h"
|
||||
|
||||
using std::string;
|
||||
using namespace Shared::Platform;
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
|
||||
@@ -35,7 +37,7 @@ public:
|
||||
{
|
||||
protected:
|
||||
DebugType debugType;
|
||||
|
||||
|
||||
public:
|
||||
SystemFlagsType() {
|
||||
this->debugType = debugSystem;
|
||||
@@ -43,6 +45,7 @@ public:
|
||||
this->fileStream = NULL;
|
||||
this->debugLogFileName = "";
|
||||
this->fileStreamOwner = false;
|
||||
this->mutex = NULL;
|
||||
}
|
||||
SystemFlagsType(DebugType debugType) {
|
||||
this->debugType = debugType;
|
||||
@@ -50,6 +53,7 @@ public:
|
||||
this->fileStream = NULL;
|
||||
this->debugLogFileName = "";
|
||||
this->fileStreamOwner = false;
|
||||
this->mutex = NULL;
|
||||
}
|
||||
SystemFlagsType(DebugType debugType,bool enabled,
|
||||
std::ofstream *fileStream,std::string debugLogFileName) {
|
||||
@@ -58,12 +62,14 @@ public:
|
||||
this->fileStream = fileStream;
|
||||
this->debugLogFileName = debugLogFileName;
|
||||
this->fileStreamOwner = false;
|
||||
this->mutex = mutex;
|
||||
}
|
||||
|
||||
bool enabled;
|
||||
std::ofstream *fileStream;
|
||||
std::string debugLogFileName;
|
||||
bool fileStreamOwner;
|
||||
Mutex *mutex;
|
||||
};
|
||||
|
||||
protected:
|
||||
|
@@ -19,8 +19,7 @@ using namespace Shared::Util;
|
||||
|
||||
namespace Shared { namespace PlatformCommon {
|
||||
|
||||
BaseThread::BaseThread() {
|
||||
|
||||
BaseThread::BaseThread() : Thread() {
|
||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
|
||||
setQuitStatus(false);
|
||||
@@ -88,7 +87,7 @@ void BaseThread::setRunningStatus(bool value) {
|
||||
}
|
||||
|
||||
void BaseThread::shutdownAndWait(BaseThread *pThread) {
|
||||
if(pThread != NULL) {
|
||||
if(pThread != NULL && pThread->getRunningStatus() == true) {
|
||||
pThread->signalQuit();
|
||||
for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= 10; ) {
|
||||
if(pThread->getRunningStatus() == false) {
|
||||
|
@@ -72,20 +72,23 @@ SystemFlags::~SystemFlags() {
|
||||
}
|
||||
|
||||
void SystemFlags::Close() {
|
||||
printf("Closing logfile\n");
|
||||
printf("START Closing logfiles\n");
|
||||
|
||||
for(std::map<SystemFlags::DebugType,SystemFlags::SystemFlagsType>::iterator iterMap = SystemFlags::debugLogFileList.begin();
|
||||
iterMap != SystemFlags::debugLogFileList.end(); iterMap++) {
|
||||
SystemFlags::SystemFlagsType ¤tDebugLog = iterMap->second;
|
||||
if( currentDebugLog.fileStream != NULL &&
|
||||
currentDebugLog.fileStream->is_open() == true) {
|
||||
currentDebugLog.fileStream->close();
|
||||
}
|
||||
|
||||
if(currentDebugLog.fileStreamOwner == true) {
|
||||
if( currentDebugLog.fileStream != NULL &&
|
||||
currentDebugLog.fileStream->is_open() == true) {
|
||||
currentDebugLog.fileStream->close();
|
||||
}
|
||||
delete currentDebugLog.fileStream;
|
||||
delete currentDebugLog.mutex;
|
||||
}
|
||||
currentDebugLog.fileStream = NULL;
|
||||
currentDebugLog.fileStreamOwner = false;
|
||||
currentDebugLog.mutex = NULL;
|
||||
}
|
||||
|
||||
if(SystemFlags::lockFile != -1) {
|
||||
@@ -101,6 +104,8 @@ void SystemFlags::Close() {
|
||||
SystemFlags::lockfilename = "";
|
||||
}
|
||||
}
|
||||
|
||||
printf("END Closing logfiles\n");
|
||||
}
|
||||
|
||||
void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
|
||||
@@ -138,6 +143,7 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
|
||||
currentDebugLog2.fileStream != NULL) {
|
||||
currentDebugLog.fileStream = currentDebugLog2.fileStream;
|
||||
currentDebugLog.fileStreamOwner = false;
|
||||
currentDebugLog.mutex = currentDebugLog2.mutex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -183,12 +189,17 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
|
||||
currentDebugLog.fileStream = new std::ofstream();
|
||||
currentDebugLog.fileStream->open(debugLog.c_str(), ios_base::out | ios_base::trunc);
|
||||
currentDebugLog.fileStreamOwner = true;
|
||||
currentDebugLog.mutex = new Mutex();
|
||||
}
|
||||
|
||||
printf("Opening logfile [%s] type = %d, currentDebugLog.fileStreamOwner = %d\n",debugLog.c_str(),type, currentDebugLog.fileStreamOwner);
|
||||
|
||||
currentDebugLog.mutex->p();
|
||||
|
||||
(*currentDebugLog.fileStream) << "Starting Mega-Glest logging for type: " << type << "\n";
|
||||
(*currentDebugLog.fileStream).flush();
|
||||
|
||||
currentDebugLog.mutex->v();
|
||||
}
|
||||
|
||||
//printf("Logfile is open [%s]\n",SystemFlags::debugLogFile);
|
||||
@@ -197,8 +208,12 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
|
||||
|
||||
assert(currentDebugLog.fileStream != NULL);
|
||||
|
||||
currentDebugLog.mutex->p();
|
||||
|
||||
(*currentDebugLog.fileStream) << "[" << szBuf2 << "] " << szBuf;
|
||||
(*currentDebugLog.fileStream).flush();
|
||||
|
||||
currentDebugLog.mutex->v();
|
||||
}
|
||||
// output to console
|
||||
else {
|
||||
|
Reference in New Issue
Block a user