internal_logging.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // Author: Sanjay Ghemawat <opensource@google.com>
  32. //
  33. // Internal logging and related utility routines.
  34. #ifndef TCMALLOC_INTERNAL_LOGGING_H_
  35. #define TCMALLOC_INTERNAL_LOGGING_H_
  36. #include <config.h>
  37. #include <stddef.h> // for size_t
  38. #if defined HAVE_STDINT_H
  39. #include <stdint.h>
  40. #elif defined HAVE_INTTYPES_H
  41. #include <inttypes.h>
  42. #else
  43. #include <sys/types.h>
  44. #endif
  45. //-------------------------------------------------------------------
  46. // Utility routines
  47. //-------------------------------------------------------------------
  48. // Safe logging helper: we write directly to the stderr file
  49. // descriptor and avoid FILE buffering because that may invoke
  50. // malloc().
  51. //
  52. // Example:
  53. // Log(kLog, __FILE__, __LINE__, "error", bytes);
  54. namespace tcmalloc {
  55. enum LogMode {
  56. kLog, // Just print the message
  57. kCrash, // Print the message and crash
  58. kCrashWithStats // Print the message, some stats, and crash
  59. };
  60. class Logger;
  61. // A LogItem holds any of the argument types that can be passed to Log()
  62. class LogItem {
  63. public:
  64. LogItem() : tag_(kEnd) { }
  65. LogItem(const char* v) : tag_(kStr) { u_.str = v; }
  66. LogItem(int v) : tag_(kSigned) { u_.snum = v; }
  67. LogItem(long v) : tag_(kSigned) { u_.snum = v; }
  68. LogItem(long long v) : tag_(kSigned) { u_.snum = v; }
  69. LogItem(unsigned int v) : tag_(kUnsigned) { u_.unum = v; }
  70. LogItem(unsigned long v) : tag_(kUnsigned) { u_.unum = v; }
  71. LogItem(unsigned long long v) : tag_(kUnsigned) { u_.unum = v; }
  72. LogItem(const void* v) : tag_(kPtr) { u_.ptr = v; }
  73. private:
  74. friend class Logger;
  75. enum Tag {
  76. kStr,
  77. kSigned,
  78. kUnsigned,
  79. kPtr,
  80. kEnd
  81. };
  82. Tag tag_;
  83. union {
  84. const char* str;
  85. const void* ptr;
  86. int64_t snum;
  87. uint64_t unum;
  88. } u_;
  89. };
  90. extern PERFTOOLS_DLL_DECL void Log(LogMode mode, const char* filename, int line,
  91. LogItem a, LogItem b = LogItem(),
  92. LogItem c = LogItem(), LogItem d = LogItem());
  93. // Tests can override this function to collect logging messages.
  94. extern PERFTOOLS_DLL_DECL void (*log_message_writer)(const char* msg, int length);
  95. } // end tcmalloc namespace
  96. // Like assert(), but executed even in NDEBUG mode
  97. #undef CHECK_CONDITION
  98. #define CHECK_CONDITION(cond) \
  99. do { \
  100. if (!(cond)) { \
  101. ::tcmalloc::Log(::tcmalloc::kCrash, __FILE__, __LINE__, #cond); \
  102. } \
  103. } while (0)
  104. #ifndef TCMALLOC_SGX
  105. // Our own version of assert() so we can avoid hanging by trying to do
  106. // all kinds of goofy printing while holding the malloc lock.
  107. #ifndef NDEBUG
  108. #define ASSERT(cond) CHECK_CONDITION(cond)
  109. #else
  110. #define ASSERT(cond) ((void) 0)
  111. #endif
  112. #else
  113. /* In SGX will always enable ASSERT */
  114. #define ASSERT(cond) CHECK_CONDITION(cond)
  115. #endif
  116. // Print into buffer
  117. class TCMalloc_Printer {
  118. private:
  119. char* buf_; // Where should we write next
  120. int left_; // Space left in buffer (including space for \0)
  121. public:
  122. // REQUIRES: "length > 0"
  123. TCMalloc_Printer(char* buf, int length) : buf_(buf), left_(length) {
  124. buf[0] = '\0';
  125. }
  126. void printf(const char* format, ...)
  127. #ifdef HAVE___ATTRIBUTE__
  128. __attribute__ ((__format__ (__printf__, 2, 3)))
  129. #endif
  130. ;
  131. };
  132. #endif // TCMALLOC_INTERNAL_LOGGING_H_