| OLD | NEW |
| 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 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/debug/activity_tracker.h" | 10 #include "base/debug/activity_tracker.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 #include <stdio.h> | 39 #include <stdio.h> |
| 40 #include <stdlib.h> | 40 #include <stdlib.h> |
| 41 #include <string.h> | 41 #include <string.h> |
| 42 #include <sys/stat.h> | 42 #include <sys/stat.h> |
| 43 #include <unistd.h> | 43 #include <unistd.h> |
| 44 #define MAX_PATH PATH_MAX | 44 #define MAX_PATH PATH_MAX |
| 45 typedef FILE* FileHandle; | 45 typedef FILE* FileHandle; |
| 46 typedef pthread_mutex_t* MutexHandle; | 46 typedef pthread_mutex_t* MutexHandle; |
| 47 #endif | 47 #endif |
| 48 | 48 |
| 49 #if defined(OS_FUCHSIA) |
| 50 #include <magenta/syscalls.h> |
| 51 #include <threads.h> |
| 52 #include <unistd.h> |
| 53 typedef FILE* FileHandle; |
| 54 typedef mtx_t MutexHandle; |
| 55 #endif |
| 56 |
| 49 #include <algorithm> | 57 #include <algorithm> |
| 50 #include <cstring> | 58 #include <cstring> |
| 51 #include <ctime> | 59 #include <ctime> |
| 52 #include <iomanip> | 60 #include <iomanip> |
| 53 #include <ostream> | 61 #include <ostream> |
| 54 #include <string> | 62 #include <string> |
| 55 | 63 |
| 56 #include "base/base_switches.h" | 64 #include "base/base_switches.h" |
| 57 #include "base/command_line.h" | 65 #include "base/command_line.h" |
| 58 #include "base/debug/alias.h" | 66 #include "base/debug/alias.h" |
| 59 #include "base/debug/debugger.h" | 67 #include "base/debug/debugger.h" |
| 60 #include "base/debug/stack_trace.h" | 68 #include "base/debug/stack_trace.h" |
| 61 #include "base/posix/eintr_wrapper.h" | 69 #include "base/posix/eintr_wrapper.h" |
| 62 #include "base/strings/string_piece.h" | 70 #include "base/strings/string_piece.h" |
| 63 #include "base/strings/string_util.h" | 71 #include "base/strings/string_util.h" |
| 64 #include "base/strings/stringprintf.h" | 72 #include "base/strings/stringprintf.h" |
| 65 #include "base/strings/sys_string_conversions.h" | 73 #include "base/strings/sys_string_conversions.h" |
| 66 #include "base/strings/utf_string_conversions.h" | 74 #include "base/strings/utf_string_conversions.h" |
| 67 #include "base/synchronization/lock_impl.h" | 75 #include "base/synchronization/lock_impl.h" |
| 68 #include "base/threading/platform_thread.h" | 76 #include "base/threading/platform_thread.h" |
| 69 #include "base/vlog.h" | 77 #include "base/vlog.h" |
| 70 #if defined(OS_POSIX) | 78 #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 71 #include "base/posix/safe_strerror.h" | 79 #include "base/posix/safe_strerror.h" |
| 72 #endif | 80 #endif |
| 73 | 81 |
| 74 #if defined(OS_ANDROID) | 82 #if defined(OS_ANDROID) |
| 75 #include <android/log.h> | 83 #include <android/log.h> |
| 76 #endif | 84 #endif |
| 77 | 85 |
| 78 namespace logging { | 86 namespace logging { |
| 79 | 87 |
| 80 namespace { | 88 namespace { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 // the debug message dialog and process termination. | 132 // the debug message dialog and process termination. |
| 125 LogAssertHandlerFunction log_assert_handler = nullptr; | 133 LogAssertHandlerFunction log_assert_handler = nullptr; |
| 126 // A log message handler that gets notified of every log message we process. | 134 // A log message handler that gets notified of every log message we process. |
| 127 LogMessageHandlerFunction log_message_handler = nullptr; | 135 LogMessageHandlerFunction log_message_handler = nullptr; |
| 128 | 136 |
| 129 // Helper functions to wrap platform differences. | 137 // Helper functions to wrap platform differences. |
| 130 | 138 |
| 131 int32_t CurrentProcessId() { | 139 int32_t CurrentProcessId() { |
| 132 #if defined(OS_WIN) | 140 #if defined(OS_WIN) |
| 133 return GetCurrentProcessId(); | 141 return GetCurrentProcessId(); |
| 134 #elif defined(OS_POSIX) | 142 #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 135 return getpid(); | 143 return getpid(); |
| 136 #endif | 144 #endif |
| 137 } | 145 } |
| 138 | 146 |
| 139 uint64_t TickCount() { | 147 uint64_t TickCount() { |
| 140 #if defined(OS_WIN) | 148 #if defined(OS_WIN) |
| 141 return GetTickCount(); | 149 return GetTickCount(); |
| 142 #elif defined(OS_MACOSX) | 150 #elif defined(OS_MACOSX) |
| 143 return mach_absolute_time(); | 151 return mach_absolute_time(); |
| 144 #elif defined(OS_NACL) | 152 #elif defined(OS_NACL) |
| 145 // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h | 153 // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h |
| 146 // So we have to use clock() for now. | 154 // So we have to use clock() for now. |
| 147 return clock(); | 155 return clock(); |
| 148 #elif defined(OS_POSIX) | 156 #elif defined(OS_POSIX) |
| 149 struct timespec ts; | 157 struct timespec ts; |
| 150 clock_gettime(CLOCK_MONOTONIC, &ts); | 158 clock_gettime(CLOCK_MONOTONIC, &ts); |
| 151 | 159 |
| 152 uint64_t absolute_micro = static_cast<int64_t>(ts.tv_sec) * 1000000 + | 160 uint64_t absolute_micro = static_cast<int64_t>(ts.tv_sec) * 1000000 + |
| 153 static_cast<int64_t>(ts.tv_nsec) / 1000; | 161 static_cast<int64_t>(ts.tv_nsec) / 1000; |
| 154 | 162 |
| 155 return absolute_micro; | 163 return absolute_micro; |
| 164 #elif defined(OS_FUCHSIA) |
| 165 return mx_time_get(MX_CLOCK_MONOTONIC) / 1000; |
| 156 #endif | 166 #endif |
| 157 } | 167 } |
| 158 | 168 |
| 159 void DeleteFilePath(const PathString& log_name) { | 169 void DeleteFilePath(const PathString& log_name) { |
| 160 #if defined(OS_WIN) | 170 #if defined(OS_WIN) |
| 161 DeleteFile(log_name.c_str()); | 171 DeleteFile(log_name.c_str()); |
| 162 #elif defined(OS_NACL) | 172 #elif defined(OS_NACL) |
| 163 // Do nothing; unlink() isn't supported on NaCl. | 173 // Do nothing; unlink() isn't supported on NaCl. |
| 164 #else | 174 #else |
| 165 unlink(log_name.c_str()); | 175 unlink(log_name.c_str()); |
| 166 #endif | 176 #endif |
| 167 } | 177 } |
| 168 | 178 |
| 169 PathString GetDefaultLogFile() { | 179 PathString GetDefaultLogFile() { |
| 170 #if defined(OS_WIN) | 180 #if defined(OS_WIN) |
| 171 // On Windows we use the same path as the exe. | 181 // On Windows we use the same path as the exe. |
| 172 wchar_t module_name[MAX_PATH]; | 182 wchar_t module_name[MAX_PATH]; |
| 173 GetModuleFileName(nullptr, module_name, MAX_PATH); | 183 GetModuleFileName(nullptr, module_name, MAX_PATH); |
| 174 | 184 |
| 175 PathString log_name = module_name; | 185 PathString log_name = module_name; |
| 176 PathString::size_type last_backslash = log_name.rfind('\\', log_name.size()); | 186 PathString::size_type last_backslash = log_name.rfind('\\', log_name.size()); |
| 177 if (last_backslash != PathString::npos) | 187 if (last_backslash != PathString::npos) |
| 178 log_name.erase(last_backslash + 1); | 188 log_name.erase(last_backslash + 1); |
| 179 log_name += L"debug.log"; | 189 log_name += L"debug.log"; |
| 180 return log_name; | 190 return log_name; |
| 181 #elif defined(OS_POSIX) | 191 #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 182 // On other platforms we just use the current directory. | 192 // On other platforms we just use the current directory. |
| 183 return PathString("debug.log"); | 193 return PathString("debug.log"); |
| 184 #endif | 194 #endif |
| 185 } | 195 } |
| 186 | 196 |
| 187 // We don't need locks on Windows for atomically appending to files. The OS | 197 // We don't need locks on Windows for atomically appending to files. The OS |
| 188 // provides this functionality. | 198 // provides this functionality. |
| 189 #if !defined(OS_WIN) | 199 #if !defined(OS_WIN) |
| 190 // This class acts as a wrapper for locking the logging files. | 200 // This class acts as a wrapper for locking the logging files. |
| 191 // LoggingLock::Init() should be called from the main thread before any logging | 201 // LoggingLock::Init() should be called from the main thread before any logging |
| (...skipping 631 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 #if defined(OS_WIN) | 833 #if defined(OS_WIN) |
| 824 // This has already been defined in the header, but defining it again as DWORD | 834 // This has already been defined in the header, but defining it again as DWORD |
| 825 // ensures that the type used in the header is equivalent to DWORD. If not, | 835 // ensures that the type used in the header is equivalent to DWORD. If not, |
| 826 // the redefinition is a compile error. | 836 // the redefinition is a compile error. |
| 827 typedef DWORD SystemErrorCode; | 837 typedef DWORD SystemErrorCode; |
| 828 #endif | 838 #endif |
| 829 | 839 |
| 830 SystemErrorCode GetLastSystemErrorCode() { | 840 SystemErrorCode GetLastSystemErrorCode() { |
| 831 #if defined(OS_WIN) | 841 #if defined(OS_WIN) |
| 832 return ::GetLastError(); | 842 return ::GetLastError(); |
| 833 #elif defined(OS_POSIX) | 843 #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 834 return errno; | 844 return errno; |
| 835 #else | 845 #else |
| 836 #error Not implemented | 846 #error Not implemented |
| 837 #endif | 847 #endif |
| 838 } | 848 } |
| 839 | 849 |
| 840 #if defined(OS_WIN) | 850 #if defined(OS_WIN) |
| 841 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { | 851 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
| 842 const int kErrorMessageBufferSize = 256; | 852 const int kErrorMessageBufferSize = 256; |
| 843 char msgbuf[kErrorMessageBufferSize]; | 853 char msgbuf[kErrorMessageBufferSize]; |
| 844 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; | 854 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; |
| 845 DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf, | 855 DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf, |
| 846 arraysize(msgbuf), nullptr); | 856 arraysize(msgbuf), nullptr); |
| 847 if (len) { | 857 if (len) { |
| 848 // Messages returned by system end with line breaks. | 858 // Messages returned by system end with line breaks. |
| 849 return base::CollapseWhitespaceASCII(msgbuf, true) + | 859 return base::CollapseWhitespaceASCII(msgbuf, true) + |
| 850 base::StringPrintf(" (0x%X)", error_code); | 860 base::StringPrintf(" (0x%X)", error_code); |
| 851 } | 861 } |
| 852 return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)", | 862 return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)", |
| 853 GetLastError(), error_code); | 863 GetLastError(), error_code); |
| 854 } | 864 } |
| 855 #elif defined(OS_POSIX) | 865 #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 856 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { | 866 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
| 857 return base::safe_strerror(error_code); | 867 return base::safe_strerror(error_code); |
| 858 } | 868 } |
| 859 #else | 869 #else |
| 860 #error Not implemented | 870 #error Not implemented |
| 861 #endif // defined(OS_WIN) | 871 #endif // defined(OS_WIN) |
| 862 | 872 |
| 863 | 873 |
| 864 #if defined(OS_WIN) | 874 #if defined(OS_WIN) |
| 865 Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, | 875 Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 947 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { | 957 BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
| 948 LogMessage(file, line, LOG_ERROR).stream() | 958 LogMessage(file, line, LOG_ERROR).stream() |
| 949 << "NOTREACHED() hit."; | 959 << "NOTREACHED() hit."; |
| 950 } | 960 } |
| 951 | 961 |
| 952 } // namespace logging | 962 } // namespace logging |
| 953 | 963 |
| 954 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { | 964 std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
| 955 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); | 965 return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
| 956 } | 966 } |
| OLD | NEW |