-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog_parser_syslog.cc
136 lines (109 loc) · 3.65 KB
/
log_parser_syslog.cc
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
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "croslog/log_parser_syslog.h"
#include <memory>
#include <string>
#include <utility>
#include "base/strings/string_number_conversions.h"
namespace {
// The length of time string like "2020-05-25T00:00:00.000000+00:00".
constexpr size_t kTimeStringLengthWithTimeZone = 32;
// The length of time string like "2020-05-25T00:00:00.000000Z".
constexpr size_t kTimeStringLengthUTC = 27;
int ParseTime(const std::string& entire_line, base::Time* time) {
DCHECK_NE(nullptr, time);
if (entire_line[26] == 'Z') {
// Case of UTC time format like "2020-05-25T00:00:00.000000Z".
std::string log_time = entire_line.substr(0, kTimeStringLengthUTC);
bool result = base::Time::FromString(log_time.c_str(), time);
if (!result)
return -1;
return kTimeStringLengthUTC;
} else if (entire_line[26] == '+' || entire_line[26] == '-') {
// Case of format with time-zone like "2020-05-25T00:00:00.000000+00:00".
std::string log_time = entire_line.substr(0, kTimeStringLengthWithTimeZone);
bool result = base::Time::FromString(log_time.c_str(), time);
if (!result)
return -1;
return kTimeStringLengthWithTimeZone;
}
return -1;
}
} // namespace
namespace croslog {
LogParserSyslog::LogParserSyslog() = default;
MaybeLogEntry LogParserSyslog::ParseInternal(std::string&& entire_line) {
if (entire_line.empty()) {
// Returns an invalid value if the line is invalid or empty.
return base::nullopt;
}
if (entire_line.size() < kTimeStringLengthUTC) {
// Parse failed. Maybe this line doesn't contains a header.
return base::nullopt;
}
base::Time time;
int message_start_pos = ParseTime(entire_line, &time);
if (message_start_pos < 0) {
// Parse failed. Maybe this line doesn't contains a header.
return base::nullopt;
}
int pos = message_start_pos;
if (entire_line[pos] != ' ') {
// Parse failed. Maybe this line doesn't contains a header.
return base::nullopt;
}
std::string severity_str;
if (entire_line[pos] == ' ') {
for (int i = pos + 1; i < entire_line.size(); i++) {
if (entire_line[i] == ' ') {
severity_str = entire_line.substr(pos + 1, i - pos - 1);
pos = i;
break;
}
}
}
Severity severity = Severity::UNSPECIFIED;
if (!severity_str.empty()) {
severity = SeverityFromString(severity_str);
}
std::string tag;
if (entire_line[pos] == ' ') {
for (int i = pos + 1; i < entire_line.size(); i++) {
if (entire_line[i] == '[' || entire_line[i] == ':' ||
entire_line[i] == ' ') {
tag = entire_line.substr(pos + 1, i - pos - 1);
pos = i;
break;
}
}
}
int pid = -1;
if (entire_line[pos] == '[') {
for (int i = pos + 1; i < entire_line.size(); i++) {
if (entire_line[i] == ']') {
std::string pid_str = entire_line.substr(pos + 1, i - pos - 1);
if (!base::StringToInt(pid_str, &pid))
pid = -1;
pos = i;
break;
}
}
DCHECK_EQ(']', entire_line[pos]);
pos++;
}
if (entire_line.size() > pos && entire_line[pos] == ':')
pos++;
std::string message;
if (entire_line.size() > pos) {
if (entire_line[pos] != ' ') {
// Parse failed. Maybe this line doesn't contains a header.
return base::nullopt;
}
pos++;
message = entire_line.substr(pos, entire_line.size() - pos);
}
return LogEntry{time, severity, std::move(tag),
pid, std::move(message), std::move(entire_line)};
}
} // namespace croslog