kmail

filterlog.cpp
1 /*
2  This file is part of KMail.
3  Copyright (c) 2003 Andreas Gungl <a.gungl@gmx.de>
4 
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.
8 
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.
13 
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
17 
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 TQt library by Trolltech AS, Norway (or with modified versions
21  of TQt that use the same license as TQt), 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  TQt. 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
27  your version.
28 */
29 
30 #include "filterlog.h"
31 
32 #include <kdebug.h>
33 
34 #include <tqdatetime.h>
35 #include <tqfile.h>
36 
37 #include <sys/stat.h>
38 
39 
40 using namespace KMail;
41 
42 
43 FilterLog * FilterLog::mSelf = NULL;
44 
45 
47 {
48  mSelf = this;
49  // start with logging disabled by default
50  mLogging = false;
51  // better limit the log to 512 KByte to avoid out of memory situations
52  // when the log i sgoing to become very long
53  mMaxLogSize = 512 * 1024;
54  mCurrentLogSize = 0;
55  mAllowedTypes = meta | patternDesc | ruleResult |
56  patternResult | appliedAction;
57 }
58 
59 
61 {}
62 
63 
65 {
66  if ( !mSelf ) mSelf = new FilterLog();
67  return mSelf;
68 }
69 
70 
71 void FilterLog::add( TQString logEntry, ContentType contentType )
72 {
73  if ( isLogging() && ( mAllowedTypes & contentType ) )
74  {
75  TQString timedLog = "[" + TQTime::currentTime().toString() + "] ";
76  if ( contentType & ~meta )
77  timedLog += logEntry;
78  else
79  timedLog = logEntry;
80  mLogEntries.append( timedLog );
81  emit logEntryAdded( timedLog );
82  mCurrentLogSize += timedLog.length();
83  checkLogSize();
84  }
85 }
86 
87 
88 void FilterLog::setMaxLogSize( long size )
89 {
90  if ( size < -1)
91  size = -1;
92  // do not allow less than 1 KByte except unlimited (-1)
93  if ( size >= 0 && size < 1024 )
94  size = 1024;
95  mMaxLogSize = size;
96  emit logStateChanged();
97  checkLogSize();
98 }
99 
100 
102 {
103 #ifndef NDEBUG
104  kdDebug(5006) << "----- starting filter log -----" << endl;
105  for ( TQStringList::Iterator it = mLogEntries.begin();
106  it != mLogEntries.end(); ++it )
107  {
108  kdDebug(5006) << *it << endl;
109  }
110  kdDebug(5006) << "------ end of filter log ------" << endl;
111 #endif
112 }
113 
114 
115 void FilterLog::checkLogSize()
116 {
117  if ( mCurrentLogSize > mMaxLogSize && mMaxLogSize > -1 )
118  {
119  kdDebug(5006) << "Filter log: memory limit reached, starting to discard old items, size = "
120  << TQString::number( mCurrentLogSize ) << endl;
121  // avoid some kind of hysteresis, shrink the log to 90% of its maximum
122  while ( mCurrentLogSize > ( mMaxLogSize * 0.9 ) )
123  {
124  TQValueListIterator<TQString> it = mLogEntries.begin();
125  if ( it != mLogEntries.end())
126  {
127  mCurrentLogSize -= (*it).length();
128  mLogEntries.remove( it );
129  kdDebug(5006) << "Filter log: new size = "
130  << TQString::number( mCurrentLogSize ) << endl;
131  }
132  else
133  {
134  kdDebug(5006) << "Filter log: size reduction disaster!" << endl;
135  clear();
136  }
137  }
138  emit logShrinked();
139  }
140 }
141 
142 
143 bool FilterLog::saveToFile( TQString fileName )
144 {
145  TQFile file( fileName );
146  if( file.open( IO_WriteOnly ) ) {
147  fchmod( file.handle(), S_IRUSR | S_IWUSR );
148  {
149  TQDataStream ds( &file );
150  for ( TQStringList::Iterator it = mLogEntries.begin();
151  it != mLogEntries.end(); ++it )
152  {
153  TQString tmpString = *it + '\n';
154  TQCString cstr( tmpString.local8Bit() );
155  ds.writeRawBytes( cstr, cstr.size() );
156  }
157  }
158  return true;
159  }
160  else
161  return false;
162 }
163 
164 
165 #include "filterlog.moc"
KMail Filter Log Collector.
Definition: filterlog.h:54
bool mLogging
the log status
Definition: filterlog.h:143
bool isLogging()
check the logging state
Definition: filterlog.h:74
TQStringList mLogEntries
The list contains the single log pieces.
Definition: filterlog.h:140
void setMaxLogSize(long size=-1)
control the size of the log
Definition: filterlog.cpp:88
void clear()
discard collected log data
Definition: filterlog.h:110
FilterLog()
Non-public constructor needed by the singleton implementation.
Definition: filterlog.cpp:46
bool saveToFile(TQString fileName)
save the log to a file - returns true if okay
Definition: filterlog.cpp:143
void add(TQString logEntry, ContentType contentType)
add a log entry
Definition: filterlog.cpp:71
static FilterLog * instance()
access to the singleton instance
Definition: filterlog.cpp:64
long mMaxLogSize
max size for kept log items, when reached the last recently added items are discarded -1 means unlimi...
Definition: filterlog.h:148
void dump()
dump the log - for testing purposes
Definition: filterlog.cpp:101
virtual ~FilterLog()
destructor
Definition: filterlog.cpp:60
int mAllowedTypes
types currently allowed to be legged
Definition: filterlog.h:152
ContentType
log data types
Definition: filterlog.h:64
folderdiaquotatab.h
Definition: aboutdata.cpp:40