诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

375 lines
12 KiB

  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 <atomic>
  33. #include <errno.h>
  34. #include <sstream>
  35. #include <stdio.h>
  36. #include <vector>
  37. #ifdef _WIN32
  38. #define WIN32_LEAN_AND_MEAN // We only need minimal includes
  39. #include <windows.h>
  40. #define snprintf _snprintf // see comment in strutil.cc
  41. #elif defined(HAVE_PTHREAD)
  42. #include <pthread.h>
  43. #else
  44. #error "No suitable threading library available."
  45. #endif
  46. #if defined(__ANDROID__)
  47. #include <android/log.h>
  48. #endif
  49. #include <google/protobuf/stubs/callback.h>
  50. #include <google/protobuf/stubs/logging.h>
  51. #include <google/protobuf/stubs/mutex.h>
  52. #include <google/protobuf/stubs/once.h>
  53. #include <google/protobuf/stubs/status.h>
  54. #include <google/protobuf/stubs/stringpiece.h>
  55. #include <google/protobuf/stubs/strutil.h>
  56. #include <google/protobuf/stubs/int128.h>
  57. #include <google/protobuf/port_def.inc>
  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. // If the minimum logging level is not set, we default to logging messages for
  104. // all levels.
  105. #ifndef GOOGLE_PROTOBUF_MIN_LOG_LEVEL
  106. #define GOOGLE_PROTOBUF_MIN_LOG_LEVEL LOGLEVEL_INFO
  107. #endif
  108. namespace internal {
  109. #if defined(__ANDROID__)
  110. inline void DefaultLogHandler(LogLevel level, const char* filename, int line,
  111. const string& message) {
  112. if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) {
  113. return;
  114. }
  115. static const char* level_names[] = {"INFO", "WARNING", "ERROR", "FATAL"};
  116. static const int android_log_levels[] = {
  117. ANDROID_LOG_INFO, // LOG(INFO),
  118. ANDROID_LOG_WARN, // LOG(WARNING)
  119. ANDROID_LOG_ERROR, // LOG(ERROR)
  120. ANDROID_LOG_FATAL, // LOG(FATAL)
  121. };
  122. // Bound the logging level.
  123. const int android_log_level = android_log_levels[level];
  124. ::std::ostringstream ostr;
  125. ostr << "[libprotobuf " << level_names[level] << " " << filename << ":"
  126. << line << "] " << message.c_str();
  127. // Output the log string the Android log at the appropriate level.
  128. __android_log_write(android_log_level, "libprotobuf-native",
  129. ostr.str().c_str());
  130. // Also output to std::cerr.
  131. fprintf(stderr, "%s", ostr.str().c_str());
  132. fflush(stderr);
  133. // Indicate termination if needed.
  134. if (android_log_level == ANDROID_LOG_FATAL) {
  135. __android_log_write(ANDROID_LOG_FATAL, "libprotobuf-native",
  136. "terminating.\n");
  137. }
  138. }
  139. #else
  140. void DefaultLogHandler(LogLevel level, const char* filename, int line,
  141. const string& message) {
  142. if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) {
  143. return;
  144. }
  145. static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" };
  146. // We use fprintf() instead of cerr because we want this to work at static
  147. // initialization time.
  148. fprintf(stderr, "[libprotobuf %s %s:%d] %s\n",
  149. level_names[level], filename, line, message.c_str());
  150. fflush(stderr); // Needed on MSVC.
  151. }
  152. #endif
  153. void NullLogHandler(LogLevel /* level */, const char* /* filename */,
  154. int /* line */, const string& /* message */) {
  155. // Nothing.
  156. }
  157. static LogHandler* log_handler_ = &DefaultLogHandler;
  158. static std::atomic<int> log_silencer_count_ = ATOMIC_VAR_INIT(0);
  159. LogMessage& LogMessage::operator<<(const string& value) {
  160. message_ += value;
  161. return *this;
  162. }
  163. LogMessage& LogMessage::operator<<(const char* value) {
  164. message_ += value;
  165. return *this;
  166. }
  167. LogMessage& LogMessage::operator<<(const StringPiece& value) {
  168. message_ += value.ToString();
  169. return *this;
  170. }
  171. LogMessage& LogMessage::operator<<(const util::Status& status) {
  172. message_ += status.ToString();
  173. return *this;
  174. }
  175. LogMessage& LogMessage::operator<<(const uint128& value) {
  176. std::ostringstream str;
  177. str << value;
  178. message_ += str.str();
  179. return *this;
  180. }
  181. // Since this is just for logging, we don't care if the current locale changes
  182. // the results -- in fact, we probably prefer that. So we use snprintf()
  183. // instead of Simple*toa().
  184. #undef DECLARE_STREAM_OPERATOR
  185. #define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \
  186. LogMessage& LogMessage::operator<<(TYPE value) { \
  187. /* 128 bytes should be big enough for any of the primitive */ \
  188. /* values which we print with this, but well use snprintf() */ \
  189. /* anyway to be extra safe. */ \
  190. char buffer[128]; \
  191. snprintf(buffer, sizeof(buffer), FORMAT, value); \
  192. /* Guard against broken MSVC snprintf(). */ \
  193. buffer[sizeof(buffer)-1] = '\0'; \
  194. message_ += buffer; \
  195. return *this; \
  196. }
  197. DECLARE_STREAM_OPERATOR(char , "%c" )
  198. DECLARE_STREAM_OPERATOR(int , "%d" )
  199. DECLARE_STREAM_OPERATOR(unsigned int , "%u" )
  200. DECLARE_STREAM_OPERATOR(long , "%ld")
  201. DECLARE_STREAM_OPERATOR(unsigned long, "%lu")
  202. DECLARE_STREAM_OPERATOR(double , "%g" )
  203. DECLARE_STREAM_OPERATOR(void* , "%p" )
  204. DECLARE_STREAM_OPERATOR(long long , "%" PROTOBUF_LL_FORMAT "d")
  205. DECLARE_STREAM_OPERATOR(unsigned long long, "%" PROTOBUF_LL_FORMAT "u")
  206. #undef DECLARE_STREAM_OPERATOR
  207. LogMessage::LogMessage(LogLevel level, const char* filename, int line)
  208. : level_(level), filename_(filename), line_(line) {}
  209. LogMessage::~LogMessage() {}
  210. void LogMessage::Finish() {
  211. bool suppress = false;
  212. if (level_ != LOGLEVEL_FATAL) {
  213. suppress = log_silencer_count_ > 0;
  214. }
  215. if (!suppress) {
  216. log_handler_(level_, filename_, line_, message_);
  217. }
  218. if (level_ == LOGLEVEL_FATAL) {
  219. #if PROTOBUF_USE_EXCEPTIONS
  220. throw FatalException(filename_, line_, message_);
  221. #else
  222. abort();
  223. #endif
  224. }
  225. }
  226. void LogFinisher::operator=(LogMessage& other) {
  227. other.Finish();
  228. }
  229. } // namespace internal
  230. LogHandler* SetLogHandler(LogHandler* new_func) {
  231. LogHandler* old = internal::log_handler_;
  232. if (old == &internal::NullLogHandler) {
  233. old = nullptr;
  234. }
  235. if (new_func == nullptr) {
  236. internal::log_handler_ = &internal::NullLogHandler;
  237. } else {
  238. internal::log_handler_ = new_func;
  239. }
  240. return old;
  241. }
  242. LogSilencer::LogSilencer() {
  243. ++internal::log_silencer_count_;
  244. };
  245. LogSilencer::~LogSilencer() {
  246. --internal::log_silencer_count_;
  247. };
  248. // ===================================================================
  249. // emulates google3/base/callback.cc
  250. Closure::~Closure() {}
  251. namespace internal { FunctionClosure0::~FunctionClosure0() {} }
  252. void DoNothing() {}
  253. // ===================================================================
  254. // emulates google3/util/endian/endian.h
  255. //
  256. // TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in
  257. // google/protobuf/io/coded_stream.h and therefore can not be used here.
  258. // Maybe move that macro definition here in the furture.
  259. uint32 ghtonl(uint32 x) {
  260. union {
  261. uint32 result;
  262. uint8 result_array[4];
  263. };
  264. result_array[0] = static_cast<uint8>(x >> 24);
  265. result_array[1] = static_cast<uint8>((x >> 16) & 0xFF);
  266. result_array[2] = static_cast<uint8>((x >> 8) & 0xFF);
  267. result_array[3] = static_cast<uint8>(x & 0xFF);
  268. return result;
  269. }
  270. // ===================================================================
  271. // Shutdown support.
  272. namespace internal {
  273. struct ShutdownData {
  274. ~ShutdownData() {
  275. std::reverse(functions.begin(), functions.end());
  276. for (auto pair : functions) pair.first(pair.second);
  277. }
  278. static ShutdownData* get() {
  279. static auto* data = new ShutdownData;
  280. return data;
  281. }
  282. std::vector<std::pair<void (*)(const void*), const void*>> functions;
  283. Mutex mutex;
  284. };
  285. static void RunZeroArgFunc(const void* arg) {
  286. void (*func)() = reinterpret_cast<void (*)()>(const_cast<void*>(arg));
  287. func();
  288. }
  289. void OnShutdown(void (*func)()) {
  290. OnShutdownRun(RunZeroArgFunc, reinterpret_cast<void*>(func));
  291. }
  292. void OnShutdownRun(void (*f)(const void*), const void* arg) {
  293. auto shutdown_data = ShutdownData::get();
  294. MutexLock lock(&shutdown_data->mutex);
  295. shutdown_data->functions.push_back(std::make_pair(f, arg));
  296. }
  297. } // namespace internal
  298. void ShutdownProtobufLibrary() {
  299. // This function should be called only once, but accepts multiple calls.
  300. static bool is_shutdown = false;
  301. if (!is_shutdown) {
  302. delete internal::ShutdownData::get();
  303. is_shutdown = true;
  304. }
  305. }
  306. #if PROTOBUF_USE_EXCEPTIONS
  307. FatalException::~FatalException() throw() {}
  308. const char* FatalException::what() const throw() {
  309. return message_.c_str();
  310. }
  311. #endif
  312. } // namespace protobuf
  313. } // namespace google