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

Side by Side Diff: base/logging.cc

Issue 19091004: Only use the LoggingLock when actually logging to a file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 7 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
« no previous file with comments | « no previous file | remoting/host/logging_posix.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 (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 "base/logging.h" 5 #include "base/logging.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <io.h> 8 #include <io.h>
9 #include <windows.h> 9 #include <windows.h>
10 typedef HANDLE FileHandle; 10 typedef HANDLE FileHandle;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 clock_gettime(CLOCK_MONOTONIC, &ts); 145 clock_gettime(CLOCK_MONOTONIC, &ts);
146 146
147 uint64 absolute_micro = 147 uint64 absolute_micro =
148 static_cast<int64>(ts.tv_sec) * 1000000 + 148 static_cast<int64>(ts.tv_sec) * 1000000 +
149 static_cast<int64>(ts.tv_nsec) / 1000; 149 static_cast<int64>(ts.tv_nsec) / 1000;
150 150
151 return absolute_micro; 151 return absolute_micro;
152 #endif 152 #endif
153 } 153 }
154 154
155 void CloseFile(FileHandle log) {
156 #if defined(OS_WIN)
157 CloseHandle(log);
158 #else
159 fclose(log);
160 #endif
161 }
162
163 void DeleteFilePath(const PathString& log_name) { 155 void DeleteFilePath(const PathString& log_name) {
164 #if defined(OS_WIN) 156 #if defined(OS_WIN)
165 DeleteFile(log_name.c_str()); 157 DeleteFile(log_name.c_str());
166 #elif defined (OS_NACL) 158 #elif defined (OS_NACL)
167 // Do nothing; unlink() isn't supported on NaCl. 159 // Do nothing; unlink() isn't supported on NaCl.
168 #else 160 #else
169 unlink(log_name.c_str()); 161 unlink(log_name.c_str());
170 #endif 162 #endif
171 } 163 }
172 164
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 #elif defined(OS_POSIX) 326 #elif defined(OS_POSIX)
335 log_file = fopen(log_file_name->c_str(), "a"); 327 log_file = fopen(log_file_name->c_str(), "a");
336 if (log_file == NULL) 328 if (log_file == NULL)
337 return false; 329 return false;
338 #endif 330 #endif
339 } 331 }
340 332
341 return true; 333 return true;
342 } 334 }
343 335
336 void CloseFile(FileHandle log) {
337 #if defined(OS_WIN)
338 CloseHandle(log);
339 #else
340 fclose(log);
341 #endif
342 }
343
344 void CloseLogFileUnlocked() {
345 if (!log_file)
346 return;
347
348 CloseFile(log_file);
349 log_file = NULL;
350 }
351
344 } // namespace 352 } // namespace
345 353
346 LoggingSettings::LoggingSettings() 354 LoggingSettings::LoggingSettings()
347 : logging_dest(LOG_DEFAULT), 355 : logging_dest(LOG_DEFAULT),
348 log_file(NULL), 356 log_file(NULL),
349 lock_log(LOCK_LOG_FILE), 357 lock_log(LOCK_LOG_FILE),
350 delete_old(APPEND_TO_OLD_LOG_FILE), 358 delete_old(APPEND_TO_OLD_LOG_FILE),
351 dcheck_state(DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) {} 359 dcheck_state(DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) {}
352 360
353 bool BaseInitLoggingImpl(const LoggingSettings& settings) { 361 bool BaseInitLoggingImpl(const LoggingSettings& settings) {
(...skipping 12 matching lines...) Expand all
366 // one. We keep track of both to avoid memory leak warnings. 374 // one. We keep track of both to avoid memory leak warnings.
367 CHECK(!g_vlog_info_prev); 375 CHECK(!g_vlog_info_prev);
368 g_vlog_info_prev = g_vlog_info; 376 g_vlog_info_prev = g_vlog_info;
369 377
370 g_vlog_info = 378 g_vlog_info =
371 new VlogInfo(command_line->GetSwitchValueASCII(switches::kV), 379 new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
372 command_line->GetSwitchValueASCII(switches::kVModule), 380 command_line->GetSwitchValueASCII(switches::kVModule),
373 &min_log_level); 381 &min_log_level);
374 } 382 }
375 383
376 LoggingLock::Init(settings.lock_log, settings.log_file);
377
378 LoggingLock logging_lock;
379
380 if (log_file) {
381 // calling InitLogging twice or after some log call has already opened the
382 // default log file will re-initialize to the new options
383 CloseFile(log_file);
384 log_file = NULL;
385 }
386
387 logging_destination = settings.logging_dest; 384 logging_destination = settings.logging_dest;
388 385
389 // ignore file options unless logging to file is set. 386 // ignore file options unless logging to file is set.
390 if ((logging_destination & LOG_TO_FILE) == 0) 387 if ((logging_destination & LOG_TO_FILE) == 0)
391 return true; 388 return true;
392 389
390 LoggingLock::Init(settings.lock_log, settings.log_file);
391 LoggingLock logging_lock;
392
393 // Calling InitLogging twice or after some log call has already opened the
394 // default log file will re-initialize to the new options.
395 CloseLogFileUnlocked();
396
393 if (!log_file_name) 397 if (!log_file_name)
394 log_file_name = new PathString(); 398 log_file_name = new PathString();
395 *log_file_name = settings.log_file; 399 *log_file_name = settings.log_file;
396 if (settings.delete_old == DELETE_OLD_LOG_FILE) 400 if (settings.delete_old == DELETE_OLD_LOG_FILE)
397 DeleteFilePath(*log_file_name); 401 DeleteFilePath(*log_file_name);
398 402
399 return InitializeLogFileHandle(); 403 return InitializeLogFileHandle();
400 } 404 }
401 405
402 void SetMinLogLevel(int level) { 406 void SetMinLogLevel(int level) {
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 fprintf(stderr, "%s", str_newline.c_str()); 605 fprintf(stderr, "%s", str_newline.c_str());
602 fflush(stderr); 606 fflush(stderr);
603 } else if (severity_ >= kAlwaysPrintErrorLevel) { 607 } else if (severity_ >= kAlwaysPrintErrorLevel) {
604 // When we're only outputting to a log file, above a certain log level, we 608 // When we're only outputting to a log file, above a certain log level, we
605 // should still output to stderr so that we can better detect and diagnose 609 // should still output to stderr so that we can better detect and diagnose
606 // problems with unit tests, especially on the buildbots. 610 // problems with unit tests, especially on the buildbots.
607 fprintf(stderr, "%s", str_newline.c_str()); 611 fprintf(stderr, "%s", str_newline.c_str());
608 fflush(stderr); 612 fflush(stderr);
609 } 613 }
610 614
611 // We can have multiple threads and/or processes, so try to prevent them
612 // from clobbering each other's writes.
613 // If the client app did not call InitLogging, and the lock has not
614 // been created do it now. We do this on demand, but if two threads try
615 // to do this at the same time, there will be a race condition to create
616 // the lock. This is why InitLogging should be called from the main
617 // thread at the beginning of execution.
618 LoggingLock::Init(LOCK_LOG_FILE, NULL);
619 // write to log file 615 // write to log file
620 if ((logging_destination & LOG_TO_FILE) != 0) { 616 if ((logging_destination & LOG_TO_FILE) != 0) {
617 // We can have multiple threads and/or processes, so try to prevent them
618 // from clobbering each other's writes.
619 // If the client app did not call InitLogging, and the lock has not
620 // been created do it now. We do this on demand, but if two threads try
621 // to do this at the same time, there will be a race condition to create
622 // the lock. This is why InitLogging should be called from the main
623 // thread at the beginning of execution.
624 LoggingLock::Init(LOCK_LOG_FILE, NULL);
621 LoggingLock logging_lock; 625 LoggingLock logging_lock;
622 if (InitializeLogFileHandle()) { 626 if (InitializeLogFileHandle()) {
623 #if defined(OS_WIN) 627 #if defined(OS_WIN)
624 SetFilePointer(log_file, 0, 0, SEEK_END); 628 SetFilePointer(log_file, 0, 0, SEEK_END);
625 DWORD num_written; 629 DWORD num_written;
626 WriteFile(log_file, 630 WriteFile(log_file,
627 static_cast<const void*>(str_newline.c_str()), 631 static_cast<const void*>(str_newline.c_str()),
628 static_cast<DWORD>(str_newline.length()), 632 static_cast<DWORD>(str_newline.length()),
629 &num_written, 633 &num_written,
630 NULL); 634 NULL);
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 log_message_(file, line, severity) { 807 log_message_(file, line, severity) {
804 } 808 }
805 809
806 ErrnoLogMessage::~ErrnoLogMessage() { 810 ErrnoLogMessage::~ErrnoLogMessage() {
807 stream() << ": " << safe_strerror(err_); 811 stream() << ": " << safe_strerror(err_);
808 } 812 }
809 #endif // OS_WIN 813 #endif // OS_WIN
810 814
811 void CloseLogFile() { 815 void CloseLogFile() {
812 LoggingLock logging_lock; 816 LoggingLock logging_lock;
813 817 CloseLogFileUnlocked();
814 if (!log_file)
815 return;
816
817 CloseFile(log_file);
818 log_file = NULL;
819 } 818 }
820 819
821 void RawLog(int level, const char* message) { 820 void RawLog(int level, const char* message) {
822 if (level >= min_log_level) { 821 if (level >= min_log_level) {
823 size_t bytes_written = 0; 822 size_t bytes_written = 0;
824 const size_t message_len = strlen(message); 823 const size_t message_len = strlen(message);
825 int rv; 824 int rv;
826 while (bytes_written < message_len) { 825 while (bytes_written < message_len) {
827 rv = HANDLE_EINTR( 826 rv = HANDLE_EINTR(
828 write(STDERR_FILENO, message + bytes_written, 827 write(STDERR_FILENO, message + bytes_written,
(...skipping 29 matching lines...) Expand all
858 return *log_file_name; 857 return *log_file_name;
859 return std::wstring(); 858 return std::wstring();
860 } 859 }
861 #endif 860 #endif
862 861
863 } // namespace logging 862 } // namespace logging
864 863
865 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { 864 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) {
866 return out << WideToUTF8(std::wstring(wstr)); 865 return out << WideToUTF8(std::wstring(wstr));
867 } 866 }
OLDNEW
« no previous file with comments | « no previous file | remoting/host/logging_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698