00001 /* 00002 Copyright © 2010 jonathan Courtois 00003 00004 This file is part of QExtend. 00005 00006 QExtend is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 any later version. 00010 00011 QExtend is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with QExtend. If not, see <http://www.gnu.org/licenses/>. 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 00034 00036 00038 00045 typedef enum { 00046 DEBUG = 0, 00047 WARNING = 1, 00048 ERROR = 2, 00049 CRITICAL = 3, 00050 FATAL = 4, 00051 } LoggerType; 00052 00053 00055 00057 00066 class QEXTEND_EXPORT ILog : public QObject 00067 { 00068 Q_OBJECT 00069 protected: 00070 QString m_logFormat; 00072 QString m_dateFormat; 00074 QStringList m_typesNames; 00079 00080 00081 00083 00102 QString createLogMessage(LoggerType type, QString message, QString location); 00103 00104 00105 00107 00108 00110 00117 void initLogAttributes(); 00118 00119 public : 00120 00121 00123 00124 00126 00145 void setLogFormat(QString logFormat); 00146 00147 00149 00150 00152 00161 void setDateFormat(QString dateFormat); 00162 00163 00165 00166 00168 00177 void setTypeName(LoggerType type, QString name); 00178 00179 00181 00182 00184 00191 const QString & getLogFormat(); 00192 00193 00195 00196 00198 00205 const QString & getDateFormat(); 00206 00207 00209 00210 00212 00221 const QString & getTypeName(LoggerType type); 00222 00223 public Q_SLOTS: 00224 00225 00227 00228 00230 00243 virtual void writeLog(LoggerType type, QString message, QString location)=0; 00244 00245 00247 00248 00250 00257 virtual void cleanLog()=0; 00258 }; 00259 00260 00262 00264 00271 class QEXTEND_EXPORT LogFile : public ILog 00272 { 00273 Q_OBJECT 00274 private: 00275 QFile *m_file; 00278 public: 00279 00280 00282 00283 00285 00294 LogFile(QString filename); 00295 00296 00298 00299 00301 00308 ~LogFile(); 00309 00310 public Q_SLOTS: 00311 00312 00314 00315 00317 void writeLog(LoggerType type, QString message, QString location); 00318 00319 00321 00322 00324 void cleanLog(); 00325 }; 00326 00327 00329 00331 00338 class QEXTEND_EXPORT LogStdout : public ILog 00339 { 00340 Q_OBJECT 00341 00342 public: 00343 00344 00346 00347 00349 LogStdout(); 00350 00351 public Q_SLOTS: 00352 00353 00355 00356 00358 void writeLog(LoggerType type, QString message, QString location); 00359 00360 00362 00363 00365 void cleanLog(); 00366 }; 00367 00368 00370 00372 00381 class QEXTEND_EXPORT Logger : public QObject 00382 { 00383 Q_OBJECT 00384 private: 00385 static QMutex m_loggerMutex; 00390 00391 00392 00394 static Logger & instance(); 00395 00396 public: 00397 00398 00400 00401 00403 00414 static void log(LoggerType type, const char * message, const char * location); 00415 00416 00418 00419 00421 00430 static void connectToLogger(ILog* receiver); 00431 00432 Q_SIGNALS: 00433 00434 00436 00437 00439 00450 void sendLog(LoggerType type, QString message, QString location); 00451 }; 00452 00453 #define STRINGIFY(x) #x 00454 #define TOSTRING(x) STRINGIFY(x) 00455 00457 00459 #define AT __FILE__ ":" TOSTRING(__LINE__) 00460 00462 00464 #define LOG(type, message) \ 00465 Logger::log(type, message, AT); 00466 00467 } // QExtend namespace 00468 00469 #endif // QEXTEND_CORE_LOGGER_HPP
© 2000-2024 - www.developpez.com