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 #ifndef DEBUG_H 14 #define DEBUG_H 15 16 #include <config.h> 17 18 #include <qdatetime.h> 19 #include <qpointer.h> 20 #include <qobject.h> 21 #include <qmutex.h> 22 23 #include <QThread> 24 25 #include "kst_export.h" 26 27 namespace Kst { 28 29 // This class has to be threadsafe 30 class KSTCORE_EXPORT Debug : public QObject { 31 Q_OBJECT 32 public: 33 enum LogLevel { 34 Trace = 1, 35 Notice = 2, 36 Warning = 4, 37 Error = 8 38 }; 39 struct LogMessage { 40 QDateTime date; 41 QString msg; 42 LogLevel level; 43 }; 44 static Debug *self(); 45 46 void clear(); 47 void log(const QString& msg, LogLevel level = Notice); 48 void setLimit(bool applyLimit, int limit); 49 QString text(); 50 51 #define DEBUG_LOG_FUNC(X, T) static void X(const QString& msg) { self()->log(msg, T); } 52 DEBUG_LOG_FUNC(error, Error); 53 DEBUG_LOG_FUNC(warning, Warning); 54 DEBUG_LOG_FUNC(notice, Notice); 55 DEBUG_LOG_FUNC(trace, Trace); 56 57 int logLength() const; 58 QList<LogMessage> messages() const; 59 Debug::LogMessage message(unsigned n) const; 60 QStringList dataSourcePlugins() const; 61 QString label(LogLevel level) const; 62 const QString& kstRevision() const; 63 64 int limit() const; 65 66 bool hasNewError() const; 67 void clearHasNewError(); 68 69 #ifdef BENCHMARK drawCounter()70 QMap<QString,int>& drawCounter() { return _drawCounter; } 71 #endif 72 73 void setHandler(QObject *handler); 74 75 private: 76 Debug(); 77 ~Debug(); 78 79 static Debug *_self; 80 static void cleanup(); 81 82 QList<LogMessage> _messages; 83 bool _applyLimit; 84 bool _hasNewError; 85 int _limit; 86 mutable QMutex _lock; 87 #ifdef BENCHMARK 88 // If this is ever public we can't do this 89 QMap<QString,int> _drawCounter; 90 #endif 91 QPointer<QObject> _handler; 92 QString _kstRevision; 93 }; 94 95 96 struct Sleep : QThread 97 { msSleep98 static void ms(int t) { QThread::msleep(t); } 99 }; 100 101 102 } 103 #endif 104 105 // vim: ts=2 sw=2 et 106