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

Side by Side Diff: chrome/common/logging_chrome.cc

Issue 4194005: This moves log output for ChromeOS to safer locations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: upload after sync Created 10 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 // Need to include this before most other files because it defines 7 // Need to include this before most other files because it defines
8 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define 8 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define
9 // IPC_MESSAGE_MACROS_LOG_ENABLED so render_messages.h will generate the 9 // IPC_MESSAGE_MACROS_LOG_ENABLED so render_messages.h will generate the
10 // ViewMsgLog et al. functions. 10 // ViewMsgLog et al. functions.
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 } 151 }
152 152
153 FilePath SetUpSymlinkIfNeeded(const FilePath& symlink_path, bool new_log) { 153 FilePath SetUpSymlinkIfNeeded(const FilePath& symlink_path, bool new_log) {
154 DCHECK(!symlink_path.empty()); 154 DCHECK(!symlink_path.empty());
155 155
156 // If not starting a new log, then just log through the existing 156 // If not starting a new log, then just log through the existing
157 // symlink, but if the symlink doesn't exist, create it. If 157 // symlink, but if the symlink doesn't exist, create it. If
158 // starting a new log, then delete the old symlink and make a new 158 // starting a new log, then delete the old symlink and make a new
159 // one to a fresh log file. 159 // one to a fresh log file.
160 FilePath target_path; 160 FilePath target_path;
161 if (new_log || !file_util::PathExists(symlink_path)) { 161 bool symlink_exists = file_util::PathExists(symlink_path);
162 if (new_log || !symlink_exists) {
162 target_path = GenerateTimestampedName(symlink_path, base::Time::Now()); 163 target_path = GenerateTimestampedName(symlink_path, base::Time::Now());
163 164
164 // We don't care if the unlink fails; we're going to continue anyway. 165 // We don't care if the unlink fails; we're going to continue anyway.
165 if (unlink(symlink_path.value().c_str()) == -1) { 166 if (unlink(symlink_path.value().c_str()) == -1) {
166 if (new_log) // only warn if we might expect it to succeed. 167 if (symlink_exists) // only warn if we might expect it to succeed.
167 PLOG(WARNING) << "Unable to unlink " << symlink_path.value(); 168 PLOG(WARNING) << "Unable to unlink " << symlink_path.value();
168 } 169 }
169 if (symlink(target_path.value().c_str(), 170 if (symlink(target_path.value().c_str(),
170 symlink_path.value().c_str()) == -1) { 171 symlink_path.value().c_str()) == -1) {
171 PLOG(ERROR) << "Unable to create symlink " << symlink_path.value() 172 PLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
172 << " pointing at " << target_path.value(); 173 << " pointing at " << target_path.value();
173 } 174 }
174 } else { 175 } else {
175 char buf[PATH_MAX]; 176 char buf[PATH_MAX];
176 ssize_t count = readlink(symlink_path.value().c_str(), buf, arraysize(buf)); 177 ssize_t count = readlink(symlink_path.value().c_str(), buf, arraysize(buf));
177 if (count > 0) { 178 if (count > 0) {
178 target_path = FilePath(FilePath::StringType(buf, count)); 179 target_path = FilePath(FilePath::StringType(buf, count));
179 } else { 180 } else {
180 PLOG(ERROR) << "Unable to read symlink " << symlink_path.value(); 181 PLOG(ERROR) << "Unable to read symlink " << symlink_path.value();
181 } 182 }
182 } 183 }
183 return target_path; 184 return target_path;
184 } 185 }
185 186
186 void RemoveSymlinkAndLog(const FilePath& link_path, 187 void RemoveSymlinkAndLog(const FilePath& link_path,
187 const FilePath& target_path) { 188 const FilePath& target_path) {
188 if (::unlink(link_path.value().c_str()) == -1) 189 if (::unlink(link_path.value().c_str()) == -1)
189 PLOG(WARNING) << "Unable to unlink symlink " << link_path.value(); 190 PLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
190 if (::unlink(target_path.value().c_str()) == -1) 191 if (::unlink(target_path.value().c_str()) == -1)
191 PLOG(WARNING) << "Unable to unlink log file " << target_path.value(); 192 PLOG(WARNING) << "Unable to unlink log file " << target_path.value();
192 } 193 }
193 194
194 } // anonymous namespace 195 } // anonymous namespace
195 196
196 void RedirectChromeLogging(const FilePath& new_log_dir, 197 FilePath GetSessionLogFile(const CommandLine& command_line) {
197 const CommandLine& command_line) { 198 FilePath log_dir;
199 std::string log_dir_str;
200 scoped_ptr<base::Environment> env(base::Environment::Create());
201 if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) &&
202 !log_dir_str.empty()) {
203 log_dir = FilePath(log_dir_str);
204 } else {
205 PathService::Get(chrome::DIR_USER_DATA, &log_dir);
206 FilePath login_profile =
207 command_line.GetSwitchValuePath(switches::kLoginProfile);
208 log_dir = log_dir.Append(login_profile);
209 }
210 return log_dir.Append(GetLogFileName().BaseName());
211 }
212
213 void RedirectChromeLogging(const CommandLine& command_line) {
198 DCHECK(!chrome_logging_redirected_) << 214 DCHECK(!chrome_logging_redirected_) <<
199 "Attempted to redirect logging when it was already initialized."; 215 "Attempted to redirect logging when it was already initialized.";
200 FilePath orig_log_path = GetLogFileName(); 216
201 FilePath log_path = new_log_dir.Append(orig_log_path.BaseName()); 217 // Redirect logs to the session log directory, if set. Otherwise
218 // defaults to the profile dir.
219 FilePath log_path = GetSessionLogFile(command_line);
202 220
203 // Always force a new symlink when redirecting. 221 // Always force a new symlink when redirecting.
204 FilePath target_path = SetUpSymlinkIfNeeded(log_path, true); 222 FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
205 223
206 // ChromeOS always logs through the symlink, so it shouldn't be 224 // ChromeOS always logs through the symlink, so it shouldn't be
207 // deleted if it already exists. 225 // deleted if it already exists.
208 if (!InitLogging(log_path.value().c_str(), 226 if (!InitLogging(log_path.value().c_str(),
209 DetermineLogMode(command_line), 227 DetermineLogMode(command_line),
210 logging::LOCK_LOG_FILE, 228 logging::LOCK_LOG_FILE,
211 logging::APPEND_TO_OLD_LOG_FILE)) { 229 logging::APPEND_TO_OLD_LOG_FILE)) {
212 LOG(ERROR) << "Unable to initialize logging to " << log_path.value(); 230 LOG(ERROR) << "Unable to initialize logging to " << log_path.value();
213 RemoveSymlinkAndLog(log_path, target_path); 231 RemoveSymlinkAndLog(log_path, target_path);
214 } else { 232 } else {
215 chrome_logging_redirected_ = true; 233 chrome_logging_redirected_ = true;
216 } 234 }
217 } 235 }
236
237
218 #endif 238 #endif
219 239
220 void InitChromeLogging(const CommandLine& command_line, 240 void InitChromeLogging(const CommandLine& command_line,
221 OldFileDeletionState delete_old_log_file) { 241 OldFileDeletionState delete_old_log_file) {
222 DCHECK(!chrome_logging_initialized_) << 242 DCHECK(!chrome_logging_initialized_) <<
223 "Attempted to initialize logging when it was already initialized."; 243 "Attempted to initialize logging when it was already initialized.";
224 244
225 #if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED) 245 #if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED)
226 IPC::Logging::SetLoggerFunctions(g_log_function_mapping); 246 IPC::Logging::SetLoggerFunctions(g_log_function_mapping);
227 #endif 247 #endif
228 248
229 FilePath log_path = GetLogFileName(); 249 FilePath log_path = GetLogFileName();
230 250
231 #if defined(OS_CHROMEOS) 251 #if defined(OS_CHROMEOS)
252 // For BWSI (Incognito) logins, we want to put the logs in the user
253 // profile directory that is created for the temporary session instead
254 // of in the system log directory, for privacy reasons.
255 if (command_line.HasSwitch(switches::kGuestSession))
256 log_path = GetSessionLogFile(command_line);
257
232 // On ChromeOS we log to the symlink. We force creation of a new 258 // On ChromeOS we log to the symlink. We force creation of a new
233 // symlink if we've been asked to delete the old log, since that 259 // symlink if we've been asked to delete the old log, since that
234 // indicates the start of a new session. 260 // indicates the start of a new session.
235 FilePath target_path = SetUpSymlinkIfNeeded( 261 FilePath target_path = SetUpSymlinkIfNeeded(
236 log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE); 262 log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE);
237 263
238 // Because ChromeOS manages the move to a new session by redirecting 264 // Because ChromeOS manages the move to a new session by redirecting
239 // the link, it shouldn't remove the old file in the logging code, 265 // the link, it shouldn't remove the old file in the logging code,
240 // since that will remove the newly created link instead. 266 // since that will remove the newly created link instead.
241 delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE; 267 delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 assertions->push_back(wide_line); 389 assertions->push_back(wide_line);
364 ++assertion_count; 390 ++assertion_count;
365 } 391 }
366 } 392 }
367 log_file.close(); 393 log_file.close();
368 394
369 return assertion_count; 395 return assertion_count;
370 } 396 }
371 397
372 } // namespace logging 398 } // namespace logging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698