-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviewer_plaintext.cc
298 lines (245 loc) · 8.35 KB
/
viewer_plaintext.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 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/viewer_plaintext.h"
#include <memory>
#include <unistd.h>
#include <utility>
#include "base/files/file_util.h"
#include "base/json/string_escape.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "croslog/cursor_util.h"
#include "croslog/log_parser_audit.h"
#include "croslog/log_parser_syslog.h"
#include "croslog/severity.h"
namespace croslog {
namespace {
const char* kLogSources[] = {
// Log files from rsyslog:
// clang-format off
"/var/log/arc.log",
"/var/log/boot.log",
"/var/log/hammerd.log",
"/var/log/messages",
"/var/log/net.log",
"/var/log/secure",
"/var/log/upstart.log",
// clang-format on
};
const char kAuditLogSources[] = "/var/log/audit/audit.log";
int64_t ToMicrosecondsSinceUnixEpoch(base::Time time) {
return (time - base::Time::UnixEpoch()).InMicroseconds();
}
} // anonymous namespace
ViewerPlaintext::ViewerPlaintext(const croslog::Config& config)
: config_(config) {
Initialize();
}
// For test
ViewerPlaintext::ViewerPlaintext(const croslog::Config& config,
BootRecords&& boot_logs)
: config_(config), boot_records_{std::move(boot_logs)} {
Initialize();
}
void ViewerPlaintext::Initialize() {
if (!config_.grep.empty()) {
config_grep_.emplace(config_.grep);
if (!config_grep_->ok())
config_grep_.reset();
}
if (!config_.after_cursor.empty()) {
if (ParseCursor(config_.after_cursor, &config_cursor_time_))
config_cursor_mode_ = CursorMode::NEWER;
else
LOG(WARNING) << "Invalid cursor format in 'after-cursor' option.";
} else if (!config_.cursor.empty()) {
if (ParseCursor(config_.cursor, &config_cursor_time_))
config_cursor_mode_ = CursorMode::SAME_AND_NEWER;
else
LOG(WARNING) << "Invalid cursor format in 'cursor' option.";
}
config_show_cursor_ = config_.show_cursor && !config_.follow;
config_boot_range_.reset();
if (config_.boot.has_value()) {
auto range = boot_records_.GetBootRange(*config_.boot);
if (range.has_value())
config_boot_range_.emplace(*range);
}
}
bool ViewerPlaintext::Run() {
bool install_change_watcher = config_.follow;
for (size_t i = 0; i < base::size(kLogSources); i++) {
base::FilePath path(kLogSources[i]);
if (!base::PathExists(path))
continue;
multiplexer_.AddSource(path, std::make_unique<LogParserSyslog>(),
install_change_watcher);
}
if (base::PathExists(base::FilePath(kAuditLogSources))) {
multiplexer_.AddSource(base::FilePath(kAuditLogSources),
std::make_unique<LogParserAudit>(),
install_change_watcher);
}
multiplexer_.AddObserver(this);
if (config_.lines >= 0) {
multiplexer_.SetLinesFromLast(config_.lines);
} else if (config_.follow) {
multiplexer_.SetLinesFromLast(10);
}
ReadRemainingLogs();
if (config_.follow) {
// Wait for file changes.
run_loop_.Run();
multiplexer_.RemoveObserver(this);
}
return true;
}
void ViewerPlaintext::OnLogFileChanged() {
ReadRemainingLogs();
}
bool ViewerPlaintext::ShouldFilterOutEntry(const LogEntry& e) {
if (config_cursor_mode_ != CursorMode::UNSPECIFIED) {
if ((config_cursor_mode_ == CursorMode::NEWER &&
config_cursor_time_ >= e.time()) ||
(config_cursor_mode_ == CursorMode::SAME_AND_NEWER &&
config_cursor_time_ > e.time())) {
// TODO(yoshiki): Consider the case that multiple logs have the same
// time.
return true;
}
}
const std::string& tag = e.tag();
if (!config_.identifier.empty() && config_.identifier != tag)
return true;
const Severity severity = e.severity();
if (config_.severity != Severity::UNSPECIFIED && config_.severity < severity)
return true;
const std::string& message = e.message();
if (config_grep_.has_value() && !RE2::PartialMatch(message, *config_grep_))
return true;
if (config_.boot.has_value()) {
if (!config_boot_range_.has_value() ||
!config_boot_range_->Contains(e.time())) {
return true;
}
}
return false;
}
void ViewerPlaintext::ReadRemainingLogs() {
base::Time last_shown_log_time;
while (true) {
const MaybeLogEntry& e = multiplexer_.Forward();
if (!e.has_value())
break;
// Shoe the last cursor regardless of visibility.
if (config_show_cursor_)
last_shown_log_time = e->time();
if (ShouldFilterOutEntry(*e))
continue;
WriteLog(*e);
}
if (config_show_cursor_) {
if (last_shown_log_time.is_null()) {
multiplexer_.SetLinesFromLast(1);
const MaybeLogEntry& e = multiplexer_.Forward();
if (e.has_value())
last_shown_log_time = e->time();
}
if (!last_shown_log_time.is_null()) {
WriteOutput("-- cursor: ");
WriteOutput(GenerateCursor(last_shown_log_time));
WriteOutput("\n", 1);
}
}
}
std::vector<std::pair<std::string, std::string>>
ViewerPlaintext::GenerateKeyValues(const LogEntry& e) {
std::vector<std::pair<std::string, std::string>> kvs;
kvs.push_back(std::make_pair(
"PRIORITY", base::NumberToString(static_cast<int>(e.severity()))));
kvs.push_back(std::make_pair("SYSLOG_IDENTIFIER", e.tag()));
const std::string& boot_id = GetBootIdAt(e.time());
if (!boot_id.empty())
kvs.push_back(std::make_pair("_BOOT_ID", boot_id));
std::string timestamp =
base::NumberToString(ToMicrosecondsSinceUnixEpoch(e.time()));
kvs.push_back(std::make_pair("__REALTIME_TIMESTAMP", timestamp));
kvs.push_back(std::make_pair("_SOURCE_REALTIME_TIMESTAMP", timestamp));
if (e.pid() != -1) {
kvs.push_back(std::make_pair("SYSLOG_PID", base::NumberToString(e.pid())));
kvs.push_back(std::make_pair("_PID", base::NumberToString(e.pid())));
}
kvs.push_back(std::make_pair("MESSAGE", e.message()));
return kvs;
}
void ViewerPlaintext::WriteLog(const LogEntry& entry) {
if (config_.output == OutputMode::EXPORT)
return WriteLogInExportFormat(entry);
if (config_.output == OutputMode::JSON)
return WriteLogInJsonFormat(entry);
const std::string& s = entry.entire_line();
WriteOutput(s);
WriteOutput("\n", 1);
}
void ViewerPlaintext::WriteLogInExportFormat(const LogEntry& entry) {
const auto&& kvs = GenerateKeyValues(entry);
for (const auto& kv : kvs) {
WriteOutput(kv.first);
WriteOutput("=", 1);
WriteOutput(kv.second);
WriteOutput("\n", 1);
}
WriteOutput("\n", 1);
}
std::string ViewerPlaintext::GetBootIdAt(base::Time time) {
const auto& boot_ranges = boot_records_.boot_ranges();
DCHECK_GE(cache_boot_range_index_, -1);
DCHECK_LT(cache_boot_range_index_, static_cast<int>(boot_ranges.size()));
// First, tries to reuse the index used at the last time. In most case, the
// logs are read sequentially and the boot id is likely to be same as the
// previous.
if (cache_boot_range_index_ != -1 &&
boot_ranges[cache_boot_range_index_].Contains(time)) {
return boot_ranges[cache_boot_range_index_].boot_id();
}
// Otherwise, searches the boot id sequentially from the boot log.
for (int i = boot_ranges.size() - 1; i >= 0; i--) {
const auto& boot_range = boot_ranges[i];
if (boot_range.Contains(time)) {
cache_boot_range_index_ = i;
return boot_range.boot_id();
}
}
return base::EmptyString();
}
void ViewerPlaintext::WriteLogInJsonFormat(const LogEntry& entry) {
const auto&& kvs = GenerateKeyValues(entry);
bool first = true;
WriteOutput("{", 1);
for (const auto& kv : kvs) {
std::string escaped_value;
bool ret_value = base::EscapeJSONString(kv.second, true, &escaped_value);
if (!ret_value)
escaped_value = "<<INVALID>>";
if (!first)
WriteOutput(", \"", 3);
else
WriteOutput("\"", 1);
// All keys are hard-corded and unnecessary to escape.
WriteOutput(kv.first);
WriteOutput("\": ", 3);
WriteOutput(escaped_value);
first = false;
}
WriteOutput("}\n", 2);
}
void ViewerPlaintext::WriteOutput(const std::string& str) {
WriteOutput(str.data(), str.size());
}
void ViewerPlaintext::WriteOutput(const char* str, size_t size) {
bool write_stdout_result =
base::WriteFileDescriptor(STDOUT_FILENO, str, size);
CHECK(write_stdout_result);
}
} // namespace croslog