mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-01-18 07:45:51 -08:00
improved logging, improved server multithreading
This commit is contained in:
@@ -2,19 +2,27 @@
|
||||
#include <QSocketNotifier>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QMutex>
|
||||
#include <QDateTime>
|
||||
#include <QThread>
|
||||
#ifdef Q_OS_UNIX
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
ServerLogger::ServerLogger(QObject *parent)
|
||||
ServerLogger::ServerLogger(const QString &logFileName, QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
logFile = new QFile("server.log", this);
|
||||
logFile->open(QIODevice::Append);
|
||||
if (!logFileName.isEmpty()) {
|
||||
logFile = new QFile(logFileName, this);
|
||||
logFile->open(QIODevice::Append);
|
||||
#ifdef Q_OS_UNIX
|
||||
::socketpair(AF_UNIX, SOCK_STREAM, 0, sigHupFD);
|
||||
::socketpair(AF_UNIX, SOCK_STREAM, 0, sigHupFD);
|
||||
#endif
|
||||
snHup = new QSocketNotifier(sigHupFD[1], QSocketNotifier::Read, this);
|
||||
connect(snHup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));
|
||||
snHup = new QSocketNotifier(sigHupFD[1], QSocketNotifier::Read, this);
|
||||
connect(snHup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));
|
||||
} else
|
||||
logFile = 0;
|
||||
}
|
||||
|
||||
ServerLogger::~ServerLogger()
|
||||
@@ -23,18 +31,30 @@ ServerLogger::~ServerLogger()
|
||||
|
||||
void ServerLogger::logMessage(QString message)
|
||||
{
|
||||
if (!logFile)
|
||||
return;
|
||||
|
||||
static QMutex mutex;
|
||||
mutex.lock();
|
||||
QTextStream stream(logFile);
|
||||
stream << message << "\n";
|
||||
stream << QDateTime::currentDateTime().toString() << " " << ((void *) QThread::currentThread()) << " " << message << "\n";
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void ServerLogger::hupSignalHandler(int /*unused*/)
|
||||
{
|
||||
if (!logFile)
|
||||
return;
|
||||
|
||||
char a = 1;
|
||||
::write(sigHupFD[0], &a, sizeof(a));
|
||||
}
|
||||
|
||||
void ServerLogger::handleSigHup()
|
||||
{
|
||||
if (!logFile)
|
||||
return;
|
||||
|
||||
snHup->setEnabled(false);
|
||||
char tmp;
|
||||
::read(sigHupFD[1], &tmp, sizeof(tmp));
|
||||
|
||||
Reference in New Issue
Block a user