-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogfileParser.cpp
More file actions
224 lines (199 loc) · 5.03 KB
/
logfileParser.cpp
File metadata and controls
224 lines (199 loc) · 5.03 KB
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
#include "logfileParser.h"
#include "logfileUtils.h"
#include "logfileAnalyser.h"
#include <sstream>
#include <vector>
LogfileParser::LogfileParser(std::string infile, std::string afile) : input_file(infile), analysis_file(afile)
{
}
int LogfileParser::open()
{
std::cout << "file is opened successfully" << std::endl;
return 0;
}
std ::string LogfileParser::findMarker(char *line)
{
return "";
}
std::string LogfileParser::getMarkerTimestamp(std::string marker)
{
std::ifstream input(input_file);
std::string line;
std::string buf;
bool found = false;
std::string markerTimestamp = "";
while (std::getline(input, line))
{
found = false;
// convert the line in to stream:
std::istringstream iss(line);
// declare vector of string (instead of fixed array)
std::vector<std::string> vec;
// read the line, word by word
while (std::getline(iss, buf, WPE_CDELIMITER))
{
vec.push_back(buf);
if (buf.compare(marker) == 0)
{
std::cout << "*****found the marker*****" << std::endl;
std::cout << line << std::endl;
found = true;
// get the timestamp
// TODO: need to check whether timestamp is available in log before adding up
markerTimestamp = vec[0] + ' ' + vec[1] + ' ' + vec[2] + ' ' + vec[3];
}
}
vec.clear();
}
input.close();
return markerTimestamp;
}
bool LogfileParser::seek(std::string timestamp)
{
return true;
}
std::vector<std::string> LogfileParser::getTokens(std::string start, std::string end)
{
std::vector<std::string> tokens = {};
return tokens;
}
std::string LogfileParser::getTimestamp(std::string line, logFormat fmt = wpeLog)
{
std::string buf;
std::vector<std::string> vec;
std::string time = "";
// convert the line in to stream:
std::istringstream iss(line);
switch (fmt)
{
case wpeLog:
{
// read the line, word by word
while (std::getline(iss, buf, WPE_CDELIMITER))
{
vec.push_back(buf);
if (vec.size() >= 4)
{
time = vec[0] + ' ' + vec[1] + ' ' + vec[2] + ' ' + vec[3];
break;
}
}
vec.clear();
}
break;
case coreLog:
{
// ignore the comments
if (line.at(0) != '[')
{
// std::cout << "Ignored line - " << line << std::endl;
return time;
}
// read the line, word by word
std::getline(iss, buf, CORE_CDELIMITER);
size_t pos = buf.find(" ");
if (pos != std::string::npos)
{
std::string extractedTime = buf.substr(pos, buf.size() - pos);
time = convertTimestampFormat(extractedTime, "%Y/%m/%d %H:%M:%S", "%Y %b %d %H:%M:%S");
}
else
{
std::cout << "Not able to find timestamp" << std::endl;
}
}
break;
}
return time;
}
std::string LogfileParser::getStartTimestamp()
{
std::ifstream input(input_file);
std::string firstLine;
std::string time = "";
if (std::getline(input, firstLine))
{
time = getTimestamp(firstLine);
}
return time;
}
std::string LogfileParser::getStartTimestamp(const std::filesystem::path &filePath, logFormat fmt)
{
std::ifstream input(filePath);
std::string firstLine;
std::string time = "";
while (std::getline(input, firstLine))
{
time = getTimestamp(firstLine, fmt);
if (time != "")
{
return time;
}
}
return time;
}
bool LogfileParser::writeLogMessageForAnalysis(std::string start, std::string end)
{
std::ifstream input(input_file);
std::ofstream analysisFile(analysis_file);
std::string line;
std::string time = "";
bool reached = false;
while (std::getline(input, line))
{
time = LogfileParser::getTimestamp(line);
if (time != "")
{
if (compareTimestamp(start, time) <= 0)
{
if (compareTimestamp(end, time) > 0)
{
analysisFile << line << std::endl;
}
else
{
std::cout << "Reached end of the log messages....." << std::endl;
break;
}
}
}
}
input.close();
analysisFile.close();
return true;
}
bool LogfileParser::parse()
{
std::ifstream input(input_file);
std::string line;
std::string buf;
bool found = false;
while (std::getline(input, line))
{
found = false;
// convert the line in to stream:
std::istringstream iss(line);
// declare vector of string (instead of fixed array)
std::vector<std::string> vec;
// read the line, word by word
while (std::getline(iss, buf, WPE_CDELIMITER))
{
vec.push_back(buf);
if (buf.compare("event=\'PLUGIN_CRASHED\'") == 0)
{
std::cout << "*****found crash marker*****" << std::endl;
std::cout << line << std::endl;
found = true;
}
}
vec.clear();
std::cout << "\n";
}
input.close();
return true;
}
bool LogfileParser::close()
{
std::cout << "filehandle is closed successfully" << std::endl;
return true;
}