SilentEye 0.4.1

sef/logger.cpp

Go to the documentation of this file.
00001 //  This file is part of SilentEye.
00002 //
00003 //  SilentEye is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  SilentEye is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with SilentEye. If not, see <http://www.gnu.org/licenses/>.
00015 
00016 #include "logger.h"
00017 
00018 #include <QTime>
00019 #include <QDebug>
00020 
00021 namespace SilentEyeFramework {
00022 
00023     QPointer<QFile> Logger::m_file;
00024     int Logger::m_nbInstance = 0;
00025     QString m_fileName = "application.log";
00026     Logger::LogLevel Logger::m_level = DEBUG_LEVEL;
00027 
00028     Logger::Logger(QObject* parent) : QObject(parent)
00029     {
00030         if (parent != 0)
00031         {
00032             m_className = parent->objectName();
00033         }
00034         m_nbInstance++;
00035     }
00036 
00037     Logger::Logger(QString className)
00038     {
00039         m_className = className;
00040         m_nbInstance++;
00041     }
00042 
00043     void Logger::init()
00044     {
00045         if (m_file.isNull()) {
00046             m_file = new QFile(m_fileName);
00047             if (!m_file->open(QIODevice::WriteOnly | QIODevice::Text))
00048             {
00049                 qWarning() << "> Cannot open file " << m_fileName << " in write mode!";
00050             }
00051         }
00052     }
00053 
00054     Logger::~Logger() {
00055         m_nbInstance--;
00056         if (m_nbInstance <= 0 && !m_file.isNull()) {
00057             m_file->close();
00058             delete m_file;
00059         }
00060     }
00061 
00062     QString Logger::className() const
00063     {
00064         return m_className;
00065     }
00066 
00067     void Logger::setClassName(const QString& name)
00068     {
00069         m_className = name;
00070     }
00071 
00072     void Logger::write(QString priority, QString text) {
00073         init();
00074         QString value = priority + "[" + m_className + "][" + QTime::currentTime().toString("hh:mm:ss") + "] " + text;
00075         qDebug() << value;
00076         m_file->write(QString(value + "\n").toLatin1());
00077         m_file->flush();
00078     }
00079 
00080     void Logger::debug(QString text)
00081     {
00082         if (m_level <= DEBUG_LEVEL)
00083             write("DEBUG", text);
00084     }
00085 
00086     void Logger::info(QString text)
00087     {
00088         if (m_level <= INFO_LEVEL)
00089             write("INFO", text);
00090     }
00091 
00092     void Logger::warning(QString text)
00093     {
00094         if (m_level <= WARNING_LEVEL)
00095             write("WARNING", text);
00096     }
00097 
00098     void Logger::error(QString text)
00099     {
00100         if (m_level <= ERROR_LEVEL)
00101             write("ERROR", text);
00102     }
00103 
00104     void Logger::setFileName(const QString& name)
00105     {
00106         m_fileName = name;
00107     }
00108 
00109     void Logger::setLevel(Logger::LogLevel level)
00110     {
00111         m_level = level;
00112     }
00113 
00114     void Logger::setLevel(const QString& level)
00115     {
00116         if (level == "INFO")
00117             m_level = INFO_LEVEL;
00118         else if (level == "WARNING")
00119             m_level = WARNING_LEVEL;
00120         else if (level == "ERROR")
00121             m_level = ERROR_LEVEL;
00122         else
00123             m_level = DEBUG_LEVEL;
00124     }
00125 
00126 }