diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 86a89aec..2ecbdbd0 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -922,6 +922,8 @@ void Game::render2d(){ if(difftime(time(NULL),lastRenderLog2d) >= 1) { lastRenderLog2d = time(NULL); SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d Statistics: %s\n",__FILE__,__FUNCTION__,__LINE__,str.c_str()); + + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"In [%s::%s] Line: %d Statistics: %s\n",__FILE__,__FUNCTION__,__LINE__,str.c_str()); } } diff --git a/source/glest_game/game/game_constants.h b/source/glest_game/game/game_constants.h index ca412aaa..3ee2f871 100644 --- a/source/glest_game/game/game_constants.h +++ b/source/glest_game/game/game_constants.h @@ -62,12 +62,13 @@ public: CardinalDir() : value(NORTH) {} CardinalDir(Enum v) : value(v) {} explicit CardinalDir(int v) { - assert(v >= 0 && v < 4); + assertDirValid(v); value = static_cast(v); } operator Enum() { return value; } //int asInt() { return (int)value; } + static void assertDirValid(int v) { assert(v >= 0 && v < 4); } void operator++() { //printf("In [%s::%s] Line: %d BEFORE +: value = %d\n",__FILE__,__FUNCTION__,__LINE__,asInt()); value = static_cast((value + 1) % 4); diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index a0fe90c5..9d68313b 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -38,7 +38,6 @@ using namespace Shared::Util; namespace Glest{ namespace Game{ -string debugLogFile = ""; bool gameInitialized = false; // ===================================================== @@ -271,9 +270,9 @@ int glestMain(int argc, char** argv){ printf("%s, STREFLOP disabled.\n",getNetworkVersionString().c_str()); #endif - SystemFlags::enableNetworkDebugInfo = true; - SystemFlags::enableDebugText = true; - SystemFlags::enablePerformanceDebugInfo = false; + SystemFlags::init(); + SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled = true; + SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled = true; MainWindow *mainWindow= NULL; Program *program= NULL; @@ -285,14 +284,24 @@ int glestMain(int argc, char** argv){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - SystemFlags::enableNetworkDebugInfo = config.getBool("DebugNetwork","false"); - SystemFlags::enableDebugText = config.getBool("DebugMode","false"); - debugLogFile = config.getString("DebugLogFile",""); + SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled = config.getBool("DebugMode","false"); + SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled = config.getBool("DebugNetwork","false"); + SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled = config.getBool("DebugPerformance","false"); + SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled = config.getBool("DebugWorldSynch","false"); + + string debugWorldSynchLogFile = config.getString("DebugLogFileWorldSynch",""); + string debugLogFile = config.getString("DebugLogFile",""); if(getGameReadWritePath() != "") { debugLogFile = getGameReadWritePath() + debugLogFile; } - SystemFlags::debugLogFile = debugLogFile.c_str(); - SystemFlags::enablePerformanceDebugInfo = config.getBool("DebugPerformance","false"); + if(debugWorldSynchLogFile == "") { + debugWorldSynchLogFile = debugLogFile; + } + + SystemFlags::getSystemSettingType(SystemFlags::debugSystem).debugLogFileName = debugLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).debugLogFileName = debugLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).debugLogFileName = debugLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).debugLogFileName = debugWorldSynchLogFile; NetworkInterface::setDisplayMessageFunction(ExceptionHandler::DisplayMessage); diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index 2cb1f2d1..c3216cc7 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -256,7 +256,7 @@ bool NetworkMessageCommandList::receive(Socket* socket){ return false; } bool result = NetworkMessage::receive(socket, &data, totalMsgSize); - if(SystemFlags::enableNetworkDebugInfo) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled == true) { for(int idx = 0 ; idx < data.commandCount; ++idx) { const NetworkCommand &cmd = data.commands[idx]; @@ -272,7 +272,7 @@ void NetworkMessageCommandList::send(Socket* socket) const{ assert(data.messageType==nmtCommandList); NetworkMessage::send(socket, &data, commandListHeaderSize + sizeof(NetworkCommand) * data.commandCount); - if(SystemFlags::enableNetworkDebugInfo) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled == true) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s %d] messageType = %d, frameCount = %d, data.commandCount = %d\n", __FILE__,__FUNCTION__,__LINE__,data.messageType,data.frameCount,data.commandCount); diff --git a/source/glest_game/network/network_types.cpp b/source/glest_game/network/network_types.cpp index 46cbb426..e970662a 100644 --- a/source/glest_game/network/network_types.cpp +++ b/source/glest_game/network/network_types.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martiņo Figueroa +// Copyright (C) 2001-2008 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -47,7 +47,8 @@ NetworkCommand::NetworkCommand(World *world, int networkCommandType, int unitId, const CommandType *ct = unit->getType()->findCommandTypeById(this->commandTypeId); if(ct != NULL && ct->getClass() == ccBuild) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str()); - assert(facing >= 0 && facing < 4 && targetId == -1); + CardinalDir::assertDirValid(facing); + assert(targetId == -1); } } } @@ -62,7 +63,7 @@ void NetworkCommand::preprocessNetworkCommand(World *world) { const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), unitTypeId); const CommandType *ct = unit->getType()->findCommandTypeById(commandTypeId); if(ct != NULL && ct->getClass() == ccBuild && targetId >= 0) { - assert(targetId < 4); + CardinalDir::assertDirValid(targetId); } } else { diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 16a4f9b0..0390737d 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -171,6 +171,8 @@ Unit::Unit(int id, const Vec2i &pos, const UnitType *type, Faction *faction, Map //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] END\n",__FILE__,__FUNCTION__); livingUnits.insert(id); livingUnitsp.insert(this); + + logSynchData(string(__FILE__) + string("::") + string(__FUNCTION__) + string(" Line: ") + intToStr(__LINE__)); } Unit::~Unit(){ @@ -403,6 +405,8 @@ void Unit::setPos(const Vec2i &pos){ this->lastPos= this->pos; this->pos= pos; this->meetingPos= pos - Vec2i(1); + + logSynchData(string(__FILE__) + string("::") + string(__FUNCTION__) + string(" Line: ") + intToStr(__LINE__)); } void Unit::setTargetPos(const Vec2i &targetPos){ @@ -413,6 +417,8 @@ void Unit::setTargetPos(const Vec2i &targetPos){ targetRef= NULL; this->targetPos= targetPos; + + logSynchData(string(__FILE__) + string("::") + string(__FUNCTION__) + string(" Line: ") + intToStr(__LINE__)); } void Unit::setVisible(const bool visible){ @@ -989,6 +995,8 @@ void Unit::updateTarget(){ //update target vec targetVec= target->getCurrVector(); + + //logSynchData(string(__FILE__) + string("::") + string(__FUNCTION__) + string(" Line: ") + intToStr(__LINE__)); } } @@ -1151,4 +1159,46 @@ void Unit::startDamageParticles(){ } } +void Unit::setTargetVec(const Vec3f &targetVec) { + this->targetVec= targetVec; + logSynchData(string(__FILE__) + string("::") + string(__FUNCTION__) + string(" Line: ") + intToStr(__LINE__)); +} + +void Unit::setMeetingPos(const Vec2i &meetingPos) { + this->meetingPos= meetingPos; + logSynchData(string(__FILE__) + string("::") + string(__FUNCTION__) + string(" Line: ") + intToStr(__LINE__)); +} + +void Unit::logSynchData(string source) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) { + + char szBuf[1024]=""; + sprintf(szBuf, + //"Unit = %d [%s] [%s] pos = %s, lastPos = %s, targetPos = %s, targetVec = %s, meetingPos = %s, lastRotation [%f], targetRotation [%f], rotation [%f], progress [%f], progress2 [%f]\n", + "Unit = %d [%s] pos = %s, lastPos = %s, targetPos = %s, targetVec = %s, meetingPos = %s, lastRotation [%f], targetRotation [%f], rotation [%f], progress [%f], progress2 [%d]\n", + id, + getFullName().c_str(), + //getDesc().c_str(), + pos.getString().c_str(), + lastPos.getString().c_str(), + targetPos.getString().c_str(), + targetVec.getString().c_str(), + meetingPos.getString().c_str(), + lastRotation, + targetRotation, + rotation, + progress, + progress2); + + if(lastSynchDataString != string(szBuf)) { + lastSynchDataString = string(szBuf); + + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "%s %s\n", + source.c_str(), + szBuf); + } + } +} + }}//end namespace diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index ebf95ca9..ac426e2e 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -190,6 +190,8 @@ private: bool allowRotateUnits; CardinalDir modelFacing; + std::string lastSynchDataString; + public: Unit(int id, const Vec2i &pos, const UnitType *type, Faction *faction, Map *map, CardinalDir placeFacing); ~Unit(); @@ -259,8 +261,8 @@ public: void setPos(const Vec2i &pos); void setTargetPos(const Vec2i &targetPos); void setTarget(const Unit *unit); - void setTargetVec(const Vec3f &targetVec) {this->targetVec= targetVec;} - void setMeetingPos(const Vec2i &meetingPos) {this->meetingPos= meetingPos;} + void setTargetVec(const Vec3f &targetVec); + void setMeetingPos(const Vec2i &meetingPos); void setVisible(const bool visible); //render related @@ -314,6 +316,7 @@ private: CommandResult undoCommand(Command *command); void stopDamageParticles(); void startDamageParticles(); + void logSynchData(string source=""); }; }}// end namespace diff --git a/source/shared_lib/include/graphics/vec.h b/source/shared_lib/include/graphics/vec.h index 6f35a560..60d49e4d 100644 --- a/source/shared_lib/include/graphics/vec.h +++ b/source/shared_lib/include/graphics/vec.h @@ -15,6 +15,8 @@ #include "streflop_cond.h" //#include +#include +#include namespace Shared{ namespace Graphics{ @@ -133,6 +135,12 @@ public: x/= m; y/= m; } + + std::string getString() { + std::ostringstream streamOut; + streamOut << "x [" << x << "] y [" << y << "]"; + return streamOut.str(); + } }; typedef Vec2 Vec2i; @@ -298,6 +306,12 @@ public: return rv; } + std::string getString() { + std::ostringstream streamOut; + streamOut << "x [" << x << "] y [" << y << "] z [" << z << "]"; + return streamOut.str(); + } + }; typedef Vec3 Vec3i; @@ -431,6 +445,13 @@ public: T dot(const Vec4 &v) const{ return x*v.x + y*v.y + z*v.z + w*v.w; } + + std::string getString() { + std::ostringstream streamOut; + streamOut << "x [" << x << "] y [" << y << "] z [" << z << "] w [" << w << "]"; + return streamOut.str(); + } + }; typedef Vec4 Vec4i; diff --git a/source/shared_lib/include/util/util.h b/source/shared_lib/include/util/util.h index 3a63a9ef..db31da26 100644 --- a/source/shared_lib/include/util/util.h +++ b/source/shared_lib/include/util/util.h @@ -14,6 +14,7 @@ #include #include +#include using std::string; @@ -21,28 +22,60 @@ namespace Shared{ namespace Util{ class SystemFlags { +public: + + enum DebugType { + debugSystem, + debugNetwork, + debugPerformance, + debugWorldSynch + }; + + class SystemFlagsType + { + protected: + DebugType debugType; + + public: + SystemFlagsType() { + this->debugType = debugSystem; + this->enabled = false; + this->fileStream = NULL; + this->debugLogFileName = ""; + } + SystemFlagsType(DebugType debugType) { + this->debugType = debugType; + this->enabled = false; + this->fileStream = NULL; + this->debugLogFileName = ""; + } + SystemFlagsType(DebugType debugType,bool enabled, + std::ofstream *fileStream,std::string debugLogFileName) { + this->debugType = debugType; + this->enabled = enabled; + this->fileStream = fileStream; + this->debugLogFileName = debugLogFileName; + } + + bool enabled; + std::ofstream *fileStream; + std::string debugLogFileName; + }; + protected: - + static int lockFile; static string lockfilename; - static std::ofstream fileStream; + static std::map debugLogFileList; + public: SystemFlags(); ~SystemFlags(); - enum DebugType { - debugSystem, - debugNetwork, - debugPerformance - }; - - static bool enableDebugText; - static bool enableNetworkDebugInfo; - static bool enablePerformanceDebugInfo; - static const char *debugLogFile; - + static void init(); + static SystemFlagsType & getSystemSettingType(DebugType type) { return debugLogFileList[type]; } static void OutputDebug(DebugType type, const char *fmt, ...); static void Close(); }; diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index b4c4b154..ca88034e 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -35,12 +35,22 @@ using namespace Shared::Platform; namespace Shared{ namespace Util{ -bool SystemFlags::enableDebugText = false; -bool SystemFlags::enableNetworkDebugInfo = false; -bool SystemFlags::enablePerformanceDebugInfo = false; +std::map SystemFlags::debugLogFileList; -const char * SystemFlags::debugLogFile = NULL; -ofstream SystemFlags::fileStream; +void SystemFlags::init() { + SystemFlags::debugLogFileList[SystemFlags::debugSystem] = SystemFlags::SystemFlagsType(SystemFlags::debugSystem); + SystemFlags::debugLogFileList[SystemFlags::debugNetwork] = SystemFlags::SystemFlagsType(SystemFlags::debugNetwork); + SystemFlags::debugLogFileList[SystemFlags::debugPerformance] = SystemFlags::SystemFlagsType(SystemFlags::debugPerformance); + SystemFlags::debugLogFileList[SystemFlags::debugWorldSynch] = SystemFlags::SystemFlagsType(SystemFlags::debugWorldSynch); +} + +//bool SystemFlags::enableDebugText = false; +//bool SystemFlags::enableNetworkDebugInfo = false; +//bool SystemFlags::enablePerformanceDebugInfo = false; +//bool SystemFlags::enableWorldSynchDebugInfo = false; + +//const char * SystemFlags::debugLogFile = NULL; +//ofstream SystemFlags::fileStream; int SystemFlags::lockFile = -1; string SystemFlags::lockfilename = ""; @@ -69,9 +79,17 @@ SystemFlags::~SystemFlags() { void SystemFlags::Close() { printf("Closing logfile\n"); - if(fileStream.is_open() == true) { - SystemFlags::fileStream.close(); + for(std::map::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(); + } + delete currentDebugLog.fileStream; + currentDebugLog.fileStream = NULL; } + if(SystemFlags::lockfilename != "") { #ifndef WIN32 close(SystemFlags::lockFile); @@ -84,9 +102,8 @@ void SystemFlags::Close() { } void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { - if((type == debugSystem && SystemFlags::enableDebugText == false) || - (type == debugNetwork && SystemFlags::enableNetworkDebugInfo == false || - type == debugPerformance && SystemFlags::enablePerformanceDebugInfo == false)) { + SystemFlags::SystemFlagsType ¤tDebugLog = SystemFlags::debugLogFileList[type]; + if(currentDebugLog.enabled == false) { return; } @@ -104,10 +121,27 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { vsprintf(szBuf,fmt, argList); // Either output to a logfile or - if(SystemFlags::debugLogFile != NULL && SystemFlags::debugLogFile[0] != 0) { - if(fileStream.is_open() == false) { - string debugLog = SystemFlags::debugLogFile; - printf("Opening logfile [%s]\n",debugLog.c_str()); + if(currentDebugLog.debugLogFileName != "") { + if( currentDebugLog.fileStream == NULL || + currentDebugLog.fileStream->is_open() == false) { + + // If the file is already open (shared) by another debug type + // do not over-write the file but append + bool appendToFileOnly = false; + for(std::map::iterator iterMap = SystemFlags::debugLogFileList.begin(); + iterMap != SystemFlags::debugLogFileList.end(); iterMap++) { + SystemFlags::SystemFlagsType ¤tDebugLog2 = iterMap->second; + + if( iterMap->first != type && + currentDebugLog.debugLogFileName == currentDebugLog2.debugLogFileName && + currentDebugLog2.fileStream != NULL) { + appendToFileOnly = true; + break; + } + } + + string debugLog = currentDebugLog.debugLogFileName; + printf("Opening logfile [%s] type = %d, appendToFileOnly = %d\n",debugLog.c_str(),type, appendToFileOnly); const string lock_file_name = "debug.lck"; string lockfile = extractDirectoryPathFromFile(debugLog); @@ -142,14 +176,28 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { printf("Opening additional logfile [%s]\n",debugLog.c_str()); } - fileStream.open(debugLog.c_str(), ios_base::out | ios_base::trunc); + if(currentDebugLog.fileStream == NULL) { + currentDebugLog.fileStream = new std::ofstream(); + } + if(appendToFileOnly == false) { + currentDebugLog.fileStream->open(debugLog.c_str(), ios_base::out | ios_base::trunc); + } + else { + currentDebugLog.fileStream->open(debugLog.c_str(), ios_base::out | ios_base::app); + } + + (*currentDebugLog.fileStream) << "Starting Mega-Glest logging for type: " << type << "\n"; + (*currentDebugLog.fileStream).flush(); } //printf("Logfile is open [%s]\n",SystemFlags::debugLogFile); //printf("writing to logfile [%s]\n",szBuf); - fileStream << "[" << szBuf2 << "] " << szBuf; - fileStream.flush(); + + assert(currentDebugLog.fileStream != NULL); + + (*currentDebugLog.fileStream) << "[" << szBuf2 << "] " << szBuf; + (*currentDebugLog.fileStream).flush(); } // output to console else {