Commit 9522da3465aff70bfc93a6a81c1ef4b56151261b
1 parent
f882771a
Exists in
master
Inicio do QLogger
Showing
6 changed files
with
401 additions
and
19 deletions
Show diff stats
cacic-daemon/cacicD/cacicD.pro
@@ -11,6 +11,7 @@ QT += network | @@ -11,6 +11,7 @@ QT += network | ||
11 | TARGET = cacic-service | 11 | TARGET = cacic-service |
12 | CONFIG += console | 12 | CONFIG += console |
13 | CONFIG -= app_bundle | 13 | CONFIG -= app_bundle |
14 | +CONFIG += static | ||
14 | win32 { | 15 | win32 { |
15 | LIBS += -LE:\LightBase\cacic-agente-project\cacic-agente\src\crypto++\lib -lcryptopp | 16 | LIBS += -LE:\LightBase\cacic-agente-project\cacic-agente\src\crypto++\lib -lcryptopp |
16 | QT += axcontainer | 17 | QT += axcontainer |
@@ -18,24 +19,25 @@ win32 { | @@ -18,24 +19,25 @@ win32 { | ||
18 | LIBS += -L/usr/lib -lcryptopp | 19 | LIBS += -L/usr/lib -lcryptopp |
19 | } | 20 | } |
20 | TEMPLATE = app | 21 | TEMPLATE = app |
21 | - | 22 | +TEMPLATE = lib |
22 | 23 | ||
23 | SOURCES += main.cpp \ | 24 | SOURCES += main.cpp \ |
24 | - ../../src/ccacic.cpp \ | ||
25 | - cacicd.cpp \ | ||
26 | - ../../src/wmi.cpp \ | ||
27 | - cacictimer.cpp \ | ||
28 | - ../../src/cacic_computer.cpp \ | ||
29 | - ../../src/operatingsystem.cpp \ | ||
30 | - | ||
31 | -HEADERS += \ | ||
32 | - ../../src/ccacic.h \ | ||
33 | - cacicd.h \ | ||
34 | - cacictimer.h \ | ||
35 | - ../../src/wmi.h \ | ||
36 | - ../../src/cacic_computer.h \ | ||
37 | - ../../src/operatingsystem.h \ | ||
38 | - ../../src/cacic_comm.h \ | 25 | + cacicd.cpp \ |
26 | + cacictimer.cpp \ | ||
27 | + ../../src/ccacic.cpp \ | ||
28 | + ../../src/wmi.cpp \ | ||
29 | + ../../src/cacic_computer.cpp \ | ||
30 | + ../../src/operatingsystem.cpp \ | ||
31 | + ../../src/QLogger.cpp | ||
32 | + | ||
33 | +HEADERS += cacicd.h \ | ||
34 | + cacictimer.h \ | ||
35 | + ../../src/ccacic.h \ | ||
36 | + ../../src/wmi.h \ | ||
37 | + ../../src/cacic_computer.h \ | ||
38 | + ../../src/operatingsystem.h \ | ||
39 | + ../../src/cacic_comm.h \ | ||
40 | + ../../src/QLogger.h | ||
39 | 41 | ||
40 | include(../../src/qtservice/src/qtservice.pri) | 42 | include(../../src/qtservice/src/qtservice.pri) |
41 | 43 |
cacic-daemon/cacicD/cacicd.h
1 | #ifndef CACICD_H | 1 | #ifndef CACICD_H |
2 | #define CACICD_H | 2 | #define CACICD_H |
3 | - | ||
4 | #include <QCoreApplication> | 3 | #include <QCoreApplication> |
5 | #include "qtservice.h" | 4 | #include "qtservice.h" |
6 | #include "cacictimer.h" | 5 | #include "cacictimer.h" |
7 | 6 | ||
8 | - | ||
9 | class cacicD : public QtService<QCoreApplication> | 7 | class cacicD : public QtService<QCoreApplication> |
10 | { | 8 | { |
11 | public: | 9 | public: |
cacic-daemon/cacicD/cacictimer.cpp
@@ -5,6 +5,8 @@ CacicTimer::CacicTimer() | @@ -5,6 +5,8 @@ CacicTimer::CacicTimer() | ||
5 | OCacicComm = new CacicComm(); | 5 | OCacicComm = new CacicComm(); |
6 | ccacic = new CCacic(); | 6 | ccacic = new CCacic(); |
7 | timer = new QTimer(this); | 7 | timer = new QTimer(this); |
8 | + //manager = QLogger::QLoggerManager::getInstance(); | ||
9 | + //manager->addDestination("cacicLog.txt", QStringList("CacicD"), QLogger::LogLevel); | ||
8 | connect(timer,SIGNAL(timeout()),this,SLOT(mslot())); | 10 | connect(timer,SIGNAL(timeout()),this,SLOT(mslot())); |
9 | } | 11 | } |
10 | 12 | ||
@@ -16,7 +18,7 @@ void CacicTimer::iniciarTimer(int x, QString applicationDirPath) | @@ -16,7 +18,7 @@ void CacicTimer::iniciarTimer(int x, QString applicationDirPath) | ||
16 | 18 | ||
17 | void CacicTimer::mslot(){ | 19 | void CacicTimer::mslot(){ |
18 | if(getTest()){ | 20 | if(getTest()){ |
19 | - qDebug() << "getTest() success. - " + QDateTime::currentDateTime().toLocalTime().toString(); | 21 | + //manager->QLogger::QLog_Trace("ModuleName", "Message: "); |
20 | if(getConfig()){ | 22 | if(getConfig()){ |
21 | qDebug() << "getConfig() success. - " + QDateTime::currentDateTime().toLocalTime().toString(); | 23 | qDebug() << "getConfig() success. - " + QDateTime::currentDateTime().toLocalTime().toString(); |
22 | 24 |
cacic-daemon/cacicD/cacictimer.h
@@ -8,6 +8,7 @@ | @@ -8,6 +8,7 @@ | ||
8 | #include "ccacic.h" | 8 | #include "ccacic.h" |
9 | #include "cacic_comm.h" | 9 | #include "cacic_comm.h" |
10 | #include "cacic_computer.h" | 10 | #include "cacic_computer.h" |
11 | +#include "QLogger.h" | ||
11 | 12 | ||
12 | class CacicTimer : public QObject | 13 | class CacicTimer : public QObject |
13 | { | 14 | { |
@@ -18,6 +19,7 @@ public: | @@ -18,6 +19,7 @@ public: | ||
18 | CacicComm *OCacicComm; | 19 | CacicComm *OCacicComm; |
19 | CACIC_Computer OCacic_Computer; | 20 | CACIC_Computer OCacic_Computer; |
20 | CCacic *ccacic; | 21 | CCacic *ccacic; |
22 | + //QLogger::QLoggerManager *manager; | ||
21 | void iniciarTimer(int x, QString applicationDirPath); | 23 | void iniciarTimer(int x, QString applicationDirPath); |
22 | bool getTest(); | 24 | bool getTest(); |
23 | bool getConfig(); | 25 | bool getConfig(); |
@@ -0,0 +1,181 @@ | @@ -0,0 +1,181 @@ | ||
1 | +#include <QDir> | ||
2 | +#include <QDateTime> | ||
3 | +#include <QTextStream> | ||
4 | +#include "QLogger.h" | ||
5 | + | ||
6 | +/**************************************************************************************** | ||
7 | + ** QLogger is a library to register and print logs into a file. | ||
8 | + ** Copyright (C) 2013 Francesc Martinez <es.linkedin.com/in/cescmm/en> | ||
9 | + ** | ||
10 | + ** This library is free software; you can redistribute it and/or | ||
11 | + ** modify it under the terms of the GNU Lesser General Public | ||
12 | + ** License as published by the Free Software Foundation; either | ||
13 | + ** version 2.1 of the License, or (at your option) any later version. | ||
14 | + ** | ||
15 | + ** This library is distributed in the hope that it will be useful, | ||
16 | + ** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | + ** Lesser General Public License for more details. | ||
19 | + ** | ||
20 | + ** You should have received a copy of the GNU Lesser General Public | ||
21 | + ** License along with this library; if not, write to the Free Software | ||
22 | + ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
23 | + ***************************************************************************************/ | ||
24 | + | ||
25 | +namespace QLogger | ||
26 | +{ | ||
27 | + void QLog_Trace(const QString &module, const QString &message) | ||
28 | + { | ||
29 | + QLog_(module, TraceLevel, message); | ||
30 | + } | ||
31 | + | ||
32 | + void QLog_Debug(const QString &module, const QString &message) | ||
33 | + { | ||
34 | + QLog_(module, DebugLevel, message); | ||
35 | + } | ||
36 | + | ||
37 | + void QLog_Info(const QString &module, const QString &message) | ||
38 | + { | ||
39 | + QLog_(module, InfoLevel, message); | ||
40 | + } | ||
41 | + | ||
42 | + void QLog_Warning(const QString &module, const QString &message) | ||
43 | + { | ||
44 | + QLog_(module, WarnLevel, message); | ||
45 | + } | ||
46 | + | ||
47 | + void QLog_Error(const QString &module, const QString &message) | ||
48 | + { | ||
49 | + QLog_(module, ErrorLevel, message); | ||
50 | + } | ||
51 | + | ||
52 | + void QLog_Fatal(const QString &module, const QString &message) | ||
53 | + { | ||
54 | + QLog_(module, FatalLevel, message); | ||
55 | + } | ||
56 | + | ||
57 | + void QLog_(const QString &module, LogLevel level, const QString &message) | ||
58 | + { | ||
59 | + QLoggerManager *manager = QLoggerManager::getInstance(); | ||
60 | + | ||
61 | + QMutexLocker(&manager->mutex); | ||
62 | + | ||
63 | + QLoggerWriter *logWriter = manager->getLogWriter(module); | ||
64 | + | ||
65 | + if (logWriter and logWriter->getLevel() <= level) | ||
66 | + logWriter->write(module,message); | ||
67 | + } | ||
68 | + | ||
69 | + //QLoggerManager | ||
70 | + QLoggerManager * QLoggerManager::INSTANCE = NULL; | ||
71 | + | ||
72 | + QLoggerManager::QLoggerManager() : QThread(), mutex(QMutex::Recursive) | ||
73 | + { | ||
74 | + start(); | ||
75 | + } | ||
76 | + | ||
77 | + QLoggerManager * QLoggerManager::getInstance() | ||
78 | + { | ||
79 | + if (!INSTANCE) | ||
80 | + INSTANCE = new QLoggerManager(); | ||
81 | + | ||
82 | + return INSTANCE; | ||
83 | + } | ||
84 | + | ||
85 | + QString QLoggerManager::levelToText(const LogLevel &level) | ||
86 | + { | ||
87 | + switch (level) | ||
88 | + { | ||
89 | + case TraceLevel: return "Trace"; | ||
90 | + case DebugLevel: return "Debug"; | ||
91 | + case InfoLevel: return "Info"; | ||
92 | + case WarnLevel: return "Warning"; | ||
93 | + case ErrorLevel: return "Error"; | ||
94 | + case FatalLevel: return "Fatal"; | ||
95 | + default: return QString(); | ||
96 | + } | ||
97 | + } | ||
98 | + | ||
99 | + bool QLoggerManager::addDestination(const QString &fileDest, const QString &module, LogLevel level) | ||
100 | + { | ||
101 | + QLoggerWriter *log; | ||
102 | + | ||
103 | + if (!moduleDest.contains(module)) | ||
104 | + { | ||
105 | + log = new QLoggerWriter(fileDest,level); | ||
106 | + moduleDest.insert(module, log); | ||
107 | + return true; | ||
108 | + } | ||
109 | + | ||
110 | + return false; | ||
111 | + } | ||
112 | + | ||
113 | + bool QLoggerManager::addDestination(const QString &fileDest, const QStringList &modules, LogLevel level) | ||
114 | + { | ||
115 | + QLoggerWriter *log; | ||
116 | + foreach (QString module, modules) | ||
117 | + { | ||
118 | + if (!moduleDest.contains(module)) | ||
119 | + { | ||
120 | + log = new QLoggerWriter(fileDest,level); | ||
121 | + moduleDest.insert(module, log); | ||
122 | + return true; | ||
123 | + } | ||
124 | + } | ||
125 | + return false; | ||
126 | + } | ||
127 | + | ||
128 | + void QLoggerManager::closeLogger() | ||
129 | + { | ||
130 | + exit(0); | ||
131 | + deleteLater(); | ||
132 | + } | ||
133 | + | ||
134 | + QLoggerWriter::QLoggerWriter(const QString &fileDestination, LogLevel level) | ||
135 | + { | ||
136 | + m_fileDestination = fileDestination; | ||
137 | + m_level = level; | ||
138 | + } | ||
139 | + | ||
140 | + void QLoggerWriter::write(const QString &module, const QString &message) | ||
141 | + { | ||
142 | + QString _fileName = m_fileDestination; | ||
143 | + | ||
144 | + int MAX_SIZE = 1024 * 1024; | ||
145 | + | ||
146 | + QDir dir(QDir::currentPath()); | ||
147 | + if (!dir.exists("logs")) | ||
148 | + dir.mkdir("logs"); | ||
149 | + | ||
150 | + QFile file(_fileName); | ||
151 | + QString toRemove = _fileName.section('.',-1); | ||
152 | + QString fileNameAux = _fileName.left(_fileName.size() - toRemove.size()-1); | ||
153 | + bool renamed = false; | ||
154 | + QString newName = fileNameAux + "_%1__%2.log"; | ||
155 | + | ||
156 | + //Renomenem l'arxiu si està ple | ||
157 | + if (file.size() >= MAX_SIZE) | ||
158 | + { | ||
159 | + //Creem un fixer nou | ||
160 | + QDateTime currentTime = QDateTime::currentDateTime(); | ||
161 | + newName = newName.arg(currentTime.date().toString("dd_MM_yy")).arg(currentTime.time().toString("hh_mm_ss")); | ||
162 | + renamed = file.rename(_fileName, newName); | ||
163 | + | ||
164 | + } | ||
165 | + | ||
166 | + file.setFileName(_fileName); | ||
167 | + if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append)) | ||
168 | + { | ||
169 | + QTextStream out(&file); | ||
170 | + QString dtFormat = QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz"); | ||
171 | + | ||
172 | + if (renamed) | ||
173 | + out << QString("%1 - Previuous log %2\n").arg(dtFormat).arg(newName); | ||
174 | + | ||
175 | + QString logLevel = QLoggerManager::levelToText(m_level); | ||
176 | + QString text = QString("[%1] [%2] {%3} %4\n").arg(dtFormat).arg(logLevel).arg(module).arg(message); | ||
177 | + out << text; | ||
178 | + file.close(); | ||
179 | + } | ||
180 | + } | ||
181 | +} |
@@ -0,0 +1,197 @@ | @@ -0,0 +1,197 @@ | ||
1 | +#ifndef QLOGGER_H | ||
2 | +#define QLOGGER_H | ||
3 | + | ||
4 | +/**************************************************************************************** | ||
5 | + ** QLogger is a library to register and print logs into a file. | ||
6 | + ** Copyright (C) 2013 Francesc Martinez <es.linkedin.com/in/cescmm/en> | ||
7 | + ** | ||
8 | + ** This library is free software; you can redistribute it and/or | ||
9 | + ** modify it under the terms of the GNU Lesser General Public | ||
10 | + ** License as published by the Free Software Foundation; either | ||
11 | + ** version 2.1 of the License, or (at your option) any later version. | ||
12 | + ** | ||
13 | + ** This library is distributed in the hope that it will be useful, | ||
14 | + ** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | + ** Lesser General Public License for more details. | ||
17 | + ** | ||
18 | + ** You should have received a copy of the GNU Lesser General Public | ||
19 | + ** License along with this library; if not, write to the Free Software | ||
20 | + ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | + ***************************************************************************************/ | ||
22 | + | ||
23 | +#include <QStringList> | ||
24 | +#include <QObject> | ||
25 | +#include <QMap> | ||
26 | +#include <QThread> | ||
27 | +#include <QMutex> | ||
28 | + | ||
29 | +/**************************************************************************************************/ | ||
30 | +/*** GENERAL USAGE ***/ | ||
31 | +/*** ***/ | ||
32 | +/*** 1. Create an instance: QLoggerManager *manager = QLoggerManager::getInstance(); ***/ | ||
33 | +/*** 2. Add a destination: manager->addDestination(filePathName, module, logLevel); ***/ | ||
34 | +/*** 3. Print the log in the file with: QLog_<Trace/Debug/Info/Warning/Error/Fatal> ***/ | ||
35 | +/*** ***/ | ||
36 | +/*** You can add as much destinations as you want. You also can add several modules for each ***/ | ||
37 | +/*** log file. ***/ | ||
38 | +/**************************************************************************************************/ | ||
39 | + | ||
40 | +namespace QLogger | ||
41 | +{ | ||
42 | + | ||
43 | + /** | ||
44 | + * @brief The LogLevel enum desfines the level of the log message. | ||
45 | + */ | ||
46 | + enum LogLevel { TraceLevel = 0, DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel }; | ||
47 | + | ||
48 | + /** | ||
49 | + * @brief Here is done the call to write the message in the module. First of all is confirmed | ||
50 | + * that the log level we want to write is less or equal to the level defined when we create the | ||
51 | + * destination. | ||
52 | + * | ||
53 | + * @param module The module that the message references. | ||
54 | + * @param level The level of the message. | ||
55 | + * @param message The message. | ||
56 | + */ | ||
57 | + void QLog_(const QString &module, LogLevel level, const QString &message); | ||
58 | + /** | ||
59 | + * @brief Used to store Trace level messages. | ||
60 | + * @param module The module that the message references. | ||
61 | + * @param message The message. | ||
62 | + */ | ||
63 | + void QLog_Trace(const QString &module, const QString &message); | ||
64 | + /** | ||
65 | + * @brief Used to store Debug level messages. | ||
66 | + * @param module The module that the message references. | ||
67 | + * @param message The message. | ||
68 | + */ | ||
69 | + void QLog_Debug(const QString &module, const QString &message); | ||
70 | + /** | ||
71 | + * @brief Used to store Info level messages. | ||
72 | + * @param module The module that the message references. | ||
73 | + * @param message The message. | ||
74 | + */ | ||
75 | + void QLog_Info(const QString &module, const QString &message); | ||
76 | + /** | ||
77 | + * @brief Used to store Warning level messages. | ||
78 | + * @param module The module that the message references. | ||
79 | + * @param message The message. | ||
80 | + */ | ||
81 | + void QLog_Warning(const QString &module, const QString &message); | ||
82 | + /** | ||
83 | + * @brief Used to store Error level messages. | ||
84 | + * @param module The module that the message references. | ||
85 | + * @param message The message. | ||
86 | + */ | ||
87 | + void QLog_Error(const QString &module, const QString &message); | ||
88 | + /** | ||
89 | + * @brief Used to store Fatal level messages. | ||
90 | + * @param module The module that the message references. | ||
91 | + * @param message The message. | ||
92 | + */ | ||
93 | + void QLog_Fatal(const QString &module, const QString &message); | ||
94 | + | ||
95 | + /** | ||
96 | + * @brief The QLoggerWriter class writes the message and manages the file where it is printed. | ||
97 | + */ | ||
98 | + class QLoggerWriter : public QObject | ||
99 | + { | ||
100 | + Q_OBJECT | ||
101 | + | ||
102 | + public: | ||
103 | + /** | ||
104 | + * @brief Constructor that gets the complete path and filename to create the file. It also | ||
105 | + * configures the level of this file to filter the logs depending on the level. | ||
106 | + * @param fileDestination The complete path. | ||
107 | + * @param level The maximum level that is allowed. | ||
108 | + */ | ||
109 | + explicit QLoggerWriter(const QString &fileDestination, LogLevel level); | ||
110 | + /** | ||
111 | + * @brief Gets the current maximum level. | ||
112 | + * @return The LogLevel. | ||
113 | + */ | ||
114 | + LogLevel getLevel() const { return m_level; } | ||
115 | + /** | ||
116 | + * @brief Within this method the message is written in the log file. If it would exceed | ||
117 | + * from 1 MByte, another file will be created and the log message will be stored in the | ||
118 | + * new one. The older file will be renamed with the date and time of this message to know | ||
119 | + * where it is updated. | ||
120 | + * | ||
121 | + * @param module The module that corresponds to the message. | ||
122 | + * @param message The message log. | ||
123 | + */ | ||
124 | + void write(const QString &module, const QString &message); | ||
125 | + | ||
126 | + private: | ||
127 | + /** | ||
128 | + * @brief Path and name of the file that will store the logs. | ||
129 | + */ | ||
130 | + QString m_fileDestination; | ||
131 | + /** | ||
132 | + * @brief Maximum log level allowed for the file. | ||
133 | + */ | ||
134 | + LogLevel m_level; | ||
135 | + }; | ||
136 | + | ||
137 | + /** | ||
138 | + * @brief The QLoggerManager class manages the different destination files that we would like to have. | ||
139 | + */ | ||
140 | + class QLoggerManager : public QThread | ||
141 | + { | ||
142 | + public: | ||
143 | + /** | ||
144 | + * @brief Gets an instance to the QLoggerManager. | ||
145 | + * @return A pointer to the instance. | ||
146 | + */ | ||
147 | + static QLoggerManager * getInstance(); | ||
148 | + /** | ||
149 | + * @brief Converts the given level in a QString. | ||
150 | + * @param level The log level in LogLevel format. | ||
151 | + * @return The string with the name of the log level. | ||
152 | + */ | ||
153 | + static QString levelToText(const LogLevel &level); | ||
154 | + /** | ||
155 | + * @brief This method creates a QLoogerWriter that stores the name of the file and the log | ||
156 | + * level assigned to it. Here is added to the map the different modules assigned to each | ||
157 | + * log file. The method returns <em>false</em> if a module is configured to be stored in | ||
158 | + * more than one file. | ||
159 | + * | ||
160 | + * @param fileDest The file name and path to print logs. | ||
161 | + * @param modules The modules that will be stored in the file. | ||
162 | + * @param level The maximum level allowed. | ||
163 | + * @return Returns true if any error have been done. | ||
164 | + */ | ||
165 | + bool addDestination(const QString &fileDest, const QStringList &modules, LogLevel level); | ||
166 | + /** | ||
167 | + * @brief Gets the QLoggerWriter instance corresponding to the module <em>module</em>. | ||
168 | + * @param module The module we look for. | ||
169 | + * @return Retrns a pointer to the object. | ||
170 | + */ | ||
171 | + QLoggerWriter * getLogWriter(const QString &module) { return moduleDest.value(module); } | ||
172 | + /** | ||
173 | + * @brief This method closes the logger and the thread it represents. | ||
174 | + */ | ||
175 | + void closeLogger(); | ||
176 | + /** | ||
177 | + * @brief Mutex to make the method thread-safe. | ||
178 | + */ | ||
179 | + QMutex mutex; | ||
180 | + | ||
181 | + private: | ||
182 | + /** | ||
183 | + * @brief Instance of the class. | ||
184 | + */ | ||
185 | + static QLoggerManager *INSTANCE; | ||
186 | + /** | ||
187 | + * @brief Map that stores the module and the file it is assigned. | ||
188 | + */ | ||
189 | + QMap<QString,QLoggerWriter*> moduleDest; | ||
190 | + /** | ||
191 | + * @brief Default builder of the class. It starts the thread. | ||
192 | + */ | ||
193 | + QLoggerManager(); | ||
194 | + }; | ||
195 | +} | ||
196 | + | ||
197 | +#endif // QLOGGER_H |