kmail
filterlog.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "filterlog.h"
00031
00032 #include <kdebug.h>
00033
00034 #include <qdatetime.h>
00035 #include <qfile.h>
00036
00037 #include <sys/stat.h>
00038
00039
00040 using namespace KMail;
00041
00042
00043 FilterLog * FilterLog::mSelf = NULL;
00044
00045
00046 FilterLog::FilterLog()
00047 {
00048 mSelf = this;
00049
00050 mLogging = false;
00051
00052
00053 mMaxLogSize = 512 * 1024;
00054 mCurrentLogSize = 0;
00055 mAllowedTypes = meta | patternDesc | ruleResult |
00056 patternResult | appliedAction;
00057 }
00058
00059
00060 FilterLog::~FilterLog()
00061 {}
00062
00063
00064 FilterLog * FilterLog::instance()
00065 {
00066 if ( !mSelf ) mSelf = new FilterLog();
00067 return mSelf;
00068 }
00069
00070
00071 void FilterLog::add( QString logEntry, ContentType contentType )
00072 {
00073 if ( isLogging() && ( mAllowedTypes & contentType ) )
00074 {
00075 QString timedLog = "[" + QTime::currentTime().toString() + "] ";
00076 if ( contentType & ~meta )
00077 timedLog += logEntry;
00078 else
00079 timedLog = logEntry;
00080 mLogEntries.append( timedLog );
00081 emit logEntryAdded( timedLog );
00082 mCurrentLogSize += timedLog.length();
00083 checkLogSize();
00084 }
00085 }
00086
00087
00088 void FilterLog::setMaxLogSize( long size )
00089 {
00090 if ( size < -1)
00091 size = -1;
00092
00093 if ( size >= 0 && size < 1024 )
00094 size = 1024;
00095 mMaxLogSize = size;
00096 emit logStateChanged();
00097 checkLogSize();
00098 }
00099
00100
00101 void FilterLog::dump()
00102 {
00103 #ifndef NDEBUG
00104 kdDebug(5006) << "----- starting filter log -----" << endl;
00105 for ( QStringList::Iterator it = mLogEntries.begin();
00106 it != mLogEntries.end(); ++it )
00107 {
00108 kdDebug(5006) << *it << endl;
00109 }
00110 kdDebug(5006) << "------ end of filter log ------" << endl;
00111 #endif
00112 }
00113
00114
00115 void FilterLog::checkLogSize()
00116 {
00117 if ( mCurrentLogSize > mMaxLogSize && mMaxLogSize > -1 )
00118 {
00119 kdDebug(5006) << "Filter log: memory limit reached, starting to discard old items, size = "
00120 << QString::number( mCurrentLogSize ) << endl;
00121
00122 while ( mCurrentLogSize > ( mMaxLogSize * 0.9 ) )
00123 {
00124 QValueListIterator<QString> it = mLogEntries.begin();
00125 if ( it != mLogEntries.end())
00126 {
00127 mCurrentLogSize -= (*it).length();
00128 mLogEntries.remove( it );
00129 kdDebug(5006) << "Filter log: new size = "
00130 << QString::number( mCurrentLogSize ) << endl;
00131 }
00132 else
00133 {
00134 kdDebug(5006) << "Filter log: size reduction disaster!" << endl;
00135 clear();
00136 }
00137 }
00138 emit logShrinked();
00139 }
00140 }
00141
00142
00143 bool FilterLog::saveToFile( QString fileName )
00144 {
00145 QFile file( fileName );
00146 if( file.open( IO_WriteOnly ) ) {
00147 fchmod( file.handle(), S_IRUSR | S_IWUSR );
00148 {
00149 QDataStream ds( &file );
00150 for ( QStringList::Iterator it = mLogEntries.begin();
00151 it != mLogEntries.end(); ++it )
00152 {
00153 QString tmpString = *it + '\n';
00154 QCString cstr( tmpString.local8Bit() );
00155 ds.writeRawBytes( cstr, cstr.size() );
00156 }
00157 }
00158 return true;
00159 }
00160 else
00161 return false;
00162 }
00163
00164
00165 #include "filterlog.moc"
|