diff --git a/cacic-daemon/cacicD/cacicD.pro b/cacic-daemon/cacicD/cacicD.pro index cbb7995..565aa22 100644 --- a/cacic-daemon/cacicD/cacicD.pro +++ b/cacic-daemon/cacicD/cacicD.pro @@ -11,6 +11,7 @@ QT += network TARGET = cacic-service CONFIG += console CONFIG -= app_bundle +CONFIG += static win32 { LIBS += -LE:\LightBase\cacic-agente-project\cacic-agente\src\crypto++\lib -lcryptopp QT += axcontainer @@ -18,24 +19,25 @@ win32 { LIBS += -L/usr/lib -lcryptopp } TEMPLATE = app - +TEMPLATE = lib SOURCES += main.cpp \ - ../../src/ccacic.cpp \ - cacicd.cpp \ - ../../src/wmi.cpp \ - cacictimer.cpp \ - ../../src/cacic_computer.cpp \ - ../../src/operatingsystem.cpp \ - -HEADERS += \ - ../../src/ccacic.h \ - cacicd.h \ - cacictimer.h \ - ../../src/wmi.h \ - ../../src/cacic_computer.h \ - ../../src/operatingsystem.h \ - ../../src/cacic_comm.h \ + cacicd.cpp \ + cacictimer.cpp \ + ../../src/ccacic.cpp \ + ../../src/wmi.cpp \ + ../../src/cacic_computer.cpp \ + ../../src/operatingsystem.cpp \ + ../../src/QLogger.cpp + +HEADERS += cacicd.h \ + cacictimer.h \ + ../../src/ccacic.h \ + ../../src/wmi.h \ + ../../src/cacic_computer.h \ + ../../src/operatingsystem.h \ + ../../src/cacic_comm.h \ + ../../src/QLogger.h include(../../src/qtservice/src/qtservice.pri) diff --git a/cacic-daemon/cacicD/cacicd.h b/cacic-daemon/cacicD/cacicd.h index f0c1890..7aa9d73 100644 --- a/cacic-daemon/cacicD/cacicd.h +++ b/cacic-daemon/cacicD/cacicd.h @@ -1,11 +1,9 @@ #ifndef CACICD_H #define CACICD_H - #include #include "qtservice.h" #include "cacictimer.h" - class cacicD : public QtService { public: diff --git a/cacic-daemon/cacicD/cacictimer.cpp b/cacic-daemon/cacicD/cacictimer.cpp index 74e8bfd..550d23e 100644 --- a/cacic-daemon/cacicD/cacictimer.cpp +++ b/cacic-daemon/cacicD/cacictimer.cpp @@ -5,6 +5,8 @@ CacicTimer::CacicTimer() OCacicComm = new CacicComm(); ccacic = new CCacic(); timer = new QTimer(this); + //manager = QLogger::QLoggerManager::getInstance(); + //manager->addDestination("cacicLog.txt", QStringList("CacicD"), QLogger::LogLevel); connect(timer,SIGNAL(timeout()),this,SLOT(mslot())); } @@ -16,7 +18,7 @@ void CacicTimer::iniciarTimer(int x, QString applicationDirPath) void CacicTimer::mslot(){ if(getTest()){ - qDebug() << "getTest() success. - " + QDateTime::currentDateTime().toLocalTime().toString(); + //manager->QLogger::QLog_Trace("ModuleName", "Message: "); if(getConfig()){ qDebug() << "getConfig() success. - " + QDateTime::currentDateTime().toLocalTime().toString(); diff --git a/cacic-daemon/cacicD/cacictimer.h b/cacic-daemon/cacicD/cacictimer.h index a02a59f..66b3152 100644 --- a/cacic-daemon/cacicD/cacictimer.h +++ b/cacic-daemon/cacicD/cacictimer.h @@ -8,6 +8,7 @@ #include "ccacic.h" #include "cacic_comm.h" #include "cacic_computer.h" +#include "QLogger.h" class CacicTimer : public QObject { @@ -18,6 +19,7 @@ public: CacicComm *OCacicComm; CACIC_Computer OCacic_Computer; CCacic *ccacic; + //QLogger::QLoggerManager *manager; void iniciarTimer(int x, QString applicationDirPath); bool getTest(); bool getConfig(); diff --git a/src/QLogger.cpp b/src/QLogger.cpp new file mode 100644 index 0000000..5ece2a5 --- /dev/null +++ b/src/QLogger.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include "QLogger.h" + +/**************************************************************************************** + ** QLogger is a library to register and print logs into a file. + ** Copyright (C) 2013 Francesc Martinez + ** + ** This library is free software; you can redistribute it and/or + ** modify it under the terms of the GNU Lesser General Public + ** License as published by the Free Software Foundation; either + ** version 2.1 of the License, or (at your option) any later version. + ** + ** This library is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + ** Lesser General Public License for more details. + ** + ** You should have received a copy of the GNU Lesser General Public + ** License along with this library; if not, write to the Free Software + ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + ***************************************************************************************/ + +namespace QLogger +{ + void QLog_Trace(const QString &module, const QString &message) + { + QLog_(module, TraceLevel, message); + } + + void QLog_Debug(const QString &module, const QString &message) + { + QLog_(module, DebugLevel, message); + } + + void QLog_Info(const QString &module, const QString &message) + { + QLog_(module, InfoLevel, message); + } + + void QLog_Warning(const QString &module, const QString &message) + { + QLog_(module, WarnLevel, message); + } + + void QLog_Error(const QString &module, const QString &message) + { + QLog_(module, ErrorLevel, message); + } + + void QLog_Fatal(const QString &module, const QString &message) + { + QLog_(module, FatalLevel, message); + } + + void QLog_(const QString &module, LogLevel level, const QString &message) + { + QLoggerManager *manager = QLoggerManager::getInstance(); + + QMutexLocker(&manager->mutex); + + QLoggerWriter *logWriter = manager->getLogWriter(module); + + if (logWriter and logWriter->getLevel() <= level) + logWriter->write(module,message); + } + + //QLoggerManager + QLoggerManager * QLoggerManager::INSTANCE = NULL; + + QLoggerManager::QLoggerManager() : QThread(), mutex(QMutex::Recursive) + { + start(); + } + + QLoggerManager * QLoggerManager::getInstance() + { + if (!INSTANCE) + INSTANCE = new QLoggerManager(); + + return INSTANCE; + } + + QString QLoggerManager::levelToText(const LogLevel &level) + { + switch (level) + { + case TraceLevel: return "Trace"; + case DebugLevel: return "Debug"; + case InfoLevel: return "Info"; + case WarnLevel: return "Warning"; + case ErrorLevel: return "Error"; + case FatalLevel: return "Fatal"; + default: return QString(); + } + } + + bool QLoggerManager::addDestination(const QString &fileDest, const QString &module, LogLevel level) + { + QLoggerWriter *log; + + if (!moduleDest.contains(module)) + { + log = new QLoggerWriter(fileDest,level); + moduleDest.insert(module, log); + return true; + } + + return false; + } + + bool QLoggerManager::addDestination(const QString &fileDest, const QStringList &modules, LogLevel level) + { + QLoggerWriter *log; + foreach (QString module, modules) + { + if (!moduleDest.contains(module)) + { + log = new QLoggerWriter(fileDest,level); + moduleDest.insert(module, log); + return true; + } + } + return false; + } + + void QLoggerManager::closeLogger() + { + exit(0); + deleteLater(); + } + + QLoggerWriter::QLoggerWriter(const QString &fileDestination, LogLevel level) + { + m_fileDestination = fileDestination; + m_level = level; + } + + void QLoggerWriter::write(const QString &module, const QString &message) + { + QString _fileName = m_fileDestination; + + int MAX_SIZE = 1024 * 1024; + + QDir dir(QDir::currentPath()); + if (!dir.exists("logs")) + dir.mkdir("logs"); + + QFile file(_fileName); + QString toRemove = _fileName.section('.',-1); + QString fileNameAux = _fileName.left(_fileName.size() - toRemove.size()-1); + bool renamed = false; + QString newName = fileNameAux + "_%1__%2.log"; + + //Renomenem l'arxiu si està ple + if (file.size() >= MAX_SIZE) + { + //Creem un fixer nou + QDateTime currentTime = QDateTime::currentDateTime(); + newName = newName.arg(currentTime.date().toString("dd_MM_yy")).arg(currentTime.time().toString("hh_mm_ss")); + renamed = file.rename(_fileName, newName); + + } + + file.setFileName(_fileName); + if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append)) + { + QTextStream out(&file); + QString dtFormat = QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz"); + + if (renamed) + out << QString("%1 - Previuous log %2\n").arg(dtFormat).arg(newName); + + QString logLevel = QLoggerManager::levelToText(m_level); + QString text = QString("[%1] [%2] {%3} %4\n").arg(dtFormat).arg(logLevel).arg(module).arg(message); + out << text; + file.close(); + } + } +} diff --git a/src/QLogger.h b/src/QLogger.h new file mode 100644 index 0000000..53373bf --- /dev/null +++ b/src/QLogger.h @@ -0,0 +1,197 @@ +#ifndef QLOGGER_H +#define QLOGGER_H + +/**************************************************************************************** + ** QLogger is a library to register and print logs into a file. + ** Copyright (C) 2013 Francesc Martinez + ** + ** This library is free software; you can redistribute it and/or + ** modify it under the terms of the GNU Lesser General Public + ** License as published by the Free Software Foundation; either + ** version 2.1 of the License, or (at your option) any later version. + ** + ** This library is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + ** Lesser General Public License for more details. + ** + ** You should have received a copy of the GNU Lesser General Public + ** License along with this library; if not, write to the Free Software + ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + ***************************************************************************************/ + +#include +#include +#include +#include +#include + +/**************************************************************************************************/ +/*** GENERAL USAGE ***/ +/*** ***/ +/*** 1. Create an instance: QLoggerManager *manager = QLoggerManager::getInstance(); ***/ +/*** 2. Add a destination: manager->addDestination(filePathName, module, logLevel); ***/ +/*** 3. Print the log in the file with: QLog_ ***/ +/*** ***/ +/*** You can add as much destinations as you want. You also can add several modules for each ***/ +/*** log file. ***/ +/**************************************************************************************************/ + +namespace QLogger +{ + + /** + * @brief The LogLevel enum desfines the level of the log message. + */ + enum LogLevel { TraceLevel = 0, DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel }; + + /** + * @brief Here is done the call to write the message in the module. First of all is confirmed + * that the log level we want to write is less or equal to the level defined when we create the + * destination. + * + * @param module The module that the message references. + * @param level The level of the message. + * @param message The message. + */ + void QLog_(const QString &module, LogLevel level, const QString &message); + /** + * @brief Used to store Trace level messages. + * @param module The module that the message references. + * @param message The message. + */ + void QLog_Trace(const QString &module, const QString &message); + /** + * @brief Used to store Debug level messages. + * @param module The module that the message references. + * @param message The message. + */ + void QLog_Debug(const QString &module, const QString &message); + /** + * @brief Used to store Info level messages. + * @param module The module that the message references. + * @param message The message. + */ + void QLog_Info(const QString &module, const QString &message); + /** + * @brief Used to store Warning level messages. + * @param module The module that the message references. + * @param message The message. + */ + void QLog_Warning(const QString &module, const QString &message); + /** + * @brief Used to store Error level messages. + * @param module The module that the message references. + * @param message The message. + */ + void QLog_Error(const QString &module, const QString &message); + /** + * @brief Used to store Fatal level messages. + * @param module The module that the message references. + * @param message The message. + */ + void QLog_Fatal(const QString &module, const QString &message); + + /** + * @brief The QLoggerWriter class writes the message and manages the file where it is printed. + */ + class QLoggerWriter : public QObject + { + Q_OBJECT + + public: + /** + * @brief Constructor that gets the complete path and filename to create the file. It also + * configures the level of this file to filter the logs depending on the level. + * @param fileDestination The complete path. + * @param level The maximum level that is allowed. + */ + explicit QLoggerWriter(const QString &fileDestination, LogLevel level); + /** + * @brief Gets the current maximum level. + * @return The LogLevel. + */ + LogLevel getLevel() const { return m_level; } + /** + * @brief Within this method the message is written in the log file. If it would exceed + * from 1 MByte, another file will be created and the log message will be stored in the + * new one. The older file will be renamed with the date and time of this message to know + * where it is updated. + * + * @param module The module that corresponds to the message. + * @param message The message log. + */ + void write(const QString &module, const QString &message); + + private: + /** + * @brief Path and name of the file that will store the logs. + */ + QString m_fileDestination; + /** + * @brief Maximum log level allowed for the file. + */ + LogLevel m_level; + }; + + /** + * @brief The QLoggerManager class manages the different destination files that we would like to have. + */ + class QLoggerManager : public QThread + { + public: + /** + * @brief Gets an instance to the QLoggerManager. + * @return A pointer to the instance. + */ + static QLoggerManager * getInstance(); + /** + * @brief Converts the given level in a QString. + * @param level The log level in LogLevel format. + * @return The string with the name of the log level. + */ + static QString levelToText(const LogLevel &level); + /** + * @brief This method creates a QLoogerWriter that stores the name of the file and the log + * level assigned to it. Here is added to the map the different modules assigned to each + * log file. The method returns false if a module is configured to be stored in + * more than one file. + * + * @param fileDest The file name and path to print logs. + * @param modules The modules that will be stored in the file. + * @param level The maximum level allowed. + * @return Returns true if any error have been done. + */ + bool addDestination(const QString &fileDest, const QStringList &modules, LogLevel level); + /** + * @brief Gets the QLoggerWriter instance corresponding to the module module. + * @param module The module we look for. + * @return Retrns a pointer to the object. + */ + QLoggerWriter * getLogWriter(const QString &module) { return moduleDest.value(module); } + /** + * @brief This method closes the logger and the thread it represents. + */ + void closeLogger(); + /** + * @brief Mutex to make the method thread-safe. + */ + QMutex mutex; + + private: + /** + * @brief Instance of the class. + */ + static QLoggerManager *INSTANCE; + /** + * @brief Map that stores the module and the file it is assigned. + */ + QMap moduleDest; + /** + * @brief Default builder of the class. It starts the thread. + */ + QLoggerManager(); + }; +} + +#endif // QLOGGER_H -- libgit2 0.21.2