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

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

Issue 2883553002: Revert of Add SingleLogSource to system_logs sources (Closed)
Patch Set: 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 // 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(base::MayBlock(), base::TaskPriority::BACKGROUND),
44 base::Bind(&SingleLogSource::ReadFile, weak_ptr_factory_.GetWeakPtr(),
45 response),
46 base::Bind(callback, base::Owned(response)));
47 }
48
49 void SingleLogSource::ReadFile(SystemLogsResponse* result) {
50 // Attempt to open the file if it was not previously opened.
51 if (!file_.IsValid()) {
52 file_.Initialize(base::FilePath(source_name()),
53 base::File::FLAG_OPEN | base::File::FLAG_READ);
54 if (!file_.IsValid())
55 return;
56 }
57
58 // Check for file size reset.
59 const size_t length = file_.GetLength();
60 if (length < num_bytes_read_) {
61 num_bytes_read_ = 0;
62 file_.Seek(base::File::FROM_BEGIN, 0);
63 }
64
65 // Read from file until end.
66 const size_t size_to_read = length - num_bytes_read_;
67 std::string result_string;
68 result_string.resize(size_to_read);
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 // If an incomplete line was read, reset the file read offset to before the
76 // most recent read.
77 file_.Seek(base::File::FROM_CURRENT, -size_read);
78 result->emplace(source_name(), "");
79 return;
80 }
81
82 num_bytes_read_ += size_read;
83
84 // Pass it back to the callback.
85 result->emplace(source_name(), anonymizer_.Anonymize(result_string));
86 }
87
88 } // namespace system_logs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698