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