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

Unified Diff: chrome/browser/chromeos/system_logs/single_log_source.cc

Issue 2844163005: Add SingleLogSource to system_logs sources (Closed)
Patch Set: Add TestBrowserThreadBundle 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/system_logs/single_log_source.cc
diff --git a/chrome/browser/chromeos/system_logs/single_log_source.cc b/chrome/browser/chromeos/system_logs/single_log_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4359af8d3703f45942d4643130db28845447574c
--- /dev/null
+++ b/chrome/browser/chromeos/system_logs/single_log_source.cc
@@ -0,0 +1,88 @@
+// Copyright 2017 The Chromium 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 "chrome/browser/chromeos/system_logs/single_log_source.h"
+
+#include "base/bind.h"
+#include "base/files/file_path.h"
+#include "base/task_scheduler/post_task.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace system_logs {
+
+namespace {
+
+// Converts a logs source type to the corresponding filename. In the future, if
+// non-file source types are added, this function should return an empty string.
+std::string GetLogFileSourceFilename(SingleLogSource::SupportedSource source) {
+ switch (source) {
+ case SingleLogSource::SupportedSource::kMessages:
+ return "/var/log/messages";
+ case SingleLogSource::SupportedSource::kUiLatest:
+ return "/var/log/ui/ui.LATEST";
+ }
+}
James Cook 2017/05/12 19:32:53 I think this needs a NOTREACHED(); return;
+
+} // namespace
+
+SingleLogSource::SingleLogSource(SupportedSource source)
+ : SystemLogsSource(GetLogFileSourceFilename(source)),
+ num_bytes_read_(0),
+ weak_ptr_factory_(this) {}
+
+SingleLogSource::~SingleLogSource() {}
+
+void SingleLogSource::Fetch(const SysLogsSourceCallback& callback) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
afakhry 2017/05/11 15:42:28 The real question is why do you need to force this
rkc 2017/05/11 19:51:13 Requiring the fetch call to be on the UI thread se
+ DCHECK(!callback.is_null());
+
+ SystemLogsResponse* response = new SystemLogsResponse;
+ base::PostTaskWithTraitsAndReply(
+ FROM_HERE,
+ base::TaskTraits(base::MayBlock(), base::TaskPriority::BACKGROUND),
+ base::Bind(&SingleLogSource::ReadFile, weak_ptr_factory_.GetWeakPtr(),
+ response),
+ base::Bind(callback, base::Owned(response)));
+}
+
+void SingleLogSource::ReadFile(SystemLogsResponse* result) {
+ // Attempt to open the file if it was not previously opened.
+ if (!file_.IsValid()) {
+ file_.Initialize(base::FilePath(source_name()),
+ base::File::FLAG_OPEN | base::File::FLAG_READ);
+ if (!file_.IsValid())
+ return;
+ }
+
+ // Check for file size reset.
+ const size_t length = file_.GetLength();
+ if (length < num_bytes_read_) {
+ num_bytes_read_ = 0;
+ file_.Seek(base::File::FROM_BEGIN, 0);
+ }
+
+ // Read from file until end.
+ const size_t size_to_read = length - num_bytes_read_;
+ std::string result_string;
+ result_string.resize(size_to_read);
+ const size_t size_read =
+ file_.ReadAtCurrentPos(&result_string[0], size_to_read);
+ result_string.resize(size_read);
+
+ // The reader may only read complete lines.
+ if (result_string.empty() || result_string.back() != '\n') {
+ // If an incomplete line was read, reset the file read offset to before the
+ // most recent read.
+ file_.Seek(base::File::FROM_CURRENT, -size_read);
+ result->emplace(source_name(), "");
+ return;
+ }
+
+ num_bytes_read_ += size_read;
+
+ // Pass it back to the callback.
+ result->emplace(source_name(), anonymizer_.Anonymize(result_string));
+}
+
+} // namespace system_logs

Powered by Google App Engine
This is Rietveld 408576698