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