fixed cygwin compilation errors
[platform/upstream/glog.git] / src / utilities.cc
index ec0e1b9..133e708 100644 (file)
@@ -44,6 +44,9 @@
 #elif defined(HAVE_SYS_SYSCALL_H)
 #include <sys/syscall.h>                 // for syscall()
 #endif
+#ifdef HAVE_SYSLOG_H
+# include <syslog.h>
+#endif
 
 #include "base/googleinit.h"
 
@@ -63,8 +66,8 @@ _END_GOOGLE_NAMESPACE_
 #include "symbolize.h"
 #include "base/commandlineflags.h"
 
-DEFINE_bool(symbolize_stacktrace, true,
-            "Symbolize the stack trace in the tombstone");
+GLOG_DEFINE_bool(symbolize_stacktrace, true,
+                 "Symbolize the stack trace in the tombstone");
 
 _START_GOOGLE_NAMESPACE_
 
@@ -74,9 +77,11 @@ typedef void DebugWriter(const char*, void*);
 // For some environments, add two extra bytes for the leading "0x".
 static const int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
 
-static void DebugWriteToStderr(const char* data, void *unused) {
+static void DebugWriteToStderr(const char* data, void *) {
   // This one is signal-safe.
-  write(STDERR_FILENO, data, strlen(data));
+  if (write(STDERR_FILENO, data, strlen(data)) < 0) {
+    // Ignore errors.
+  }
 }
 
 void DebugWriteToString(const char* data, void *arg) {
@@ -131,13 +136,18 @@ static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
 static void DumpStackTraceAndExit() {
   DumpStackTrace(1, DebugWriteToStderr, NULL);
 
-  // Set the default signal handler for SIGABRT, to avoid invoking our
-  // own signal handler installed by InstallFailedSignalHandler().
-  struct sigaction sig_action;
-  memset(&sig_action, 0, sizeof(sig_action));
-  sigemptyset(&sig_action.sa_mask);
-  sig_action.sa_handler = SIG_DFL;
-  sigaction(SIGABRT, &sig_action, NULL);
+  // TOOD(hamaji): Use signal instead of sigaction?
+#ifdef HAVE_SIGACTION
+  if (IsFailureSignalHandlerInstalled()) {
+    // Set the default signal handler for SIGABRT, to avoid invoking our
+    // own signal handler installed by InstallFailureSignalHandler().
+    struct sigaction sig_action;
+    memset(&sig_action, 0, sizeof(sig_action));
+    sigemptyset(&sig_action.sa_mask);
+    sig_action.sa_handler = SIG_DFL;
+    sigaction(SIGABRT, &sig_action, NULL);
+  }
+#endif  // HAVE_SIGACTION
 
   abort();
 }
@@ -218,9 +228,18 @@ int32 GetMainThreadPid() {
   return g_main_thread_pid;
 }
 
+bool PidHasChanged() {
+  int32 pid = getpid();
+  if (g_main_thread_pid == pid) {
+    return false;
+  }
+  g_main_thread_pid = pid;
+  return true;
+}
+
 pid_t GetTID() {
-  // On Linux and FreeBSD, we try to use gettid().
-#if defined OS_LINUX || defined OS_FREEBSD || defined OS_MACOSX
+  // On Linux and MacOSX, we try to use gettid().
+#if defined OS_LINUX || defined OS_MACOSX
 #ifndef __NR_gettid
 #ifdef OS_MACOSX
 #define __NR_gettid SYS_gettid
@@ -242,16 +261,16 @@ pid_t GetTID() {
     // the value change to "true".
     lacks_gettid = true;
   }
-#endif  // OS_LINUX || OS_FREEBSD
+#endif  // OS_LINUX || OS_MACOSX
 
   // If gettid() could not be used, we use one of the following.
 #if defined OS_LINUX
   return getpid();  // Linux:  getpid returns thread ID when gettid is absent
-#elif defined OS_WINDOWS || defined OS_CYGWIN
+#elif defined OS_WINDOWS && !defined OS_CYGWIN
   return GetCurrentThreadId();
 #else
   // If none of the techniques above worked, we use pthread_self().
-  return (pid_t)pthread_self();
+  return (pid_t)(uintptr_t)pthread_self();
 #endif
 }
 
@@ -299,9 +318,7 @@ void SetCrashReason(const CrashReason* r) {
                             r);
 }
 
-}  // namespace glog_internal_namespace_
-
-void InitGoogleLogging(const char* argv0) {
+void InitGoogleLoggingUtilities(const char* argv0) {
   CHECK(!IsGoogleLoggingInitialized())
       << "You called InitGoogleLogging() twice!";
   const char* slash = strrchr(argv0, '/');
@@ -316,6 +333,17 @@ void InitGoogleLogging(const char* argv0) {
 #endif
 }
 
+void ShutdownGoogleLoggingUtilities() {
+  CHECK(IsGoogleLoggingInitialized())
+      << "You called ShutdownGoogleLogging() without calling InitGoogleLogging() first!";
+  g_program_invocation_short_name = NULL;
+#ifdef HAVE_SYSLOG_H
+  closelog();
+#endif
+}
+
+}  // namespace glog_internal_namespace_
+
 _END_GOOGLE_NAMESPACE_
 
 // Make an implementation of stacktrace compiled.