1 /***************************************************************************
2  *                                                                         *
3  *   copyright : (C) 2004 The University of Toronto                        *
4  *                   netterfield@astro.utoronto.ca                         *
5 *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  ***************************************************************************/
12 
13 #include "debug.h"
14 
15 #include <config.h>
16 
17 #ifdef KST_HAVE_REVISION_H
18 #include "kstrevision.h"
19 #endif
20 
21 #include "datasource.h"
22 #include "logevents.h"
23 
24 #include <qlocale.h>
25 #include <qapplication.h>
26 #include <qdebug.h>
27 
28 #include "datasourcepluginmanager.h"
29 
30 #include "ksttimers.h"
31 
32 namespace Kst {
33 
34 Debug *Debug::_self = 0L;
cleanup()35 void Debug::cleanup() {
36     delete _self;
37     _self = 0;
38 }
39 
40 
41 static QMutex soLock;
self()42 Debug *Debug::self() {
43   QMutexLocker ml(&soLock);
44   if (!_self) {
45     _self = new Debug;
46     qAddPostRoutine(Debug::cleanup);
47   }
48 
49   return _self;
50 }
51 
52 
Debug()53 Debug::Debug()
54 : QObject() {
55   _applyLimit = false;
56   _limit = 10000;
57 #ifdef KST_REVISION
58   _kstRevision = QString::fromLatin1(KST_REVISION);
59 #else
60   _kstRevision = -1;
61 #endif
62   _hasNewError = false;
63 }
64 
65 
~Debug()66 Debug::~Debug() {
67 #ifdef BENCHMARK
68   qDebug() << "DRAW COUNTS ---------------------------------------" << endl;
69   for (QMap<QString,int>::ConstIterator i = _drawCounter.begin(); i != _drawCounter.end(); ++i) {
70     qDebug() << i.key() << ": " << i.value() << endl;
71   }
72 #endif
73 }
74 
75 
limit() const76 int Debug::limit() const {
77   QMutexLocker ml(&_lock);
78   return _limit;
79 }
80 
81 
dataSourcePlugins() const82 QStringList Debug::dataSourcePlugins() const {
83   return DataSourcePluginManager::pluginList();
84 }
85 
86 
setHandler(QObject * handler)87 void Debug::setHandler(QObject *handler) {
88   _handler = handler;
89 }
90 
91 
log(const QString & msg,LogLevel level)92 void Debug::log(const QString& msg, LogLevel level) {
93   QMutexLocker ml(&_lock);
94   LogMessage message;
95 
96   message.date  = QDateTime::currentDateTime();
97   message.msg   = msg;
98   message.level = level;
99 
100   _messages.append(message);
101   if (_applyLimit && int(_messages.size()) > _limit) {
102     QList<LogMessage>::Iterator first = _messages.begin();
103     QList<LogMessage>::Iterator last = first;
104     last += _messages.size() - _limit;
105     _messages.erase(first, last);
106   }
107 
108   if (level == Error) {
109     _hasNewError = true;
110   }
111 
112   if (_handler) {
113     LogEvent *e = new LogEvent(LogEvent::LogAdded);
114     e->_msg = message;
115     QApplication::postEvent(_handler, e);
116   }
117 }
118 
119 
clear()120 void Debug::clear() {
121   clearHasNewError(); // has to be before the lock is acquired
122   QMutexLocker ml(&_lock);
123   _messages.clear();
124   LogEvent *e = new LogEvent(LogEvent::LogCleared);
125   QApplication::postEvent(_handler, e);
126 }
127 
128 
label(LogLevel level) const129 QString Debug::label(LogLevel level) const {
130   switch (level) {
131     case Error:
132       return tr("Error");
133     case Warning:
134       return tr("Warning");
135     case Notice:
136       return tr("Notice");
137     case Trace:
138       return tr("Trace");
139     default:
140       return tr("Other");
141   }
142 }
143 
144 
text()145 QString Debug::text() {
146   QMutexLocker ml(&_lock);
147   QString body = tr("Kst version %1\n\n\nKst log:\n").arg(KSTVERSION);
148 
149   QLocale locale;
150   for (int i = 0; i < _messages.count(); i++ ) {
151     body += QString("%1 %2: %3\n").arg(_messages[i].date.toString(locale.dateFormat())).arg(label(_messages[i].level)).arg(_messages[i].msg);
152   }
153 
154   body += tr("\n\nData-source plugins:");
155   QStringList dsp = dataSourcePlugins();
156   for (QStringList::ConstIterator it = dsp.constBegin(); it != dsp.constEnd(); ++it) {
157     body += '\n';
158     body += *it;
159   }
160   body += "\n\n";
161   return body;
162 }
163 
164 
setLimit(bool applyLimit,int limit)165 void Debug::setLimit(bool applyLimit, int limit) {
166   QMutexLocker ml(&_lock);
167   _applyLimit = applyLimit;
168   _limit = limit;
169 }
170 
171 
messages() const172 QList<Debug::LogMessage> Debug::messages() const {
173   QMutexLocker ml(&_lock);
174   return _messages;
175 }
176 
177 
message(unsigned n) const178 Debug::LogMessage Debug::message(unsigned n) const {
179   QMutexLocker ml(&_lock);
180   if (_messages.size() > int(n)) {
181     return _messages[n];
182   }
183   return Debug::LogMessage();
184 }
185 
186 
logLength() const187 int Debug::logLength() const {
188   QMutexLocker ml(&_lock);
189   return _messages.size();
190 }
191 
192 
kstRevision() const193 const QString& Debug::kstRevision() const {
194   QMutexLocker ml(&_lock);
195   return _kstRevision;
196 }
197 
198 
hasNewError() const199 bool Debug::hasNewError() const {
200   QMutexLocker ml(&_lock);
201   return _hasNewError;
202 }
203 
204 
clearHasNewError()205 void Debug::clearHasNewError() {
206   QMutexLocker ml(&_lock);
207   _hasNewError = false;
208 }
209 
210 }
211 // vim: ts=2 sw=2 et
212