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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/system_logs/single_log_source.h" 5 #include "chrome/browser/chromeos/system_logs/single_log_source.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/task_scheduler/post_task.h" 9 #include "base/task_scheduler/post_task.h"
10 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
(...skipping 12 matching lines...) Expand all
23 switch (source) { 23 switch (source) {
24 case SingleLogSource::SupportedSource::kMessages: 24 case SingleLogSource::SupportedSource::kMessages:
25 return base::FilePath("messages"); 25 return base::FilePath("messages");
26 case SingleLogSource::SupportedSource::kUiLatest: 26 case SingleLogSource::SupportedSource::kUiLatest:
27 return base::FilePath("ui/ui.LATEST"); 27 return base::FilePath("ui/ui.LATEST");
28 } 28 }
29 NOTREACHED(); 29 NOTREACHED();
30 return base::FilePath(); 30 return base::FilePath();
31 } 31 }
32 32
33 // Attempts to store a string |value| in |*response| under |key|. If there is
34 // already a string in |*response| under |key|, appends |value| to the existing
35 // string value.
36 void AppendToSystemLogsResponse(SystemLogsResponse* response,
37 const std::string& key,
38 const std::string& value) {
39 auto iter = response->find(key);
40 if (iter == response->end())
41 response->emplace(key, value);
42 else
43 iter->second += value;
44 }
45
33 } // namespace 46 } // namespace
34 47
35 SingleLogSource::SingleLogSource(SupportedSource source) 48 SingleLogSource::SingleLogSource(SupportedSource source)
36 : SystemLogsSource(GetLogFileSourceRelativeFilePath(source).value()), 49 : SystemLogsSource(GetLogFileSourceRelativeFilePath(source).value()),
37 log_file_dir_path_(kDefaultSystemLogDirPath), 50 log_file_dir_path_(kDefaultSystemLogDirPath),
38 num_bytes_read_(0), 51 num_bytes_read_(0),
39 weak_ptr_factory_(this) {} 52 weak_ptr_factory_(this) {}
40 53
41 SingleLogSource::~SingleLogSource() {} 54 SingleLogSource::~SingleLogSource() {}
42 55
(...skipping 23 matching lines...) Expand all
66 const size_t length = file_.GetLength(); 79 const size_t length = file_.GetLength();
67 if (length < num_bytes_read_) { 80 if (length < num_bytes_read_) {
68 num_bytes_read_ = 0; 81 num_bytes_read_ = 0;
69 file_.Seek(base::File::FROM_BEGIN, 0); 82 file_.Seek(base::File::FROM_BEGIN, 0);
70 } 83 }
71 84
72 // Read from file until end. 85 // Read from file until end.
73 const size_t size_to_read = length - num_bytes_read_; 86 const size_t size_to_read = length - num_bytes_read_;
74 std::string result_string; 87 std::string result_string;
75 result_string.resize(size_to_read); 88 result_string.resize(size_to_read);
76 const size_t size_read = 89 size_t size_read = file_.ReadAtCurrentPos(&result_string[0], size_to_read);
77 file_.ReadAtCurrentPos(&result_string[0], size_to_read);
78 result_string.resize(size_read); 90 result_string.resize(size_read);
79 91
80 // The reader may only read complete lines. 92 // The reader may only read complete lines.
81 if (result_string.empty() || result_string.back() != '\n') { 93 if (result_string.empty() || result_string.back() != '\n') {
82 // If an incomplete line was read, reset the file read offset to before the 94 // If an incomplete line was read, return only the part that includes whole
83 // most recent read. 95 // lines.
84 file_.Seek(base::File::FROM_CURRENT, -size_read); 96 size_t last_newline_pos = result_string.find_last_of('\n');
85 result->emplace(source_name(), ""); 97 if (last_newline_pos == std::string::npos) {
86 return; 98 file_.Seek(base::File::FROM_CURRENT, -size_read);
99 AppendToSystemLogsResponse(result, source_name(), "");
100 return;
101 }
102 // The part of the string that will be returned includes the newline itself.
103 size_t adjusted_size_read = last_newline_pos + 1;
104 file_.Seek(base::File::FROM_CURRENT, -size_read + adjusted_size_read);
105 result_string.resize(adjusted_size_read);
106
107 // Update |size_read| to reflect that the read was only up to the last
108 // newline.
109 size_read = adjusted_size_read;
87 } 110 }
88 111
89 num_bytes_read_ += size_read; 112 num_bytes_read_ += size_read;
90 113
91 // Pass it back to the callback. 114 // Pass it back to the callback.
92 result->emplace(source_name(), anonymizer_.Anonymize(result_string)); 115 AppendToSystemLogsResponse(result, source_name(),
116 anonymizer_.Anonymize(result_string));
93 } 117 }
94 118
95 } // namespace system_logs 119 } // namespace system_logs
OLDNEW
« 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