You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logging.h 10 kB

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