kmail
filterlog.cpp
Go to the documentation of this file.00001 /* 00002 This file is part of KMail. 00003 Copyright (c) 2003 Andreas Gungl <a.gungl@gmx.de> 00004 00005 KMail is free software; you can redistribute it and/or modify it 00006 under the terms of the GNU General Public License, version 2, as 00007 published by the Free Software Foundation. 00008 00009 KMail is distributed in the hope that it will be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00017 00018 In addition, as a special exception, the copyright holders give 00019 permission to link the code of this program with any edition of 00020 the Qt library by Trolltech AS, Norway (or with modified versions 00021 of Qt that use the same license as Qt), and distribute linked 00022 combinations including the two. You must obey the GNU General 00023 Public License in all respects for all of the code used other than 00024 Qt. If you modify this file, you may extend this exception to 00025 your version of the file, but you are not obligated to do so. If 00026 you do not wish to do so, delete this exception statement from 00027 your version. 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 // start with logging disabled by default 00050 mLogging = false; 00051 // better limit the log to 512 KByte to avoid out of memory situations 00052 // when the log i sgoing to become very long 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 // do not allow less than 1 KByte except unlimited (-1) 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 // avoid some kind of hysteresis, shrink the log to 90% of its maximum 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"