QSapecNG
 All Classes Functions Enumerations Properties
qlogger.h
00001 /*
00002     QSapecNG - Qt based SapecNG GUI front-end
00003     Copyright (C) 2009, Michele Caini
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 
00020 #ifndef QLOGGER_H
00021 #define QLOGGER_H
00022 
00023 
00024 #include "logger/logpolicy.h"
00025 #include "logger/logger.h"
00026 
00027 #include <QtCore/QObject>
00028 #include <QtCore/QRegExp>
00029 #include <QtGui/QColor>
00030 
00031 
00032 typedef sapecng::LogPolicy SapecNGLogger;
00033 typedef sapecng::LogPolicy SapecNGLogPolicy;
00034 
00035 
00036 namespace qsapecng
00037 {
00038 
00039 
00040 class QLogPolicy: public QObject, public SapecNGLogPolicy
00041 {
00042 
00043   Q_OBJECT
00044 
00045 public:
00046   QLogPolicy(QObject* parent = 0)
00047     : QObject(parent), SapecNGLogPolicy() { }
00048   ~QLogPolicy() { }
00049 
00050   void operator()(const std::string& msg)
00051   {
00052     QString qmsg = QString::fromStdString(msg);
00053     if(qmsg.contains(QRegExp("^\\[DEBUG\\]\\s*")) && debugColor_.isValid())
00054       emit textColorChanged(debugColor_);
00055     if(qmsg.contains(QRegExp("^\\[INFO\\]\\s*")) && infoColor_.isValid())
00056       emit textColorChanged(infoColor_);
00057     if(qmsg.contains(QRegExp("^\\[WARNING\\]\\s*")) && warningColor_.isValid())
00058       emit textColorChanged(warningColor_);
00059     if(qmsg.contains(QRegExp("^\\[ERROR\\]\\s*")) && errorColor_.isValid())
00060       emit textColorChanged(errorColor_);
00061     if(qmsg.contains(QRegExp("^\\[FATAL\\]\\s*")) && fatalColor_.isValid())
00062       emit textColorChanged(fatalColor_);
00063     emit log(qmsg);
00064   }
00065 
00066   inline QColor debugColor() const
00067     { return debugColor_; }
00068   inline void setDebugColor(QColor color)
00069     { debugColor_ = color; }
00070 
00071   inline QColor infoColor() const
00072     { return infoColor_; }
00073   inline void setInfoColor(QColor color)
00074     { infoColor_ = color; }
00075 
00076   inline QColor warningColor() const
00077     { return warningColor_; }
00078   inline void setWarningColor(QColor color)
00079     { warningColor_ = color; }
00080 
00081   inline QColor errorColor() const
00082     { return errorColor_; }
00083   inline void setErrorColor(QColor color)
00084     { errorColor_ = color; }
00085 
00086   inline QColor fatalColor() const
00087     { return fatalColor_; }
00088   inline void setFatalColor(QColor color)
00089     { fatalColor_ = color; }
00090 
00091 signals:
00092   void log(const QString& msg);
00093   void textColorChanged(const QColor& color);
00094 
00095 private:
00096   QColor debugColor_;
00097   QColor infoColor_;
00098   QColor warningColor_;
00099   QColor errorColor_;
00100   QColor fatalColor_;
00101 
00102 };
00103 
00104 
00105 class QLogger
00106 {
00107 
00108 public:
00109   static void setLevel(sapecng::Logger::LogLevel lvl)
00110     { sapecng::Logger::setLevel(lvl); }
00111 
00112   static void setPolicy(QLogPolicy* policy)
00113     { sapecng::Logger::setPolicy(policy); }
00114 
00115   static void debug(const QString& msg)
00116     {
00117       sapecng::Logger().get(sapecng::Logger::DEBUG)
00118         << (QObject::tr("[DEBUG] ") + msg).toStdString();
00119     }
00120 
00121   static void info(const QString& msg)
00122     {
00123       sapecng::Logger().get(sapecng::Logger::INFO)
00124         << (QObject::tr("[INFO] ") + msg).toStdString();
00125     }
00126 
00127   static void warning(const QString& msg)
00128     {
00129       sapecng::Logger().get(sapecng::Logger::WARNING)
00130         << (QObject::tr("[WARNING] ") + msg).toStdString();
00131     }
00132 
00133   static void error(const QString& msg)
00134     {
00135       sapecng::Logger().get(sapecng::Logger::ERROR)
00136         << (QObject::tr("[ERROR] ") + msg).toStdString();
00137     }
00138 
00139   static void fatal(const QString& msg)
00140     {
00141       sapecng::Logger().get(sapecng::Logger::FATAL)
00142         << (QObject::tr("[FATAL] ") + msg).toStdString();
00143     }
00144 
00145 };
00146 
00147 
00148 }
00149 
00150 
00151 #endif // QLOGGER_H
 All Classes Functions Enumerations Properties