logging.h 8.7 KB

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