File indexing completed on 2024-12-22 04:17:20
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2004 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include "debug.h" 0014 0015 #include <config.h> 0016 0017 #ifdef KST_HAVE_REVISION_H 0018 #include "kstrevision.h" 0019 #endif 0020 0021 #include "datasource.h" 0022 #include "logevents.h" 0023 0024 #include <qlocale.h> 0025 #include <qapplication.h> 0026 #include <qdebug.h> 0027 0028 #include "datasourcepluginmanager.h" 0029 0030 #include "ksttimers.h" 0031 0032 namespace Kst { 0033 0034 Debug *Debug::_self = 0L; 0035 void Debug::cleanup() { 0036 delete _self; 0037 _self = 0; 0038 } 0039 0040 0041 static QMutex soLock; 0042 Debug *Debug::self() { 0043 QMutexLocker ml(&soLock); 0044 if (!_self) { 0045 _self = new Debug; 0046 qAddPostRoutine(Debug::cleanup); 0047 } 0048 0049 return _self; 0050 } 0051 0052 0053 Debug::Debug() 0054 : QObject() { 0055 _applyLimit = false; 0056 _limit = 10000; 0057 #ifdef KST_REVISION 0058 _kstRevision = QString::fromLatin1(KST_REVISION); 0059 #else 0060 _kstRevision = -1; 0061 #endif 0062 _hasNewError = false; 0063 } 0064 0065 0066 Debug::~Debug() { 0067 #ifdef BENCHMARK 0068 qDebug() << "DRAW COUNTS ---------------------------------------" << endl; 0069 for (QMap<QString,int>::ConstIterator i = _drawCounter.begin(); i != _drawCounter.end(); ++i) { 0070 qDebug() << i.key() << ": " << i.value() << endl; 0071 } 0072 #endif 0073 } 0074 0075 0076 int Debug::limit() const { 0077 QMutexLocker ml(&_lock); 0078 return _limit; 0079 } 0080 0081 0082 QStringList Debug::dataSourcePlugins() const { 0083 return DataSourcePluginManager::pluginList(); 0084 } 0085 0086 0087 void Debug::setHandler(QObject *handler) { 0088 _handler = handler; 0089 } 0090 0091 0092 void Debug::log(const QString& msg, LogLevel level) { 0093 QMutexLocker ml(&_lock); 0094 LogMessage message; 0095 0096 message.date = QDateTime::currentDateTime(); 0097 message.msg = msg; 0098 message.level = level; 0099 0100 _messages.append(message); 0101 if (_applyLimit && int(_messages.size()) > _limit) { 0102 QList<LogMessage>::Iterator first = _messages.begin(); 0103 QList<LogMessage>::Iterator last = first; 0104 last += _messages.size() - _limit; 0105 _messages.erase(first, last); 0106 } 0107 0108 if (level == Error) { 0109 _hasNewError = true; 0110 } 0111 0112 if (_handler) { 0113 LogEvent *e = new LogEvent(LogEvent::LogAdded); 0114 e->_msg = message; 0115 QApplication::postEvent(_handler, e); 0116 } 0117 } 0118 0119 0120 void Debug::clear() { 0121 clearHasNewError(); // has to be before the lock is acquired 0122 QMutexLocker ml(&_lock); 0123 _messages.clear(); 0124 LogEvent *e = new LogEvent(LogEvent::LogCleared); 0125 QApplication::postEvent(_handler, e); 0126 } 0127 0128 0129 QString Debug::label(LogLevel level) const { 0130 switch (level) { 0131 case Error: 0132 return tr("Error"); 0133 case Warning: 0134 return tr("Warning"); 0135 case Notice: 0136 return tr("Notice"); 0137 case Trace: 0138 return tr("Trace"); 0139 default: 0140 return tr("Other"); 0141 } 0142 } 0143 0144 0145 QString Debug::text() { 0146 QMutexLocker ml(&_lock); 0147 QString body = tr("Kst version %1\n\n\nKst log:\n").arg(KSTVERSION); 0148 0149 QLocale locale; 0150 for (int i = 0; i < _messages.count(); i++ ) { 0151 body += QString("%1 %2: %3\n").arg(_messages[i].date.toString(locale.dateFormat())).arg(label(_messages[i].level)).arg(_messages[i].msg); 0152 } 0153 0154 body += tr("\n\nData-source plugins:"); 0155 QStringList dsp = dataSourcePlugins(); 0156 for (QStringList::ConstIterator it = dsp.constBegin(); it != dsp.constEnd(); ++it) { 0157 body += '\n'; 0158 body += *it; 0159 } 0160 body += "\n\n"; 0161 return body; 0162 } 0163 0164 0165 void Debug::setLimit(bool applyLimit, int limit) { 0166 QMutexLocker ml(&_lock); 0167 _applyLimit = applyLimit; 0168 _limit = limit; 0169 } 0170 0171 0172 QList<Debug::LogMessage> Debug::messages() const { 0173 QMutexLocker ml(&_lock); 0174 return _messages; 0175 } 0176 0177 0178 Debug::LogMessage Debug::message(unsigned n) const { 0179 QMutexLocker ml(&_lock); 0180 if (_messages.size() > int(n)) { 0181 return _messages[n]; 0182 } 0183 return Debug::LogMessage(); 0184 } 0185 0186 0187 int Debug::logLength() const { 0188 QMutexLocker ml(&_lock); 0189 return _messages.size(); 0190 } 0191 0192 0193 const QString& Debug::kstRevision() const { 0194 QMutexLocker ml(&_lock); 0195 return _kstRevision; 0196 } 0197 0198 0199 bool Debug::hasNewError() const { 0200 QMutexLocker ml(&_lock); 0201 return _hasNewError; 0202 } 0203 0204 0205 void Debug::clearHasNewError() { 0206 QMutexLocker ml(&_lock); 0207 _hasNewError = false; 0208 } 0209 0210 } 0211 // vim: ts=2 sw=2 et