2009-05-24 13:22:12 +02:00
|
|
|
/******************************************************************************\
|
|
|
|
* Copyright (c) 2004-2009
|
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#include "serverlogging.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Implementation *************************************************************/
|
2009-05-24 17:25:04 +02:00
|
|
|
CServerLogging::CServerLogging() :
|
|
|
|
bDoLogging ( false ), File ( DEFAULT_LOG_FILE_NAME )
|
2009-05-24 13:22:12 +02:00
|
|
|
{
|
2009-05-24 17:25:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
2009-05-24 13:22:12 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// constants defining the plot properties
|
2009-05-24 16:11:22 +02:00
|
|
|
const int iYAxisStart = 0;
|
|
|
|
const int iYAxisEnd = 24;
|
|
|
|
const int iNumTicksX = 9;
|
|
|
|
const int iNumTicksY = 5;
|
|
|
|
const int iPlotWidth = 500;
|
|
|
|
const int iPlotHeight = 500;
|
|
|
|
const int iGridFrameOffset = 10;
|
|
|
|
const int iTextOffsetToGrid = 3;
|
|
|
|
const int iYAxisTextHeight = 20;
|
|
|
|
const QFont AxisFont ( "Arial", 10 );
|
2009-05-24 13:22:12 +02:00
|
|
|
const QColor PlotBackgroundColor ( Qt::white ); // white background
|
|
|
|
const QColor PlotFrameColor ( Qt::black ); // black frame
|
|
|
|
const QColor PlotGridColor ( Qt::gray ); // gray grid
|
|
|
|
const QColor PlotTextColor ( Qt::black ); // black text
|
2009-06-05 17:46:39 +02:00
|
|
|
const QColor PlotMarkerColor ( Qt::red ); // red marker
|
2009-05-24 13:22:12 +02:00
|
|
|
|
2009-06-05 17:46:39 +02:00
|
|
|
// get current date (this is the right edge of the x-axis)
|
|
|
|
const QDate curDate = QDate::currentDate();
|
2009-05-24 13:22:12 +02:00
|
|
|
|
|
|
|
// create base pixmap for plot
|
|
|
|
QRect PlotCanvasRect ( QPoint ( 0, 0 ), QPoint ( iPlotWidth, iPlotHeight ) );
|
|
|
|
QPixmap PlotPixmap ( PlotCanvasRect.size() );
|
|
|
|
PlotPixmap.fill ( PlotBackgroundColor ); // fill background
|
|
|
|
|
|
|
|
// create painter for plot
|
|
|
|
QPainter PlotPainter ( &PlotPixmap );
|
|
|
|
|
|
|
|
|
|
|
|
// create actual plot region (grid frame) ----------------------------------
|
|
|
|
QRect PlotGridFrame (
|
|
|
|
PlotCanvasRect.x() + iGridFrameOffset,
|
|
|
|
PlotCanvasRect.y() + iGridFrameOffset,
|
|
|
|
PlotCanvasRect.width() - 2 * iGridFrameOffset,
|
2009-05-24 16:11:22 +02:00
|
|
|
PlotCanvasRect.height() - 2 * iGridFrameOffset - iYAxisTextHeight );
|
2009-05-24 13:22:12 +02:00
|
|
|
|
|
|
|
PlotPainter.setPen ( PlotFrameColor );
|
|
|
|
PlotPainter.drawRect ( PlotGridFrame );
|
|
|
|
|
|
|
|
// grid (ticks) for x-axis
|
2009-05-24 16:11:22 +02:00
|
|
|
const int iTextOffsetX = 20;
|
|
|
|
const int iXSpace = PlotGridFrame.width() / ( iNumTicksX + 1 );
|
|
|
|
for ( i = 0; i < iNumTicksX; i++ )
|
2009-05-24 13:22:12 +02:00
|
|
|
{
|
|
|
|
const int iCurX = PlotGridFrame.x() + iXSpace * ( i + 1 );
|
|
|
|
|
2009-05-24 16:11:22 +02:00
|
|
|
// text (only every second tick)
|
|
|
|
if ( !( i % 2 ) )
|
|
|
|
{
|
|
|
|
PlotPainter.setPen ( PlotTextColor );
|
|
|
|
PlotPainter.setFont ( AxisFont );
|
|
|
|
PlotPainter.drawText (
|
|
|
|
QPoint ( iCurX - iTextOffsetX,
|
|
|
|
PlotGridFrame.bottom() + iYAxisTextHeight + iTextOffsetToGrid ),
|
2009-06-05 17:46:39 +02:00
|
|
|
curDate.addDays ( i - iNumTicksX + 1 ).toString ( "dd.MM." ) );
|
2009-05-24 16:11:22 +02:00
|
|
|
}
|
2009-05-24 13:22:12 +02:00
|
|
|
|
|
|
|
// grid
|
|
|
|
PlotPainter.setPen ( PlotGridColor );
|
|
|
|
PlotPainter.drawLine ( iCurX, PlotGridFrame.y(),
|
|
|
|
iCurX, PlotGridFrame.bottom() );
|
|
|
|
}
|
|
|
|
|
2009-05-24 17:25:04 +02:00
|
|
|
// grid (ticks) for y-axis, draw iNumTicksY - 2 grid lines and
|
|
|
|
// iNumTicksY - 1 text labels (the lowest grid line is the grid frame)
|
2009-05-24 13:22:12 +02:00
|
|
|
const int iYSpace = PlotGridFrame.height() / ( iNumTicksY - 1 );
|
2009-05-24 17:25:04 +02:00
|
|
|
for ( i = 0; i < ( iNumTicksY - 1 ); i++ )
|
2009-05-24 13:22:12 +02:00
|
|
|
{
|
|
|
|
const int iCurY = PlotGridFrame.y() + iYSpace * ( i + 1 );
|
|
|
|
|
|
|
|
// text
|
|
|
|
PlotPainter.setPen ( PlotTextColor );
|
2009-05-24 16:11:22 +02:00
|
|
|
PlotPainter.setFont ( AxisFont );
|
|
|
|
PlotPainter.drawText ( QPoint (
|
|
|
|
PlotGridFrame.x() + iTextOffsetToGrid,
|
|
|
|
iCurY - iTextOffsetToGrid ),
|
2009-05-24 17:25:04 +02:00
|
|
|
QString ( "%1:00" ).arg (
|
2009-05-24 16:11:22 +02:00
|
|
|
( iYAxisEnd - iYAxisStart ) / ( iNumTicksY - 1 ) *
|
|
|
|
( ( iNumTicksY - 2 ) - i ) ) );
|
2009-05-24 13:22:12 +02:00
|
|
|
|
2009-05-24 17:25:04 +02:00
|
|
|
// grid (do not overwrite frame)
|
|
|
|
if ( i < ( iNumTicksY - 2 ) )
|
|
|
|
{
|
|
|
|
PlotPainter.setPen ( PlotGridColor );
|
|
|
|
PlotPainter.drawLine ( PlotGridFrame.x(), iCurY,
|
|
|
|
PlotGridFrame.right(), iCurY );
|
|
|
|
}
|
2009-05-24 13:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-05 17:46:39 +02:00
|
|
|
|
|
|
|
// TEST add some points in the graph
|
|
|
|
const QDate testDate = QDate::currentDate().addDays ( -3 );
|
|
|
|
const QTime testTime = QTime ( 18, 0, 0, 0 );
|
|
|
|
|
|
|
|
const int iXAxisOffs = curDate.daysTo ( testDate );
|
|
|
|
const int iYAxisOffs = 24 - testTime.hour();
|
|
|
|
|
|
|
|
const QPoint curPoint (
|
|
|
|
PlotGridFrame.x() + PlotGridFrame.width() / iNumTicksX * ( iNumTicksX + iXAxisOffs ),
|
|
|
|
PlotGridFrame.y() + PlotGridFrame.height() / ( iYAxisEnd - iYAxisStart ) * iYAxisOffs );
|
|
|
|
|
|
|
|
PlotPainter.setPen ( QPen ( QBrush ( PlotMarkerColor ), 9 ) );
|
|
|
|
PlotPainter.drawPoint ( curPoint );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-05-24 16:11:22 +02:00
|
|
|
// save plot as a file
|
|
|
|
PlotPixmap.save ( "test.jpg", "JPG", 90 );
|
2009-05-24 13:22:12 +02:00
|
|
|
|
2009-05-24 17:25:04 +02:00
|
|
|
#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();
|
2009-05-24 13:22:12 +02:00
|
|
|
}
|