logging.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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. #ifndef GOOGLE_PROTOBUF_STUBS_LOGGING_H_
  31. #define GOOGLE_PROTOBUF_STUBS_LOGGING_H_
  32. #include <google/protobuf/stubs/macros.h>
  33. #include <google/protobuf/stubs/port.h>
  34. #include <google/protobuf/stubs/status.h>
  35. #include <google/protobuf/stubs/stringpiece.h>
  36. #include <google/protobuf/port_def.inc>
  37. // ===================================================================
  38. // emulates google3/base/logging.h
  39. namespace google {
  40. namespace protobuf {
  41. enum LogLevel {
  42. LOGLEVEL_INFO, // Informational. This is never actually used by
  43. // libprotobuf.
  44. LOGLEVEL_WARNING, // Warns about issues that, although not technically a
  45. // problem now, could cause problems in the future. For
  46. // example, a // warning will be printed when parsing a
  47. // message that is near the message size limit.
  48. LOGLEVEL_ERROR, // An error occurred which should never happen during
  49. // normal use.
  50. LOGLEVEL_FATAL, // An error occurred from which the library cannot
  51. // recover. This usually indicates a programming error
  52. // in the code which calls the library, especially when
  53. // compiled in debug mode.
  54. #ifdef NDEBUG
  55. LOGLEVEL_DFATAL = LOGLEVEL_ERROR
  56. #else
  57. LOGLEVEL_DFATAL = LOGLEVEL_FATAL
  58. #endif
  59. };
  60. class uint128;
  61. namespace internal {
  62. class LogFinisher;
  63. class PROTOBUF_EXPORT LogMessage {
  64. public:
  65. LogMessage(LogLevel level, const char* filename, int line);
  66. ~LogMessage();
  67. LogMessage& operator<<(const std::string& value);
  68. LogMessage& operator<<(const char* value);
  69. LogMessage& operator<<(char value);
  70. LogMessage& operator<<(int value);
  71. LogMessage& operator<<(uint value);
  72. LogMessage& operator<<(long value);
  73. LogMessage& operator<<(unsigned long value);
  74. LogMessage& operator<<(long long value);
  75. LogMessage& operator<<(unsigned long long value);
  76. LogMessage& operator<<(double value);
  77. LogMessage& operator<<(void* value);
  78. LogMessage& operator<<(const StringPiece& value);
  79. LogMessage& operator<<(const util::Status& status);
  80. LogMessage& operator<<(const uint128& value);
  81. private:
  82. friend class LogFinisher;
  83. void Finish();
  84. LogLevel level_;
  85. const char* filename_;
  86. int line_;
  87. std::string message_;
  88. };
  89. // Used to make the entire "LOG(BLAH) << etc." expression have a void return
  90. // type and print a newline after each message.
  91. class PROTOBUF_EXPORT LogFinisher {
  92. public:
  93. void operator=(LogMessage& other);
  94. };
  95. template<typename T>
  96. bool IsOk(T status) { return status.ok(); }
  97. template<>
  98. inline bool IsOk(bool status) { return status; }
  99. } // namespace internal
  100. // Undef everything in case we're being mixed with some other Google library
  101. // which already defined them itself. Presumably all Google libraries will
  102. // support the same syntax for these so it should not be a big deal if they
  103. // end up using our definitions instead.
  104. #undef GOOGLE_LOG
  105. #undef GOOGLE_LOG_IF
  106. #undef GOOGLE_CHECK
  107. #undef GOOGLE_CHECK_OK
  108. #undef GOOGLE_CHECK_EQ
  109. #undef GOOGLE_CHECK_NE
  110. #undef GOOGLE_CHECK_LT
  111. #undef GOOGLE_CHECK_LE
  112. #undef GOOGLE_CHECK_GT
  113. #undef GOOGLE_CHECK_GE
  114. #undef GOOGLE_CHECK_NOTNULL
  115. #undef GOOGLE_DLOG
  116. #undef GOOGLE_DCHECK
  117. #undef GOOGLE_DCHECK_OK
  118. #undef GOOGLE_DCHECK_EQ
  119. #undef GOOGLE_DCHECK_NE
  120. #undef GOOGLE_DCHECK_LT
  121. #undef GOOGLE_DCHECK_LE
  122. #undef GOOGLE_DCHECK_GT
  123. #undef GOOGLE_DCHECK_GE
  124. #define GOOGLE_LOG(LEVEL) \
  125. ::google::protobuf::internal::LogFinisher() = \
  126. ::google::protobuf::internal::LogMessage( \
  127. ::google::protobuf::LOGLEVEL_##LEVEL, __FILE__, __LINE__)
  128. #define GOOGLE_LOG_IF(LEVEL, CONDITION) \
  129. !(CONDITION) ? (void)0 : GOOGLE_LOG(LEVEL)
  130. #define GOOGLE_CHECK(EXPRESSION) \
  131. GOOGLE_LOG_IF(FATAL, !(EXPRESSION)) << "CHECK failed: " #EXPRESSION ": "
  132. #define GOOGLE_CHECK_OK(A) GOOGLE_CHECK(::google::protobuf::internal::IsOk(A))
  133. #define GOOGLE_CHECK_EQ(A, B) GOOGLE_CHECK((A) == (B))
  134. #define GOOGLE_CHECK_NE(A, B) GOOGLE_CHECK((A) != (B))
  135. #define GOOGLE_CHECK_LT(A, B) GOOGLE_CHECK((A) < (B))
  136. #define GOOGLE_CHECK_LE(A, B) GOOGLE_CHECK((A) <= (B))
  137. #define GOOGLE_CHECK_GT(A, B) GOOGLE_CHECK((A) > (B))
  138. #define GOOGLE_CHECK_GE(A, B) GOOGLE_CHECK((A) >= (B))
  139. namespace internal {
  140. template<typename T>
  141. T* CheckNotNull(const char* /* file */, int /* line */,
  142. const char* name, T* val) {
  143. if (val == nullptr) {
  144. GOOGLE_LOG(FATAL) << name;
  145. }
  146. return val;
  147. }
  148. } // namespace internal
  149. #define GOOGLE_CHECK_NOTNULL(A) \
  150. ::google::protobuf::internal::CheckNotNull( \
  151. __FILE__, __LINE__, "'" #A "' must not be nullptr", (A))
  152. #ifdef NDEBUG
  153. #define GOOGLE_DLOG(LEVEL) GOOGLE_LOG_IF(LEVEL, false)
  154. #define GOOGLE_DCHECK(EXPRESSION) while(false) GOOGLE_CHECK(EXPRESSION)
  155. #define GOOGLE_DCHECK_OK(E) GOOGLE_DCHECK(::google::protobuf::internal::IsOk(E))
  156. #define GOOGLE_DCHECK_EQ(A, B) GOOGLE_DCHECK((A) == (B))
  157. #define GOOGLE_DCHECK_NE(A, B) GOOGLE_DCHECK((A) != (B))
  158. #define GOOGLE_DCHECK_LT(A, B) GOOGLE_DCHECK((A) < (B))
  159. #define GOOGLE_DCHECK_LE(A, B) GOOGLE_DCHECK((A) <= (B))
  160. #define GOOGLE_DCHECK_GT(A, B) GOOGLE_DCHECK((A) > (B))
  161. #define GOOGLE_DCHECK_GE(A, B) GOOGLE_DCHECK((A) >= (B))
  162. #else // NDEBUG
  163. #define GOOGLE_DLOG GOOGLE_LOG
  164. #define GOOGLE_DCHECK GOOGLE_CHECK
  165. #define GOOGLE_DCHECK_OK GOOGLE_CHECK_OK
  166. #define GOOGLE_DCHECK_EQ GOOGLE_CHECK_EQ
  167. #define GOOGLE_DCHECK_NE GOOGLE_CHECK_NE
  168. #define GOOGLE_DCHECK_LT GOOGLE_CHECK_LT
  169. #define GOOGLE_DCHECK_LE GOOGLE_CHECK_LE
  170. #define GOOGLE_DCHECK_GT GOOGLE_CHECK_GT
  171. #define GOOGLE_DCHECK_GE GOOGLE_CHECK_GE
  172. #endif // !NDEBUG
  173. typedef void LogHandler(LogLevel level, const char* filename, int line,
  174. const std::string& message);
  175. // The protobuf library sometimes writes warning and error messages to
  176. // stderr. These messages are primarily useful for developers, but may
  177. // also help end users figure out a problem. If you would prefer that
  178. // these messages be sent somewhere other than stderr, call SetLogHandler()
  179. // to set your own handler. This returns the old handler. Set the handler
  180. // to nullptr to ignore log messages (but see also LogSilencer, below).
  181. //
  182. // Obviously, SetLogHandler is not thread-safe. You should only call it
  183. // at initialization time, and probably not from library code. If you
  184. // simply want to suppress log messages temporarily (e.g. because you
  185. // have some code that tends to trigger them frequently and you know
  186. // the warnings are not important to you), use the LogSilencer class
  187. // below.
  188. PROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func);
  189. // Create a LogSilencer if you want to temporarily suppress all log
  190. // messages. As long as any LogSilencer objects exist, non-fatal
  191. // log messages will be discarded (the current LogHandler will *not*
  192. // be called). Constructing a LogSilencer is thread-safe. You may
  193. // accidentally suppress log messages occurring in another thread, but
  194. // since messages are generally for debugging purposes only, this isn't
  195. // a big deal. If you want to intercept log messages, use SetLogHandler().
  196. class PROTOBUF_EXPORT LogSilencer {
  197. public:
  198. LogSilencer();
  199. ~LogSilencer();
  200. };
  201. } // namespace protobuf
  202. } // namespace google
  203. #include <google/protobuf/port_undef.inc>
  204. #endif // GOOGLE_PROTOBUF_STUBS_LOGGING_H_