Added a special file locking mechanism to the debug logging which allows testing multi-user to seperate logfiles automatically

This commit is contained in:
Mark Vejvoda
2010-04-13 16:00:19 +00:00
parent 8c438b812d
commit feb61e0598
3 changed files with 75 additions and 4 deletions

View File

@@ -257,6 +257,7 @@ void MainWindow::setProgram(Program *program) {
// ===================================================== // =====================================================
// Main // Main
// ===================================================== // =====================================================
SystemFlags debugger;
int glestMain(int argc, char** argv){ int glestMain(int argc, char** argv){
SystemFlags::enableNetworkDebugInfo = true; SystemFlags::enableNetworkDebugInfo = true;

View File

@@ -1,7 +1,7 @@
// ============================================================== // ==============================================================
// This file is part of Glest Shared Library (www.glest.org) // This file is part of Glest Shared Library (www.glest.org)
// //
// Copyright (C) 2001-2008 Marti<EFBFBD>o Figueroa // Copyright (C) 2001-2008 Martio Figueroa
// //
// You can redistribute this code and/or modify it under // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
@@ -23,9 +23,15 @@ class SystemFlags
{ {
protected: protected:
static int lockFile;
static string lockfilename;
static std::ofstream fileStream; static std::ofstream fileStream;
public: public:
SystemFlags();
~SystemFlags();
enum DebugType { enum DebugType {
debugSystem, debugSystem,
debugNetwork debugNetwork

View File

@@ -19,21 +19,57 @@
#include <stdarg.h> #include <stdarg.h>
#include <time.h> #include <time.h>
#ifndef WIN32
#include <fcntl.h> // for open()
#else
#include <io.h> // for open()
#endif
#include "platform_util.h"
#include "conversion.h"
#include "leak_dumper.h" #include "leak_dumper.h"
using namespace std; using namespace std;
using namespace Shared::Platform;
namespace Shared{ namespace Util{ namespace Shared{ namespace Util{
bool SystemFlags::enableDebugText = false; bool SystemFlags::enableDebugText = false;
bool SystemFlags::enableNetworkDebugInfo = false; bool SystemFlags::enableNetworkDebugInfo = false;
const char * SystemFlags::debugLogFile = NULL; const char * SystemFlags::debugLogFile = NULL;
ofstream SystemFlags::fileStream; ofstream SystemFlags::fileStream;
int SystemFlags::lockFile = -1;
string SystemFlags::lockfilename = "";
inline bool acquire_file_lock(int hnd)
{
struct ::flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
return -1 != ::fcntl(hnd, F_SETLK, &lock);
}
SystemFlags::SystemFlags() {
}
SystemFlags::~SystemFlags() {
SystemFlags::Close();
}
void SystemFlags::Close() { void SystemFlags::Close() {
printf("Closing logfile\n");
if(fileStream.is_open() == true) { if(fileStream.is_open() == true) {
SystemFlags::fileStream.close(); SystemFlags::fileStream.close();
} }
if(SystemFlags::lockfilename != "") {
close(SystemFlags::lockFile);
remove(SystemFlags::lockfilename.c_str());
SystemFlags::lockfilename = "";
}
} }
void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
@@ -58,8 +94,36 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
// Either output to a logfile or // Either output to a logfile or
if(SystemFlags::debugLogFile != NULL && SystemFlags::debugLogFile[0] != 0) { if(SystemFlags::debugLogFile != NULL && SystemFlags::debugLogFile[0] != 0) {
if(fileStream.is_open() == false) { if(fileStream.is_open() == false) {
printf("Opening logfile [%s]\n",SystemFlags::debugLogFile); string debugLog = SystemFlags::debugLogFile;
fileStream.open(SystemFlags::debugLogFile, ios_base::out | ios_base::trunc); printf("Opening logfile [%s]\n",debugLog.c_str());
const string lock_file_name = "debug.lck";
string lockfile = extractDirectoryPathFromFile(debugLog);
lockfile += lock_file_name;
SystemFlags::lockfilename = lockfile;
//SystemFlags::lockFile = open(lockfile.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR);
SystemFlags::lockFile = open(lockfile.c_str(), O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR);
if (SystemFlags::lockFile < 0 || acquire_file_lock(SystemFlags::lockFile) == false) {
string newlockfile = lockfile;
int idx = 1;
for(idx = 1; idx <= 100; ++idx) {
newlockfile = lockfile + intToStr(idx);
//SystemFlags::lockFile = open(newlockfile.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR);
SystemFlags::lockFile = open(newlockfile.c_str(), O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR);
if(SystemFlags::lockFile >= 0 && acquire_file_lock(SystemFlags::lockFile) == true) {
break;
}
}
SystemFlags::lockfilename = newlockfile;
debugLog += intToStr(idx);
printf("Opening additional logfile [%s]\n",debugLog.c_str());
}
fileStream.open(debugLog.c_str(), ios_base::out | ios_base::trunc);
} }
//printf("Logfile is open [%s]\n",SystemFlags::debugLogFile); //printf("Logfile is open [%s]\n",SystemFlags::debugLogFile);