Added mutex guards around logging

This commit is contained in:
Mark Vejvoda 2010-05-01 09:10:52 +00:00
parent a911088d39
commit e379905942
7 changed files with 56 additions and 30 deletions

Binary file not shown.

View File

@ -30,6 +30,7 @@
#include "ImageReaders.h"
#include "renderer.h"
#include "simple_threads.h"
#include <memory>
#include "leak_dumper.h"
@ -264,6 +265,11 @@ void MainWindow::setProgram(Program *program) {
SystemFlags debugger;
int glestMain(int argc, char** argv){
#ifdef SL_LEAK_DUMP
AllocRegistry memoryLeaks = AllocRegistry::getInstance();
#endif
#ifdef STREFLOP_H
streflop_init<streflop::Simple>();
printf("%s, STREFLOP enabled.\n",getNetworkVersionString().c_str());
@ -280,9 +286,8 @@ int glestMain(int argc, char** argv){
ExceptionHandler exceptionHandler;
exceptionHandler.install( getCrashDumpFileName() );
FileCRCPreCacheThread preCacheThread;
try{
std::auto_ptr<FileCRCPreCacheThread> preCacheThread;
Config &config = Config::getInstance();
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -344,8 +349,9 @@ int glestMain(int argc, char** argv){
if(config.getBool("AllowGameDataSynchCheck","false") == true) {
vector<string> techDataPaths = config.getPathListForType(ptTechs);
preCacheThread.setTechDataPaths(techDataPaths);
preCacheThread.start();
preCacheThread.reset(new FileCRCPreCacheThread());
preCacheThread->setTechDataPaths(techDataPaths);
preCacheThread->start();
}
// test
@ -357,35 +363,31 @@ int glestMain(int argc, char** argv){
while(Window::handleEvent()){
program->loop();
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
catch(const exception &e){
preCacheThread.shutdownAndWait();
//exceptionMessage(e);
ExceptionHandler::handleRuntimeError(e.what());
}
catch(const char *e){
//exceptionMessage(e);
preCacheThread.shutdownAndWait();
ExceptionHandler::handleRuntimeError(e);
}
catch(const string &ex){
//exceptionMessage(e);
preCacheThread.shutdownAndWait();
ExceptionHandler::handleRuntimeError(ex.c_str());
}
catch(...){
//exceptionMessage(e);
preCacheThread.shutdownAndWait();
ExceptionHandler::handleRuntimeError("Unknown error!");
}
preCacheThread.shutdownAndWait();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//SoundRenderer &soundRenderer= SoundRenderer::getInstance();
//soundRenderer.stopAllSounds();
delete mainWindow;
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//SystemFlags::Close();
return 0;

View File

@ -120,7 +120,6 @@ void Program::ShowMessageProgramState::update() {
Program::Program() {
programState= NULL;
singleton = this;
soundThreadManager = NULL;
}
@ -165,7 +164,7 @@ Program::~Program(){
singleton = NULL;
BaseThread::shutdownAndWait(soundThreadManager);
delete soundThreadManager;
delete soundThreadManager;
soundThreadManager = NULL;
}
@ -205,7 +204,9 @@ void Program::loop(){
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
//!!!SoundRenderer::getInstance().update();
if(soundThreadManager == NULL) {
SoundRenderer::getInstance().update();
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -363,10 +364,13 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){
SoundRenderer &soundRenderer= SoundRenderer::getInstance();
soundRenderer.init(window);
BaseThread::shutdownAndWait(soundThreadManager);
delete soundThreadManager;
soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,50);
soundThreadManager->start();
// Run sound streaming in a background thread if enabled
if(config.getBool("ThreadedSoundStream","true") == true) {
BaseThread::shutdownAndWait(soundThreadManager);
delete soundThreadManager;
soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,50);
soundThreadManager->start();
}
}
NetworkInterface::setAllowGameDataSynchCheck(Config::getInstance().getBool("AllowGameDataSynchCheck","0"));

View File

@ -42,7 +42,7 @@ public:
void signalQuit();
bool getQuitStatus();
bool getRunningStatus();
static void shutdownAndWait(BaseThread *pThread);
static void shutdownAndWait(BaseThread *ppThread);
void shutdownAndWait();
};

View File

@ -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:

View File

@ -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) {

View File

@ -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 &currentDebugLog = 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 {