-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgss_log_parser.cpp
275 lines (257 loc) · 10.1 KB
/
gss_log_parser.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: gss_log_parser.cpp
* Author: user
*
* Created on 11 de marzo de 2020, 17:51
*/
#include "gss_log_parser.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <fcntl.h> // open
#include <unistd.h> // read, write, close
using namespace std;
gss_log_parser::gss_log_parser(const char * config_filename_c_str)
{
char currentDir[FILENAME_MAX];
/* get current dir for full minimal dir */
#ifdef __CYGWIN__
getwd(currentDir);
#else
getcwd(currentDir, FILENAME_MAX);
#endif
if(config_filename_c_str == NULL)
{
config_filename = (string)currentDir + "\\gss_log_parser.ini";
}
else
{
ostringstream config_filename_ss;
config_filename_ss << config_filename_c_str;
config_filename = config_filename_ss.str();
}
status = NO_ERROR;
number_of_lines = 0;
current_line = 0;
}
int gss_log_parser::parseReport(uint32_t &testsParsedRef)
{
string rawLogFilename;
string tcrFilename;
ifstream configFile;
configFile.open(config_filename);
if(!configFile.is_open())
{
status = CANT_OPEN_CONFIG_FILE;
return 1;
}
if(!getline(configFile, rawLogFilename))
{
status = CONFIG_FILE_NOT_VALID;
configFile.close();
return 1;
}
if(!getline(configFile, tcrFilename))
{
status = CONFIG_FILE_NOT_VALID;
configFile.close();
return 1;
}
configFile.close();
ifstream rawLogFile;
ofstream tcrFile;
tcrFile.open(tcrFilename);
if(!tcrFile.is_open())
{
wrong_value = tcrFilename;
status = CANT_CREATE_TCR_FILE;
return 1;
}
rawLogFile.open(rawLogFilename);
if(!rawLogFile.is_open())
{
wrong_value = rawLogFilename;
status = CANT_OPEN_LOG_FILE;
return 1;
}
current_line = 0;
number_of_lines = 0;
string logLine;
bool headerFound = false;
bool testInProgress = false;
while (getline(rawLogFile, logLine))
++number_of_lines;
rawLogFile.clear();
rawLogFile.seekg(0, ios::beg);
while(getline(rawLogFile, logLine))
{
current_line++;
cout << "\r" << fixed << setprecision(2) << (current_line*100.0)/number_of_lines << "%" <<
" (" << current_line << "/" << number_of_lines << ")";
if(logLine.substr(0, 5).compare("Test ") == 0)
{
if(logLine.substr(logLine.length()-31, 5).compare("began") == 0)
{
if(testInProgress == true)
{
/* another test starts but didn't end */
tcrFile << "\t\t\t<run bold=\"true\">" << endl;
tcrFile << "\t\t\t\t<text>not ended</text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text>.</text>" << endl;
tcrFile << "\t\t</evidence>" << endl;
tcrFile << "\t\t<status>Fail</status>" << endl;
tcrFile << "\t</VTCRTestReport>" << endl;
}
tcrFile << "\t<VTCRTestReport test_procedure=\"" <<
logLine.substr(5, logLine.length()-37) << "\">" << endl;
tcrFile << "\t\t<evidence>" << endl;
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text>File: </text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run italics=\"true\">" << endl;
tcrFile << "\t\t\t\t<text>" << rawLogFilename << "</text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text>. Test began at </text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run bold=\"true\">" << endl;
tcrFile << "\t\t\t\t<text>" << logLine.substr(logLine.length()-24, 24) << "</text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text> (line " << current_line << "), </text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
testInProgress = true;
}
else if(logLine.substr(5, 5).compare("ended") == 0)
{
string ended = "ended";
string status = "Pass";
size_t posDate = logLine.find(".") + 2;
if((logLine.find("not expected packets") != string::npos) ||
(logLine.find("error") != string::npos) ||
(logLine.find("action check KO") != string::npos))
{
ended += logLine.substr(10, posDate-11);
status = "Fail";
}
if(ended.size() == 5)//no errors
{
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text>ended</text>" << endl;
}
else
{
tcrFile << "\t\t\t<run italics=\"true\" color=\"red\">" << endl;
tcrFile << "\t\t\t\t<text>" << ended << "</text>" << endl;
}
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text> at </text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run bold=\"true\">" << endl;
tcrFile << "\t\t\t\t<text>" << logLine.substr(posDate, 24) << "</text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text> (line " << current_line << ").</text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t</evidence>" << endl;
tcrFile << "\t\t<status>" << status << "</status>" << endl;
tcrFile << "\t</VTCRTestReport>" << endl;
testsParsedRef++;
testInProgress = false;
}
else if(logLine.substr(5, 8).compare("canceled") == 0)
{
size_t posDate = logLine.find(".") + 2;
tcrFile << "\t\t\t<run italics=\"true\" color=\"red\">" << endl;
tcrFile << "\t\t\t\t<text>canceled</text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text> at </text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run bold=\"true\">" << endl;
tcrFile << "\t\t\t\t<text>" << logLine.substr(posDate, 24) << "</text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t\t<run>" << endl;
tcrFile << "\t\t\t\t<text> (line " << current_line << ").</text>" << endl;
tcrFile << "\t\t\t</run>" << endl;
tcrFile << "\t\t</evidence>" << endl;
tcrFile << "\t\t<status>Fail</status>" << endl;
tcrFile << "\t</VTCRTestReport>" << endl;
testsParsedRef++;
testInProgress = false;
}
else if((logLine.substr(5, 8).compare("campaign") == 0) &&
(!headerFound))
{
headerFound = true;
string name = "";
string issue = "0";
string revision = "0";
string date = "";
size_t posIss = 0;
if((posIss = logLine.find(" version")) != string::npos)
{
name = logLine.substr(14, posIss-14);
size_t posRev = 0;
if((posRev = logLine.substr(posIss+9).find(".")) != string::npos)
{
issue = logLine.substr(posIss+9, posRev);
size_t posDate = 0;
if((posDate = logLine.substr(posIss+9+posRev+1).find(".")) != string::npos)
{
revision = logLine.substr(posIss+9+posRev+1, posDate);
size_t posEnd = 0;
if((posEnd = logLine.substr(posIss+9+posRev+1+posDate+2).find(" ")) != string::npos)
{
date = logLine.substr(posIss+9+posRev+1+posDate+2, posEnd);
}
}
}
}
tcrFile << "<TestCampaignReport name=\"" << name <<
"\" id=\"" << name << "_" << issue << "." << revision <<
"\" issue=\"" << issue << "\" revision=\"" << revision <<
"\" date=\"" << date << "\">" << endl;
}
}
}
cout << endl;
tcrFile << "</TestCampaignReport>" << endl;
tcrFile.close();
rawLogFile.close();
return 0;
}
string gss_log_parser::displayParseReportError()
{
ostringstream error_ss;
if(errorSection != "")
error_ss << "At " << errorSection << ": ";
switch(status)
{
case CANT_OPEN_CONFIG_FILE:
error_ss << "Can't open config file \"" << config_filename << "\"";
break;
case CONFIG_FILE_NOT_VALID:
error_ss << "Config file \"" << config_filename << "\" is not valid";
break;
case CANT_CREATE_TCR_FILE:
error_ss << "Can't create TCR file \"" << wrong_value << "\"";
break;
case CANT_OPEN_LOG_FILE:
error_ss << "Can't open log file \"" << wrong_value << "\"";
break;
default:
error_ss << "Unknown error " << +status;
break;
}
return error_ss.str();
}