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

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

Issue 2901173002: Move RedirectChromeLogging to src/chromeos. (Closed)
Patch Set: 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 29 matching lines...) Expand all
40 #include "base/debug/dump_without_crashing.h" 40 #include "base/debug/dump_without_crashing.h"
41 #include "base/environment.h" 41 #include "base/environment.h"
42 #include "base/files/file_path.h" 42 #include "base/files/file_path.h"
43 #include "base/files/file_util.h" 43 #include "base/files/file_util.h"
44 #include "base/metrics/statistics_recorder.h" 44 #include "base/metrics/statistics_recorder.h"
45 #include "base/path_service.h" 45 #include "base/path_service.h"
46 #include "base/strings/string_number_conversions.h" 46 #include "base/strings/string_number_conversions.h"
47 #include "base/strings/string_util.h" 47 #include "base/strings/string_util.h"
48 #include "base/strings/stringprintf.h" 48 #include "base/strings/stringprintf.h"
49 #include "base/strings/utf_string_conversions.h" 49 #include "base/strings/utf_string_conversions.h"
50 #include "base/threading/thread_restrictions.h"
51 #include "chrome/common/chrome_constants.h" 50 #include "chrome/common/chrome_constants.h"
52 #include "chrome/common/chrome_paths.h" 51 #include "chrome/common/chrome_paths.h"
53 #include "chrome/common/chrome_switches.h" 52 #include "chrome/common/chrome_switches.h"
54 #include "chrome/common/env_vars.h" 53 #include "chrome/common/env_vars.h"
55 #include "content/public/common/content_switches.h" 54 #include "content/public/common/content_switches.h"
56 #include "ipc/ipc_logging.h" 55 #include "ipc/ipc_logging.h"
57 56
58 #if defined(OS_CHROMEOS) 57 #if defined(OS_CHROMEOS)
59 #include "chromeos/chromeos_switches.h" 58 #include "chromeos/chromeos_switches.h"
60 #endif 59 #endif
61 60
62 #if defined(OS_WIN) 61 #if defined(OS_WIN)
63 #include <initguid.h> 62 #include <initguid.h>
64 #include "base/logging_win.h" 63 #include "base/logging_win.h"
65 #include "base/syslog_logging.h" 64 #include "base/syslog_logging.h"
66 #include "chrome/install_static/install_details.h" 65 #include "chrome/install_static/install_details.h"
67 #endif 66 #endif
68 67
68 namespace logging {
69 namespace { 69 namespace {
70 70
71 // When true, this means that error dialogs should not be shown. 71 // When true, this means that error dialogs should not be shown.
72 bool dialogs_are_suppressed_ = false; 72 bool dialogs_are_suppressed_ = false;
73 logging::ScopedLogAssertHandler* assert_handler_ = nullptr; 73 ScopedLogAssertHandler* assert_handler_ = nullptr;
74 74
75 // This should be true for exactly the period between the end of 75 // This should be true for exactly the period between the end of
76 // InitChromeLogging() and the beginning of CleanupChromeLogging(). 76 // InitChromeLogging() and the beginning of CleanupChromeLogging().
77 bool chrome_logging_initialized_ = false; 77 bool chrome_logging_initialized_ = false;
78 78
79 // Set if we called InitChromeLogging() but failed to initialize. 79 // Set if we called InitChromeLogging() but failed to initialize.
80 bool chrome_logging_failed_ = false; 80 bool chrome_logging_failed_ = false;
81 81
82 // This should be true for exactly the period between the end of 82 // This should be true for exactly the period between the end of
83 // InitChromeLogging() and the beginning of CleanupChromeLogging(). 83 // InitChromeLogging() and the beginning of CleanupChromeLogging().
(...skipping 17 matching lines...) Expand all
101 base::debug::BreakDebugger(); 101 base::debug::BreakDebugger();
102 } 102 }
103 MSVC_ENABLE_OPTIMIZE(); 103 MSVC_ENABLE_OPTIMIZE();
104 104
105 // Suppresses error/assertion dialogs and enables the logging of 105 // Suppresses error/assertion dialogs and enables the logging of
106 // those errors into silenced_errors_. 106 // those errors into silenced_errors_.
107 void SuppressDialogs() { 107 void SuppressDialogs() {
108 if (dialogs_are_suppressed_) 108 if (dialogs_are_suppressed_)
109 return; 109 return;
110 110
111 assert_handler_ = new logging::ScopedLogAssertHandler( 111 assert_handler_ =
112 base::Bind(SilentRuntimeAssertHandler)); 112 new ScopedLogAssertHandler(base::Bind(SilentRuntimeAssertHandler));
113 113
114 #if defined(OS_WIN) 114 #if defined(OS_WIN)
115 UINT new_flags = SEM_FAILCRITICALERRORS | 115 UINT new_flags = SEM_FAILCRITICALERRORS |
116 SEM_NOGPFAULTERRORBOX | 116 SEM_NOGPFAULTERRORBOX |
117 SEM_NOOPENFILEERRORBOX; 117 SEM_NOOPENFILEERRORBOX;
118 118
119 // Preserve existing error mode, as discussed at http://t/dmea 119 // Preserve existing error mode, as discussed at http://t/dmea
120 UINT existing_flags = SetErrorMode(new_flags); 120 UINT existing_flags = SetErrorMode(new_flags);
121 SetErrorMode(existing_flags | new_flags); 121 SetErrorMode(existing_flags | new_flags);
122 #endif 122 #endif
123 123
124 dialogs_are_suppressed_ = true; 124 dialogs_are_suppressed_ = true;
125 } 125 }
126 126
127 } // anonymous namespace 127 } // anonymous namespace
128 128
129 namespace logging {
130
131 LoggingDestination DetermineLogMode(const base::CommandLine& command_line) { 129 LoggingDestination DetermineLogMode(const base::CommandLine& command_line) {
132 // only use OutputDebugString in debug mode 130 // only use OutputDebugString in debug mode
133 #ifdef NDEBUG 131 #ifdef NDEBUG
134 bool enable_logging = false; 132 bool enable_logging = false;
135 const char *kInvertLoggingSwitch = switches::kEnableLogging; 133 const char *kInvertLoggingSwitch = switches::kEnableLogging;
136 const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_FILE; 134 const LoggingDestination kDefaultLoggingMode = LOG_TO_FILE;
137 #else 135 #else
138 bool enable_logging = true; 136 bool enable_logging = true;
139 const char *kInvertLoggingSwitch = switches::kDisableLogging; 137 const char *kInvertLoggingSwitch = switches::kDisableLogging;
140 const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_ALL; 138 const LoggingDestination kDefaultLoggingMode = LOG_TO_ALL;
141 #endif 139 #endif
142 140
143 if (command_line.HasSwitch(kInvertLoggingSwitch)) 141 if (command_line.HasSwitch(kInvertLoggingSwitch))
144 enable_logging = !enable_logging; 142 enable_logging = !enable_logging;
145 143
146 logging::LoggingDestination log_mode; 144 LoggingDestination log_mode;
147 if (enable_logging) { 145 if (enable_logging) {
148 // Let --enable-logging=stderr force only stderr, particularly useful for 146 // Let --enable-logging=stderr force only stderr, particularly useful for
149 // non-debug builds where otherwise you can't get logs to stderr at all. 147 // non-debug builds where otherwise you can't get logs to stderr at all.
150 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") 148 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
151 log_mode = logging::LOG_TO_SYSTEM_DEBUG_LOG; 149 log_mode = LOG_TO_SYSTEM_DEBUG_LOG;
152 else 150 else
153 log_mode = kDefaultLoggingMode; 151 log_mode = kDefaultLoggingMode;
154 } else { 152 } else {
155 log_mode = logging::LOG_NONE; 153 log_mode = LOG_NONE;
156 } 154 }
157 return log_mode; 155 return log_mode;
158 } 156 }
159 157
160 #if defined(OS_CHROMEOS) 158 #if defined(OS_CHROMEOS)
161 namespace {
162 base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path, 159 base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path,
163 bool new_log) { 160 bool new_log) {
164 DCHECK(!symlink_path.empty()); 161 DCHECK(!symlink_path.empty());
165 162
166 // If not starting a new log, then just log through the existing 163 // If not starting a new log, then just log through the existing
167 // symlink, but if the symlink doesn't exist, create it. If 164 // symlink, but if the symlink doesn't exist, create it. If
168 // starting a new log, then delete the old symlink and make a new 165 // starting a new log, then delete the old symlink and make a new
169 // one to a fresh log file. 166 // one to a fresh log file.
170 base::FilePath target_path; 167 base::FilePath target_path;
171 bool symlink_exists = base::PathExists(symlink_path); 168 bool symlink_exists = base::PathExists(symlink_path);
(...skipping 17 matching lines...) Expand all
189 } 186 }
190 187
191 void RemoveSymlinkAndLog(const base::FilePath& link_path, 188 void RemoveSymlinkAndLog(const base::FilePath& link_path,
192 const base::FilePath& target_path) { 189 const base::FilePath& target_path) {
193 if (::unlink(link_path.value().c_str()) == -1) 190 if (::unlink(link_path.value().c_str()) == -1)
194 DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value(); 191 DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
195 if (::unlink(target_path.value().c_str()) == -1) 192 if (::unlink(target_path.value().c_str()) == -1)
196 DPLOG(WARNING) << "Unable to unlink log file " << target_path.value(); 193 DPLOG(WARNING) << "Unable to unlink log file " << target_path.value();
197 } 194 }
198 195
199 } // anonymous namespace
200
201 base::FilePath GetSessionLogDir(const base::CommandLine& command_line) { 196 base::FilePath GetSessionLogDir(const base::CommandLine& command_line) {
202 base::FilePath log_dir; 197 base::FilePath log_dir;
203 std::string log_dir_str; 198 std::string log_dir_str;
204 std::unique_ptr<base::Environment> env(base::Environment::Create()); 199 std::unique_ptr<base::Environment> env(base::Environment::Create());
205 if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) && 200 if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) &&
206 !log_dir_str.empty()) { 201 !log_dir_str.empty()) {
207 log_dir = base::FilePath(log_dir_str); 202 log_dir = base::FilePath(log_dir_str);
208 } else if (command_line.HasSwitch(chromeos::switches::kLoginProfile)) { 203 } else if (command_line.HasSwitch(chromeos::switches::kLoginProfile)) {
209 PathService::Get(chrome::DIR_USER_DATA, &log_dir); 204 PathService::Get(chrome::DIR_USER_DATA, &log_dir);
210 base::FilePath profile_dir; 205 base::FilePath profile_dir;
(...skipping 10 matching lines...) Expand all
221 } 216 }
222 log_dir = log_dir.Append(profile_dir); 217 log_dir = log_dir.Append(profile_dir);
223 } 218 }
224 return log_dir; 219 return log_dir;
225 } 220 }
226 221
227 base::FilePath GetSessionLogFile(const base::CommandLine& command_line) { 222 base::FilePath GetSessionLogFile(const base::CommandLine& command_line) {
228 return GetSessionLogDir(command_line).Append(GetLogFileName().BaseName()); 223 return GetSessionLogDir(command_line).Append(GetLogFileName().BaseName());
229 } 224 }
230 225
231 void RedirectChromeLogging(const base::CommandLine& command_line) {
232 if (chrome_logging_redirected_) {
233 // TODO(nkostylev): Support multiple active users. http://crbug.com/230345
234 LOG(WARNING) << "NOT redirecting logging for multi-profiles case.";
235 return;
236 }
237
238 DCHECK(!chrome_logging_redirected_) <<
239 "Attempted to redirect logging when it was already initialized.";
240
241 // Redirect logs to the session log directory, if set. Otherwise
242 // defaults to the profile dir.
243 base::FilePath log_path = GetSessionLogFile(command_line);
244
245 // Creating symlink causes us to do blocking IO on UI thread.
246 // Temporarily allow it until we fix http://crbug.com/61143
247 base::ThreadRestrictions::ScopedAllowIO allow_io;
248 // Always force a new symlink when redirecting.
249 base::FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
250
251 // ChromeOS always logs through the symlink, so it shouldn't be
252 // deleted if it already exists.
253 logging::LoggingSettings settings;
254 settings.logging_dest = DetermineLogMode(command_line);
255 settings.log_file = log_path.value().c_str();
256 if (!logging::InitLogging(settings)) {
257 DLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
258 RemoveSymlinkAndLog(log_path, target_path);
259 } else {
260 chrome_logging_redirected_ = true;
261 }
262 }
263
264 #endif // OS_CHROMEOS 226 #endif // OS_CHROMEOS
265 227
266 void InitChromeLogging(const base::CommandLine& command_line, 228 void InitChromeLogging(const base::CommandLine& command_line,
267 OldFileDeletionState delete_old_log_file) { 229 OldFileDeletionState delete_old_log_file) {
268 DCHECK(!chrome_logging_initialized_) << 230 DCHECK(!chrome_logging_initialized_) <<
269 "Attempted to initialize logging when it was already initialized."; 231 "Attempted to initialize logging when it was already initialized.";
270 232
271 LoggingDestination logging_dest = DetermineLogMode(command_line); 233 LoggingDestination logging_dest = DetermineLogMode(command_line);
272 LogLockingState log_locking_state = LOCK_LOG_FILE; 234 LogLockingState log_locking_state = LOCK_LOG_FILE;
273 base::FilePath log_path; 235 base::FilePath log_path;
(...skipping 10 matching lines...) Expand all
284 // For BWSI (Incognito) logins, we want to put the logs in the user 246 // For BWSI (Incognito) logins, we want to put the logs in the user
285 // profile directory that is created for the temporary session instead 247 // profile directory that is created for the temporary session instead
286 // of in the system log directory, for privacy reasons. 248 // of in the system log directory, for privacy reasons.
287 if (command_line.HasSwitch(chromeos::switches::kGuestSession)) 249 if (command_line.HasSwitch(chromeos::switches::kGuestSession))
288 log_path = GetSessionLogFile(command_line); 250 log_path = GetSessionLogFile(command_line);
289 251
290 // On ChromeOS we log to the symlink. We force creation of a new 252 // On ChromeOS we log to the symlink. We force creation of a new
291 // symlink if we've been asked to delete the old log, since that 253 // symlink if we've been asked to delete the old log, since that
292 // indicates the start of a new session. 254 // indicates the start of a new session.
293 target_path = SetUpSymlinkIfNeeded( 255 target_path = SetUpSymlinkIfNeeded(
294 log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE); 256 log_path, delete_old_log_file == DELETE_OLD_LOG_FILE);
295 257
296 // Because ChromeOS manages the move to a new session by redirecting 258 // Because ChromeOS manages the move to a new session by redirecting
297 // the link, it shouldn't remove the old file in the logging code, 259 // the link, it shouldn't remove the old file in the logging code,
298 // since that will remove the newly created link instead. 260 // since that will remove the newly created link instead.
299 delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE; 261 delete_old_log_file = APPEND_TO_OLD_LOG_FILE;
300 #endif 262 #endif
301 } else { 263 } else {
302 log_locking_state = DONT_LOCK_LOG_FILE; 264 log_locking_state = DONT_LOCK_LOG_FILE;
303 } 265 }
304 266
305 logging::LoggingSettings settings; 267 LoggingSettings settings;
306 settings.logging_dest = logging_dest; 268 settings.logging_dest = logging_dest;
307 settings.log_file = log_path.value().c_str(); 269 settings.log_file = log_path.value().c_str();
308 settings.lock_log = log_locking_state; 270 settings.lock_log = log_locking_state;
309 settings.delete_old = delete_old_log_file; 271 settings.delete_old = delete_old_log_file;
310 bool success = logging::InitLogging(settings); 272 bool success = InitLogging(settings);
311 273
312 #if defined(OS_CHROMEOS) 274 #if defined(OS_CHROMEOS)
313 if (!success) { 275 if (!success) {
314 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value() 276 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value()
315 << " (which should be a link to " << target_path.value() << ")"; 277 << " (which should be a link to " << target_path.value() << ")";
316 RemoveSymlinkAndLog(log_path, target_path); 278 RemoveSymlinkAndLog(log_path, target_path);
317 chrome_logging_failed_ = true; 279 chrome_logging_failed_ = true;
318 return; 280 return;
319 } 281 }
320 #else 282 #else
321 if (!success) { 283 if (!success) {
322 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value(); 284 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
323 chrome_logging_failed_ = true; 285 chrome_logging_failed_ = true;
324 return; 286 return;
325 } 287 }
326 #endif 288 #endif
327 289
328 // Default to showing error dialogs. 290 // Default to showing error dialogs.
329 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 291 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
330 switches::kNoErrorDialogs)) 292 switches::kNoErrorDialogs))
331 logging::SetShowErrorDialogs(true); 293 SetShowErrorDialogs(true);
332 294
333 // we want process and thread IDs because we have a lot of things running 295 // we want process and thread IDs because we have a lot of things running
334 logging::SetLogItems(true, // enable_process_id 296 SetLogItems(true, // enable_process_id
335 true, // enable_thread_id 297 true, // enable_thread_id
336 true, // enable_timestamp 298 true, // enable_timestamp
337 false); // enable_tickcount 299 false); // enable_tickcount
338 300
339 // We call running in unattended mode "headless", and allow 301 // We call running in unattended mode "headless", and allow
340 // headless mode to be configured either by the Environment 302 // headless mode to be configured either by the Environment
341 // Variable or by the Command Line Switch. This is for 303 // Variable or by the Command Line Switch. This is for
342 // automated test purposes. 304 // automated test purposes.
343 std::unique_ptr<base::Environment> env(base::Environment::Create()); 305 std::unique_ptr<base::Environment> env(base::Environment::Create());
344 if (env->HasVar(env_vars::kHeadless) || 306 if (env->HasVar(env_vars::kHeadless) ||
345 command_line.HasSwitch(switches::kNoErrorDialogs)) 307 command_line.HasSwitch(switches::kNoErrorDialogs))
346 SuppressDialogs(); 308 SuppressDialogs();
347 309
348 // Use a minimum log level if the command line asks for one. Ignore this 310 // Use a minimum log level if the command line asks for one. Ignore this
349 // switch if there's vlog level switch present too (as both of these switches 311 // switch if there's vlog level switch present too (as both of these switches
350 // refer to the same underlying log level, and the vlog level switch has 312 // refer to the same underlying log level, and the vlog level switch has
351 // already been processed inside logging::InitLogging). If there is neither 313 // already been processed inside InitLogging). If there is neither
352 // log level nor vlog level specified, then just leave the default level 314 // log level nor vlog level specified, then just leave the default level
353 // (INFO). 315 // (INFO).
354 if (command_line.HasSwitch(switches::kLoggingLevel) && 316 if (command_line.HasSwitch(switches::kLoggingLevel) &&
355 logging::GetMinLogLevel() >= 0) { 317 GetMinLogLevel() >= 0) {
356 std::string log_level = 318 std::string log_level =
357 command_line.GetSwitchValueASCII(switches::kLoggingLevel); 319 command_line.GetSwitchValueASCII(switches::kLoggingLevel);
358 int level = 0; 320 int level = 0;
359 if (base::StringToInt(log_level, &level) && level >= 0 && 321 if (base::StringToInt(log_level, &level) && level >= 0 &&
360 level < LOG_NUM_SEVERITIES) { 322 level < LOG_NUM_SEVERITIES) {
361 logging::SetMinLogLevel(level); 323 SetMinLogLevel(level);
362 } else { 324 } else {
363 DLOG(WARNING) << "Bad log level: " << log_level; 325 DLOG(WARNING) << "Bad log level: " << log_level;
364 } 326 }
365 } 327 }
366 328
367 #if defined(OS_WIN) 329 #if defined(OS_WIN)
368 // Enable trace control and transport through event tracing for Windows. 330 // Enable trace control and transport through event tracing for Windows.
369 logging::LogEventProvider::Initialize(kChromeTraceProviderName); 331 LogEventProvider::Initialize(kChromeTraceProviderName);
370 332
371 // Enable logging to the Windows Event Log. 333 // Enable logging to the Windows Event Log.
372 logging::SetEventSourceName(base::UTF16ToASCII( 334 SetEventSourceName(base::UTF16ToASCII(
373 install_static::InstallDetails::Get().install_full_name())); 335 install_static::InstallDetails::Get().install_full_name()));
374 #endif 336 #endif
375 337
376 base::StatisticsRecorder::InitLogOnShutdown(); 338 base::StatisticsRecorder::InitLogOnShutdown();
377 339
378 chrome_logging_initialized_ = true; 340 chrome_logging_initialized_ = true;
379 } 341 }
380 342
381 // This is a no-op, but we'll keep it around in case 343 // This is a no-op, but we'll keep it around in case
382 // we need to do more cleanup in the future. 344 // we need to do more cleanup in the future.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 time_deets.year, 384 time_deets.year,
423 time_deets.month, 385 time_deets.month,
424 time_deets.day_of_month, 386 time_deets.day_of_month,
425 time_deets.hour, 387 time_deets.hour,
426 time_deets.minute, 388 time_deets.minute,
427 time_deets.second); 389 time_deets.second);
428 return base_path.InsertBeforeExtensionASCII(suffix); 390 return base_path.InsertBeforeExtensionASCII(suffix);
429 } 391 }
430 392
431 } // namespace logging 393 } // namespace logging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698