You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tdenetwork/ksirc/logfile.cpp

94 lines
2.1 KiB

/* This file is part of the KDE project
Copyright (C) 2001 Simon Hausmann <hausmann@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the Artistic License.
*/
#include "logfile.h"
#include <assert.h>
#include <tqdir.h>
#include <kstandarddirs.h>
LogFile::LogFile( const TQString &channel, const TQString &server )
: m_channel( channel ), m_server( server ), m_file( new TQFile() ),
m_flushTimerId( -1 )
{
}
LogFile::~LogFile()
{
closeLog();
delete m_file;
}
void LogFile::open()
{
int suffix = 1;
m_file->setName( makeLogFileName( m_channel, m_server ) );
while ( !m_file->open( IO_WriteOnly | IO_Append ) && suffix < 16000 ) // arbitrary limit ;)
{
m_file->setName( makeLogFileName( m_channel, m_server, suffix ) );
suffix++;
}
assert( m_file->isOpen() == true );
log( TQString::tqfromLatin1( "### Log session started at " )
+ TQDateTime::tqcurrentDateTime().toString()
+ TQString::tqfromLatin1( "###\n" ) );
}
void LogFile::closeLog()
{
log( TQString::tqfromLatin1( "### Log session terminated at " )
+ TQDateTime::tqcurrentDateTime().toString()
+ TQString::tqfromLatin1( "###\n" ) );
if ( m_flushTimerId != -1 )
killTimer( m_flushTimerId );
m_file->close();
}
void LogFile::log( const TQString &message )
{
m_file->writeBlock( message.local8Bit(), message.length() );
if ( m_flushTimerId == -1 )
m_flushTimerId = startTimer( 60000 ); // flush each minute
}
void LogFile::timerEvent( TQTimerEvent * )
{
if ( m_file )
m_file->flush();
killTimer( m_flushTimerId );
m_flushTimerId = -1;
}
TQString LogFile::makeLogFileName( const TQString &channel, const TQString &server, int suffix )
{
TQString res = channel + '_';
TQDate dt = TQDate::tqcurrentDate();
TQString dateStr;
dateStr.sprintf( "%.4d_%.2d_%.2d_", dt.year(), dt.month(), dt.day() );
res += dateStr;
res += server;
res += ".log";
if ( suffix > -1 )
res += '.' + TQString::number( suffix );
return locateLocal( "appdata", "logs/" + res );
}