common.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. // Author: kenton@google.com (Kenton Varda)
  31. #include <google/protobuf/stubs/common.h>
  32. #include <google/protobuf/stubs/once.h>
  33. #include <google/protobuf/stubs/status.h>
  34. #include <google/protobuf/stubs/stringpiece.h>
  35. #include <google/protobuf/stubs/strutil.h>
  36. #include <google/protobuf/stubs/int128.h>
  37. #include <errno.h>
  38. #include <sstream>
  39. #include <stdio.h>
  40. #include <vector>
  41. #if TARGET_OS_IPHONE
  42. #include "ios_config.h"
  43. #else
  44. #include "config.h"
  45. #endif
  46. #ifdef _WIN32
  47. #define WIN32_LEAN_AND_MEAN // We only need minimal includes
  48. #include <windows.h>
  49. #define snprintf _snprintf // see comment in strutil.cc
  50. #elif defined(HAVE_PTHREAD)
  51. #include <pthread.h>
  52. #else
  53. #error "No suitable threading library available."
  54. #endif
  55. #if defined(__ANDROID__)
  56. #include <android/log.h>
  57. #endif
  58. namespace google {
  59. namespace protobuf {
  60. namespace internal {
  61. void VerifyVersion(int headerVersion,
  62. int minLibraryVersion,
  63. const char* filename) {
  64. if (GOOGLE_PROTOBUF_VERSION < minLibraryVersion) {
  65. // Library is too old for headers.
  66. GOOGLE_LOG(FATAL)
  67. << "This program requires version " << VersionString(minLibraryVersion)
  68. << " of the Protocol Buffer runtime library, but the installed version "
  69. "is " << VersionString(GOOGLE_PROTOBUF_VERSION) << ". Please update "
  70. "your library. If you compiled the program yourself, make sure that "
  71. "your headers are from the same version of Protocol Buffers as your "
  72. "link-time library. (Version verification failed in \""
  73. << filename << "\".)";
  74. }
  75. if (headerVersion < kMinHeaderVersionForLibrary) {
  76. // Headers are too old for library.
  77. GOOGLE_LOG(FATAL)
  78. << "This program was compiled against version "
  79. << VersionString(headerVersion) << " of the Protocol Buffer runtime "
  80. "library, which is not compatible with the installed version ("
  81. << VersionString(GOOGLE_PROTOBUF_VERSION) << "). Contact the program "
  82. "author for an update. If you compiled the program yourself, make "
  83. "sure that your headers are from the same version of Protocol Buffers "
  84. "as your link-time library. (Version verification failed in \""
  85. << filename << "\".)";
  86. }
  87. }
  88. string VersionString(int version) {
  89. int major = version / 1000000;
  90. int minor = (version / 1000) % 1000;
  91. int micro = version % 1000;
  92. // 128 bytes should always be enough, but we use snprintf() anyway to be
  93. // safe.
  94. char buffer[128];
  95. snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro);
  96. // Guard against broken MSVC snprintf().
  97. buffer[sizeof(buffer)-1] = '\0';
  98. return buffer;
  99. }
  100. } // namespace internal
  101. // ===================================================================
  102. // emulates google3/base/logging.cc
  103. namespace internal {
  104. #if defined(__ANDROID__)
  105. inline void DefaultLogHandler(LogLevel level, const char* filename, int line,
  106. const string& message) {
  107. #ifdef GOOGLE_PROTOBUF_MIN_LOG_LEVEL
  108. if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) {
  109. return;
  110. }
  111. static const char* level_names[] = {"INFO", "WARNING", "ERROR", "FATAL"};
  112. static const int android_log_levels[] = {
  113. ANDROID_LOG_INFO, // LOG(INFO),
  114. ANDROID_LOG_WARN, // LOG(WARNING)
  115. ANDROID_LOG_ERROR, // LOG(ERROR)
  116. ANDROID_LOG_FATAL, // LOG(FATAL)
  117. };
  118. // Bound the logging level.
  119. const int android_log_level = android_log_levels[level];
  120. ::std::ostringstream ostr;
  121. ostr << "[libprotobuf " << level_names[level] << " " << filename << ":"
  122. << line << "] " << message.c_str();
  123. // Output the log string the Android log at the appropriate level.
  124. __android_log_write(android_log_level, "libprotobuf-native",
  125. ostr.str().c_str());
  126. // Also output to std::cerr.
  127. fprintf(stderr, "%s", ostr.str().c_str());
  128. fflush(stderr);
  129. // Indicate termination if needed.
  130. if (android_log_level == ANDROID_LOG_FATAL) {
  131. __android_log_write(ANDROID_LOG_FATAL, "libprotobuf-native",
  132. "terminating.\n");
  133. }
  134. #endif
  135. }
  136. #else
  137. void DefaultLogHandler(LogLevel level, const char* filename, int line,
  138. const string& message) {
  139. static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" };
  140. // We use fprintf() instead of cerr because we want this to work at static
  141. // initialization time.
  142. fprintf(stderr, "[libprotobuf %s %s:%d] %s\n",
  143. level_names[level], filename, line, message.c_str());
  144. fflush(stderr); // Needed on MSVC.
  145. }
  146. #endif
  147. void NullLogHandler(LogLevel /* level */, const char* /* filename */,
  148. int /* line */, const string& /* message */) {
  149. // Nothing.
  150. }
  151. static LogHandler* log_handler_ = &DefaultLogHandler;
  152. static int log_silencer_count_ = 0;
  153. static Mutex* log_silencer_count_mutex_ = NULL;
  154. GOOGLE_PROTOBUF_DECLARE_ONCE(log_silencer_count_init_);
  155. void DeleteLogSilencerCount() {
  156. delete log_silencer_count_mutex_;
  157. log_silencer_count_mutex_ = NULL;
  158. }
  159. void InitLogSilencerCount() {
  160. log_silencer_count_mutex_ = new Mutex;
  161. OnShutdown(&DeleteLogSilencerCount);
  162. }
  163. void InitLogSilencerCountOnce() {
  164. GoogleOnceInit(&log_silencer_count_init_, &InitLogSilencerCount);
  165. }
  166. LogMessage& LogMessage::operator<<(const string& value) {
  167. message_ += value;
  168. return *this;
  169. }
  170. LogMessage& LogMessage::operator<<(const char* value) {
  171. message_ += value;
  172. return *this;
  173. }
  174. LogMessage& LogMessage::operator<<(const StringPiece& value) {
  175. message_ += value.ToString();
  176. return *this;
  177. }
  178. LogMessage& LogMessage::operator<<(
  179. const ::google::protobuf::util::Status& status) {
  180. message_ += status.ToString();
  181. return *this;
  182. }
  183. LogMessage& LogMessage::operator<<(const uint128& value) {
  184. std::ostringstream str;
  185. str << value;
  186. message_ += str.str();
  187. return *this;
  188. }
  189. // Since this is just for logging, we don't care if the current locale changes
  190. // the results -- in fact, we probably prefer that. So we use snprintf()
  191. // instead of Simple*toa().
  192. #undef DECLARE_STREAM_OPERATOR
  193. #define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \
  194. LogMessage& LogMessage::operator<<(TYPE value) { \
  195. /* 128 bytes should be big enough for any of the primitive */ \
  196. /* values which we print with this, but well use snprintf() */ \
  197. /* anyway to be extra safe. */ \
  198. char buffer[128]; \
  199. snprintf(buffer, sizeof(buffer), FORMAT, value); \
  200. /* Guard against broken MSVC snprintf(). */ \
  201. buffer[sizeof(buffer)-1] = '\0'; \
  202. message_ += buffer; \
  203. return *this; \
  204. }
  205. DECLARE_STREAM_OPERATOR(char , "%c" )
  206. DECLARE_STREAM_OPERATOR(int , "%d" )
  207. DECLARE_STREAM_OPERATOR(unsigned int , "%u" )
  208. DECLARE_STREAM_OPERATOR(long , "%ld")
  209. DECLARE_STREAM_OPERATOR(unsigned long, "%lu")
  210. DECLARE_STREAM_OPERATOR(double , "%g" )
  211. DECLARE_STREAM_OPERATOR(void* , "%p" )
  212. DECLARE_STREAM_OPERATOR(long long , "%" GOOGLE_LL_FORMAT "d")
  213. DECLARE_STREAM_OPERATOR(unsigned long long, "%" GOOGLE_LL_FORMAT "u")
  214. #undef DECLARE_STREAM_OPERATOR
  215. LogMessage::LogMessage(LogLevel level, const char* filename, int line)
  216. : level_(level), filename_(filename), line_(line) {}
  217. LogMessage::~LogMessage() {}
  218. void LogMessage::Finish() {
  219. bool suppress = false;
  220. if (level_ != LOGLEVEL_FATAL) {
  221. InitLogSilencerCountOnce();
  222. MutexLock lock(log_silencer_count_mutex_);
  223. suppress = log_silencer_count_ > 0;
  224. }
  225. if (!suppress) {
  226. log_handler_(level_, filename_, line_, message_);
  227. }
  228. if (level_ == LOGLEVEL_FATAL) {
  229. #if PROTOBUF_USE_EXCEPTIONS
  230. throw FatalException(filename_, line_, message_);
  231. #else
  232. abort();
  233. #endif
  234. }
  235. }
  236. void LogFinisher::operator=(LogMessage& other) {
  237. other.Finish();
  238. }
  239. } // namespace internal
  240. LogHandler* SetLogHandler(LogHandler* new_func) {
  241. LogHandler* old = internal::log_handler_;
  242. if (old == &internal::NullLogHandler) {
  243. old = NULL;
  244. }
  245. if (new_func == NULL) {
  246. internal::log_handler_ = &internal::NullLogHandler;
  247. } else {
  248. internal::log_handler_ = new_func;
  249. }
  250. return old;
  251. }
  252. LogSilencer::LogSilencer() {
  253. internal::InitLogSilencerCountOnce();
  254. MutexLock lock(internal::log_silencer_count_mutex_);
  255. ++internal::log_silencer_count_;
  256. };
  257. LogSilencer::~LogSilencer() {
  258. internal::InitLogSilencerCountOnce();
  259. MutexLock lock(internal::log_silencer_count_mutex_);
  260. --internal::log_silencer_count_;
  261. };
  262. // ===================================================================
  263. // emulates google3/base/callback.cc
  264. Closure::~Closure() {}
  265. namespace internal { FunctionClosure0::~FunctionClosure0() {} }
  266. void DoNothing() {}
  267. // ===================================================================
  268. // emulates google3/base/mutex.cc
  269. #ifdef _WIN32
  270. struct Mutex::Internal {
  271. CRITICAL_SECTION mutex;
  272. #ifndef NDEBUG
  273. // Used only to implement AssertHeld().
  274. DWORD thread_id;
  275. #endif
  276. };
  277. Mutex::Mutex()
  278. : mInternal(new Internal) {
  279. InitializeCriticalSection(&mInternal->mutex);
  280. }
  281. Mutex::~Mutex() {
  282. DeleteCriticalSection(&mInternal->mutex);
  283. delete mInternal;
  284. }
  285. void Mutex::Lock() {
  286. EnterCriticalSection(&mInternal->mutex);
  287. #ifndef NDEBUG
  288. mInternal->thread_id = GetCurrentThreadId();
  289. #endif
  290. }
  291. void Mutex::Unlock() {
  292. #ifndef NDEBUG
  293. mInternal->thread_id = 0;
  294. #endif
  295. LeaveCriticalSection(&mInternal->mutex);
  296. }
  297. void Mutex::AssertHeld() {
  298. #ifndef NDEBUG
  299. GOOGLE_DCHECK_EQ(mInternal->thread_id, GetCurrentThreadId());
  300. #endif
  301. }
  302. #elif defined(HAVE_PTHREAD)
  303. struct Mutex::Internal {
  304. pthread_mutex_t mutex;
  305. };
  306. Mutex::Mutex()
  307. : mInternal(new Internal) {
  308. pthread_mutex_init(&mInternal->mutex, NULL);
  309. }
  310. Mutex::~Mutex() {
  311. pthread_mutex_destroy(&mInternal->mutex);
  312. delete mInternal;
  313. }
  314. void Mutex::Lock() {
  315. int result = pthread_mutex_lock(&mInternal->mutex);
  316. if (result != 0) {
  317. GOOGLE_LOG(FATAL) << "pthread_mutex_lock: " << strerror(result);
  318. }
  319. }
  320. void Mutex::Unlock() {
  321. int result = pthread_mutex_unlock(&mInternal->mutex);
  322. if (result != 0) {
  323. GOOGLE_LOG(FATAL) << "pthread_mutex_unlock: " << strerror(result);
  324. }
  325. }
  326. void Mutex::AssertHeld() {
  327. // pthreads dosn't provide a way to check which thread holds the mutex.
  328. // TODO(kenton): Maybe keep track of locking thread ID like with WIN32?
  329. }
  330. #endif
  331. // ===================================================================
  332. // emulates google3/util/endian/endian.h
  333. //
  334. // TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in
  335. // google/protobuf/io/coded_stream.h and therefore can not be used here.
  336. // Maybe move that macro definition here in the furture.
  337. uint32 ghtonl(uint32 x) {
  338. union {
  339. uint32 result;
  340. uint8 result_array[4];
  341. };
  342. result_array[0] = static_cast<uint8>(x >> 24);
  343. result_array[1] = static_cast<uint8>((x >> 16) & 0xFF);
  344. result_array[2] = static_cast<uint8>((x >> 8) & 0xFF);
  345. result_array[3] = static_cast<uint8>(x & 0xFF);
  346. return result;
  347. }
  348. // ===================================================================
  349. // Shutdown support.
  350. namespace internal {
  351. typedef void OnShutdownFunc();
  352. vector<void (*)()>* shutdown_functions = NULL;
  353. Mutex* shutdown_functions_mutex = NULL;
  354. GOOGLE_PROTOBUF_DECLARE_ONCE(shutdown_functions_init);
  355. void InitShutdownFunctions() {
  356. shutdown_functions = new vector<void (*)()>;
  357. shutdown_functions_mutex = new Mutex;
  358. }
  359. inline void InitShutdownFunctionsOnce() {
  360. GoogleOnceInit(&shutdown_functions_init, &InitShutdownFunctions);
  361. }
  362. void OnShutdown(void (*func)()) {
  363. InitShutdownFunctionsOnce();
  364. MutexLock lock(shutdown_functions_mutex);
  365. shutdown_functions->push_back(func);
  366. }
  367. } // namespace internal
  368. void ShutdownProtobufLibrary() {
  369. internal::InitShutdownFunctionsOnce();
  370. // We don't need to lock shutdown_functions_mutex because it's up to the
  371. // caller to make sure that no one is using the library before this is
  372. // called.
  373. // Make it safe to call this multiple times.
  374. if (internal::shutdown_functions == NULL) return;
  375. for (int i = 0; i < internal::shutdown_functions->size(); i++) {
  376. internal::shutdown_functions->at(i)();
  377. }
  378. delete internal::shutdown_functions;
  379. internal::shutdown_functions = NULL;
  380. delete internal::shutdown_functions_mutex;
  381. internal::shutdown_functions_mutex = NULL;
  382. }
  383. #if PROTOBUF_USE_EXCEPTIONS
  384. FatalException::~FatalException() throw() {}
  385. const char* FatalException::what() const throw() {
  386. return message_.c_str();
  387. }
  388. #endif
  389. } // namespace protobuf
  390. } // namespace google