Line data Source code
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 : // ===================================================================
38 : // emulates google3/base/logging.h
39 :
40 : namespace google {
41 : namespace protobuf {
42 :
43 : enum LogLevel {
44 : LOGLEVEL_INFO, // Informational. This is never actually used by
45 : // libprotobuf.
46 : LOGLEVEL_WARNING, // Warns about issues that, although not technically a
47 : // problem now, could cause problems in the future. For
48 : // example, a // warning will be printed when parsing a
49 : // message that is near the message size limit.
50 : LOGLEVEL_ERROR, // An error occurred which should never happen during
51 : // normal use.
52 : LOGLEVEL_FATAL, // An error occurred from which the library cannot
53 : // recover. This usually indicates a programming error
54 : // in the code which calls the library, especially when
55 : // compiled in debug mode.
56 :
57 : #ifdef NDEBUG
58 : LOGLEVEL_DFATAL = LOGLEVEL_ERROR
59 : #else
60 : LOGLEVEL_DFATAL = LOGLEVEL_FATAL
61 : #endif
62 : };
63 :
64 : class StringPiece;
65 : namespace util {
66 : class Status;
67 : }
68 : class uint128;
69 : namespace internal {
70 :
71 : class LogFinisher;
72 :
73 : class LIBPROTOBUF_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 ::google::protobuf::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.
105 : class LIBPROTOBUF_EXPORT LogFinisher {
106 : public:
107 : void operator=(LogMessage& other);
108 : };
109 :
110 : template<typename T>
111 : bool IsOk(T status) { return status.ok(); }
112 : template<>
113 : inline 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 :
161 : namespace internal {
162 : template<typename T>
163 0 : T* CheckNotNull(const char* /* file */, int /* line */,
164 : const char* name, T* val) {
165 0 : if (val == NULL) {
166 0 : GOOGLE_LOG(FATAL) << name;
167 : }
168 0 : return val;
169 : }
170 : } // namespace internal
171 : #define GOOGLE_CHECK_NOTNULL(A) \
172 : ::google::protobuf::internal::CheckNotNull(\
173 : __FILE__, __LINE__, "'" #A "' must not be NULL", (A))
174 :
175 : #ifdef NDEBUG
176 :
177 : #define GOOGLE_DLOG GOOGLE_LOG_IF(INFO, 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 :
203 : typedef 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 NULL 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.
219 : LIBPROTOBUF_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().
228 : class LIBPROTOBUF_EXPORT LogSilencer {
229 : public:
230 : LogSilencer();
231 : ~LogSilencer();
232 : };
233 :
234 : } // namespace protobuf
235 : } // namespace google
236 :
237 : #endif // GOOGLE_PROTOBUF_STUBS_LOGGING_H_
|