Chromium Code Reviews| 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"; | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 SingleLogSource::SingleLogSource(SupportedSource source) | |
| 30 : SystemLogsSource(GetLogFileSourceFilename(source)), | |
| 31 num_bytes_read_(0), | |
| 32 weak_ptr_factory_(this) {} | |
| 33 | |
| 34 SingleLogSource::~SingleLogSource() {} | |
| 35 | |
| 36 void SingleLogSource::Fetch(const SysLogsSourceCallback& callback) { | |
| 37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 38 DCHECK(!callback.is_null()); | |
| 39 | |
| 40 SystemLogsResponse* response = new SystemLogsResponse; | |
| 41 base::PostTaskWithTraitsAndReply( | |
| 42 FROM_HERE, | |
| 43 base::TaskTraits().MayBlock().WithPriority( | |
| 44 base::TaskPriority::BACKGROUND), | |
| 45 base::Bind(&SingleLogSource::ReadFile, weak_ptr_factory_.GetWeakPtr(), | |
| 46 response), | |
| 47 base::Bind(callback, base::Owned(response))); | |
| 48 } | |
| 49 | |
| 50 void SingleLogSource::ReadFile(SystemLogsResponse* result) { | |
| 51 // Attempt to open the file if it was not previously opened. | |
| 52 if (!file_.IsValid()) { | |
| 53 file_.Initialize(base::FilePath(source_name()), | |
| 54 base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 55 if (!file_.IsValid()) | |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 // Check for file size reset. | |
| 60 const size_t length = file_.GetLength(); | |
| 61 if (length < num_bytes_read_) { | |
| 62 num_bytes_read_ = 0; | |
| 63 file_.Seek(base::File::FROM_BEGIN, 0); | |
| 64 } | |
| 65 | |
| 66 // Read from file until end. | |
| 67 const size_t size_to_read = length - num_bytes_read_; | |
| 68 std::string result_string(size_to_read, ' '); | |
|
afakhry
2017/05/10 04:28:03
Nit: I think it would be better if we default cons
Simon Que
2017/05/10 14:53:15
Done.
| |
| 69 const size_t size_read = | |
| 70 file_.ReadAtCurrentPos(&result_string[0], size_to_read); | |
| 71 result_string.resize(size_read); | |
| 72 | |
| 73 // The reader may only read complete lines. | |
| 74 if (!result_string.empty() && result_string.back() == '\n') { | |
| 75 num_bytes_read_ += size_read; | |
| 76 } else { | |
| 77 // If an incomplete line was read, reset the file read offset to before the | |
| 78 // most recent read. | |
| 79 file_.Seek(base::File::FROM_CURRENT, -size_read); | |
| 80 result_string.clear(); | |
|
afakhry
2017/05/10 04:28:03
Nit: You might consider doing the following here:
Simon Que
2017/05/10 14:53:15
Done.
| |
| 81 } | |
| 82 | |
| 83 // Pass it back to the callback. | |
| 84 result->emplace(source_name(), anonymizer_.Anonymize(result_string)); | |
| 85 } | |
| 86 | |
| 87 } // namespace system_logs | |
| OLD | NEW |