moved logging functionality in new class

This commit is contained in:
Volker Fischer 2009-05-24 15:25:04 +00:00
parent 9800baca21
commit 280f0091f3
6 changed files with 104 additions and 89 deletions

View File

@ -164,11 +164,6 @@ int main ( int argc, char** argv )
//CTestbench Testbench ( "127.0.0.1", LLCON_DEFAULT_PORT_NUMBER ); //CTestbench Testbench ( "127.0.0.1", LLCON_DEFAULT_PORT_NUMBER );
// TEST
CServerLogging ServerLogging;
exit(1);
try try
{ {
if ( bIsClient ) if ( bIsClient )

View File

@ -104,27 +104,13 @@ void CServer::Stop()
Timer.stop(); Timer.stop();
// logging // logging
const QString strLogStr = CLogTimeDate::toString() + ",, server stopped " Logging.AddServerStopped();
"-------------------------------------";
#ifndef _WIN32
QTextStream tsConsoloeStream ( stdout );
tsConsoloeStream << strLogStr << endl; // on console
#endif
Logging << strLogStr; // in log file
} }
void CServer::OnNewChannel ( CHostAddress ChanAddr ) void CServer::OnNewChannel ( CHostAddress ChanAddr )
{ {
// logging of new connected channel // logging of new connected channel
const QString strLogStr = CLogTimeDate::toString() + ", " + Logging.AddNewConnection ( ChanAddr.InetAddr );
ChanAddr.InetAddr.toString() + ", connected";
#ifndef _WIN32
QTextStream tsConsoloeStream ( stdout );
tsConsoloeStream << strLogStr << endl; // on console
#endif
Logging << strLogStr; // in log file
} }
void CServer::OnTimer() void CServer::OnTimer()

View File

@ -33,6 +33,7 @@
#include "socket.h" #include "socket.h"
#include "channel.h" #include "channel.h"
#include "util.h" #include "util.h"
#include "serverlogging.h"
/* Classes ********************************************************************/ /* Classes ********************************************************************/
@ -82,7 +83,7 @@ protected:
CCycleTimeVariance CycleTimeVariance; CCycleTimeVariance CycleTimeVariance;
// logging // logging
CLogging Logging; CServerLogging Logging;
public slots: public slots:
void OnTimer(); void OnTimer();

View File

@ -26,8 +26,13 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
CServerLogging::CServerLogging() CServerLogging::CServerLogging() :
bDoLogging ( false ), File ( DEFAULT_LOG_FILE_NAME )
{ {
#if 0
int i; int i;
// constants defining the plot properties // constants defining the plot properties
@ -90,9 +95,10 @@ CServerLogging::CServerLogging()
iCurX, PlotGridFrame.bottom() ); 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 ); 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 ); const int iCurY = PlotGridFrame.y() + iYSpace * ( i + 1 );
@ -102,14 +108,17 @@ CServerLogging::CServerLogging()
PlotPainter.drawText ( QPoint ( PlotPainter.drawText ( QPoint (
PlotGridFrame.x() + iTextOffsetToGrid, PlotGridFrame.x() + iTextOffsetToGrid,
iCurY - iTextOffsetToGrid ), iCurY - iTextOffsetToGrid ),
QString().setNum ( QString ( "%1:00" ).arg (
( iYAxisEnd - iYAxisStart ) / ( iNumTicksY - 1 ) * ( iYAxisEnd - iYAxisStart ) / ( iNumTicksY - 1 ) *
( ( iNumTicksY - 2 ) - i ) ) ); ( ( iNumTicksY - 2 ) - i ) ) );
// grid // grid (do not overwrite frame)
PlotPainter.setPen ( PlotGridColor ); if ( i < ( iNumTicksY - 2 ) )
PlotPainter.drawLine ( PlotGridFrame.x(), iCurY, {
PlotGridFrame.right(), iCurY ); PlotPainter.setPen ( PlotGridColor );
PlotPainter.drawLine ( PlotGridFrame.x(), iCurY,
PlotGridFrame.right(), iCurY );
}
} }
@ -117,4 +126,72 @@ CServerLogging::CServerLogging()
// save plot as a file // save plot as a file
PlotPixmap.save ( "test.jpg", "JPG", 90 ); 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();
} }

View File

@ -28,6 +28,9 @@
#include <qpixmap.h> #include <qpixmap.h>
#include <qpainter.h> #include <qpainter.h>
#include <qdatetime.h> #include <qdatetime.h>
#include <qhostaddress.h>
#include <qfile.h>
#include <qstring.h>
#include "global.h" #include "global.h"
@ -36,6 +39,18 @@ class CServerLogging
{ {
public: public:
CServerLogging(); 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_ ) */ #endif /* !defined ( SERVERLOGGING_HOIHOKIH83JH8_3_43445KJIUHF1912__INCLUDED_ ) */

View File

@ -323,7 +323,6 @@ template<class TData> void CMovingAv<TData>::Add ( const TData tNewD )
} }
/******************************************************************************\ /******************************************************************************\
* GUI utilities * * 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 * * Cycle Time Variance Measurement *
\******************************************************************************/ \******************************************************************************/