00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef QEXTEND_CORE_LOGGER_HPP
00021 #define QEXTEND_CORE_LOGGER_HPP
00022
00023 #include <cstdio>
00024 #include <QtCore/QObject>
00025 #include <QtCore/QStringList>
00026 #include <QtCore/QFile>
00027 #include <QtCore/QMutex>
00028 #include <QExtend/Core/qextend_global>
00029 #include <QExtend/Core/ExtendPtr>
00030
00031 namespace QExtend
00032 {
00033
00036
00043 typedef enum {
00044 DEBUG = 0,
00045 WARNING = 1,
00046 ERROR = 2,
00047 CRITICAL = 3,
00048 FATAL = 4,
00049 } LoggerType;
00050
00053
00062 class QEXTEND_EXPORT ILog : public QObject
00063 {
00064 Q_OBJECT
00065 protected:
00066 QString m_logFormat;
00068 QString m_dateFormat;
00070 QStringList m_typesNames;
00073
00074
00075
00094 QString createLogMessage(LoggerType type, QString message, QString location);
00095
00098
00105 void initLogAttributes();
00106
00107 public :
00110
00129 void setLogFormat(QString logFormat);
00132
00141 void setDateFormat(QString dateFormat);
00144
00153 void setTypeName(LoggerType type, QString name);
00156
00163 const QString & getLogFormat();
00166
00173 const QString & getDateFormat();
00176
00185 const QString & getTypeName(LoggerType type);
00186
00187 public Q_SLOTS:
00190
00203 virtual void writeLog(LoggerType type, QString message, QString location)=0;
00206
00213 virtual void cleanLog()=0;
00214 };
00215
00218
00225 class QEXTEND_EXPORT LogFile : public ILog
00226 {
00227 Q_OBJECT
00228 private:
00229 QFile *m_file;
00232 public:
00235
00244 LogFile(QString filename);
00247
00254 ~LogFile();
00255
00256 public Q_SLOTS:
00259 void writeLog(LoggerType type, QString message, QString location);
00262 void cleanLog();
00263 };
00264
00267
00274 class QEXTEND_EXPORT LogStdout : public ILog
00275 {
00276 Q_OBJECT
00277
00278 public:
00281 LogStdout();
00282
00283 public Q_SLOTS:
00286 void writeLog(LoggerType type, QString message, QString location);
00289 void cleanLog();
00290 };
00291
00294
00303 class QEXTEND_EXPORT Logger : public QObject
00304 {
00305 Q_OBJECT
00306 private:
00307 static QMutex m_loggerMutex;
00310
00311
00312 static Logger & instance();
00313
00314 public:
00317
00328 static void log(LoggerType type, const char * message, const char * location);
00331
00340 static void connectToLogger(ILog* receiver);
00341
00342 Q_SIGNALS:
00345
00356 void sendLog(LoggerType type, QString message, QString location);
00357 };
00358
00359 #define STRINGIFY(x) #x
00360 #define TOSTRING(x) STRINGIFY(x)
00361
00362
00363 #define AT __FILE__ ":" TOSTRING(__LINE__)
00364
00365
00366 #define LOG(type, message) \
00367 Logger::log(type, message, AT);
00368
00369 }
00370
00371 #endif // QEXTEND_CORE_LOGGER_HPP