OLD | NEW |
---|---|
(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 // Converts a logs source type to the corresponding filename. In the future, if | |
17 // non-file source types are added, this function should return an empty string. | |
18 std::string GetLogFileSourceFilename(SingleLogSource::SupportedSource source) { | |
19 switch (source) { | |
20 case SingleLogSource::SupportedSource::kMessages: | |
21 return "/var/log/messages"; | |
22 case SingleLogSource::SupportedSource::kUiLatest: | |
23 return "/var/log/ui/ui.LATEST"; | |
afakhry
2017/05/08 19:20:48
The question that I really want to ask is why do w
Simon Que
2017/05/08 19:58:36
We are using debugd because it lets us get non-fil
| |
24 } | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 SingleLogSource::SingleLogSource(SupportedSource source) | |
30 : SystemLogsSource(GetLogFileSourceFilename(source)), num_bytes_read_(0) {} | |
31 | |
32 SingleLogSource::~SingleLogSource() {} | |
33 | |
34 void SingleLogSource::Fetch(const SysLogsSourceCallback& callback) { | |
35 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
36 DCHECK(!callback.is_null()); | |
37 | |
38 SystemLogsResponse* response = new SystemLogsResponse; | |
39 base::PostTaskWithTraitsAndReply( | |
40 FROM_HERE, | |
41 base::TaskTraits().MayBlock().WithPriority( | |
42 base::TaskPriority::BACKGROUND), | |
43 base::Bind(&SingleLogSource::ReadFile, base::Unretained(this), response), | |
44 base::Bind(callback, base::Owned(response))); | |
45 } | |
46 | |
47 void SingleLogSource::ReadFile(SystemLogsResponse* result) { | |
48 // Attempt to open the file if it was not previously opened. | |
49 if (!file_.IsValid()) { | |
50 file_.Initialize(base::FilePath(source_name()), | |
51 base::File::FLAG_OPEN | base::File::FLAG_READ); | |
52 if (!file_.IsValid()) | |
53 return; | |
54 } | |
55 | |
56 // Check for file size reset. | |
57 const size_t length = file_.GetLength(); | |
58 if (length < num_bytes_read_) { | |
59 num_bytes_read_ = 0; | |
60 file_.Seek(base::File::FROM_BEGIN, 0); | |
61 } | |
62 | |
63 // Read from file until end. | |
64 const size_t size_to_read = length - num_bytes_read_; | |
65 std::vector<char> read_result(size_to_read); | |
66 const size_t size_read = | |
67 file_.ReadAtCurrentPos(read_result.data(), size_to_read); | |
68 read_result.resize(size_read); | |
69 | |
70 // Create a string from the read results. | |
71 std::string result_string; | |
72 | |
73 // The reader may only read complete lines. | |
74 if (!read_result.empty() && read_result.back() == '\n') { | |
75 result_string.reserve(read_result.size()); | |
76 result_string.assign(read_result.begin(), read_result.end()); | |
77 num_bytes_read_ += size_read; | |
78 } else { | |
79 // If an incomplete line was read, reset the file read offset to before the | |
80 // most recent read. | |
81 file_.Seek(base::File::FROM_CURRENT, -size_read); | |
82 } | |
83 | |
84 // Pass it back to the callback. | |
85 result->emplace(source_name(), anonymizer_.Anonymize(result_string)); | |
86 } | |
87 | |
88 } // namespace system_logs | |
OLD | NEW |