summaryrefslogtreecommitdiffstats
path: root/ksirc/logfile.cpp
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commitbcb704366cb5e333a626c18c308c7e0448a8e69f (patch)
treef0d6ab7d78ecdd9207cf46536376b44b91a1ca71 /ksirc/logfile.cpp
downloadtdenetwork-bcb704366cb5e333a626c18c308c7e0448a8e69f.tar.gz
tdenetwork-bcb704366cb5e333a626c18c308c7e0448a8e69f.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdenetwork@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'ksirc/logfile.cpp')
-rw-r--r--ksirc/logfile.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/ksirc/logfile.cpp b/ksirc/logfile.cpp
new file mode 100644
index 00000000..6a15e51e
--- /dev/null
+++ b/ksirc/logfile.cpp
@@ -0,0 +1,93 @@
+/* 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 <qdir.h>
+
+#include <kstandarddirs.h>
+
+LogFile::LogFile( const QString &channel, const QString &server )
+ : m_channel( channel ), m_server( server ), m_file( new QFile() ),
+ 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( QString::fromLatin1( "### Log session started at " )
+ + QDateTime::currentDateTime().toString()
+ + QString::fromLatin1( "###\n" ) );
+}
+
+void LogFile::closeLog()
+{
+ log( QString::fromLatin1( "### Log session terminated at " )
+ + QDateTime::currentDateTime().toString()
+ + QString::fromLatin1( "###\n" ) );
+
+ if ( m_flushTimerId != -1 )
+ killTimer( m_flushTimerId );
+
+ m_file->close();
+}
+
+void LogFile::log( const QString &message )
+{
+ m_file->writeBlock( message.local8Bit(), message.length() );
+
+ if ( m_flushTimerId == -1 )
+ m_flushTimerId = startTimer( 60000 ); // flush each minute
+}
+
+void LogFile::timerEvent( QTimerEvent * )
+{
+ if ( m_file )
+ m_file->flush();
+
+ killTimer( m_flushTimerId );
+ m_flushTimerId = -1;
+}
+
+QString LogFile::makeLogFileName( const QString &channel, const QString &server, int suffix )
+{
+ QString res = channel + '_';
+
+ QDate dt = QDate::currentDate();
+ QString dateStr;
+ dateStr.sprintf( "%.4d_%.2d_%.2d_", dt.year(), dt.month(), dt.day() );
+ res += dateStr;
+
+ res += server;
+
+ res += ".log";
+
+ if ( suffix > -1 )
+ res += '.' + QString::number( suffix );
+
+ return locateLocal( "appdata", "logs/" + res );
+}