Introduce mock-log.h for unittests.
[platform/upstream/glog.git] / src / utilities.h
1 // Copyright 2008 Google Inc. All Rights Reserved.
2 // Author: hamaji@google.com (Shinichiro Hamaji)
3 //
4 // Define utilties for glog internal usage.
5
6 #ifndef UTILITIES_H__
7 #define UTILITIES_H__
8
9 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
10 # define OS_WINDOWS
11 #elif defined(linux) || defined(__linux) || defined(__linux__)
12 # define OS_LINUX
13 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
14 # define OS_MACOSX
15 #elif defined(__FreeBSD__)
16 # define OS_FREEBSD
17 #else
18 // TODO(hamaji): Add other platforms.
19 #endif
20
21 // printf macros for size_t, in the style of inttypes.h
22 #ifdef _LP64
23 #define __PRIS_PREFIX "z"
24 #else
25 #define __PRIS_PREFIX
26 #endif
27
28 // Use these macros after a % in a printf format string
29 // to get correct 32/64 bit behavior, like this:
30 // size_t size = records.size();
31 // printf("%"PRIuS"\n", size);
32
33 #define PRIdS __PRIS_PREFIX "d"
34 #define PRIxS __PRIS_PREFIX "x"
35 #define PRIuS __PRIS_PREFIX "u"
36 #define PRIXS __PRIS_PREFIX "X"
37 #define PRIoS __PRIS_PREFIX "o"
38
39 #include "base/mutex.h"  // This must go first so we get _XOPEN_SOURCE
40
41 #include <string>
42
43 #ifdef OS_WINDOWS
44 # include "port.h"
45 #endif
46
47 #include "config.h"
48 #include "glog/logging.h"
49
50 // There are three different ways we can try to get the stack trace:
51 //
52 // 1) The libunwind library.  This is still in development, and as a
53 //    separate library adds a new dependency, but doesn't need a frame
54 //    pointer.  It also doesn't call malloc.
55 //
56 // 2) Our hand-coded stack-unwinder.  This depends on a certain stack
57 //    layout, which is used by gcc (and those systems using a
58 //    gcc-compatible ABI) on x86 systems, at least since gcc 2.95.
59 //    It uses the frame pointer to do its work.
60 //
61 // 3) The gdb unwinder -- also the one used by the c++ exception code.
62 //    It's obviously well-tested, but has a fatal flaw: it can call
63 //    malloc() from the unwinder.  This is a problem because we're
64 //    trying to use the unwinder to instrument malloc().
65 //
66 // Note: if you add a new implementation here, make sure it works
67 // correctly when GetStackTrace() is called with max_depth == 0.
68 // Some code may do that.
69
70 #if defined(HAVE_LIB_UNWIND)
71 # define STACKTRACE_H "stacktrace_libunwind-inl.h"
72 #elif !defined(NO_FRAME_POINTER)
73 # if defined(__i386__) && __GNUC__ >= 2
74 #  define STACKTRACE_H "stacktrace_x86-inl.h"
75 # elif defined(__x86_64__) && __GNUC__ >= 2
76 #  define STACKTRACE_H "stacktrace_x86_64-inl.h"
77 # elif ((__ppc__) || defined(__PPC__)) && __GNUC__ >= 2
78 #  define STACKTRACE_H "stacktrace_powerpc-inl.h"
79 # endif
80 #endif
81
82 #if !defined(STACKTRACE_H) && defined(HAVE_EXECINFO_H)
83 # define STACKTRACE_H "stacktrace_generic-inl.h"
84 #endif
85
86 #if defined(STACKTRACE_H)
87 # define HAVE_STACKTRACE
88 #endif
89
90 // defined by gcc
91 #if defined(__ELF__) && defined(OS_LINUX)
92 # define HAVE_SYMBOLIZE
93 #elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
94 // Use dladdr to symbolize.
95 # define HAVE_SYMBOLIZE
96 #endif
97
98 // There is a better way, but this is good enough in this file.
99 #define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
100
101 _START_GOOGLE_NAMESPACE_
102
103 namespace glog_internal_namespace_ {
104
105 #ifdef HAVE___ATTRIBUTE__
106 # define ATTRIBUTE_NOINLINE __attribute__ ((noinline))
107 # define HAVE_ATTRIBUTE_NOINLINE
108 #else
109 # define ATTRIBUTE_NOINLINE
110 #endif
111
112 const char* ProgramInvocationShortName();
113
114 bool IsGoogleLoggingInitialized();
115
116 bool is_default_thread();
117
118 int64 CycleClock_Now();
119
120 int64 UsecToCycles(int64 usec);
121
122 int32 GetMainThreadPid();
123
124 const std::string& MyUserName();
125
126 // Get the part of filepath after the last path separator.
127 // (Doesn't modify filepath, contrary to basename() in libgen.h.)
128 const char* const_basename(const char* filepath);
129
130 // Wrapper of __sync_val_compare_and_swap. If the GCC extension isn't
131 // defined, we try the CPU specific logics (we only support x86 and
132 // x86_64 for now) first, then use a naive implementation, which has a
133 // race condition.
134 template<typename T>
135 inline T sync_val_compare_and_swap(T* ptr, T oldval, T newval) {
136 #if defined(HAVE___SYNC_VAL_COMPARE_AND_SWAP)
137   return __sync_val_compare_and_swap(ptr, oldval, newval);
138 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
139   T ret;
140   __asm__ __volatile__("lock; cmpxchg %1, (%2);"
141                        :"=a"(ret)
142                         // GCC may produces %sil or %dil for
143                         // constraint "r", but some of apple's gas
144                         // dosn't know the 8 bit registers.
145                         // We use "q" to avoid these registers.
146                        :"q"(newval), "q"(ptr), "a"(oldval)
147                        :"memory", "cc");
148   return ret;
149 #else
150   T ret = *ptr;
151   if (ret == oldval) {
152     *ptr = newval;
153   }
154   return ret;
155 #endif
156 }
157
158 }  // namespace glog_internal_namespace_
159
160 _END_GOOGLE_NAMESPACE_
161
162 using namespace GOOGLE_NAMESPACE::glog_internal_namespace_;
163
164 #endif  // UTILITIES_H__