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

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

Issue 2844163005: Add SingleLogSource to system_logs sources (Closed)
Patch Set: Directly read from file into string 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..5862596f4aa16737774beff7b2a2fe1bdb719de2
--- /dev/null
+++ b/chrome/browser/chromeos/system_logs/single_log_source.cc
@@ -0,0 +1,87 @@
+// 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";
+ }
+}
+
+} // 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);
+ DCHECK(!callback.is_null());
+
+ SystemLogsResponse* response = new SystemLogsResponse;
+ base::PostTaskWithTraitsAndReply(
+ FROM_HERE,
+ base::TaskTraits().MayBlock().WithPriority(
+ 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(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.
+ 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') {
+ num_bytes_read_ += size_read;
+ } else {
+ // 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_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.
+ }
+
+ // 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