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 #ifndef DEBUG_H
0014 #define DEBUG_H
0015 
0016 #include <config.h>
0017 
0018 #include <qdatetime.h>
0019 #include <qpointer.h>
0020 #include <qobject.h>
0021 #include <qmutex.h>
0022 
0023 #include <QThread>
0024 
0025 #include "kst_export.h"
0026 
0027 namespace Kst {
0028 
0029 // This class has to be threadsafe
0030 class KSTCORE_EXPORT Debug : public QObject {
0031   Q_OBJECT
0032   public:
0033     enum LogLevel {
0034       Trace     = 1,
0035       Notice    = 2,
0036       Warning   = 4,
0037       Error     = 8
0038     };
0039     struct LogMessage {
0040       QDateTime date;
0041       QString msg;
0042       LogLevel level;
0043     };
0044     static Debug *self();
0045 
0046     void clear();
0047     void log(const QString& msg, LogLevel level = Notice);
0048     void setLimit(bool applyLimit, int limit);
0049     QString text();
0050 
0051 #define DEBUG_LOG_FUNC(X, T) static void X(const QString& msg) { self()->log(msg, T); }
0052     DEBUG_LOG_FUNC(error, Error);
0053     DEBUG_LOG_FUNC(warning, Warning);
0054     DEBUG_LOG_FUNC(notice, Notice);
0055     DEBUG_LOG_FUNC(trace, Trace);
0056 
0057     int logLength() const;
0058     QList<LogMessage> messages() const;
0059     Debug::LogMessage message(unsigned n) const;
0060     QStringList dataSourcePlugins() const;
0061     QString label(LogLevel level) const;
0062     const QString& kstRevision() const;
0063 
0064     int limit() const;
0065 
0066     bool hasNewError() const;
0067     void clearHasNewError();
0068 
0069 #ifdef BENCHMARK
0070     QMap<QString,int>& drawCounter() { return _drawCounter; }
0071 #endif
0072 
0073     void setHandler(QObject *handler);
0074 
0075   private:
0076     Debug();
0077     ~Debug();
0078 
0079     static Debug *_self;
0080     static void cleanup();
0081 
0082     QList<LogMessage> _messages;
0083     bool _applyLimit;
0084     bool _hasNewError;
0085     int _limit;
0086     mutable QMutex _lock;
0087 #ifdef BENCHMARK
0088     // If this is ever public we can't do this
0089     QMap<QString,int> _drawCounter;
0090 #endif
0091     QPointer<QObject> _handler;
0092     QString _kstRevision;
0093 };
0094 
0095 
0096 struct Sleep : QThread
0097 {
0098     static void ms(int t) { QThread::msleep(t); }
0099 };
0100 
0101 
0102 }
0103 #endif
0104 
0105 // vim: ts=2 sw=2 et