Commit 23179c2034eab9e411447a80253dab38d9a22de8
Exists in
master
Merge branch 'master' of https://github.com/lightbase/cacic-agente
Showing
6 changed files
with
102 additions
and
32 deletions
Show diff stats
cacic-daemon/cacicD/cacicd.cpp
1 | 1 | #include "cacicd.h" |
2 | 2 | |
3 | -cacicD::cacicD(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, "CacicD") | |
3 | +cacicD::cacicD(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, "CacicDaemon") | |
4 | 4 | { |
5 | 5 | try{ |
6 | + this->createApplication(argc, argv); | |
7 | + | |
6 | 8 | Ocacictimer = new CacicTimer(); |
7 | 9 | ccacic = new CCacic; |
8 | 10 | setServiceDescription("Cacic Daemon"); |
9 | 11 | setServiceFlags(QtService::Default); |
12 | + | |
13 | + logManager = QLogger::QLoggerManager::getInstance(); | |
14 | + logManager->addDestination(application()->applicationDirPath().append("/log.txt"),"Cacic Daemon",QLogger::InfoLevel); | |
15 | + logManager->addDestination(application()->applicationDirPath().append("/log.txt"),"Cacic Daemon",QLogger::ErrorLevel); | |
10 | 16 | } catch (...){ |
11 | 17 | qCritical() << "Error desconhecido no construtor."; |
12 | 18 | } |
... | ... | @@ -15,7 +21,8 @@ cacicD::cacicD(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, |
15 | 21 | cacicD::~cacicD() |
16 | 22 | { |
17 | 23 | try{ |
18 | - | |
24 | + logManager->closeLogger(); | |
25 | + application()->exit(); | |
19 | 26 | } catch (...){ |
20 | 27 | qCritical() << "Error desconhecido no desconstrutor."; |
21 | 28 | } |
... | ... | @@ -23,15 +30,19 @@ cacicD::~cacicD() |
23 | 30 | |
24 | 31 | void cacicD::start() { |
25 | 32 | try{ |
26 | - this->app = application(); | |
33 | + qDebug() << "Inicio do daemon"; | |
27 | 34 | |
28 | - qDebug() << "\nServiço iniciado em" << app->applicationDirPath(); | |
29 | - QJsonObject result = ccacic->getJsonFromFile(app->applicationDirPath() + "/getConfig.json"); | |
35 | + qDebug() << "\nServiço iniciado em" << application()->applicationDirPath(); | |
36 | + QLogger::QLog_Info("Cacic Daemon", QString("Servico iniciado em ").append(application()->applicationDirPath()).append(".")); | |
37 | + | |
38 | + QJsonObject result = ccacic->getJsonFromFile(application()->applicationDirPath() + "/getConfig.json"); | |
30 | 39 | if(!result.contains("error") && !result.isEmpty()){ |
31 | - Ocacictimer->iniciarTimer(result["codestatus"].toInt(), app->applicationDirPath()); | |
40 | + Ocacictimer->iniciarTimer(result["codestatus"].toInt(), application()->applicationDirPath()); | |
32 | 41 | }else{ |
33 | 42 | qDebug() << "getConfig.json não encontrado."; |
34 | 43 | } |
44 | + | |
45 | + QLogger::QLog_Info("Cacic Daemon", "Serviço finalizado."); | |
35 | 46 | }catch (...){ |
36 | 47 | qCritical() << "Error desconhecido ao iniciar o serviço."; |
37 | 48 | } | ... | ... |
cacic-daemon/cacicD/cacicd.h
1 | 1 | #ifndef CACICD_H |
2 | 2 | #define CACICD_H |
3 | 3 | #include <QCoreApplication> |
4 | +#include <QEventLoop> | |
4 | 5 | #include "qtservice.h" |
5 | 6 | #include "cacictimer.h" |
7 | +#include "QLogger.h" | |
8 | + | |
6 | 9 | |
7 | 10 | class cacicD : public QtService<QCoreApplication> |
8 | 11 | { |
9 | 12 | public: |
10 | - QCoreApplication *app; | |
13 | +// QCoreApplication *app; | |
14 | + QLogger::QLoggerManager *logManager; | |
11 | 15 | CacicTimer *Ocacictimer; |
12 | 16 | CCacic *ccacic; |
13 | 17 | cacicD(int argc, char **argv); |
14 | 18 | ~cacicD(); |
19 | + | |
15 | 20 | void start(); |
16 | 21 | void pause(); |
17 | 22 | void resume(); |
18 | 23 | void stop(); |
24 | + | |
25 | +private: | |
26 | + | |
19 | 27 | }; |
20 | 28 | #endif // CACICD_H | ... | ... |
cacic-daemon/cacicD/main.cpp
cacic-teste/testcacic.cpp
... | ... | @@ -266,25 +266,67 @@ void CTestCacic::testColeta() |
266 | 266 | void CTestCacic::testLogger() |
267 | 267 | { |
268 | 268 | QLogger::QLoggerManager *logManager = QLogger::QLoggerManager::getInstance(); |
269 | - logManager->addDestination("log.txt","teste",QLogger::DebugLevel); | |
270 | - | |
271 | - QLogger::QLog_Debug("teste", "Teste do modulo logger."); | |
272 | - | |
273 | - QFile logFile("log.txt"); | |
274 | - if(logFile.exists()) logFile.open(QIODevice::ReadOnly); | |
269 | + logManager->addDestination("log01.txt","teste01",QLogger::DebugLevel); | |
270 | + logManager->addDestination("./log02.txt","teste02",QLogger::DebugLevel); | |
271 | + logManager->addDestination("../log03.txt","teste03",QLogger::DebugLevel); | |
272 | + logManager->addDestination("logs/log04.txt","teste04",QLogger::DebugLevel); | |
273 | + logManager->addDestination("./logs/log05.txt","teste05",QLogger::DebugLevel); | |
274 | + logManager->addDestination("../logs/log06.txt","teste06",QLogger::DebugLevel); | |
275 | + | |
276 | + QLogger::QLog_Debug("teste01", "Teste do modulo logger."); | |
277 | + QLogger::QLog_Debug("teste02", "Teste do modulo logger."); | |
278 | + QLogger::QLog_Debug("teste03", "Teste do modulo logger."); | |
279 | + QLogger::QLog_Debug("teste04", "Teste do modulo logger."); | |
280 | + QLogger::QLog_Debug("teste05", "Teste do modulo logger."); | |
281 | + QLogger::QLog_Debug("teste06", "Teste do modulo logger."); | |
282 | + | |
283 | + QFile logFile01("log01.txt"); | |
284 | + if(logFile01.exists()) logFile01.open(QIODevice::ReadOnly); | |
285 | + QFile logFile02("./log02.txt"); | |
286 | + if(logFile02.exists()) logFile02.open(QIODevice::ReadOnly); | |
287 | + QFile logFile03("../log03.txt"); | |
288 | + if(logFile03.exists()) logFile03.open(QIODevice::ReadOnly); | |
289 | + QFile logFile04("logs/log04.txt"); | |
290 | + if(logFile04.exists()) logFile04.open(QIODevice::ReadOnly); | |
291 | + QFile logFile05("./logs/log05.txt"); | |
292 | + if(logFile05.exists()) logFile05.open(QIODevice::ReadOnly); | |
293 | + QFile logFile06("../logs/log06.txt"); | |
294 | + if(logFile06.exists()) logFile06.open(QIODevice::ReadOnly); | |
275 | 295 | |
276 | 296 | QVERIFY(logManager && |
277 | - logFile.exists() && | |
278 | - logFile.readLine().contains("Teste do modulo logger.") | |
297 | + logFile01.exists() && | |
298 | + logFile01.readLine().contains("Teste do modulo logger.") && | |
299 | + logFile02.exists() && | |
300 | + logFile02.readLine().contains("Teste do modulo logger.") && | |
301 | + logFile03.exists() && | |
302 | + logFile03.readLine().contains("Teste do modulo logger.") && | |
303 | + logFile04.exists() && | |
304 | + logFile04.readLine().contains("Teste do modulo logger.") && | |
305 | + logFile05.exists() && | |
306 | + logFile05.readLine().contains("Teste do modulo logger.") && | |
307 | + logFile06.exists() && | |
308 | + logFile06.readLine().contains("Teste do modulo logger.") | |
279 | 309 | ); |
280 | 310 | |
281 | 311 | logManager->closeLogger(); |
282 | - logFile.close(); | |
312 | + logFile01.close(); | |
313 | + logFile02.close(); | |
314 | + logFile03.close(); | |
315 | + logFile04.close(); | |
316 | + logFile05.close(); | |
317 | + logFile06.close(); | |
283 | 318 | } |
284 | 319 | |
285 | 320 | void CTestCacic::cleanupTestCase() |
286 | 321 | { |
287 | - OCacic.deleteFile("log.txt"); | |
322 | + OCacic.deleteFile("log01.txt"); | |
323 | + OCacic.deleteFile("./log02.txt"); | |
324 | + OCacic.deleteFile("../log03.txt"); | |
325 | + OCacic.deleteFile("logs/log04.txt"); | |
326 | + OCacic.deleteFile("./logs/log05.txt"); | |
327 | + OCacic.deleteFolder("./logs"); | |
328 | + OCacic.deleteFile("../logs/log06.txt"); | |
329 | + OCacic.deleteFolder("../logs"); | |
288 | 330 | OCacic.deleteFile("configRequest.json"); |
289 | 331 | OCacic.deleteFile("teste.json"); |
290 | 332 | OCacic.deleteFile("configReq.json"); | ... | ... |
src/QLogger.cpp
... | ... | @@ -140,29 +140,31 @@ namespace QLogger |
140 | 140 | |
141 | 141 | void QLoggerWriter::write(const QString &module, const QString &message) |
142 | 142 | { |
143 | - QString originalDir = QDir::currentPath(); | |
143 | + | |
144 | 144 | QString _fileName = m_fileDestination; |
145 | + QStringList fileDestSplit; | |
146 | + QString dirDest; | |
145 | 147 | |
146 | 148 | int MAX_SIZE = 1024 * 1024; |
147 | 149 | |
148 | - | |
149 | - QStringList fileDestSplit = _fileName.split("/"); | |
150 | - QString dirDest; | |
151 | - | |
152 | - for(int i = 0 ; i < fileDestSplit.size() ; ++i ) | |
150 | + if (_fileName.contains("/")) | |
153 | 151 | { |
154 | - if( !(i == fileDestSplit.size() - 1) ) // last fileDestSplit element | |
152 | + fileDestSplit = _fileName.split("/"); | |
153 | + | |
154 | + for(int i = 0 ; i < fileDestSplit.size() ; ++i ) | |
155 | 155 | { |
156 | - dirDest.append(fileDestSplit[i] + "/"); | |
156 | + if( !(i == fileDestSplit.size() - 1) ) // last fileDestSplit element | |
157 | + { | |
158 | + dirDest.append(fileDestSplit[i] + "/"); | |
159 | + } | |
157 | 160 | } |
158 | - } | |
159 | 161 | |
160 | - QDir dir(QDir::currentPath()); | |
161 | 162 | |
162 | - if (!dir.exists(dirDest)) | |
163 | - dir.mkdir(dirDest); | |
163 | + QDir dir(QDir::currentPath()); | |
164 | 164 | |
165 | -// dir.cd(dirDest); | |
165 | + if (!dir.exists(dirDest)) | |
166 | + dir.mkdir(dirDest); | |
167 | + } | |
166 | 168 | |
167 | 169 | QFile file(_fileName); |
168 | 170 | QString toRemove = _fileName.section('.',-1); |
... | ... | @@ -170,10 +172,10 @@ namespace QLogger |
170 | 172 | bool renamed = false; |
171 | 173 | QString newName = fileNameAux + "_%1__%2.log"; |
172 | 174 | |
173 | - //Renomenem l'arxiu si està ple | |
175 | + //Renomeia o arquivo se ele está cheio | |
174 | 176 | if (file.size() >= MAX_SIZE) |
175 | 177 | { |
176 | - //Creem un fixer nou | |
178 | + //Cria um novo arquivo | |
177 | 179 | QDateTime currentTime = QDateTime::currentDateTime(); |
178 | 180 | newName = newName.arg(currentTime.date().toString("dd_MM_yy")).arg(currentTime.time().toString("hh_mm_ss")); |
179 | 181 | renamed = file.rename(_fileName, newName); |
... | ... | @@ -183,6 +185,7 @@ namespace QLogger |
183 | 185 | file.setFileName(_fileName); |
184 | 186 | if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append)) |
185 | 187 | { |
188 | + | |
186 | 189 | QTextStream out(&file); |
187 | 190 | QString dtFormat = QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz"); |
188 | 191 | |
... | ... | @@ -191,8 +194,12 @@ namespace QLogger |
191 | 194 | |
192 | 195 | QString logLevel = QLoggerManager::levelToText(m_level); |
193 | 196 | QString text = QString("[%1] [%2] {%3} %4\n").arg(dtFormat).arg(logLevel).arg(module).arg(message); |
197 | + | |
194 | 198 | out << text; |
199 | + | |
195 | 200 | file.close(); |
201 | + } else { | |
202 | + qWarning() << "Não foi possível abrir arquivo de log."; | |
196 | 203 | } |
197 | 204 | } |
198 | 205 | } | ... | ... |
src/QLogger.h