common.cc
Go to the documentation of this file.
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 
34 
35 #include <atomic>
36 #include <errno.h>
37 #include <sstream>
38 #include <stdio.h>
39 #include <vector>
40 
41 #ifdef _WIN32
42 #define WIN32_LEAN_AND_MEAN // We only need minimal includes
43 #include <windows.h>
44 #define snprintf _snprintf // see comment in strutil.cc
45 #elif defined(HAVE_PTHREAD)
46 #include <pthread.h>
47 #else
48 #error "No suitable threading library available."
49 #endif
50 #if defined(__ANDROID__)
51 #include <android/log.h>
52 #endif
53 
62 
63 #include <google/protobuf/port_def.inc>
64 
65 namespace google {
66 namespace protobuf {
67 
68 namespace internal {
69 
70 void VerifyVersion(int headerVersion,
71  int minLibraryVersion,
72  const char* filename) {
73  if (GOOGLE_PROTOBUF_VERSION < minLibraryVersion) {
74  // Library is too old for headers.
76  << "This program requires version " << VersionString(minLibraryVersion)
77  << " of the Protocol Buffer runtime library, but the installed version "
78  "is " << VersionString(GOOGLE_PROTOBUF_VERSION) << ". Please update "
79  "your library. If you compiled the program yourself, make sure that "
80  "your headers are from the same version of Protocol Buffers as your "
81  "link-time library. (Version verification failed in \""
82  << filename << "\".)";
83  }
84  if (headerVersion < kMinHeaderVersionForLibrary) {
85  // Headers are too old for library.
87  << "This program was compiled against version "
88  << VersionString(headerVersion) << " of the Protocol Buffer runtime "
89  "library, which is not compatible with the installed version ("
90  << VersionString(GOOGLE_PROTOBUF_VERSION) << "). Contact the program "
91  "author for an update. If you compiled the program yourself, make "
92  "sure that your headers are from the same version of Protocol Buffers "
93  "as your link-time library. (Version verification failed in \""
94  << filename << "\".)";
95  }
96 }
97 
98 string VersionString(int version) {
99  int major = version / 1000000;
100  int minor = (version / 1000) % 1000;
101  int micro = version % 1000;
102 
103  // 128 bytes should always be enough, but we use snprintf() anyway to be
104  // safe.
105  char buffer[128];
106  snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro);
107 
108  // Guard against broken MSVC snprintf().
109  buffer[sizeof(buffer)-1] = '\0';
110 
111  return buffer;
112 }
113 
114 } // namespace internal
115 
116 // ===================================================================
117 // emulates google3/base/logging.cc
118 
119 // If the minimum logging level is not set, we default to logging messages for
120 // all levels.
121 #ifndef GOOGLE_PROTOBUF_MIN_LOG_LEVEL
122 #define GOOGLE_PROTOBUF_MIN_LOG_LEVEL LOGLEVEL_INFO
123 #endif
124 
125 namespace internal {
126 
127 #if defined(__ANDROID__)
128 inline void DefaultLogHandler(LogLevel level, const char* filename, int line,
129  const string& message) {
131  return;
132  }
133  static const char* level_names[] = {"INFO", "WARNING", "ERROR", "FATAL"};
134 
135  static const int android_log_levels[] = {
136  ANDROID_LOG_INFO, // LOG(INFO),
137  ANDROID_LOG_WARN, // LOG(WARNING)
138  ANDROID_LOG_ERROR, // LOG(ERROR)
139  ANDROID_LOG_FATAL, // LOG(FATAL)
140  };
141 
142  // Bound the logging level.
143  const int android_log_level = android_log_levels[level];
144  ::std::ostringstream ostr;
145  ostr << "[libprotobuf " << level_names[level] << " " << filename << ":"
146  << line << "] " << message.c_str();
147 
148  // Output the log string the Android log at the appropriate level.
149  __android_log_write(android_log_level, "libprotobuf-native",
150  ostr.str().c_str());
151  // Also output to std::cerr.
152  fprintf(stderr, "%s", ostr.str().c_str());
153  fflush(stderr);
154 
155  // Indicate termination if needed.
156  if (android_log_level == ANDROID_LOG_FATAL) {
157  __android_log_write(ANDROID_LOG_FATAL, "libprotobuf-native",
158  "terminating.\n");
159  }
160 }
161 
162 #else
163 void DefaultLogHandler(LogLevel level, const char* filename, int line,
164  const string& message) {
166  return;
167  }
168  static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" };
169 
170  // We use fprintf() instead of cerr because we want this to work at static
171  // initialization time.
172  fprintf(stderr, "[libprotobuf %s %s:%d] %s\n",
173  level_names[level], filename, line, message.c_str());
174  fflush(stderr); // Needed on MSVC.
175 }
176 #endif
177 
178 void NullLogHandler(LogLevel /* level */, const char* /* filename */,
179  int /* line */, const string& /* message */) {
180  // Nothing.
181 }
182 
184 static std::atomic<int> log_silencer_count_ = ATOMIC_VAR_INIT(0);
185 
186 LogMessage& LogMessage::operator<<(const string& value) {
187  message_ += value;
188  return *this;
189 }
190 
192  message_ += value;
193  return *this;
194 }
195 
197  message_ += value.ToString();
198  return *this;
199 }
200 
202  message_ += status.ToString();
203  return *this;
204 }
205 
207  std::ostringstream str;
208  str << value;
209  message_ += str.str();
210  return *this;
211 }
212 
213 // Since this is just for logging, we don't care if the current locale changes
214 // the results -- in fact, we probably prefer that. So we use snprintf()
215 // instead of Simple*toa().
216 #undef DECLARE_STREAM_OPERATOR
217 #define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \
218  LogMessage& LogMessage::operator<<(TYPE value) { \
219  /* 128 bytes should be big enough for any of the primitive */ \
220  /* values which we print with this, but well use snprintf() */ \
221  /* anyway to be extra safe. */ \
222  char buffer[128]; \
223  snprintf(buffer, sizeof(buffer), FORMAT, value); \
224  /* Guard against broken MSVC snprintf(). */ \
225  buffer[sizeof(buffer)-1] = '\0'; \
226  message_ += buffer; \
227  return *this; \
228  }
229 
230 DECLARE_STREAM_OPERATOR(char , "%c" )
231 DECLARE_STREAM_OPERATOR(int , "%d" )
232 DECLARE_STREAM_OPERATOR(unsigned int , "%u" )
233 DECLARE_STREAM_OPERATOR(long , "%ld")
234 DECLARE_STREAM_OPERATOR(unsigned long, "%lu")
235 DECLARE_STREAM_OPERATOR(double , "%g" )
236 DECLARE_STREAM_OPERATOR(void* , "%p" )
237 DECLARE_STREAM_OPERATOR(long long , "%" PROTOBUF_LL_FORMAT "d")
238 DECLARE_STREAM_OPERATOR(unsigned long long, "%" PROTOBUF_LL_FORMAT "u")
239 #undef DECLARE_STREAM_OPERATOR
240 
241 LogMessage::LogMessage(LogLevel level, const char* filename, int line)
242  : level_(level), filename_(filename), line_(line) {}
244 
246  bool suppress = false;
247 
248  if (level_ != LOGLEVEL_FATAL) {
249  suppress = log_silencer_count_ > 0;
250  }
251 
252  if (!suppress) {
254  }
255 
256  if (level_ == LOGLEVEL_FATAL) {
257 #if PROTOBUF_USE_EXCEPTIONS
258  throw FatalException(filename_, line_, message_);
259 #else
260  abort();
261 #endif
262  }
263 }
264 
266  other.Finish();
267 }
268 
269 } // namespace internal
270 
273  if (old == &internal::NullLogHandler) {
274  old = nullptr;
275  }
276  if (new_func == nullptr) {
278  } else {
279  internal::log_handler_ = new_func;
280  }
281  return old;
282 }
283 
286 };
287 
290 };
291 
292 // ===================================================================
293 // emulates google3/base/callback.cc
294 
296 
298 
299 void DoNothing() {}
300 
301 // ===================================================================
302 // emulates google3/util/endian/endian.h
303 //
304 // TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in
305 // google/protobuf/io/coded_stream.h and therefore can not be used here.
306 // Maybe move that macro definition here in the furture.
308  union {
309  uint32 result;
310  uint8 result_array[4];
311  };
312  result_array[0] = static_cast<uint8>(x >> 24);
313  result_array[1] = static_cast<uint8>((x >> 16) & 0xFF);
314  result_array[2] = static_cast<uint8>((x >> 8) & 0xFF);
315  result_array[3] = static_cast<uint8>(x & 0xFF);
316  return result;
317 }
318 
319 // ===================================================================
320 // Shutdown support.
321 
322 namespace internal {
323 
324 struct ShutdownData {
326  std::reverse(functions.begin(), functions.end());
327  for (auto pair : functions) pair.first(pair.second);
328  }
329 
330  static ShutdownData* get() {
331  static auto* data = new ShutdownData;
332  return data;
333  }
334 
335  std::vector<std::pair<void (*)(const void*), const void*>> functions;
337 };
338 
339 static void RunZeroArgFunc(const void* arg) {
340  void (*func)() = reinterpret_cast<void (*)()>(const_cast<void*>(arg));
341  func();
342 }
343 
344 void OnShutdown(void (*func)()) {
345  OnShutdownRun(RunZeroArgFunc, reinterpret_cast<void*>(func));
346 }
347 
348 void OnShutdownRun(void (*f)(const void*), const void* arg) {
349  auto shutdown_data = ShutdownData::get();
350  MutexLock lock(&shutdown_data->mutex);
351  shutdown_data->functions.push_back(std::make_pair(f, arg));
352 }
353 
354 } // namespace internal
355 
357  // This function should be called only once, but accepts multiple calls.
358  static bool is_shutdown = false;
359  if (!is_shutdown) {
361  is_shutdown = true;
362  }
363 }
364 
365 #if PROTOBUF_USE_EXCEPTIONS
366 FatalException::~FatalException() throw() {}
367 
368 const char* FatalException::what() const throw() {
369  return message_.c_str();
370 }
371 #endif
372 
373 } // namespace protobuf
374 } // namespace google
google::protobuf.internal::ShutdownData::~ShutdownData
~ShutdownData()
Definition: common.cc:325
google::protobuf.internal::ShutdownData::get
static ShutdownData * get()
Definition: common.cc:330
google::protobuf.internal::FunctionClosure0::~FunctionClosure0
~FunctionClosure0()
Definition: common.cc:297
google::protobuf.internal::RunZeroArgFunc
static void RunZeroArgFunc(const void *arg)
Definition: common.cc:339
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
google::protobuf.internal::LogFinisher::operator=
void operator=(LogMessage &other)
Definition: common.cc:265
google::protobuf::LOGLEVEL_FATAL
@ LOGLEVEL_FATAL
Definition: logging.h:54
g
GLboolean GLboolean g
Definition: glcorearb.h:3228
google::protobuf.internal::LogMessage::message_
std::string message_
Definition: logging.h:102
google::protobuf.internal::LogMessage::operator<<
LogMessage & operator<<(const std::string &value)
LogMessage
google::protobuf::uint8
uint8_t uint8
Definition: protobuf/src/google/protobuf/stubs/port.h:153
FATAL
const int FATAL
Definition: log_severity.h:60
google::protobuf.internal::VerifyVersion
void VerifyVersion(int headerVersion, int minLibraryVersion, const char *filename)
Definition: common.cc:70
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::LogLevel
LogLevel
Definition: logging.h:45
google::protobuf.internal::NullLogHandler
void NullLogHandler(LogLevel, const char *, int, const string &)
Definition: common.cc:178
x
GLint GLenum GLint x
Definition: glcorearb.h:2834
google::protobuf.internal::LogMessage::~LogMessage
~LogMessage()
Definition: common.cc:243
callback.h
google::protobuf.internal::OnShutdown
void OnShutdown(void(*func)())
Definition: common.cc:344
stringpiece.h
google::protobuf::ghtonl
uint32 ghtonl(uint32 x)
Definition: common.cc:307
DECLARE_STREAM_OPERATOR
#define DECLARE_STREAM_OPERATOR(TYPE, FORMAT)
Definition: common.cc:217
google::protobuf.internal::ShutdownData::mutex
Mutex mutex
Definition: common.cc:336
google::protobuf.internal::OnShutdownRun
void OnShutdownRun(void(*f)(const void *), const void *arg)
Definition: common.cc:348
snprintf
int snprintf(char *str, size_t size, const char *format,...)
Definition: port.cc:64
GOOGLE_PROTOBUF_MIN_LOG_LEVEL
#define GOOGLE_PROTOBUF_MIN_LOG_LEVEL
Definition: common.cc:122
google::protobuf.internal::kMinHeaderVersionForLibrary
static const int kMinHeaderVersionForLibrary
Definition: common.h:92
strutil.h
line_
int line_
Definition: objectivec_helpers.cc:1468
buffer
GLuint buffer
Definition: glcorearb.h:2939
google::protobuf::StringPiece
Definition: stringpiece.h:180
int128.h
update_failure_list.str
str
Definition: update_failure_list.py:41
p
const char * p
Definition: gmock-matchers_test.cc:3863
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf.internal::LogMessage::Finish
void Finish()
Definition: common.cc:245
major
int major
Definition: gl3w.c:93
google::protobuf.internal::LogMessage::line_
int line_
Definition: logging.h:101
buffer
Definition: buffer_processor.h:43
google::protobuf::LogSilencer::~LogSilencer
~LogSilencer()
Definition: common.cc:288
google::protobuf.internal::MutexLock
Definition: protobuf/src/google/protobuf/stubs/mutex.h:116
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
minor
int minor
Definition: gl3w.c:93
google::protobuf::DoNothing
void PROTOBUF_EXPORT DoNothing()
Definition: common.cc:299
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: protobuf/src/google/protobuf/stubs/mutex.h:113
google::protobuf::uint128
Definition: int128.h:53
google::protobuf.internal::ShutdownData::functions
std::vector< std::pair< void(*)(const void *), const void * > > functions
Definition: common.cc:335
google::protobuf.internal::VersionString
string VersionString(int version)
Definition: common.cc:98
google::protobuf::LogSilencer::LogSilencer
LogSilencer()
Definition: common.cc:284
common.h
google::protobuf::SetLogHandler
LogHandler * SetLogHandler(LogHandler *new_func)
Definition: common.cc:271
func
GLenum func
Definition: glcorearb.h:3052
once.h
logging.h
google::protobuf.internal::ShutdownData
Definition: common.cc:324
google::protobuf::util::Status
Definition: status.h:67
google::protobuf::Closure::~Closure
virtual ~Closure()
Definition: common.cc:295
mutex.h
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
internal
Definition: any.pb.h:40
google::protobuf.internal::LogMessage::LogMessage
LogMessage(LogLevel level, const char *filename, int line)
Definition: common.cc:241
GOOGLE_PROTOBUF_VERSION
#define GOOGLE_PROTOBUF_VERSION
Definition: common.h:84
f
GLfloat f
Definition: glcorearb.h:3964
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf.internal::LogMessage::filename_
const char * filename_
Definition: logging.h:100
google::protobuf::util::Status::ToString
string ToString() const
Definition: status.cc:114
google::protobuf.internal::log_silencer_count_
static std::atomic< int > log_silencer_count_
Definition: common.cc:184
version
static struct @0 version
google::protobuf::ShutdownProtobufLibrary
void ShutdownProtobufLibrary()
Definition: common.cc:356
google::protobuf::LogHandler
void LogHandler(LogLevel level, const char *filename, int line, const std::string &message)
Definition: logging.h:205
level
GLint level
Definition: glcorearb.h:2773
google::protobuf.internal::log_handler_
static LogHandler * log_handler_
Definition: common.cc:183
status.h
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf.internal::LogMessage::level_
LogLevel level_
Definition: logging.h:99
google::protobuf.internal::DefaultLogHandler
void DefaultLogHandler(LogLevel level, const char *filename, int line, const string &message)
Definition: common.cc:163


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48