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

View File

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

View File

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

View File

@ -15,8 +15,10 @@
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <map> #include <map>
#include "thread.h"
using std::string; using std::string;
using namespace Shared::Platform;
namespace Shared{ namespace Util{ namespace Shared{ namespace Util{
@ -35,7 +37,7 @@ public:
{ {
protected: protected:
DebugType debugType; DebugType debugType;
public: public:
SystemFlagsType() { SystemFlagsType() {
this->debugType = debugSystem; this->debugType = debugSystem;
@ -43,6 +45,7 @@ public:
this->fileStream = NULL; this->fileStream = NULL;
this->debugLogFileName = ""; this->debugLogFileName = "";
this->fileStreamOwner = false; this->fileStreamOwner = false;
this->mutex = NULL;
} }
SystemFlagsType(DebugType debugType) { SystemFlagsType(DebugType debugType) {
this->debugType = debugType; this->debugType = debugType;
@ -50,6 +53,7 @@ public:
this->fileStream = NULL; this->fileStream = NULL;
this->debugLogFileName = ""; this->debugLogFileName = "";
this->fileStreamOwner = false; this->fileStreamOwner = false;
this->mutex = NULL;
} }
SystemFlagsType(DebugType debugType,bool enabled, SystemFlagsType(DebugType debugType,bool enabled,
std::ofstream *fileStream,std::string debugLogFileName) { std::ofstream *fileStream,std::string debugLogFileName) {
@ -58,12 +62,14 @@ public:
this->fileStream = fileStream; this->fileStream = fileStream;
this->debugLogFileName = debugLogFileName; this->debugLogFileName = debugLogFileName;
this->fileStreamOwner = false; this->fileStreamOwner = false;
this->mutex = mutex;
} }
bool enabled; bool enabled;
std::ofstream *fileStream; std::ofstream *fileStream;
std::string debugLogFileName; std::string debugLogFileName;
bool fileStreamOwner; bool fileStreamOwner;
Mutex *mutex;
}; };
protected: protected:

View File

@ -19,8 +19,7 @@ using namespace Shared::Util;
namespace Shared { namespace PlatformCommon { namespace Shared { namespace PlatformCommon {
BaseThread::BaseThread() { BaseThread::BaseThread() : Thread() {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
setQuitStatus(false); setQuitStatus(false);
@ -88,7 +87,7 @@ void BaseThread::setRunningStatus(bool value) {
} }
void BaseThread::shutdownAndWait(BaseThread *pThread) { void BaseThread::shutdownAndWait(BaseThread *pThread) {
if(pThread != NULL) { if(pThread != NULL && pThread->getRunningStatus() == true) {
pThread->signalQuit(); pThread->signalQuit();
for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= 10; ) { for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= 10; ) {
if(pThread->getRunningStatus() == false) { if(pThread->getRunningStatus() == false) {

View File

@ -72,20 +72,23 @@ SystemFlags::~SystemFlags() {
} }
void SystemFlags::Close() { void SystemFlags::Close() {
printf("Closing logfile\n"); printf("START Closing logfiles\n");
for(std::map<SystemFlags::DebugType,SystemFlags::SystemFlagsType>::iterator iterMap = SystemFlags::debugLogFileList.begin(); for(std::map<SystemFlags::DebugType,SystemFlags::SystemFlagsType>::iterator iterMap = SystemFlags::debugLogFileList.begin();
iterMap != SystemFlags::debugLogFileList.end(); iterMap++) { iterMap != SystemFlags::debugLogFileList.end(); iterMap++) {
SystemFlags::SystemFlagsType &currentDebugLog = iterMap->second; SystemFlags::SystemFlagsType &currentDebugLog = iterMap->second;
if( currentDebugLog.fileStream != NULL &&
currentDebugLog.fileStream->is_open() == true) {
currentDebugLog.fileStream->close();
}
if(currentDebugLog.fileStreamOwner == true) { if(currentDebugLog.fileStreamOwner == true) {
if( currentDebugLog.fileStream != NULL &&
currentDebugLog.fileStream->is_open() == true) {
currentDebugLog.fileStream->close();
}
delete currentDebugLog.fileStream; delete currentDebugLog.fileStream;
delete currentDebugLog.mutex;
} }
currentDebugLog.fileStream = NULL; currentDebugLog.fileStream = NULL;
currentDebugLog.fileStreamOwner = false; currentDebugLog.fileStreamOwner = false;
currentDebugLog.mutex = NULL;
} }
if(SystemFlags::lockFile != -1) { if(SystemFlags::lockFile != -1) {
@ -101,6 +104,8 @@ void SystemFlags::Close() {
SystemFlags::lockfilename = ""; SystemFlags::lockfilename = "";
} }
} }
printf("END Closing logfiles\n");
} }
void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
@ -138,6 +143,7 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
currentDebugLog2.fileStream != NULL) { currentDebugLog2.fileStream != NULL) {
currentDebugLog.fileStream = currentDebugLog2.fileStream; currentDebugLog.fileStream = currentDebugLog2.fileStream;
currentDebugLog.fileStreamOwner = false; currentDebugLog.fileStreamOwner = false;
currentDebugLog.mutex = currentDebugLog2.mutex;
break; break;
} }
} }
@ -183,12 +189,17 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
currentDebugLog.fileStream = new std::ofstream(); currentDebugLog.fileStream = new std::ofstream();
currentDebugLog.fileStream->open(debugLog.c_str(), ios_base::out | ios_base::trunc); currentDebugLog.fileStream->open(debugLog.c_str(), ios_base::out | ios_base::trunc);
currentDebugLog.fileStreamOwner = true; currentDebugLog.fileStreamOwner = true;
currentDebugLog.mutex = new Mutex();
} }
printf("Opening logfile [%s] type = %d, currentDebugLog.fileStreamOwner = %d\n",debugLog.c_str(),type, currentDebugLog.fileStreamOwner); 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) << "Starting Mega-Glest logging for type: " << type << "\n";
(*currentDebugLog.fileStream).flush(); (*currentDebugLog.fileStream).flush();
currentDebugLog.mutex->v();
} }
//printf("Logfile is open [%s]\n",SystemFlags::debugLogFile); //printf("Logfile is open [%s]\n",SystemFlags::debugLogFile);
@ -197,8 +208,12 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
assert(currentDebugLog.fileStream != NULL); assert(currentDebugLog.fileStream != NULL);
currentDebugLog.mutex->p();
(*currentDebugLog.fileStream) << "[" << szBuf2 << "] " << szBuf; (*currentDebugLog.fileStream) << "[" << szBuf2 << "] " << szBuf;
(*currentDebugLog.fileStream).flush(); (*currentDebugLog.fileStream).flush();
currentDebugLog.mutex->v();
} }
// output to console // output to console
else { else {