-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_parser.cpp
More file actions
136 lines (111 loc) · 3.15 KB
/
Copy pathlog_parser.cpp
File metadata and controls
136 lines (111 loc) · 3.15 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
#include "log_parser.h"
#include "action.h"
#include <boost/optional.hpp>
#include <string>
#include <cassert>
using std::string;
/* Debugging */
#include <iostream>
using std::cout;
using std::endl;
bool LogParser::ReadCharacter(char need,
FailType fail /* = TERMINATE_ON_FAIL */) {
char c;
bool read_what_was_needed = (Input >> c) && c == need;
if (!read_what_was_needed) {
if (fail == IGNORE_FAIL)
Input.putback(c);
else {
cout << "Fail. Needed '" << need << "', but read '" << c << "'" << endl;
assert(false);
}
return false;
}
return true;
}
void LogParser::ReadQuotedString(string& output_string) {
char c;
ReadCharacter('"');
c = '\0'; // Reset
while (Input && Input.get(c)) {
if (c == '"')
break;
output_string += c;
}
assert(c == '"' && "Quoted string must end with \"");
}
bool LogParser::NextFieldsPresent() {
if (ReadCharacter('}', IGNORE_FAIL)) {
return false;
} else {
ReadCharacter(',');
return true;
}
}
bool LogParser::ReadPropertiesKeyAndValue() {
string key;
ReadQuotedString(key);
string prefix = key.substr(0, 4);
assert( prefix == "prop" );
string suffix = key.substr(4);
int property_id = stoi(suffix);
--property_id; // Start from 0
assert( 0 <= property_id && property_id < Action::NUM_PROPERTIES );
ReadCharacter(':');
ReadOptional(CurrentAction.Properties[property_id]);
return NextFieldsPresent();
}
void LogParser::ReadProperties() {
assert(ReadCharacter('{') && "Properties dict must begin with {");
while (ReadPropertiesKeyAndValue())
;
}
bool LogParser::ReadJsonKeyAndValue() {
string key;
ReadQuotedString(key);
ReadCharacter(':');
if (key == "ts_fact") {
ReadOptional(CurrentAction.Timestamp);
} else if (key == "fact_name") {
string name;
ReadQuotedString(name);
CurrentAction.Name = name;
} else if (key == "actor_id") {
ReadOptional(CurrentAction.ActorId);
} else if (key == "props") {
ReadProperties();
} else {
assert(false && "Incorrect JSON key");
}
return NextFieldsPresent();
}
void LogParser::ReadJson() {
bool json_present = ReadCharacter('{', IGNORE_FAIL);
if (!json_present)
return;
while (ReadJsonKeyAndValue()) {
/* empty */
}
Action action;
{
/* Fill in present action fields */
for (int i = 0; i < Action::NUM_PROPERTIES; ++i) {
assert(CurrentAction.Properties[i] && "All 10 properties must be provided in JSON");
action.Properties[i] = CurrentAction.Properties[i].value();
}
if (CurrentAction.Timestamp) action.Timestamp = CurrentAction.Timestamp.value();
if (CurrentAction.Name) action.Name = CurrentAction.Name.value();
if (CurrentAction.ActorId) action.ActorId = CurrentAction.ActorId.value();
/* Clear CurrentAction */
CurrentAction = ActionWithOptionalFields();
}
OnActionParsed(action);
}
void LogParser::Parse(const string& logfile,
OnActionParsedCallback on_action_parsed) {
CurrentAction = ActionWithOptionalFields(); /* Initialized empty */
Input = std::ifstream(logfile);
OnActionParsed = on_action_parsed;
while (Input)
ReadJson();
}