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

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

Issue 2956513004: Rename SingleLogSource to SingleLogFileLogSource (Closed)
Patch Set: Rebased Created 3 years, 5 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
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_file_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/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/process/process_info.h" 10 #include "base/process/process_info.h"
11 #include "base/strings/string_split.h" 11 #include "base/strings/string_split.h"
12 #include "base/task_scheduler/post_task.h" 12 #include "base/task_scheduler/post_task.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 15
(...skipping 11 matching lines...) Expand all
27 base::TimeDelta::FromMinutes(10); 27 base::TimeDelta::FromMinutes(10);
28 28
29 // A custom timestamp for when the current Chrome session started. Used during 29 // A custom timestamp for when the current Chrome session started. Used during
30 // testing to override the actual time. 30 // testing to override the actual time.
31 const base::Time* g_chrome_start_time_for_test = nullptr; 31 const base::Time* g_chrome_start_time_for_test = nullptr;
32 32
33 // Converts a logs source type to the corresponding file path, relative to the 33 // Converts a logs source type to the corresponding file path, relative to the
34 // base system log directory path. In the future, if non-file source types are 34 // base system log directory path. In the future, if non-file source types are
35 // added, this function should return an empty file path. 35 // added, this function should return an empty file path.
36 base::FilePath GetLogFileSourceRelativeFilePath( 36 base::FilePath GetLogFileSourceRelativeFilePath(
37 SingleLogSource::SupportedSource source) { 37 SingleLogFileLogSource::SupportedSource source) {
38 switch (source) { 38 switch (source) {
39 case SingleLogSource::SupportedSource::kMessages: 39 case SingleLogFileLogSource::SupportedSource::kMessages:
40 return base::FilePath("messages"); 40 return base::FilePath("messages");
41 case SingleLogSource::SupportedSource::kUiLatest: 41 case SingleLogFileLogSource::SupportedSource::kUiLatest:
42 return base::FilePath("ui/ui.LATEST"); 42 return base::FilePath("ui/ui.LATEST");
43 case SingleLogSource::SupportedSource::kAtrusLog: 43 case SingleLogFileLogSource::SupportedSource::kAtrusLog:
44 return base::FilePath("atrus.log"); 44 return base::FilePath("atrus.log");
45 } 45 }
46 NOTREACHED(); 46 NOTREACHED();
47 return base::FilePath(); 47 return base::FilePath();
48 } 48 }
49 49
50 // Returns the inode value of file at |path|, or 0 if it doesn't exist or is 50 // Returns the inode value of file at |path|, or 0 if it doesn't exist or is
51 // otherwise unable to be accessed for file system info. 51 // otherwise unable to be accessed for file system info.
52 ino_t GetInodeValue(const base::FilePath& path) { 52 ino_t GetInodeValue(const base::FilePath& path) {
53 struct stat file_stats; 53 struct stat file_stats;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 // a newline, |lines| would end with an extra empty line after the newline. 127 // a newline, |lines| would end with an extra empty line after the newline.
128 // This would have resulted in an extra nonexistent newline being counted 128 // This would have resulted in an extra nonexistent newline being counted
129 // during the computation of |offset|. 129 // during the computation of |offset|.
130 --offset; 130 --offset;
131 } 131 }
132 return offset; 132 return offset;
133 } 133 }
134 134
135 } // namespace 135 } // namespace
136 136
137 SingleLogSource::SingleLogSource(SupportedSource source_type) 137 SingleLogFileLogSource::SingleLogFileLogSource(SupportedSource source_type)
138 : SystemLogsSource(GetLogFileSourceRelativeFilePath(source_type).value()), 138 : SystemLogsSource(GetLogFileSourceRelativeFilePath(source_type).value()),
139 source_type_(source_type), 139 source_type_(source_type),
140 log_file_dir_path_(kDefaultSystemLogDirPath), 140 log_file_dir_path_(kDefaultSystemLogDirPath),
141 num_bytes_read_(0), 141 num_bytes_read_(0),
142 file_inode_(0), 142 file_inode_(0),
143 weak_ptr_factory_(this) {} 143 weak_ptr_factory_(this) {}
144 144
145 SingleLogSource::~SingleLogSource() {} 145 SingleLogFileLogSource::~SingleLogFileLogSource() {}
146 146
147 // static 147 // static
148 void SingleLogSource::SetChromeStartTimeForTesting( 148 void SingleLogFileLogSource::SetChromeStartTimeForTesting(
149 const base::Time* start_time) { 149 const base::Time* start_time) {
150 g_chrome_start_time_for_test = start_time; 150 g_chrome_start_time_for_test = start_time;
151 } 151 }
152 152
153 void SingleLogSource::Fetch(const SysLogsSourceCallback& callback) { 153 void SingleLogFileLogSource::Fetch(const SysLogsSourceCallback& callback) {
154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
155 DCHECK(!callback.is_null()); 155 DCHECK(!callback.is_null());
156 156
157 SystemLogsResponse* response = new SystemLogsResponse; 157 SystemLogsResponse* response = new SystemLogsResponse;
158 base::PostTaskWithTraitsAndReply( 158 base::PostTaskWithTraitsAndReply(
159 FROM_HERE, 159 FROM_HERE,
160 base::TaskTraits(base::MayBlock(), base::TaskPriority::BACKGROUND), 160 base::TaskTraits(base::MayBlock(), base::TaskPriority::BACKGROUND),
161 base::Bind(&SingleLogSource::ReadFile, weak_ptr_factory_.GetWeakPtr(), 161 base::Bind(&SingleLogFileLogSource::ReadFile,
162 weak_ptr_factory_.GetWeakPtr(),
162 kMaxNumAllowedLogRotationsDuringFileRead, response), 163 kMaxNumAllowedLogRotationsDuringFileRead, response),
163 base::Bind(callback, base::Owned(response))); 164 base::Bind(callback, base::Owned(response)));
164 } 165 }
165 166
166 base::FilePath SingleLogSource::GetLogFilePath() const { 167 base::FilePath SingleLogFileLogSource::GetLogFilePath() const {
167 return base::FilePath(log_file_dir_path_).Append(source_name()); 168 return base::FilePath(log_file_dir_path_).Append(source_name());
168 } 169 }
169 170
170 void SingleLogSource::ReadFile(size_t num_rotations_allowed, 171 void SingleLogFileLogSource::ReadFile(size_t num_rotations_allowed,
171 SystemLogsResponse* result) { 172 SystemLogsResponse* result) {
172 // Attempt to open the file if it was not previously opened. 173 // Attempt to open the file if it was not previously opened.
173 if (!file_.IsValid()) { 174 if (!file_.IsValid()) {
174 file_.Initialize(GetLogFilePath(), 175 file_.Initialize(GetLogFilePath(),
175 base::File::FLAG_OPEN | base::File::FLAG_READ); 176 base::File::FLAG_OPEN | base::File::FLAG_READ);
176 if (!file_.IsValid()) 177 if (!file_.IsValid())
177 return; 178 return;
178 179
179 // Determine actual offset from which to start reading. 180 // Determine actual offset from which to start reading.
180 if (source_type_ == SupportedSource::kMessages) { 181 if (source_type_ == SupportedSource::kMessages) {
181 const base::Time earliest_log_time = 182 const base::Time earliest_log_time =
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 // again, to read from the new file. 243 // again, to read from the new file.
243 if (should_handle_file_rotation) { 244 if (should_handle_file_rotation) {
244 file_.Close(); 245 file_.Close();
245 num_bytes_read_ = 0; 246 num_bytes_read_ = 0;
246 file_inode_ = 0; 247 file_inode_ = 0;
247 ReadFile(num_rotations_allowed - 1, result); 248 ReadFile(num_rotations_allowed - 1, result);
248 } 249 }
249 } 250 }
250 251
251 } // namespace system_logs 252 } // namespace system_logs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698