jamulus/src/serverlogging.cpp
2009-06-05 20:22:47 +00:00

259 lines
8.0 KiB
C++
Executable File

/******************************************************************************\
* 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 *************************************************************/
CHistoryGraph::CHistoryGraph() :
iYAxisStart ( 0 ),
iYAxisEnd ( 24 ),
iNumTicksX ( 9 ),
iNumTicksY ( 5 ),
iGridFrameOffset ( 10 ),
iTextOffsetToGrid ( 3 ),
iYAxisTextHeight ( 20 ),
AxisFont ( "Arial", 10 ),
iMarkerSize ( 9 ),
PlotBackgroundColor ( Qt::white ), // white background
PlotFrameColor ( Qt::black ), // black frame
PlotGridColor ( Qt::gray ), // gray grid
PlotTextColor ( Qt::black ), // black text
PlotMarkerNewColor ( Qt::blue ), // blue marker for new connection
PlotMarkerStopColor ( Qt::red ), // red marker server stop
PlotCanvasRect ( 0, 0, 500, 500 ),
PlotPixmap ( 1, 1 )
{
// generate plot grid frame rectangle
PlotGridFrame.setCoords ( PlotCanvasRect.x() + iGridFrameOffset,
PlotCanvasRect.y() + iGridFrameOffset,
PlotCanvasRect.width() - 2 * iGridFrameOffset,
PlotCanvasRect.height() - 2 * iGridFrameOffset - iYAxisTextHeight );
// scale pixmap to correct size
PlotPixmap = PlotPixmap.scaled (
PlotCanvasRect.width(), PlotCanvasRect.height() );
}
void CHistoryGraph::DrawFrame()
{
int i;
// get current date (this is the right edge of the x-axis)
curDate = QDate::currentDate();
// clear base pixmap for new plotting
PlotPixmap.fill ( PlotBackgroundColor ); // fill background
// create painter
QPainter PlotPainter ( &PlotPixmap );
// create actual plot region (grid frame) ----------------------------------
PlotPainter.setPen ( PlotFrameColor );
PlotPainter.drawRect ( PlotGridFrame );
// grid (ticks) for x-axis
const int iTextOffsetX = 20;
iXSpace = PlotGridFrame.width() / ( iNumTicksX + 1 );
for ( i = 0; i < iNumTicksX; i++ )
{
const int iCurX = PlotGridFrame.x() + iXSpace * ( i + 1 );
// text (only every second tick)
if ( !( i % 2 ) )
{
PlotPainter.setPen ( PlotTextColor );
PlotPainter.setFont ( AxisFont );
PlotPainter.drawText (
QPoint ( iCurX - iTextOffsetX,
PlotGridFrame.bottom() + iYAxisTextHeight + iTextOffsetToGrid ),
curDate.addDays ( i - iNumTicksX + 1 ).toString ( "dd.MM." ) );
}
// grid
PlotPainter.setPen ( PlotGridColor );
PlotPainter.drawLine ( iCurX, PlotGridFrame.y(),
iCurX, PlotGridFrame.bottom() );
}
// grid (ticks) for y-axis, draw iNumTicksY - 2 grid lines and
// iNumTicksY - 1 text labels (the lowest grid line is the grid frame)
iYSpace = PlotGridFrame.height() / ( iNumTicksY - 1 );
for ( i = 0; i < ( iNumTicksY - 1 ); i++ )
{
const int iCurY = PlotGridFrame.y() + iYSpace * ( i + 1 );
// text
PlotPainter.setPen ( PlotTextColor );
PlotPainter.setFont ( AxisFont );
PlotPainter.drawText ( QPoint (
PlotGridFrame.x() + iTextOffsetToGrid,
iCurY - iTextOffsetToGrid ),
QString ( "%1:00" ).arg (
( iYAxisEnd - iYAxisStart ) / ( iNumTicksY - 1 ) *
( ( iNumTicksY - 2 ) - i ) ) );
// grid (do not overwrite frame)
if ( i < ( iNumTicksY - 2 ) )
{
PlotPainter.setPen ( PlotGridColor );
PlotPainter.drawLine ( PlotGridFrame.x(), iCurY,
PlotGridFrame.right(), iCurY );
}
}
}
void CHistoryGraph::AddMarker ( const QDateTime curDateTime,
const bool bIsServerStop )
{
// create painter for plot
QPainter PlotPainter ( &PlotPixmap );
// TODO add range check for date
const int iXAxisOffs = curDate.daysTo ( curDateTime.date() );
const double dYAxisOffs = 24 - curDateTime.time().hour() -
static_cast<double> ( curDateTime.time().minute() ) / 60;
const QPoint curPoint (
PlotGridFrame.x() + iXSpace * ( iNumTicksX + iXAxisOffs ),
PlotGridFrame.y() + static_cast<int> ( static_cast<double> (
PlotGridFrame.height() ) / ( iYAxisEnd - iYAxisStart ) * dYAxisOffs ) );
// we use different markers for new connection and server stop items
if ( bIsServerStop )
{
// filled circle marker
PlotPainter.setPen ( QPen ( QBrush ( PlotMarkerStopColor ),
iMarkerSize, Qt::SolidLine, Qt::RoundCap ) );
}
else
{
// filled square marker
PlotPainter.setPen ( QPen ( QBrush ( PlotMarkerNewColor ),
iMarkerSize ) );
}
PlotPainter.drawPoint ( curPoint );
}
void CHistoryGraph::Save ( const QString sFileName )
{
// save plot as a file
PlotPixmap.save ( sFileName, "JPG", 90 );
}
CServerLogging::CServerLogging() :
bDoLogging ( false ), File ( DEFAULT_LOG_FILE_NAME )
{
#if 0
HistoryGraph.DrawFrame();
// TEST
HistoryGraph.AddMarker (
QDateTime ( QDate::currentDate().addDays ( -3 ),
QTime ( 17, 50, 0, 0 ) ), false );
HistoryGraph.AddMarker (
QDateTime ( QDate::currentDate().addDays ( -3 ),
QTime ( 18, 0, 0, 0 ) ), true );
HistoryGraph.Save ( "test.jpg" );
#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();
}