From fb2b6b4925276bd45ca528a14010942cdd08d253 Mon Sep 17 00:00:00 2001 From: Andreas Gungl Date: Thu, 19 Feb 2004 22:52:56 +0000 Subject: [PATCH] new support for a limit of the log size in the backend, small changes to the output format svn path=/trunk/kdepim/; revision=289528 --- filterlog.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++-- filterlog.h | 27 ++++++++++++++------- filterlogdlg.cpp | 14 +++++++++++ filterlogdlg.h | 1 + kmsearchpattern.cpp | 18 +++++++------- 5 files changed, 99 insertions(+), 19 deletions(-) diff --git a/filterlog.cpp b/filterlog.cpp index 8102c3e8c..042936d57 100644 --- a/filterlog.cpp +++ b/filterlog.cpp @@ -37,6 +37,21 @@ using namespace KMail; FilterLog * FilterLog::self = NULL; + +FilterLog::FilterLog() +{ + self = this; + // start with logging enabled by default + logging = true; + // better limit the log to 512 KByte to avoid out of memory situations + // when the log i sgoing to become very long + maxLogSize = 512 * 1024; + currentLogSize = 0; + allowedTypes = meta | patternDesc | ruleResult | + patternResult | appliedAction; +}; + + FilterLog::~FilterLog() {} @@ -57,12 +72,24 @@ void FilterLog::add( QString logEntry, ContentType contentType ) { logEntries.append( logEntry ); emit logEntryAdded( logEntry ); -// FIXME remove it -kdDebug(5006) << "New filter log entry added."<< endl; + currentLogSize += logEntry.length(); + checkLogSize(); } } +void FilterLog::setMaxLogSize( long size ) +{ + if ( size < -1) + size = -1; + // do not allow less than 1 KByte except unlimited (-1) + if ( size >= 0 && size < 1024 ) + size = 1024; + maxLogSize = size; + checkLogSize(); +}; + + void FilterLog::dump() { #ifndef NDEBUG @@ -77,4 +104,31 @@ void FilterLog::dump() } +void FilterLog::checkLogSize() +{ + if ( currentLogSize > maxLogSize && maxLogSize >= -1 ) + { + kdDebug(5006) << "Filter log: memory limit reached, starting to discard old items, size = " + << QString::number( currentLogSize ) << endl; + // avoid some kind of hysteresis, shrink the log to 90% of its maximum + while ( currentLogSize > ( maxLogSize * 0.9 ) ) + { + QValueListIterator it = logEntries.begin(); + if ( it != logEntries.end()) + { + currentLogSize -= (*it).length(); + logEntries.remove( it ); + kdDebug(5006) << "Filter log: new size = " << QString::number( currentLogSize ) << endl; + } + else + { + kdDebug(5006) << "Filter log: size reduction disaster!" << endl; + clear(); + } + } + emit logShrinked(); + } +} + + #include "filterlog.moc" diff --git a/filterlog.h b/filterlog.h index 8749f126b..b99031e39 100644 --- a/filterlog.h +++ b/filterlog.h @@ -63,8 +63,12 @@ namespace KMail { bool isLogging() { return logging; }; /** set the logging state */ void setLogging( bool active ) { logging = active; }; + + /** control the size of the log */ + void setMaxLogSize( long size = -1 ); + /** add a content type to the set of logged ones */ - void enableContentType( ContentType contentType ) { allowedTypes |= contentType; }; + void enableContentType( ContentType contentType ) { allowedTypes |= contentType; }; /** remove a content type from the set of logged ones */ void disableContentType( ContentType contentType ) { allowedTypes &= ~contentType; }; @@ -73,7 +77,7 @@ namespace KMail { /** add a separating line in the log */ void addSeparator() { add( "------------------------------", meta ); }; /** discard collected log data */ - void clear() { logEntries.clear(); }; + void clear() { logEntries.clear(); currentLogSize = 0; }; /** get access to the log entries */ const QStringList & getLogEntries() { return logEntries; }; @@ -85,22 +89,29 @@ namespace KMail { signals: void logEntryAdded( QString ); + void logShrinked(); protected: /** Non-public constructor needed by the singleton implementation */ - FilterLog() - { - self = this; - logging = true; - allowedTypes = meta | patternDesc | ruleResult | patternResult | appliedAction; - }; + FilterLog(); /** The list contains the single log pieces */ QStringList logEntries; + /** the log status */ bool logging; + + /** max size for kept log items, when reached + the last recently added items are discarded + -1 means unlimited */ + long maxLogSize; + long currentLogSize; + + /** types currently allowed to be legged */ int allowedTypes; + void checkLogSize(); + private: static FilterLog * self; }; diff --git a/filterlogdlg.cpp b/filterlogdlg.cpp index 0222426b3..387a772aa 100644 --- a/filterlogdlg.cpp +++ b/filterlogdlg.cpp @@ -57,6 +57,8 @@ FilterLogDialog::FilterLogDialog( QWidget * parent ) connect(FilterLog::instance(), SIGNAL(logEntryAdded(QString)), this, SLOT(slotLogEntryAdded(QString))); + connect(FilterLog::instance(), SIGNAL(logShrinked(void)), + this, SLOT(slotLogShrinked(void))); setInitialSize( QSize( 500, 300 ) ); } @@ -68,6 +70,18 @@ void FilterLogDialog::slotLogEntryAdded( QString logEntry ) } +void FilterLogDialog::slotLogShrinked() +{ + textEdit->clear(); + QStringList logEntries = FilterLog::instance()->getLogEntries(); + for ( QStringList::Iterator it = logEntries.begin(); + it != logEntries.end(); ++it ) + { + textEdit->append( *it ); + } +} + + void FilterLogDialog::slotUser1() { FilterLog::instance()->clear(); diff --git a/filterlogdlg.h b/filterlogdlg.h index 0ec36b4cb..da4ebb9a6 100644 --- a/filterlogdlg.h +++ b/filterlogdlg.h @@ -52,6 +52,7 @@ namespace KMail { protected slots: void slotLogEntryAdded( QString logEntry ); + void slotLogShrinked(); protected: virtual void slotUser1(); diff --git a/kmsearchpattern.cpp b/kmsearchpattern.cpp index 2a640aad6..d12db4c22 100644 --- a/kmsearchpattern.cpp +++ b/kmsearchpattern.cpp @@ -137,7 +137,7 @@ bool KMSearchRule::matches( const DwString & aStr, KMMessage & msg, const QString KMSearchRule::asString() const { - QString result = "\t\"" + mField + "\" <"; + QString result = "\"" + mField + "\" <"; result += funcConfigNames[(int)mFunction]; result += "> \"" + mContents + "\""; @@ -285,7 +285,7 @@ bool KMSearchRuleString::matches( const KMMessage * msg ) const } bool rc = matchesInternal( msgContents ); if ( FilterLog::instance()->isLogging() ) { - QString msg = ( rc ? "\t1 = " : "\t0 = " ); + QString msg = ( rc ? "1 = " : "0 = " ); msg += asString() + " (" + msgContents + ")"; FilterLog::instance()->add( msg, FilterLog::ruleResult ); } @@ -378,9 +378,9 @@ bool KMSearchRuleNumerical::matches( const KMMessage * msg ) const } bool rc = matchesInternal( numericalValue, numericalMsgContents, msgContents ); if ( FilterLog::instance()->isLogging() ) { - QString msg = ( rc ? "\t1 = " : "\t0 = " ); - msg += asString() + " ( " + QString::number(numericalValue); - msg += " / " + QString::number(numericalMsgContents) + " )"; + QString msg = ( rc ? "1 = " : "0 = " ); + msg += asString() + " ( " + QString::number( numericalMsgContents ); + msg += " / " + QString::number( numericalValue ) + " )"; FilterLog::instance()->add( msg, FilterLog::ruleResult ); } return rc; @@ -514,7 +514,7 @@ bool KMSearchRuleStatus::matches( const KMMessage * msg ) const } if ( FilterLog::instance()->isLogging() ) { - QString msg = ( rc ? "\t1 = " : "\t0 = " ); + QString msg = ( rc ? "1 = " : "0 = " ); msg += asString(); FilterLog::instance()->add( msg, FilterLog::ruleResult ); } @@ -722,12 +722,12 @@ void KMSearchPattern::init() { QString KMSearchPattern::asString() const { QString result = "\t"; if ( mOperator == OpOr ) - result += i18n("(match any of the following)") + "\n"; + result += i18n("(match any of the following)") + '\n'; else - result += i18n("(match all of the following)") + "\n"; + result += i18n("(match all of the following)") + '\n'; for ( QPtrListIterator it( *this ) ; it.current() ; ++it ) - result += (*it)->asString() + '\n'; + result += '\t' + (*it)->asString() + '\n'; return result; }