Commit abdd064eb08692afef6da0b7ce84d960f76389db

Authored by Thiago Rocha
1 parent c0f01be4
Exists in master

Daemon fazendo log no diretório de build; registrando início e fim do serviço.

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&lt;QCoreApplication&gt;(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
... ... @@ -5,5 +5,6 @@
5 5 int main(int argc, char **argv)
6 6 {
7 7 cacicD service(argc, argv);
  8 +
8 9 return service.exec();
9 10 }
... ...
src/QLogger.cpp
... ... @@ -147,21 +147,24 @@ namespace QLogger
147 147  
148 148 int MAX_SIZE = 1024 * 1024;
149 149  
150   - fileDestSplit = _fileName.split("/");
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 + if (!dir.exists(dirDest))
  166 + dir.mkdir(dirDest);
  167 + }
165 168  
166 169 QFile file(_fileName);
167 170 QString toRemove = _fileName.section('.',-1);
... ... @@ -169,10 +172,10 @@ namespace QLogger
169 172 bool renamed = false;
170 173 QString newName = fileNameAux + "_%1__%2.log";
171 174  
172   - //Renomenem l'arxiu si està ple
  175 + //Renomeia o arquivo se ele está cheio
173 176 if (file.size() >= MAX_SIZE)
174 177 {
175   - //Creem un fixer nou
  178 + //Cria um novo arquivo
176 179 QDateTime currentTime = QDateTime::currentDateTime();
177 180 newName = newName.arg(currentTime.date().toString("dd_MM_yy")).arg(currentTime.time().toString("hh_mm_ss"));
178 181 renamed = file.rename(_fileName, newName);
... ... @@ -182,6 +185,7 @@ namespace QLogger
182 185 file.setFileName(_fileName);
183 186 if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
184 187 {
  188 +
185 189 QTextStream out(&file);
186 190 QString dtFormat = QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz");
187 191  
... ... @@ -190,8 +194,12 @@ namespace QLogger
190 194  
191 195 QString logLevel = QLoggerManager::levelToText(m_level);
192 196 QString text = QString("[%1] [%2] {%3} %4\n").arg(dtFormat).arg(logLevel).arg(module).arg(message);
  197 +
193 198 out << text;
  199 +
194 200 file.close();
  201 + } else {
  202 + qWarning() << "Não foi possível abrir arquivo de log.";
195 203 }
196 204 }
197 205 }
... ...
src/QLogger.h
... ... @@ -25,6 +25,7 @@
25 25 #include <QMap>
26 26 #include <QThread>
27 27 #include <QMutex>
  28 +#include <QDebug>
28 29  
29 30 /**************************************************************************************************/
30 31 /*** GENERAL USAGE ***/
... ...