moved logging functionality in new class
This commit is contained in:
parent
9800baca21
commit
280f0091f3
6 changed files with 104 additions and 89 deletions
|
@ -164,11 +164,6 @@ int main ( int argc, char** argv )
|
|||
//CTestbench Testbench ( "127.0.0.1", LLCON_DEFAULT_PORT_NUMBER );
|
||||
|
||||
|
||||
// TEST
|
||||
CServerLogging ServerLogging;
|
||||
exit(1);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
if ( bIsClient )
|
||||
|
|
|
@ -104,27 +104,13 @@ void CServer::Stop()
|
|||
Timer.stop();
|
||||
|
||||
// logging
|
||||
const QString strLogStr = CLogTimeDate::toString() + ",, server stopped "
|
||||
"-------------------------------------";
|
||||
|
||||
#ifndef _WIN32
|
||||
QTextStream tsConsoloeStream ( stdout );
|
||||
tsConsoloeStream << strLogStr << endl; // on console
|
||||
#endif
|
||||
Logging << strLogStr; // in log file
|
||||
Logging.AddServerStopped();
|
||||
}
|
||||
|
||||
void CServer::OnNewChannel ( CHostAddress ChanAddr )
|
||||
{
|
||||
// logging of new connected channel
|
||||
const QString strLogStr = CLogTimeDate::toString() + ", " +
|
||||
ChanAddr.InetAddr.toString() + ", connected";
|
||||
|
||||
#ifndef _WIN32
|
||||
QTextStream tsConsoloeStream ( stdout );
|
||||
tsConsoloeStream << strLogStr << endl; // on console
|
||||
#endif
|
||||
Logging << strLogStr; // in log file
|
||||
Logging.AddNewConnection ( ChanAddr.InetAddr );
|
||||
}
|
||||
|
||||
void CServer::OnTimer()
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
#include "socket.h"
|
||||
#include "channel.h"
|
||||
#include "util.h"
|
||||
#include "serverlogging.h"
|
||||
|
||||
|
||||
/* Classes ********************************************************************/
|
||||
|
@ -82,7 +83,7 @@ protected:
|
|||
CCycleTimeVariance CycleTimeVariance;
|
||||
|
||||
// logging
|
||||
CLogging Logging;
|
||||
CServerLogging Logging;
|
||||
|
||||
public slots:
|
||||
void OnTimer();
|
||||
|
|
|
@ -26,8 +26,13 @@
|
|||
|
||||
|
||||
/* Implementation *************************************************************/
|
||||
CServerLogging::CServerLogging()
|
||||
CServerLogging::CServerLogging() :
|
||||
bDoLogging ( false ), File ( DEFAULT_LOG_FILE_NAME )
|
||||
{
|
||||
|
||||
|
||||
#if 0
|
||||
|
||||
int i;
|
||||
|
||||
// constants defining the plot properties
|
||||
|
@ -90,9 +95,10 @@ CServerLogging::CServerLogging()
|
|||
iCurX, PlotGridFrame.bottom() );
|
||||
}
|
||||
|
||||
// grid (ticks) for y-axis
|
||||
// grid (ticks) for y-axis, draw iNumTicksY - 2 grid lines and
|
||||
// iNumTicksY - 1 text labels (the lowest grid line is the grid frame)
|
||||
const int iYSpace = PlotGridFrame.height() / ( iNumTicksY - 1 );
|
||||
for ( i = 0; i < ( iNumTicksY - 2 ); i++ )
|
||||
for ( i = 0; i < ( iNumTicksY - 1 ); i++ )
|
||||
{
|
||||
const int iCurY = PlotGridFrame.y() + iYSpace * ( i + 1 );
|
||||
|
||||
|
@ -102,14 +108,17 @@ CServerLogging::CServerLogging()
|
|||
PlotPainter.drawText ( QPoint (
|
||||
PlotGridFrame.x() + iTextOffsetToGrid,
|
||||
iCurY - iTextOffsetToGrid ),
|
||||
QString().setNum (
|
||||
QString ( "%1:00" ).arg (
|
||||
( iYAxisEnd - iYAxisStart ) / ( iNumTicksY - 1 ) *
|
||||
( ( iNumTicksY - 2 ) - i ) ) );
|
||||
|
||||
// grid
|
||||
PlotPainter.setPen ( PlotGridColor );
|
||||
PlotPainter.drawLine ( PlotGridFrame.x(), iCurY,
|
||||
PlotGridFrame.right(), iCurY );
|
||||
// grid (do not overwrite frame)
|
||||
if ( i < ( iNumTicksY - 2 ) )
|
||||
{
|
||||
PlotPainter.setPen ( PlotGridColor );
|
||||
PlotPainter.drawLine ( PlotGridFrame.x(), iCurY,
|
||||
PlotGridFrame.right(), iCurY );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -117,4 +126,72 @@ CServerLogging::CServerLogging()
|
|||
// save plot as a file
|
||||
PlotPixmap.save ( "test.jpg", "JPG", 90 );
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
CServerLogging::~CServerLogging()
|
||||
{
|
||||
// close logging file of open
|
||||
if ( File.isOpen() )
|
||||
{
|
||||
File.close();
|
||||
}
|
||||
}
|
||||
|
||||
void CServerLogging::Start ( const QString& strLoggingFileName )
|
||||
{
|
||||
// open file
|
||||
File.setFileName ( strLoggingFileName );
|
||||
if ( File.open ( QIODevice::Append | QIODevice::Text ) )
|
||||
{
|
||||
bDoLogging = true;
|
||||
}
|
||||
}
|
||||
|
||||
void CServerLogging::AddNewConnection ( const QHostAddress& ClientInetAddr )
|
||||
{
|
||||
// logging of new connected channel
|
||||
const QString strLogStr = CurTimeDatetoLogString() + ", " +
|
||||
ClientInetAddr.toString() + ", connected";
|
||||
|
||||
#ifndef _WIN32
|
||||
QTextStream tsConsoloeStream ( stdout );
|
||||
tsConsoloeStream << strLogStr << endl; // on console
|
||||
#endif
|
||||
*this << strLogStr; // in log file
|
||||
}
|
||||
|
||||
void CServerLogging::AddServerStopped()
|
||||
{
|
||||
const QString strLogStr = CurTimeDatetoLogString() + ",, server stopped "
|
||||
"-------------------------------------";
|
||||
|
||||
#ifndef _WIN32
|
||||
QTextStream tsConsoloeStream ( stdout );
|
||||
tsConsoloeStream << strLogStr << endl; // on console
|
||||
#endif
|
||||
*this << strLogStr; // in log file
|
||||
}
|
||||
|
||||
void CServerLogging::operator<< ( const QString& sNewStr )
|
||||
{
|
||||
if ( bDoLogging )
|
||||
{
|
||||
// append new line in logging file
|
||||
QTextStream out ( &File );
|
||||
out << sNewStr << endl;
|
||||
File.flush();
|
||||
}
|
||||
}
|
||||
|
||||
QString CServerLogging::CurTimeDatetoLogString()
|
||||
{
|
||||
// time and date to string conversion
|
||||
const QDateTime curDateTime = QDateTime::currentDateTime();
|
||||
|
||||
// format date and time output according to "3.9.2006, 11:38:08"
|
||||
return QString().setNum ( curDateTime.date().day() ) + "." +
|
||||
QString().setNum ( curDateTime.date().month() ) + "." +
|
||||
QString().setNum ( curDateTime.date().year() ) + ", " +
|
||||
curDateTime.time().toString();
|
||||
}
|
||||
|
|
|
@ -28,6 +28,9 @@
|
|||
#include <qpixmap.h>
|
||||
#include <qpainter.h>
|
||||
#include <qdatetime.h>
|
||||
#include <qhostaddress.h>
|
||||
#include <qfile.h>
|
||||
#include <qstring.h>
|
||||
#include "global.h"
|
||||
|
||||
|
||||
|
@ -36,6 +39,18 @@ class CServerLogging
|
|||
{
|
||||
public:
|
||||
CServerLogging();
|
||||
virtual ~CServerLogging();
|
||||
|
||||
void Start ( const QString& strLoggingFileName );
|
||||
void AddNewConnection ( const QHostAddress& ClientInetAddr );
|
||||
void AddServerStopped();
|
||||
|
||||
protected:
|
||||
void operator<< ( const QString& sNewStr );
|
||||
QString CurTimeDatetoLogString();
|
||||
|
||||
bool bDoLogging;
|
||||
QFile File;
|
||||
};
|
||||
|
||||
#endif /* !defined ( SERVERLOGGING_HOIHOKIH83JH8_3_43445KJIUHF1912__INCLUDED_ ) */
|
||||
|
|
59
src/util.h
59
src/util.h
|
@ -323,7 +323,6 @@ template<class TData> void CMovingAv<TData>::Add ( const TData tNewD )
|
|||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************************\
|
||||
* GUI utilities *
|
||||
\******************************************************************************/
|
||||
|
@ -545,64 +544,6 @@ public:
|
|||
};
|
||||
|
||||
|
||||
// Time and Data to String conversion ------------------------------------------
|
||||
class CLogTimeDate
|
||||
{
|
||||
public:
|
||||
static QString toString()
|
||||
{
|
||||
const QDateTime curDateTime = QDateTime::currentDateTime();
|
||||
|
||||
// format date and time output according to "3.9.2006, 11:38:08"
|
||||
return QString().setNum ( curDateTime.date().day() ) + "." +
|
||||
QString().setNum ( curDateTime.date().month() ) + "." +
|
||||
QString().setNum ( curDateTime.date().year() ) + ", " +
|
||||
curDateTime.time().toString();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Logging ---------------------------------------------------------------------
|
||||
class CLogging
|
||||
{
|
||||
public:
|
||||
CLogging() : bDoLogging ( false ), File ( DEFAULT_LOG_FILE_NAME ) {}
|
||||
virtual ~CLogging()
|
||||
{
|
||||
if ( File.isOpen() )
|
||||
{
|
||||
File.close();
|
||||
}
|
||||
}
|
||||
|
||||
void Start ( const QString& strLoggingFileName )
|
||||
{
|
||||
// open file
|
||||
File.setFileName ( strLoggingFileName );
|
||||
if ( File.open ( QIODevice::Append | QIODevice::Text ) )
|
||||
{
|
||||
bDoLogging = true;
|
||||
}
|
||||
}
|
||||
|
||||
void operator<< ( const QString& sNewStr )
|
||||
{
|
||||
if ( bDoLogging )
|
||||
{
|
||||
// append new line in logging file
|
||||
QTextStream out ( &File );
|
||||
out << sNewStr << endl;
|
||||
File.flush();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
bool bDoLogging;
|
||||
QFile File;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/******************************************************************************\
|
||||
* Cycle Time Variance Measurement *
|
||||
\******************************************************************************/
|
||||
|
|
Loading…
Reference in a new issue