Index: base/logging.cc |
diff --git a/base/logging.cc b/base/logging.cc |
index 1a2f7745aa23bba9a74f1f78acf2bac2e16819ad..4ed731ffe893f58ee5190a2564bf46b0e9fa9cbf 100644 |
--- a/base/logging.cc |
+++ b/base/logging.cc |
@@ -4,16 +4,7 @@ |
#include "base/logging.h" |
-#if defined(OS_WIN) |
-#include <io.h> |
-#include <windows.h> |
-typedef HANDLE FileHandle; |
-typedef HANDLE MutexHandle; |
-// Windows warns on using write(). It prefers _write(). |
-#define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) |
-// Windows doesn't define STDERR_FILENO. Define it here. |
-#define STDERR_FILENO 2 |
-#elif defined(OS_MACOSX) |
+#if defined(OS_MACOSX) |
#include <mach/mach.h> |
#include <mach/mach_time.h> |
#include <mach-o/dyld.h> |
@@ -92,11 +83,7 @@ const int kAlwaysPrintErrorLevel = LOG_ERROR; |
// Which log file to use? This is initialized by InitLogging or |
// will be lazily initialized to the default value when it is |
// first needed. |
-#if defined(OS_WIN) |
-typedef std::wstring PathString; |
-#else |
typedef std::string PathString; |
-#endif |
PathString* g_log_file_name = nullptr; |
// This file is lazily opened and the handle may be nullptr |
@@ -120,17 +107,11 @@ LogMessageHandlerFunction log_message_handler = nullptr; |
// Helper functions to wrap platform differences. |
int32 CurrentProcessId() { |
-#if defined(OS_WIN) |
- return GetCurrentProcessId(); |
-#elif defined(OS_POSIX) |
return getpid(); |
-#endif |
} |
uint64 TickCount() { |
-#if defined(OS_WIN) |
- return GetTickCount(); |
-#elif defined(OS_MACOSX) |
+#if defined(OS_MACOSX) |
return mach_absolute_time(); |
#elif defined(OS_NACL) |
// NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h |
@@ -149,9 +130,7 @@ uint64 TickCount() { |
} |
void DeleteFilePath(const PathString& log_name) { |
-#if defined(OS_WIN) |
- DeleteFile(log_name.c_str()); |
-#elif defined(OS_NACL) |
+#if defined(OS_NACL) |
// Do nothing; unlink() isn't supported on NaCl. |
#else |
unlink(log_name.c_str()); |
@@ -159,21 +138,8 @@ void DeleteFilePath(const PathString& log_name) { |
} |
PathString GetDefaultLogFile() { |
-#if defined(OS_WIN) |
- // On Windows we use the same path as the exe. |
- wchar_t module_name[MAX_PATH]; |
- GetModuleFileName(nullptr, module_name, MAX_PATH); |
- |
- PathString log_name = module_name; |
- PathString::size_type last_backslash = log_name.rfind('\\', log_name.size()); |
- if (last_backslash != PathString::npos) |
- log_name.erase(last_backslash + 1); |
- log_name += L"debug.log"; |
- return log_name; |
-#elif defined(OS_POSIX) |
// On other platforms we just use the current directory. |
return PathString("debug.log"); |
-#endif |
} |
// This class acts as a wrapper for locking the logging files. |
@@ -197,30 +163,6 @@ class LoggingLock { |
return; |
lock_log_file = lock_log; |
if (lock_log_file == LOCK_LOG_FILE) { |
-#if defined(OS_WIN) |
- if (!log_mutex) { |
- std::wstring safe_name; |
- if (new_log_file) |
- safe_name = new_log_file; |
- else |
- safe_name = GetDefaultLogFile(); |
- // \ is not a legal character in mutex names so we replace \ with / |
- std::replace(safe_name.begin(), safe_name.end(), '\\', '/'); |
- std::wstring t(L"Global\\"); |
- t.append(safe_name); |
- log_mutex = ::CreateMutex(nullptr, FALSE, t.c_str()); |
- |
- if (log_mutex == nullptr) { |
-#if DEBUG |
- // Keep the error code for debugging |
- int error = GetLastError(); // NOLINT |
- base::debug::BreakDebugger(); |
-#endif |
- // Return nicely without putting initialized to true. |
- return; |
- } |
- } |
-#endif |
} else { |
log_lock = new base::internal::LockImpl(); |
} |
@@ -230,14 +172,7 @@ class LoggingLock { |
private: |
static void LockLogging() { |
if (lock_log_file == LOCK_LOG_FILE) { |
-#if defined(OS_WIN) |
- ::WaitForSingleObject(log_mutex, INFINITE); |
- // WaitForSingleObject could have returned WAIT_ABANDONED. We don't |
- // abort the process here. UI tests might be crashy sometimes, |
- // and aborting the test binary only makes the problem worse. |
- // We also don't use LOG macros because that might lead to an infinite |
- // loop. For more info see http://crbug.com/18028. |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
pthread_mutex_lock(&log_mutex); |
#endif |
} else { |
@@ -248,9 +183,7 @@ class LoggingLock { |
static void UnlockLogging() { |
if (lock_log_file == LOCK_LOG_FILE) { |
-#if defined(OS_WIN) |
- ReleaseMutex(log_mutex); |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
pthread_mutex_unlock(&log_mutex); |
#endif |
} else { |
@@ -265,9 +198,7 @@ class LoggingLock { |
// When we don't use a lock, we are using a global mutex. We need to do this |
// because LockFileEx is not thread safe. |
-#if defined(OS_WIN) |
- static MutexHandle log_mutex; |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
static pthread_mutex_t log_mutex; |
#endif |
@@ -282,10 +213,7 @@ base::internal::LockImpl* LoggingLock::log_lock = nullptr; |
// static |
LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE; |
-#if defined(OS_WIN) |
-// static |
-MutexHandle LoggingLock::log_mutex = nullptr; |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER; |
#endif |
@@ -303,22 +231,7 @@ bool InitializeLogFileHandle() { |
} |
if ((g_logging_destination & LOG_TO_FILE) != 0) { |
-#if defined(OS_WIN) |
- g_log_file = CreateFile(g_log_file_name->c_str(), GENERIC_WRITE, |
- FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, |
- OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
- if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) { |
- // try the current directory |
- g_log_file = CreateFile(L".\\debug.log", GENERIC_WRITE, |
- FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, |
- OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
- if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) { |
- g_log_file = nullptr; |
- return false; |
- } |
- } |
- SetFilePointer(g_log_file, 0, 0, FILE_END); |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
g_log_file = fopen(g_log_file_name->c_str(), "a"); |
if (g_log_file == nullptr) |
return false; |
@@ -329,11 +242,7 @@ bool InitializeLogFileHandle() { |
} |
void CloseFile(FileHandle log) { |
-#if defined(OS_WIN) |
- CloseHandle(log); |
-#else |
fclose(log); |
-#endif |
} |
void CloseLogFileUnlocked() { |
@@ -466,56 +375,9 @@ void DisplayDebugMessageInDialog(const std::string& str) { |
if (!show_error_dialogs) |
return; |
- |
-#if defined(OS_WIN) |
- // For Windows programs, it's possible that the message loop is |
- // messed up on a fatal error, and creating a MessageBox will cause |
- // that message loop to be run. Instead, we try to spawn another |
- // process that displays its command line. We look for "Debug |
- // Message.exe" in the same directory as the application. If it |
- // exists, we use it, otherwise, we use a regular message box. |
- wchar_t prog_name[MAX_PATH]; |
- GetModuleFileNameW(nullptr, prog_name, MAX_PATH); |
- wchar_t* backslash = wcsrchr(prog_name, '\\'); |
- if (backslash) |
- backslash[1] = 0; |
- wcscat_s(prog_name, MAX_PATH, L"debug_message.exe"); |
- |
- std::wstring cmdline = base::UTF8ToWide(str); |
- if (cmdline.empty()) |
- return; |
- |
- STARTUPINFO startup_info; |
- memset(&startup_info, 0, sizeof(startup_info)); |
- startup_info.cb = sizeof(startup_info); |
- |
- PROCESS_INFORMATION process_info; |
- if (CreateProcessW(prog_name, &cmdline[0], nullptr, nullptr, false, 0, |
- nullptr, nullptr, &startup_info, &process_info)) { |
- WaitForSingleObject(process_info.hProcess, INFINITE); |
- CloseHandle(process_info.hThread); |
- CloseHandle(process_info.hProcess); |
- } else { |
- // debug process broken, let's just do a message box |
- MessageBoxW(nullptr, &cmdline[0], L"Fatal error", |
- MB_OK | MB_ICONHAND | MB_TOPMOST); |
- } |
-#else |
- // We intentionally don't implement a dialog on other platforms. |
- // You can just look at stderr. |
-#endif // defined(OS_WIN) |
} |
#endif // !defined(NDEBUG) |
-#if defined(OS_WIN) |
-LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) { |
-} |
- |
-LogMessage::SaveLastError::~SaveLastError() { |
- ::SetLastError(last_error_); |
-} |
-#endif // defined(OS_WIN) |
- |
LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
: severity_(severity), file_(file), line_(line) { |
Init(file, line); |
@@ -557,9 +419,7 @@ LogMessage::~LogMessage() { |
} |
if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { |
-#if defined(OS_WIN) |
- OutputDebugStringA(str_newline.c_str()); |
-#elif defined(OS_ANDROID) |
+#if defined(OS_ANDROID) |
android_LogPriority priority = |
(severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN; |
switch (severity_) { |
@@ -600,19 +460,9 @@ LogMessage::~LogMessage() { |
LoggingLock::Init(LOCK_LOG_FILE, nullptr); |
LoggingLock logging_lock; |
if (InitializeLogFileHandle()) { |
-#if defined(OS_WIN) |
- SetFilePointer(g_log_file, 0, 0, SEEK_END); |
- DWORD num_written; |
- WriteFile(g_log_file, |
- static_cast<const void*>(str_newline.c_str()), |
- static_cast<DWORD>(str_newline.length()), |
- &num_written, |
- nullptr); |
-#else |
ignore_result(fwrite( |
str_newline.data(), str_newline.size(), 1, g_log_file)); |
fflush(g_log_file); |
-#endif |
} |
} |
@@ -685,64 +535,23 @@ void LogMessage::Init(const char* file, int line) { |
message_start_ = stream_.str().length(); |
} |
-#if defined(OS_WIN) |
-// This has already been defined in the header, but defining it again as DWORD |
-// ensures that the type used in the header is equivalent to DWORD. If not, |
-// the redefinition is a compile error. |
-typedef DWORD SystemErrorCode; |
-#endif |
- |
SystemErrorCode GetLastSystemErrorCode() { |
-#if defined(OS_WIN) |
- return ::GetLastError(); |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
return errno; |
#else |
#error Not implemented |
#endif |
} |
-#if defined(OS_WIN) |
-BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
- const int kErrorMessageBufferSize = 256; |
- char msgbuf[kErrorMessageBufferSize]; |
- DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; |
- DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf, |
- arraysize(msgbuf), nullptr); |
- if (len) { |
- // Messages returned by system end with line breaks. |
- return base::CollapseWhitespaceASCII(msgbuf, true) + |
- base::StringPrintf(" (0x%X)", error_code); |
- } |
- return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)", |
- GetLastError(), error_code); |
-} |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
return base::safe_strerror(error_code); |
} |
#else |
#error Not implemented |
-#endif // defined(OS_WIN) |
- |
- |
-#if defined(OS_WIN) |
-Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
- int line, |
- LogSeverity severity, |
- SystemErrorCode err) |
- : err_(err), |
- log_message_(file, line, severity) { |
-} |
+#endif // defined(OS_POSIX) |
-Win32ErrorLogMessage::~Win32ErrorLogMessage() { |
- stream() << ": " << SystemErrorCodeToString(err_); |
- // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a |
- // field) and use Alias in hopes that it makes it into crash dumps. |
- DWORD last_error = err_; |
- base::debug::Alias(&last_error); |
-} |
-#elif defined(OS_POSIX) |
+#if defined(OS_POSIX) |
ErrnoLogMessage::ErrnoLogMessage(const char* file, |
int line, |
LogSeverity severity, |
@@ -754,7 +563,7 @@ ErrnoLogMessage::ErrnoLogMessage(const char* file, |
ErrnoLogMessage::~ErrnoLogMessage() { |
stream() << ": " << SystemErrorCodeToString(err_); |
} |
-#endif // defined(OS_WIN) |
+#endif // defined(OS_POSIX) |
void CloseLogFile() { |
LoggingLock logging_lock; |
@@ -795,14 +604,6 @@ void RawLog(int level, const char* message) { |
// This was defined at the beginning of this file. |
#undef write |
-#if defined(OS_WIN) |
-std::wstring GetLogFileFullPath() { |
- if (g_log_file_name) |
- return *g_log_file_name; |
- return std::wstring(); |
-} |
-#endif |
- |
BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
LogMessage(file, line, LOG_ERROR).stream() |
<< "NOTREACHED() hit."; |