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 // Author: kenton@google.com (Kenton Varda)
32
33 #include <google/protobuf/stubs/common.h>
34
35 #include <atomic>
36 #include <errno.h>
37 #include <sstream>
38 #include <stdio.h>
39 #include <vector>
40
41 #ifdef _WIN32
42 #ifndef WIN32_LEAN_AND_MEAN
43 #define WIN32_LEAN_AND_MEAN // We only need minimal includes
44 #endif
45 #include <windows.h>
46 #define snprintf _snprintf // see comment in strutil.cc
47 #elif defined(HAVE_PTHREAD)
48 #include <pthread.h>
49 #else
50 #error "No suitable threading library available."
51 #endif
52 #if defined(__ANDROID__)
53 #include <hilog/log.h>
54 #endif
55
56 #include <google/protobuf/stubs/callback.h>
57 #include <google/protobuf/stubs/logging.h>
58 #include <google/protobuf/stubs/once.h>
59 #include <google/protobuf/stubs/status.h>
60 #include <google/protobuf/stubs/stringpiece.h>
61 #include <google/protobuf/stubs/strutil.h>
62 #include <google/protobuf/stubs/int128.h>
63
64 #include <google/protobuf/port_def.inc>
65
66 namespace google {
67 namespace protobuf {
68
69 namespace internal {
70
VerifyVersion(int headerVersion,int minLibraryVersion,const char * filename)71 void VerifyVersion(int headerVersion,
72 int minLibraryVersion,
73 const char* filename) {
74 if (GOOGLE_PROTOBUF_VERSION < minLibraryVersion) {
75 // Library is too old for headers.
76 GOOGLE_LOG(FATAL)
77 << "This program requires version " << VersionString(minLibraryVersion)
78 << " of the Protocol Buffer runtime library, but the installed version "
79 "is " << VersionString(GOOGLE_PROTOBUF_VERSION) << ". Please update "
80 "your library. If you compiled the program yourself, make sure that "
81 "your headers are from the same version of Protocol Buffers as your "
82 "link-time library. (Version verification failed in \""
83 << filename << "\".)";
84 }
85 if (headerVersion < kMinHeaderVersionForLibrary) {
86 // Headers are too old for library.
87 GOOGLE_LOG(FATAL)
88 << "This program was compiled against version "
89 << VersionString(headerVersion) << " of the Protocol Buffer runtime "
90 "library, which is not compatible with the installed version ("
91 << VersionString(GOOGLE_PROTOBUF_VERSION) << "). Contact the program "
92 "author for an update. If you compiled the program yourself, make "
93 "sure that your headers are from the same version of Protocol Buffers "
94 "as your link-time library. (Version verification failed in \""
95 << filename << "\".)";
96 }
97 }
98
VersionString(int version)99 string VersionString(int version) {
100 int major = version / 1000000;
101 int minor = (version / 1000) % 1000;
102 int micro = version % 1000;
103
104 // 128 bytes should always be enough, but we use snprintf() anyway to be
105 // safe.
106 char buffer[128];
107 snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro);
108
109 // Guard against broken MSVC snprintf().
110 buffer[sizeof(buffer)-1] = '\0';
111
112 return buffer;
113 }
114
115 } // namespace internal
116
117 // ===================================================================
118 // emulates google3/base/logging.cc
119
120 // If the minimum logging level is not set, we default to logging messages for
121 // all levels.
122 #ifndef GOOGLE_PROTOBUF_MIN_LOG_LEVEL
123 #define GOOGLE_PROTOBUF_MIN_LOG_LEVEL LOGLEVEL_INFO
124 #endif
125
126 namespace internal {
127
128
DefaultLogHandler(LogLevel level,const char * filename,int line,const string & message)129 void DefaultLogHandler(LogLevel level, const char* filename, int line,
130 const string& message) {
131 if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) {
132 return;
133 }
134 static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" };
135
136 // We use fprintf() instead of cerr because we want this to work at static
137 // initialization time.
138 fprintf(stderr, "[libprotobuf %s %s:%d] %s\n",
139 level_names[level], filename, line, message.c_str());
140 fflush(stderr); // Needed on MSVC.
141 }
142
143
NullLogHandler(LogLevel,const char *,int,const string &)144 void NullLogHandler(LogLevel /* level */, const char* /* filename */,
145 int /* line */, const string& /* message */) {
146 // Nothing.
147 }
148
149 static LogHandler* log_handler_ = &DefaultLogHandler;
150 static std::atomic<int> log_silencer_count_ = ATOMIC_VAR_INIT(0);
151
operator <<(const string & value)152 LogMessage& LogMessage::operator<<(const string& value) {
153 message_ += value;
154 return *this;
155 }
156
operator <<(const char * value)157 LogMessage& LogMessage::operator<<(const char* value) {
158 message_ += value;
159 return *this;
160 }
161
operator <<(const StringPiece & value)162 LogMessage& LogMessage::operator<<(const StringPiece& value) {
163 message_ += value.ToString();
164 return *this;
165 }
166
operator <<(const util::Status & status)167 LogMessage& LogMessage::operator<<(const util::Status& status) {
168 message_ += status.ToString();
169 return *this;
170 }
171
operator <<(const uint128 & value)172 LogMessage& LogMessage::operator<<(const uint128& value) {
173 std::ostringstream str;
174 str << value;
175 message_ += str.str();
176 return *this;
177 }
178
179 // Since this is just for logging, we don't care if the current locale changes
180 // the results -- in fact, we probably prefer that. So we use snprintf()
181 // instead of Simple*toa().
182 #undef DECLARE_STREAM_OPERATOR
183 #define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \
184 LogMessage& LogMessage::operator<<(TYPE value) { \
185 /* 128 bytes should be big enough for any of the primitive */ \
186 /* values which we print with this, but well use snprintf() */ \
187 /* anyway to be extra safe. */ \
188 char buffer[128]; \
189 snprintf(buffer, sizeof(buffer), FORMAT, value); \
190 /* Guard against broken MSVC snprintf(). */ \
191 buffer[sizeof(buffer)-1] = '\0'; \
192 message_ += buffer; \
193 return *this; \
194 }
195
196 DECLARE_STREAM_OPERATOR(char , "%c" )
197 DECLARE_STREAM_OPERATOR(int , "%d" )
198 DECLARE_STREAM_OPERATOR(unsigned int , "%u" )
199 DECLARE_STREAM_OPERATOR(long , "%ld")
200 DECLARE_STREAM_OPERATOR(unsigned long, "%lu")
201 DECLARE_STREAM_OPERATOR(double , "%g" )
202 DECLARE_STREAM_OPERATOR(void* , "%p" )
203 DECLARE_STREAM_OPERATOR(long long , "%" PROTOBUF_LL_FORMAT "d")
204 DECLARE_STREAM_OPERATOR(unsigned long long, "%" PROTOBUF_LL_FORMAT "u")
205 #undef DECLARE_STREAM_OPERATOR
206
LogMessage(LogLevel level,const char * filename,int line)207 LogMessage::LogMessage(LogLevel level, const char* filename, int line)
208 : level_(level), filename_(filename), line_(line) {}
~LogMessage()209 LogMessage::~LogMessage() {}
210
Finish()211 void LogMessage::Finish() {
212 bool suppress = false;
213
214 if (level_ != LOGLEVEL_FATAL) {
215 suppress = log_silencer_count_ > 0;
216 }
217
218 if (!suppress) {
219 log_handler_(level_, filename_, line_, message_);
220 }
221
222 if (level_ == LOGLEVEL_FATAL) {
223 #if PROTOBUF_USE_EXCEPTIONS
224 throw FatalException(filename_, line_, message_);
225 #else
226 abort();
227 #endif
228 }
229 }
230
operator =(LogMessage & other)231 void LogFinisher::operator=(LogMessage& other) {
232 other.Finish();
233 }
234
235 } // namespace internal
236
SetLogHandler(LogHandler * new_func)237 LogHandler* SetLogHandler(LogHandler* new_func) {
238 LogHandler* old = internal::log_handler_;
239 if (old == &internal::NullLogHandler) {
240 old = nullptr;
241 }
242 if (new_func == nullptr) {
243 internal::log_handler_ = &internal::NullLogHandler;
244 } else {
245 internal::log_handler_ = new_func;
246 }
247 return old;
248 }
249
LogSilencer()250 LogSilencer::LogSilencer() {
251 ++internal::log_silencer_count_;
252 };
253
~LogSilencer()254 LogSilencer::~LogSilencer() {
255 --internal::log_silencer_count_;
256 };
257
258 // ===================================================================
259 // emulates google3/base/callback.cc
260
~Closure()261 Closure::~Closure() {}
262
~FunctionClosure0()263 namespace internal { FunctionClosure0::~FunctionClosure0() {} }
264
DoNothing()265 void DoNothing() {}
266
267 // ===================================================================
268 // emulates google3/util/endian/endian.h
269 //
270 // TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in
271 // google/protobuf/io/coded_stream.h and therefore can not be used here.
272 // Maybe move that macro definition here in the furture.
ghtonl(uint32 x)273 uint32 ghtonl(uint32 x) {
274 union {
275 uint32 result;
276 uint8 result_array[4];
277 };
278 result_array[0] = static_cast<uint8>(x >> 24);
279 result_array[1] = static_cast<uint8>((x >> 16) & 0xFF);
280 result_array[2] = static_cast<uint8>((x >> 8) & 0xFF);
281 result_array[3] = static_cast<uint8>(x & 0xFF);
282 return result;
283 }
284
285 #if PROTOBUF_USE_EXCEPTIONS
~FatalException()286 FatalException::~FatalException() throw() {}
287
what() const288 const char* FatalException::what() const throw() {
289 return message_.c_str();
290 }
291 #endif
292
293 } // namespace protobuf
294 } // namespace google
295