Commit abdd064eb08692afef6da0b7ce84d960f76389db
1 parent
c0f01be4
Exists in
master
Daemon fazendo log no diretório de build; registrando início e fim do serviço.
Showing
5 changed files
with
47 additions
and
18 deletions
Show diff stats
cacic-daemon/cacicD/cacicd.cpp
1 | #include "cacicd.h" | 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 | try{ | 5 | try{ |
6 | + this->createApplication(argc, argv); | ||
7 | + | ||
6 | Ocacictimer = new CacicTimer(); | 8 | Ocacictimer = new CacicTimer(); |
7 | ccacic = new CCacic; | 9 | ccacic = new CCacic; |
8 | setServiceDescription("Cacic Daemon"); | 10 | setServiceDescription("Cacic Daemon"); |
9 | setServiceFlags(QtService::Default); | 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 | } catch (...){ | 16 | } catch (...){ |
11 | qCritical() << "Error desconhecido no construtor."; | 17 | qCritical() << "Error desconhecido no construtor."; |
12 | } | 18 | } |
@@ -15,7 +21,8 @@ cacicD::cacicD(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, | @@ -15,7 +21,8 @@ cacicD::cacicD(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, | ||
15 | cacicD::~cacicD() | 21 | cacicD::~cacicD() |
16 | { | 22 | { |
17 | try{ | 23 | try{ |
18 | - | 24 | + logManager->closeLogger(); |
25 | + application()->exit(); | ||
19 | } catch (...){ | 26 | } catch (...){ |
20 | qCritical() << "Error desconhecido no desconstrutor."; | 27 | qCritical() << "Error desconhecido no desconstrutor."; |
21 | } | 28 | } |
@@ -23,15 +30,19 @@ cacicD::~cacicD() | @@ -23,15 +30,19 @@ cacicD::~cacicD() | ||
23 | 30 | ||
24 | void cacicD::start() { | 31 | void cacicD::start() { |
25 | try{ | 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 | if(!result.contains("error") && !result.isEmpty()){ | 39 | if(!result.contains("error") && !result.isEmpty()){ |
31 | - Ocacictimer->iniciarTimer(result["codestatus"].toInt(), app->applicationDirPath()); | 40 | + Ocacictimer->iniciarTimer(result["codestatus"].toInt(), application()->applicationDirPath()); |
32 | }else{ | 41 | }else{ |
33 | qDebug() << "getConfig.json não encontrado."; | 42 | qDebug() << "getConfig.json não encontrado."; |
34 | } | 43 | } |
44 | + | ||
45 | + QLogger::QLog_Info("Cacic Daemon", "Serviço finalizado."); | ||
35 | }catch (...){ | 46 | }catch (...){ |
36 | qCritical() << "Error desconhecido ao iniciar o serviço."; | 47 | qCritical() << "Error desconhecido ao iniciar o serviço."; |
37 | } | 48 | } |
cacic-daemon/cacicD/cacicd.h
1 | #ifndef CACICD_H | 1 | #ifndef CACICD_H |
2 | #define CACICD_H | 2 | #define CACICD_H |
3 | #include <QCoreApplication> | 3 | #include <QCoreApplication> |
4 | +#include <QEventLoop> | ||
4 | #include "qtservice.h" | 5 | #include "qtservice.h" |
5 | #include "cacictimer.h" | 6 | #include "cacictimer.h" |
7 | +#include "QLogger.h" | ||
8 | + | ||
6 | 9 | ||
7 | class cacicD : public QtService<QCoreApplication> | 10 | class cacicD : public QtService<QCoreApplication> |
8 | { | 11 | { |
9 | public: | 12 | public: |
10 | - QCoreApplication *app; | 13 | +// QCoreApplication *app; |
14 | + QLogger::QLoggerManager *logManager; | ||
11 | CacicTimer *Ocacictimer; | 15 | CacicTimer *Ocacictimer; |
12 | CCacic *ccacic; | 16 | CCacic *ccacic; |
13 | cacicD(int argc, char **argv); | 17 | cacicD(int argc, char **argv); |
14 | ~cacicD(); | 18 | ~cacicD(); |
19 | + | ||
15 | void start(); | 20 | void start(); |
16 | void pause(); | 21 | void pause(); |
17 | void resume(); | 22 | void resume(); |
18 | void stop(); | 23 | void stop(); |
24 | + | ||
25 | +private: | ||
26 | + | ||
19 | }; | 27 | }; |
20 | #endif // CACICD_H | 28 | #endif // CACICD_H |
cacic-daemon/cacicD/main.cpp
src/QLogger.cpp
@@ -147,21 +147,24 @@ namespace QLogger | @@ -147,21 +147,24 @@ namespace QLogger | ||
147 | 147 | ||
148 | int MAX_SIZE = 1024 * 1024; | 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 | QFile file(_fileName); | 169 | QFile file(_fileName); |
167 | QString toRemove = _fileName.section('.',-1); | 170 | QString toRemove = _fileName.section('.',-1); |
@@ -169,10 +172,10 @@ namespace QLogger | @@ -169,10 +172,10 @@ namespace QLogger | ||
169 | bool renamed = false; | 172 | bool renamed = false; |
170 | QString newName = fileNameAux + "_%1__%2.log"; | 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 | if (file.size() >= MAX_SIZE) | 176 | if (file.size() >= MAX_SIZE) |
174 | { | 177 | { |
175 | - //Creem un fixer nou | 178 | + //Cria um novo arquivo |
176 | QDateTime currentTime = QDateTime::currentDateTime(); | 179 | QDateTime currentTime = QDateTime::currentDateTime(); |
177 | newName = newName.arg(currentTime.date().toString("dd_MM_yy")).arg(currentTime.time().toString("hh_mm_ss")); | 180 | newName = newName.arg(currentTime.date().toString("dd_MM_yy")).arg(currentTime.time().toString("hh_mm_ss")); |
178 | renamed = file.rename(_fileName, newName); | 181 | renamed = file.rename(_fileName, newName); |
@@ -182,6 +185,7 @@ namespace QLogger | @@ -182,6 +185,7 @@ namespace QLogger | ||
182 | file.setFileName(_fileName); | 185 | file.setFileName(_fileName); |
183 | if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append)) | 186 | if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append)) |
184 | { | 187 | { |
188 | + | ||
185 | QTextStream out(&file); | 189 | QTextStream out(&file); |
186 | QString dtFormat = QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz"); | 190 | QString dtFormat = QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz"); |
187 | 191 | ||
@@ -190,8 +194,12 @@ namespace QLogger | @@ -190,8 +194,12 @@ namespace QLogger | ||
190 | 194 | ||
191 | QString logLevel = QLoggerManager::levelToText(m_level); | 195 | QString logLevel = QLoggerManager::levelToText(m_level); |
192 | QString text = QString("[%1] [%2] {%3} %4\n").arg(dtFormat).arg(logLevel).arg(module).arg(message); | 196 | QString text = QString("[%1] [%2] {%3} %4\n").arg(dtFormat).arg(logLevel).arg(module).arg(message); |
197 | + | ||
193 | out << text; | 198 | out << text; |
199 | + | ||
194 | file.close(); | 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,6 +25,7 @@ | ||
25 | #include <QMap> | 25 | #include <QMap> |
26 | #include <QThread> | 26 | #include <QThread> |
27 | #include <QMutex> | 27 | #include <QMutex> |
28 | +#include <QDebug> | ||
28 | 29 | ||
29 | /**************************************************************************************************/ | 30 | /**************************************************************************************************/ |
30 | /*** GENERAL USAGE ***/ | 31 | /*** GENERAL USAGE ***/ |