2 This file is part of KMail.
3 Copyright (c) 2003 Andreas Gungl <a.gungl@gmx.de>
5 KMail is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License, version 2, as
7 published by the Free Software Foundation.
9 KMail is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 In addition, as a special exception, the copyright holders give
19 permission to link the code of this program with any edition of
20 the Qt library by Trolltech AS, Norway (or with modified versions
21 of Qt that use the same license as Qt), and distribute linked
22 combinations including the two. You must obey the GNU General
23 Public License in all respects for all of the code used other than
24 Qt. If you modify this file, you may extend this exception to
25 your version of the file, but you are not obligated to do so. If
26 you do not wish to do so, delete this exception statement from
31 #include "filterlog.h"
33 #include "messageviewer/util.h"
44 using namespace KMail
;
47 FilterLog
* FilterLog::mSelf
= NULL
;
50 FilterLog::FilterLog()
53 // start with logging disabled by default
55 // better limit the log to 512 KByte to avoid out of memory situations
56 // when the log i sgoing to become very long
57 mMaxLogSize
= 512 * 1024;
59 mAllowedTypes
= meta
| patternDesc
| ruleResult
|
60 patternResult
| appliedAction
;
64 FilterLog::~FilterLog()
68 FilterLog
* FilterLog::instance()
70 if ( !mSelf
) mSelf
= new FilterLog();
75 void FilterLog::add( const QString
& logEntry
, ContentType contentType
)
77 if ( isLogging() && ( mAllowedTypes
& contentType
) )
79 QString timedLog
= '[' + QTime::currentTime().toString() + "] ";
80 if ( contentType
& ~meta
)
84 mLogEntries
.append( timedLog
);
85 emit
logEntryAdded( timedLog
);
86 mCurrentLogSize
+= timedLog
.length();
92 void FilterLog::setMaxLogSize( long size
)
96 // do not allow less than 1 KByte except unlimited (-1)
97 if ( size
>= 0 && size
< 1024 )
100 emit
logStateChanged();
105 void FilterLog::dump()
108 kDebug() << "----- starting filter log -----";
109 for ( QStringList::ConstIterator it
= mLogEntries
.constBegin();
110 it
!= mLogEntries
.constEnd(); ++it
)
114 kDebug() << "------ end of filter log ------";
119 void FilterLog::checkLogSize()
121 if ( mCurrentLogSize
> mMaxLogSize
&& mMaxLogSize
> -1 )
123 kDebug() << "Filter log: memory limit reached, starting to discard old items, size ="
124 << QString::number( mCurrentLogSize
);
125 // avoid some kind of hysteresis, shrink the log to 90% of its maximum
126 while ( mCurrentLogSize
> ( mMaxLogSize
* 0.9 ) )
128 QStringList::Iterator it
= mLogEntries
.begin();
129 if ( it
!= mLogEntries
.end())
131 mCurrentLogSize
-= (*it
).length();
132 mLogEntries
.erase( it
);
133 kDebug() << "Filter log: new size ="
134 << QString::number( mCurrentLogSize
);
138 kDebug() << "Filter log: size reduction disaster!";
147 bool FilterLog::saveToFile( const QString
&fileName
)
149 QFile
file( fileName
);
150 if( file
.open( QIODevice::WriteOnly
) ) {
151 fchmod( file
.handle(), MessageViewer::Util::getWritePermissions() );
153 QDataStream
ds( &file
);
154 for ( QStringList::const_iterator it
= mLogEntries
.constBegin();
155 it
!= mLogEntries
.constEnd(); ++it
)
157 const QString tmpString
= *it
+ '\n';
158 QByteArray
cstr( tmpString
.toLocal8Bit() );
159 ds
.writeRawData( cstr
.data(), cstr
.size() );
169 #include "filterlog.moc"