-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogReader.cpp
69 lines (56 loc) · 1.5 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
#include "LogReader.h"
bool CLogReader::Open(const wchar_t* const filename)
{
this->Close();
const bool succeeded = this->_lineReader.Open(filename);
return succeeded;
}
void CLogReader::Close()
{
this->_lineReader.Close();
}
bool CLogReader::SetFilter(const char* const filter)
{
if (filter == nullptr)
{
return false;
}
const size_t patternLen = strlen(filter);
const bool allocatedOk = this->_pattern.Allocate(patternLen);
if (!allocatedOk)
{
return false;
}
memcpy(this->_pattern.ptr, filter, patternLen);
return true;
}
__declspec(noinline) // noinline is added to help CPU profiling in release version
std::optional<std::string_view> CLogReader::GetNextLine()
{
const std::string_view pattern = { this->_pattern.ptr, this->_pattern.size };
while (true)
{
const auto line = this->_lineReader.GetNextLine();
if (!line)
{
// error or end of file
return {};
}
std::string_view matchView = *line;
// Ignore CRLF/LF during matching:
if (!matchView.empty() && matchView.back() == '\n')
{
matchView.remove_suffix(1);
if (!matchView.empty() && matchView.back() == '\r')
{
matchView.remove_suffix(1);
}
}
const bool matched = this->_lineMatcher.Match(matchView, pattern);
if (matched)
{
// line matched
return line;
}
}
}