| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 <windows.h> | 8 #include <windows.h> |
| 9 typedef HANDLE FileHandle; | 9 typedef HANDLE FileHandle; |
| 10 typedef HANDLE MutexHandle; | 10 typedef HANDLE MutexHandle; |
| 11 #elif defined(OS_MACOSX) | 11 #elif defined(OS_MACOSX) |
| 12 #include <CoreFoundation/CoreFoundation.h> | 12 #include <CoreFoundation/CoreFoundation.h> |
| 13 #include <mach/mach.h> | 13 #include <mach/mach.h> |
| 14 #include <mach/mach_time.h> | 14 #include <mach/mach_time.h> |
| 15 #include <mach-o/dyld.h> | 15 #include <mach-o/dyld.h> |
| 16 #elif defined(OS_LINUX) | 16 #elif defined(OS_LINUX) |
| 17 #include <sys/syscall.h> | 17 #include <sys/syscall.h> |
| 18 #include <time.h> | 18 #include <time.h> |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 #if defined(OS_POSIX) | 21 #if defined(OS_POSIX) |
| 22 #include <fcntl.h> |
| 22 #include <stdlib.h> | 23 #include <stdlib.h> |
| 23 #include <stdio.h> | 24 #include <stdio.h> |
| 24 #include <string.h> | 25 #include <string.h> |
| 26 #include <sys/types.h> |
| 27 #include <sys/stat.h> |
| 25 #include <unistd.h> | 28 #include <unistd.h> |
| 26 #define MAX_PATH PATH_MAX | 29 #define MAX_PATH PATH_MAX |
| 27 typedef FILE* FileHandle; | 30 typedef FILE* FileHandle; |
| 28 typedef pthread_mutex_t* MutexHandle; | 31 typedef pthread_mutex_t* MutexHandle; |
| 29 #endif | 32 #endif |
| 30 | 33 |
| 31 #include <ctime> | 34 #include <ctime> |
| 32 #include <iomanip> | 35 #include <iomanip> |
| 33 #include <cstring> | 36 #include <cstring> |
| 34 #include <algorithm> | 37 #include <algorithm> |
| 35 | 38 |
| 36 #include "base/base_switches.h" | 39 #include "base/base_switches.h" |
| 37 #include "base/command_line.h" | 40 #include "base/command_line.h" |
| 38 #include "base/debug_util.h" | 41 #include "base/debug_util.h" |
| 39 #include "base/lock_impl.h" | 42 #include "base/lock_impl.h" |
| 43 #include "base/reserved_file_descriptors.h" |
| 40 #include "base/string_piece.h" | 44 #include "base/string_piece.h" |
| 41 #include "base/string_util.h" | 45 #include "base/string_util.h" |
| 42 #include "base/sys_string_conversions.h" | 46 #include "base/sys_string_conversions.h" |
| 43 | 47 |
| 44 namespace logging { | 48 namespace logging { |
| 45 | 49 |
| 46 bool g_enable_dcheck = false; | 50 bool g_enable_dcheck = false; |
| 47 | 51 |
| 48 const char* const log_severity_names[LOG_NUM_SEVERITIES] = { | 52 const char* const log_severity_names[LOG_NUM_SEVERITIES] = { |
| 49 "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL" }; | 53 "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL" }; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 log_file = CreateFile(L".\\debug.log", GENERIC_WRITE, | 202 log_file = CreateFile(L".\\debug.log", GENERIC_WRITE, |
| 199 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | 203 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 200 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | 204 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 201 if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { | 205 if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { |
| 202 log_file = NULL; | 206 log_file = NULL; |
| 203 return false; | 207 return false; |
| 204 } | 208 } |
| 205 } | 209 } |
| 206 SetFilePointer(log_file, 0, 0, FILE_END); | 210 SetFilePointer(log_file, 0, 0, FILE_END); |
| 207 #elif defined(OS_POSIX) | 211 #elif defined(OS_POSIX) |
| 212 // Reserve global fd slots. |
| 213 int reserved_fds[kReservedFds]; |
| 214 for (int i=0; i < kReservedFds; i++) |
| 215 reserved_fds[i] = open("/dev/null", O_RDONLY, 0); |
| 216 |
| 208 log_file = fopen(log_file_name->c_str(), "a"); | 217 log_file = fopen(log_file_name->c_str(), "a"); |
| 218 |
| 219 // Release the reserved fds. |
| 220 for (int i=0; i < kReservedFds; i++) |
| 221 close(reserved_fds[i]); |
| 222 |
| 209 if (log_file == NULL) | 223 if (log_file == NULL) |
| 210 return false; | 224 return false; |
| 211 #endif | 225 #endif |
| 212 } | 226 } |
| 213 | 227 |
| 214 return true; | 228 return true; |
| 215 } | 229 } |
| 216 | 230 |
| 231 #if defined(OS_LINUX) |
| 232 int GetLoggingFileDescriptor() { |
| 233 // No locking needed, since this is only called by the zygote server, |
| 234 // which is single-threaded. |
| 235 if (log_file) |
| 236 return fileno(log_file); |
| 237 return -1; |
| 238 } |
| 239 #endif |
| 240 |
| 217 void InitLogMutex() { | 241 void InitLogMutex() { |
| 218 #if defined(OS_WIN) | 242 #if defined(OS_WIN) |
| 219 if (!log_mutex) { | 243 if (!log_mutex) { |
| 220 // \ is not a legal character in mutex names so we replace \ with / | 244 // \ is not a legal character in mutex names so we replace \ with / |
| 221 std::wstring safe_name(*log_file_name); | 245 std::wstring safe_name(*log_file_name); |
| 222 std::replace(safe_name.begin(), safe_name.end(), '\\', '/'); | 246 std::replace(safe_name.begin(), safe_name.end(), '\\', '/'); |
| 223 std::wstring t(L"Global\\"); | 247 std::wstring t(L"Global\\"); |
| 224 t.append(safe_name); | 248 t.append(safe_name); |
| 225 log_mutex = ::CreateMutex(NULL, FALSE, t.c_str()); | 249 log_mutex = ::CreateMutex(NULL, FALSE, t.c_str()); |
| 226 } | 250 } |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 | 583 |
| 560 CloseFile(log_file); | 584 CloseFile(log_file); |
| 561 log_file = NULL; | 585 log_file = NULL; |
| 562 } | 586 } |
| 563 | 587 |
| 564 } // namespace logging | 588 } // namespace logging |
| 565 | 589 |
| 566 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { | 590 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { |
| 567 return out << base::SysWideToUTF8(std::wstring(wstr)); | 591 return out << base::SysWideToUTF8(std::wstring(wstr)); |
| 568 } | 592 } |
| OLD | NEW |