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

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

Issue 2930383002: SingleLogSource reads whole lines whenever possible (Closed)
Patch Set: Created 3 years, 6 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
« no previous file with comments | « no previous file | chrome/browser/chromeos/system_logs/single_log_source_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
index 22eedff884c1028f03462e8bf20ab249441c0462..1d09e8b398b36439db90502e783aa66c0a13c16f 100644
--- a/chrome/browser/chromeos/system_logs/single_log_source.cc
+++ b/chrome/browser/chromeos/system_logs/single_log_source.cc
@@ -30,6 +30,19 @@ base::FilePath GetLogFileSourceRelativeFilePath(
return base::FilePath();
}
+// Attempts to store a string |value| in |*response| under |key|. If there is
+// already a string in |*response| under |key|, appends |value| to the existing
+// string value.
+void AppendToSystemLogsResponse(SystemLogsResponse* response,
+ const std::string& key,
+ const std::string& value) {
+ auto iter = response->find(key);
+ if (iter == response->end())
+ response->emplace(key, value);
+ else
+ iter->second += value;
+}
+
} // namespace
SingleLogSource::SingleLogSource(SupportedSource source)
@@ -73,23 +86,34 @@ void SingleLogSource::ReadFile(SystemLogsResponse* result) {
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);
+ 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;
+ // If an incomplete line was read, return only the part that includes whole
+ // lines.
+ size_t last_newline_pos = result_string.find_last_of('\n');
+ if (last_newline_pos == std::string::npos) {
+ file_.Seek(base::File::FROM_CURRENT, -size_read);
+ AppendToSystemLogsResponse(result, source_name(), "");
+ return;
+ }
+ // The part of the string that will be returned includes the newline itself.
+ size_t adjusted_size_read = last_newline_pos + 1;
+ file_.Seek(base::File::FROM_CURRENT, -size_read + adjusted_size_read);
+ result_string.resize(adjusted_size_read);
+
+ // Update |size_read| to reflect that the read was only up to the last
+ // newline.
+ size_read = adjusted_size_read;
}
num_bytes_read_ += size_read;
// Pass it back to the callback.
- result->emplace(source_name(), anonymizer_.Anonymize(result_string));
+ AppendToSystemLogsResponse(result, source_name(),
+ anonymizer_.Anonymize(result_string));
}
} // namespace system_logs
« no previous file with comments | « no previous file | chrome/browser/chromeos/system_logs/single_log_source_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698