Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(486)

Side by Side Diff: chrome/browser/chromeos/system_logs/single_log_source.cc

Issue 2844163005: Add SingleLogSource to system_logs sources (Closed)
Patch Set: Remove TODO, already done Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/system_logs/single_log_source.h"
6
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/task_scheduler/post_task.h"
10 #include "content/public/browser/browser_thread.h"
11
12 namespace system_logs {
13
14 namespace {
15
16 // Defines a pair: log source name and log filename.
17 struct LogSource {
18 const char* name;
19 const char* filename;
20 };
21
22 // Whitelist of log sources that SingleLogSource is permitted to access.
23 const LogSource kLogFileSources[] = {
24 {"messages", "/var/log/messages"},
25 {"ui_latest", "/var/log/ui/ui.LATEST"},
26 };
27
28 // Given a valid log source name from |kLogFileSources|, returns the
29 // corresponding log filename. If the source name is not in the whitelist,
30 // returns an empty filename.
31 std::string GetLogFileSourceFilename(const std::string& source_name) {
afakhry 2017/05/06 01:12:13 This can now be a simple switch statement if we us
Simon Que 2017/05/06 14:19:42 Done.
32 for (const LogSource& source : kLogFileSources) {
33 if (source.name == source_name)
34 return source.filename;
35 }
36 return "";
37 }
38
39 } // namespace
40
41 SingleLogSource::SingleLogSource() : SystemLogsSource(""), num_bytes_read_(0) {}
afakhry 2017/05/06 01:12:13 Please remove this default constructor. It seems u
Simon Que 2017/05/06 14:19:42 Done.
42
43 SingleLogSource::SingleLogSource(const std::string& source_name)
44 : SystemLogsSource(source_name), num_bytes_read_(0) {}
45
46 SingleLogSource::~SingleLogSource() {}
47
48 void SingleLogSource::Fetch(const SysLogsSourceCallback& callback) {
49 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
50 DCHECK(!callback.is_null());
51
52 SystemLogsResponse* response = new SystemLogsResponse;
53 base::PostTaskWithTraitsAndReply(
54 FROM_HERE,
55 base::TaskTraits().MayBlock().WithPriority(
56 base::TaskPriority::BACKGROUND),
57 base::Bind(&SingleLogSource::ReadFile, base::Unretained(this), response),
58 base::Bind(callback, base::Owned(response)));
59 }
60
61 void SingleLogSource::ReadFile(SystemLogsResponse* result) {
62 // Attempt to open the file if it was not previously opened.
63 if (!file_.IsValid()) {
64 file_.Initialize(base::FilePath(GetLogFileSourceFilename(source_name())),
65 base::File::FLAG_OPEN | base::File::FLAG_READ);
66 if (!file_.IsValid())
67 return;
68 }
69
70 // Check for file size reset.
71 size_t length = file_.GetLength();
afakhry 2017/05/06 01:12:13 Nit: const here and everywhere below please.
Simon Que 2017/05/06 14:19:42 Done.
72 if (length < num_bytes_read_) {
73 num_bytes_read_ = 0;
74 file_.Seek(base::File::FROM_BEGIN, 0);
75 }
76
77 // Read from file until end.
78 size_t size_to_read = length - num_bytes_read_;
79 std::vector<char> read_result(size_to_read);
80 size_t size_read = file_.ReadAtCurrentPos(read_result.data(), size_to_read);
81 read_result.resize(size_read);
82
83 // Create a string from the read results.
84 std::string result_string;
afakhry 2017/05/06 01:12:13 Why don't you read into the string right away and
Simon Que 2017/05/06 14:19:43 ReadAtCurrentPos() writes to a char*, but std::str
afakhry 2017/05/08 19:20:47 You can use &result_string[0]. C++17 should provid
afakhry 2017/05/09 23:46:18 I see you missed this comment. Please take a look.
85
86 // The reader may only read complete lines.
87 if (!read_result.empty() && read_result.back() == '\n') {
88 result_string.reserve(read_result.size());
89 result_string.assign(read_result.begin(), read_result.end());
90 num_bytes_read_ += size_read;
91 } else {
92 // If an incomplete line was read, reset the file read offset to before the
93 // most recent read.
94 file_.Seek(base::File::FROM_CURRENT, -size_read);
95 }
96
97 // Pass it back to the callback.
98 (*result)[source_name()] = anonymizer_.Anonymize(result_string);
afakhry 2017/05/06 01:12:13 result->emplace(source_name_, anonymizer_.Anonymiz
Simon Que 2017/05/06 14:19:43 Done.
99 }
100
101 } // namespace system_logs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698