OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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; |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 #include <ctime> | 30 #include <ctime> |
31 #include <iomanip> | 31 #include <iomanip> |
32 #include <cstring> | 32 #include <cstring> |
33 #include <algorithm> | 33 #include <algorithm> |
34 | 34 |
35 #include "base/base_switches.h" | 35 #include "base/base_switches.h" |
36 #include "base/command_line.h" | 36 #include "base/command_line.h" |
37 #include "base/debug_util.h" | 37 #include "base/debug_util.h" |
38 #include "base/lock_impl.h" | 38 #include "base/lock_impl.h" |
| 39 #include "base/platform_thread.h" |
| 40 #include "base/process_util.h" |
39 #include "base/string_piece.h" | 41 #include "base/string_piece.h" |
40 #include "base/string_util.h" | 42 #include "base/string_util.h" |
41 #include "base/sys_string_conversions.h" | 43 #include "base/sys_string_conversions.h" |
42 | 44 #include "base/time.h" |
| 45 |
43 namespace logging { | 46 namespace logging { |
44 | 47 |
45 bool g_enable_dcheck = false; | 48 bool g_enable_dcheck = false; |
46 | 49 |
47 const char* const log_severity_names[LOG_NUM_SEVERITIES] = { | 50 const char* const log_severity_names[LOG_NUM_SEVERITIES] = { |
48 "INFO", "WARNING", "ERROR", "FATAL" }; | 51 "INFO", "WARNING", "ERROR", "FATAL" }; |
49 | 52 |
50 int min_log_level = 0; | 53 int min_log_level = 0; |
51 LogLockingState lock_log_file = LOCK_LOG_FILE; | 54 LogLockingState lock_log_file = LOCK_LOG_FILE; |
52 | 55 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // When we don't use a lock, we are using a global mutex. We need to do this | 102 // When we don't use a lock, we are using a global mutex. We need to do this |
100 // because LockFileEx is not thread safe. | 103 // because LockFileEx is not thread safe. |
101 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
102 MutexHandle log_mutex = NULL; | 105 MutexHandle log_mutex = NULL; |
103 #elif defined(OS_POSIX) | 106 #elif defined(OS_POSIX) |
104 pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; | 107 pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; |
105 #endif | 108 #endif |
106 | 109 |
107 // Helper functions to wrap platform differences. | 110 // Helper functions to wrap platform differences. |
108 | 111 |
109 int32 CurrentProcessId() { | |
110 #if defined(OS_WIN) | |
111 return GetCurrentProcessId(); | |
112 #elif defined(OS_POSIX) | |
113 return getpid(); | |
114 #endif | |
115 } | |
116 | |
117 int32 CurrentThreadId() { | |
118 #if defined(OS_WIN) | |
119 return GetCurrentThreadId(); | |
120 #elif defined(OS_MACOSX) | |
121 return mach_thread_self(); | |
122 #else | |
123 NOTIMPLEMENTED(); | |
124 return 0; | |
125 #endif | |
126 } | |
127 | |
128 uint64 TickCount() { | |
129 #if defined(OS_WIN) | |
130 return GetTickCount(); | |
131 #elif defined(OS_MACOSX) | |
132 return mach_absolute_time(); | |
133 #else | |
134 NOTIMPLEMENTED(); | |
135 return 0; | |
136 #endif | |
137 } | |
138 | |
139 void CloseFile(FileHandle log) { | 112 void CloseFile(FileHandle log) { |
140 #if defined(OS_WIN) | 113 #if defined(OS_WIN) |
141 CloseHandle(log); | 114 CloseHandle(log); |
142 #else | 115 #else |
143 fclose(log); | 116 fclose(log); |
144 #endif | 117 #endif |
145 } | 118 } |
146 | 119 |
147 void DeleteFilePath(const PathString& log_name) { | 120 void DeleteFilePath(const PathString& log_name) { |
148 #if defined(OS_WIN) | 121 #if defined(OS_WIN) |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 void LogMessage::Init(const char* file, int line) { | 328 void LogMessage::Init(const char* file, int line) { |
356 // log only the filename | 329 // log only the filename |
357 const char* last_slash = strrchr(file, '\\'); | 330 const char* last_slash = strrchr(file, '\\'); |
358 if (last_slash) | 331 if (last_slash) |
359 file = last_slash + 1; | 332 file = last_slash + 1; |
360 | 333 |
361 // TODO(darin): It might be nice if the columns were fixed width. | 334 // TODO(darin): It might be nice if the columns were fixed width. |
362 | 335 |
363 stream_ << '['; | 336 stream_ << '['; |
364 if (log_process_id) | 337 if (log_process_id) |
365 stream_ << CurrentProcessId() << ':'; | 338 stream_ << process_util::GetCurrentProcId() << ':'; |
366 if (log_thread_id) | 339 if (log_thread_id) |
367 stream_ << CurrentThreadId() << ':'; | 340 stream_ << PlatformThread::CurrentId() << ':'; |
368 if (log_timestamp) { | 341 if (log_timestamp) { |
369 time_t t = time(NULL); | 342 time_t t = time(NULL); |
370 #if _MSC_VER >= 1400 | 343 #if _MSC_VER >= 1400 |
371 struct tm local_time = {0}; | 344 struct tm local_time = {0}; |
372 localtime_s(&local_time, &t); | 345 localtime_s(&local_time, &t); |
373 struct tm* tm_time = &local_time; | 346 struct tm* tm_time = &local_time; |
374 #else | 347 #else |
375 struct tm* tm_time = localtime(&t); | 348 struct tm* tm_time = localtime(&t); |
376 #endif | 349 #endif |
377 stream_ << std::setfill('0') | 350 stream_ << std::setfill('0') |
378 << std::setw(2) << 1 + tm_time->tm_mon | 351 << std::setw(2) << 1 + tm_time->tm_mon |
379 << std::setw(2) << tm_time->tm_mday | 352 << std::setw(2) << tm_time->tm_mday |
380 << '/' | 353 << '/' |
381 << std::setw(2) << tm_time->tm_hour | 354 << std::setw(2) << tm_time->tm_hour |
382 << std::setw(2) << tm_time->tm_min | 355 << std::setw(2) << tm_time->tm_min |
383 << std::setw(2) << tm_time->tm_sec | 356 << std::setw(2) << tm_time->tm_sec |
384 << ':'; | 357 << ':'; |
385 } | 358 } |
386 if (log_tickcount) | 359 if (log_tickcount) |
387 stream_ << TickCount() << ':'; | 360 stream_ << base::TimeTicks::Now().ToInternalValue() << ':'; |
388 stream_ << log_severity_names[severity_] << ":" << file << "(" << line << ")]
"; | 361 stream_ << log_severity_names[severity_] << ":" << file << "(" << line << ")]
"; |
389 | 362 |
390 message_start_ = stream_.tellp(); | 363 message_start_ = stream_.tellp(); |
391 } | 364 } |
392 | 365 |
393 LogMessage::~LogMessage() { | 366 LogMessage::~LogMessage() { |
394 // TODO(brettw) modify the macros so that nothing is executed when the log | 367 // TODO(brettw) modify the macros so that nothing is executed when the log |
395 // level is too high. | 368 // level is too high. |
396 if (severity_ < min_log_level) | 369 if (severity_ < min_log_level) |
397 return; | 370 return; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 CloseFile(log_file); | 482 CloseFile(log_file); |
510 log_file = NULL; | 483 log_file = NULL; |
511 } | 484 } |
512 | 485 |
513 } // namespace logging | 486 } // namespace logging |
514 | 487 |
515 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { | 488 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { |
516 return out << base::SysWideToUTF8(std::wstring(wstr)); | 489 return out << base::SysWideToUTF8(std::wstring(wstr)); |
517 } | 490 } |
518 | 491 |
OLD | NEW |