logging.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // This file contains #include information about logging-related stuff.
  32. // Pretty much everybody needs to #include this file so that they can
  33. // log various happenings.
  34. //
  35. #ifndef _LOGGING_H_
  36. #define _LOGGING_H_
  37. #include <config.h>
  38. #include <stdarg.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h> // for write()
  43. #endif
  44. #include <string.h> // for strlen(), strcmp()
  45. #include <assert.h>
  46. #include <errno.h> // for errno
  47. #include "base/commandlineflags.h"
  48. // On some systems (like freebsd), we can't call write() at all in a
  49. // global constructor, perhaps because errno hasn't been set up.
  50. // (In windows, we can't call it because it might call malloc.)
  51. // Calling the write syscall is safer (it doesn't set errno), so we
  52. // prefer that. Note we don't care about errno for logging: we just
  53. // do logging on a best-effort basis.
  54. #if defined(_MSC_VER)
  55. #define WRITE_TO_STDERR(buf, len) WriteToStderr(buf, len); // in port.cc
  56. #elif defined(HAVE_SYS_SYSCALL_H)
  57. #include <sys/syscall.h>
  58. #define WRITE_TO_STDERR(buf, len) syscall(SYS_write, STDERR_FILENO, buf, len)
  59. #else
  60. #define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len)
  61. #endif
  62. // MSVC and mingw define their own, safe version of vnsprintf (the
  63. // windows one in broken) in port.cc. Everyone else can use the
  64. // version here. We had to give it a unique name for windows.
  65. #ifndef _WIN32
  66. # define perftools_vsnprintf vsnprintf
  67. #endif
  68. // We log all messages at this log-level and below.
  69. // INFO == -1, WARNING == -2, ERROR == -3, FATAL == -4
  70. DECLARE_int32(verbose);
  71. // CHECK dies with a fatal error if condition is not true. It is *not*
  72. // controlled by NDEBUG, so the check will be executed regardless of
  73. // compilation mode. Therefore, it is safe to do things like:
  74. // CHECK(fp->Write(x) == 4)
  75. // Note we use write instead of printf/puts to avoid the risk we'll
  76. // call malloc().
  77. #define CHECK(condition) \
  78. do { \
  79. if (!(condition)) { \
  80. WRITE_TO_STDERR("Check failed: " #condition "\n", \
  81. sizeof("Check failed: " #condition "\n")-1); \
  82. abort(); \
  83. } \
  84. } while (0)
  85. // This takes a message to print. The name is historical.
  86. #define RAW_CHECK(condition, message) \
  87. do { \
  88. if (!(condition)) { \
  89. WRITE_TO_STDERR("Check failed: " #condition ": " message "\n", \
  90. sizeof("Check failed: " #condition ": " message "\n")-1);\
  91. abort(); \
  92. } \
  93. } while (0)
  94. // This is like RAW_CHECK, but only in debug-mode
  95. #ifdef NDEBUG
  96. enum { DEBUG_MODE = 0 };
  97. #define RAW_DCHECK(condition, message)
  98. #else
  99. enum { DEBUG_MODE = 1 };
  100. #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message)
  101. #endif
  102. // This prints errno as well. Note we use write instead of printf/puts to
  103. // avoid the risk we'll call malloc().
  104. #define PCHECK(condition) \
  105. do { \
  106. if (!(condition)) { \
  107. const int err_no = errno; \
  108. WRITE_TO_STDERR("Check failed: " #condition ": ", \
  109. sizeof("Check failed: " #condition ": ")-1); \
  110. WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no))); \
  111. WRITE_TO_STDERR("\n", sizeof("\n")-1); \
  112. abort(); \
  113. } \
  114. } while (0)
  115. // Helper macro for binary operators; prints the two values on error
  116. // Don't use this macro directly in your code, use CHECK_EQ et al below
  117. // WARNING: These don't compile correctly if one of the arguments is a pointer
  118. // and the other is NULL. To work around this, simply static_cast NULL to the
  119. // type of the desired pointer.
  120. // TODO(jandrews): Also print the values in case of failure. Requires some
  121. // sort of type-sensitive ToString() function.
  122. #define CHECK_OP(op, val1, val2) \
  123. do { \
  124. if (!((val1) op (val2))) { \
  125. fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \
  126. abort(); \
  127. } \
  128. } while (0)
  129. #define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2)
  130. #define CHECK_NE(val1, val2) CHECK_OP(!=, val1, val2)
  131. #define CHECK_LE(val1, val2) CHECK_OP(<=, val1, val2)
  132. #define CHECK_LT(val1, val2) CHECK_OP(< , val1, val2)
  133. #define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2)
  134. #define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2)
  135. // Synonyms for CHECK_* that are used in some unittests.
  136. #define EXPECT_EQ(val1, val2) CHECK_EQ(val1, val2)
  137. #define EXPECT_NE(val1, val2) CHECK_NE(val1, val2)
  138. #define EXPECT_LE(val1, val2) CHECK_LE(val1, val2)
  139. #define EXPECT_LT(val1, val2) CHECK_LT(val1, val2)
  140. #define EXPECT_GE(val1, val2) CHECK_GE(val1, val2)
  141. #define EXPECT_GT(val1, val2) CHECK_GT(val1, val2)
  142. #define ASSERT_EQ(val1, val2) EXPECT_EQ(val1, val2)
  143. #define ASSERT_NE(val1, val2) EXPECT_NE(val1, val2)
  144. #define ASSERT_LE(val1, val2) EXPECT_LE(val1, val2)
  145. #define ASSERT_LT(val1, val2) EXPECT_LT(val1, val2)
  146. #define ASSERT_GE(val1, val2) EXPECT_GE(val1, val2)
  147. #define ASSERT_GT(val1, val2) EXPECT_GT(val1, val2)
  148. // As are these variants.
  149. #define EXPECT_TRUE(cond) CHECK(cond)
  150. #define EXPECT_FALSE(cond) CHECK(!(cond))
  151. #define EXPECT_STREQ(a, b) CHECK(strcmp(a, b) == 0)
  152. #define ASSERT_TRUE(cond) EXPECT_TRUE(cond)
  153. #define ASSERT_FALSE(cond) EXPECT_FALSE(cond)
  154. #define ASSERT_STREQ(a, b) EXPECT_STREQ(a, b)
  155. // Used for (libc) functions that return -1 and set errno
  156. #define CHECK_ERR(invocation) PCHECK((invocation) != -1)
  157. // A few more checks that only happen in debug mode
  158. #ifdef NDEBUG
  159. #define DCHECK_EQ(val1, val2)
  160. #define DCHECK_NE(val1, val2)
  161. #define DCHECK_LE(val1, val2)
  162. #define DCHECK_LT(val1, val2)
  163. #define DCHECK_GE(val1, val2)
  164. #define DCHECK_GT(val1, val2)
  165. #else
  166. #define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
  167. #define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
  168. #define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
  169. #define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
  170. #define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
  171. #define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
  172. #endif
  173. #ifdef ERROR
  174. #undef ERROR // may conflict with ERROR macro on windows
  175. #endif
  176. enum LogSeverity {INFO = -1, WARNING = -2, ERROR = -3, FATAL = -4};
  177. // NOTE: we add a newline to the end of the output if it's not there already
  178. inline void LogPrintf(int severity, const char* pat, va_list ap) {
  179. #ifndef TCMALLOC_SGX
  180. // We write directly to the stderr file descriptor and avoid FILE
  181. // buffering because that may invoke malloc()
  182. char buf[600];
  183. perftools_vsnprintf(buf, sizeof(buf)-1, pat, ap);
  184. if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') {
  185. assert(strlen(buf)+1 < sizeof(buf));
  186. strcat(buf, "\n");
  187. }
  188. WRITE_TO_STDERR(buf, strlen(buf));
  189. #endif
  190. if ((severity) == FATAL)
  191. abort(); // LOG(FATAL) indicates a big problem, so don't run atexit() calls
  192. }
  193. // Note that since the order of global constructors is unspecified,
  194. // global code that calls RAW_LOG may execute before FLAGS_verbose is set.
  195. // Such code will run with verbosity == 0 no matter what.
  196. #define VLOG_IS_ON(severity) (FLAGS_verbose >= severity)
  197. // In a better world, we'd use __VA_ARGS__, but VC++ 7 doesn't support it.
  198. #define LOG_PRINTF(severity, pat) do { \
  199. if (VLOG_IS_ON(severity)) { \
  200. va_list ap; \
  201. va_start(ap, pat); \
  202. LogPrintf(severity, pat, ap); \
  203. va_end(ap); \
  204. } \
  205. } while (0)
  206. // RAW_LOG is the main function; some synonyms are used in unittests.
  207. inline void RAW_LOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
  208. inline void RAW_VLOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
  209. inline void LOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
  210. inline void VLOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
  211. inline void LOG_IF(int lvl, bool cond, const char* pat, ...) {
  212. if (cond) LOG_PRINTF(lvl, pat);
  213. }
  214. // This isn't technically logging, but it's also IO and also is an
  215. // attempt to be "raw" -- that is, to not use any higher-level libc
  216. // routines that might allocate memory or (ideally) try to allocate
  217. // locks. We use an opaque file handle (not necessarily an int)
  218. // to allow even more low-level stuff in the future.
  219. // Like other "raw" routines, these functions are best effort, and
  220. // thus don't return error codes (except RawOpenForWriting()).
  221. #if defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
  222. #ifndef NOMINMAX
  223. #define NOMINMAX // @#!$& windows
  224. #endif
  225. #include <windows.h>
  226. typedef HANDLE RawFD;
  227. const RawFD kIllegalRawFD = INVALID_HANDLE_VALUE;
  228. #else
  229. typedef int RawFD;
  230. const RawFD kIllegalRawFD = -1; // what open returns if it fails
  231. #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
  232. RawFD RawOpenForWriting(const char* filename); // uses default permissions
  233. void RawWrite(RawFD fd, const char* buf, size_t len);
  234. void RawClose(RawFD fd);
  235. #endif // _LOGGING_H_