-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
71 lines (57 loc) · 1.51 KB
/
log.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 <iostream>
#include <regex>
#include "log.h"
#include "util.h"
static uint32_t hms(uint8_t h, uint8_t m, uint8_t s) {
return h*3600 + m*60 +s;
}
Log::Log()
{
}
void Log::load(std::string filename)
{
entries.clear();
auto data = file_get_contents(filename);
std::vector<std::string> lines;
split(data, '\n', lines);
std::regex regex("^(.*)\\[(\\d+):(\\d+):(\\d+)\\] (.+)$");
for (auto &line : lines) {
std::cmatch cm;
if (std::regex_match(line.c_str(), cm, regex)) {
uint8_t hour = atoi(cm[2].str().c_str());
uint8_t min = atoi(cm[3].str().c_str());
uint8_t sec = atoi(cm[4].str().c_str());
Entry e;
e.time = hms(hour, min, sec);
e.message = cm[0];
entries.push_back(e);
}
}
}
std::vector<Log::Entry> Log::entriesBetween(uint8_t hour1, uint8_t min1, uint8_t sec1,
uint8_t hour2, uint8_t min2, uint8_t sec2)
{
std::vector<Entry> result;
if (!entries.size()) {
return result;
}
uint32_t from = hms(hour1, min1, sec1);
uint32_t to = hms(hour2, min2, sec2);
size_t A = 0;
size_t B = entries.size()-1;
while (A != B) {
size_t M = (A+B)/2;
if (entries[M].time > from) {
B = M;
} else {
A = M+1;
}
}
while (A<entries.size() && entries[A].time <= to) {
if (entries[A].time > from) {
result.push_back(entries[A]);
}
A++;
}
return result;
}