-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogreader.cpp
171 lines (151 loc) · 6.69 KB
/
logreader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/****************************************************************************
**
** Druid - Is a piece of program for read text file and store as json data.
** This is part of it's code.
** Copyright (C) 2016 Gustavo Brondani Schenkel
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "logreader.h"
#include <QtCore/QDebug>
#include <QtCore/QDateTime>
LogReader::LogReader(QObject *parent) : QObject(parent)
{
if (!isRunning("mongod.exe")){
qDebug() << "MongoDB Server is not running!";
exit(0);
}
connect(&watcher, SIGNAL(fileChanged(QString)), this, SLOT(fileChanged(QString)));
connect(this, SIGNAL(dataReceived(QString)), &sHandle, SLOT(checkString(QString)));
connect(&sHandle, SIGNAL(codeChanged(CodeType)), &json, SLOT(inputCode(CodeType)));
connect(&sHandle, SIGNAL(codeChanged(CodeType)), &db, SLOT(inputCode(CodeType)));
connect(&sHandle, SIGNAL(dataValidated(QRegularExpressionMatch)), &json, SLOT(inputData(QRegularExpressionMatch)));
connect(&json, SIGNAL(jsonCreated(QJsonObject)), &db, SLOT(receiveData(QJsonObject)));
connect(this, SIGNAL(readToWrite()), &db, SLOT(openMongoConn()));
connect(this, SIGNAL(writenDone()), &db, SLOT(closeMongoConn()));
directory.setPath("Z:/Log");
fileList << "/Pvqvscmw.log" << "/Pvqvscmw1.log"
<< "/Pvqvscmw2.log" << "/Pvqvscmw3.log"
<< "/Pvqvscmw4.log" << "/Pvqvscmw5.log"
<< "/Pvqvscmw6.log" << "/Pvqvscmw7.log"
<< "/Pvqvscmw8.log" << "/Pvqvscmw9.log";
// directory.setPath("C:/Temp/Data");
// fileList << "/Pvqvscmw.log";
// directory.setPath("C:/Temp/Data");
// fileList << "/Log (1).log" << "/Log (2).log"
// << "/Log (3).log" << "/Log (4).log"
// << "/Log (5).log" << "/Log (6).log"
// << "/Log (7).log" << "/Log (8).log"
// << "/Log (9).log" << "/Log (10).log"
// << "/Log (11).log" << "/Log (12).log"
// << "/Log (13).log" << "/Log (14).log"
// << "/Log (15).log" << "/Log (16).log"
// << "/Log (17).log" << "/Log (18).log"
// << "/Log (19).log" << "/Log (20).log"
// << "/Log (21).log" << "/Log (22).log"
// << "/Log (23).log" << "/Log (24).log"
// << "/Log (25).log" << "/Log (26).log"
// << "/Log (27).log" << "/Log (28).log"
// << "/Log (29).log" << "/Log (30).log"
// << "/Log (31).log" << "/Log (32).log"
// << "/Log (33).log" << "/Log (34).log"
// << "/Log (35).log" << "/Log (36).log"
// << "/Log (37).log" << "/Log (38).log"
// << "/Log (39).log" << "/Log (40).log"
// << "/Log (41).log" << "/Log (42).log";
// Força a leitura no arquivo na inicialização.
readFile(0);
watcher.addPath(directory.absolutePath()+fileList.at(0));
}
void LogReader::fileChanged(QString str)
{
if (watcher.files().empty())
watcher.addPath(directory.absolutePath()+fileList.at(0));
str.remove(directory.path());
readFile(fileList.indexOf(str));
}
void LogReader::readFile(int i)
{
qint64 index = configuration.getIndex();
QString lastLine;
QFile file(directory.absolutePath()+fileList.at(i));
if (!file.exists())
return;
if (!file.open(QIODevice::ReadOnly)){
qDebug() << "File not found or can't be read.";
return;
}
qDebug() << "File " + directory.absolutePath()+fileList.at(i) + " opened at " + QDateTime::currentDateTime().toString();
QTextStream in(&file);
in.seek(index);
// O objeto recebe o conteúdo da linha na posição 'index' do arquivo texto
lastLine = in.readLine();
/*
* Testa se o index não aponta para o inicio da linha.
* Testa se o conteudo do objeto é o mesmo do conteudo do registro salvo.
* No caso de ser diferente, ele invoca este mesmo programa \
* para ver se o conteudo do objeto está no outro arquivo.
* Se no final do processo ele não achar, seta o index como 0 \
* para que o arquivo seja lido desde o inicio.
*/
if (lastLine != configuration.getLastLine()) {
if (i < fileList.size()-1) {
qDebug() << "Last string readed not found, searching in a older file...";
readFile(i+1);
}
index = 0;
in.seek(index);
}
qDebug() << "Reading file...";
emit(readToWrite());
/*
* Posiciona entrada de dados na última posição lida no arquivo;
* Como essa linha já foi lida e processada na última vez, ele lê ela
* e não faz nada, para que no próximo passo ele possa processar dados novos.
* O objeto lastLine recebe os dados para que caso ele não tenha mais nada que processar,
* não salve o lastLine em branco no procedimento de salvar.
*/
while (!in.atEnd()) {
lastLine = in.readLine();
//qDebug () << lastLine;
/*
* Aqui processará os dados contidos no objeto lastLine
*/
emit dataReceived(lastLine);
}
emit(writenDone());
/*
* O valor do index é igual a posição atual no arquivo, diminuido do tamanho do registro anterior,
* e decrementado 2(duas) unidades, que é o valor do '\n'.
*
*/
index = in.pos()-lastLine.size()-2;
configuration.setIndex(index);
configuration.setLastLine(lastLine);
configuration.doWrite();
file.close();
qDebug() << "File " + directory.absolutePath()+fileList.at(i) + " closed at " + QDateTime::currentDateTime().toString();
}
bool LogReader::isRunning(const QString &process) {
QProcess tasklist;
tasklist.start(
"tasklist",
QStringList() << "/NH"
<< "/FO" << "CSV"
<< "/FI" << QString("IMAGENAME eq %1").arg(process));
tasklist.waitForFinished();
QString output = tasklist.readAllStandardOutput();
return output.startsWith(QString("\"%1").arg(process));
}