OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // emulates google3/base/logging.cc | 109 // emulates google3/base/logging.cc |
110 | 110 |
111 namespace internal { | 111 namespace internal { |
112 #if defined(__ANDROID__) | 112 #if defined(__ANDROID__) |
113 inline void DefaultLogHandler(LogLevel level, const char* filename, int line, | 113 inline void DefaultLogHandler(LogLevel level, const char* filename, int line, |
114 const string& message) { | 114 const string& message) { |
115 #ifdef GOOGLE_PROTOBUF_MIN_LOG_LEVEL | 115 #ifdef GOOGLE_PROTOBUF_MIN_LOG_LEVEL |
116 if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) { | 116 if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) { |
117 return; | 117 return; |
118 } | 118 } |
119 static const char* const level_names[] = {"INFO", "WARNING", "ERROR", | 119 static const char* level_names[] = {"INFO", "WARNING", "ERROR", "FATAL"}; |
120 "FATAL"}; | |
121 | 120 |
122 static const int android_log_levels[] = { | 121 static const int android_log_levels[] = { |
123 ANDROID_LOG_INFO, // LOG(INFO), | 122 ANDROID_LOG_INFO, // LOG(INFO), |
124 ANDROID_LOG_WARN, // LOG(WARNING) | 123 ANDROID_LOG_WARN, // LOG(WARNING) |
125 ANDROID_LOG_ERROR, // LOG(ERROR) | 124 ANDROID_LOG_ERROR, // LOG(ERROR) |
126 ANDROID_LOG_FATAL, // LOG(FATAL) | 125 ANDROID_LOG_FATAL, // LOG(FATAL) |
127 }; | 126 }; |
128 | 127 |
129 // Bound the logging level. | 128 // Bound the logging level. |
130 const int android_log_level = android_log_levels[level]; | 129 const int android_log_level = android_log_levels[level]; |
(...skipping 11 matching lines...) Expand all Loading... |
142 // Indicate termination if needed. | 141 // Indicate termination if needed. |
143 if (android_log_level == ANDROID_LOG_FATAL) { | 142 if (android_log_level == ANDROID_LOG_FATAL) { |
144 __android_log_write(ANDROID_LOG_FATAL, "libprotobuf-native", | 143 __android_log_write(ANDROID_LOG_FATAL, "libprotobuf-native", |
145 "terminating.\n"); | 144 "terminating.\n"); |
146 } | 145 } |
147 #endif | 146 #endif |
148 } | 147 } |
149 #else | 148 #else |
150 void DefaultLogHandler(LogLevel level, const char* filename, int line, | 149 void DefaultLogHandler(LogLevel level, const char* filename, int line, |
151 const string& message) { | 150 const string& message) { |
152 static const char* const level_names[] = {"INFO", "WARNING", "ERROR", | 151 static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" }; |
153 "FATAL"}; | |
154 | 152 |
155 // We use fprintf() instead of cerr because we want this to work at static | 153 // We use fprintf() instead of cerr because we want this to work at static |
156 // initialization time. | 154 // initialization time. |
157 fprintf(stderr, "[libprotobuf %s %s:%d] %s\n", | 155 fprintf(stderr, "[libprotobuf %s %s:%d] %s\n", |
158 level_names[level], filename, line, message.c_str()); | 156 level_names[level], filename, line, message.c_str()); |
159 fflush(stderr); // Needed on MSVC. | 157 fflush(stderr); // Needed on MSVC. |
160 } | 158 } |
161 #endif | 159 #endif |
162 | 160 |
163 void NullLogHandler(LogLevel /* level */, const char* /* filename */, | 161 void NullLogHandler(LogLevel /* level */, const char* /* filename */, |
164 int /* line */, const string& /* message */) { | 162 int /* line */, const string& /* message */) { |
165 // Nothing. | 163 // Nothing. |
166 } | 164 } |
167 | 165 |
168 extern LogHandler* cr_log_handler_; | 166 static LogHandler* log_handler_ = &DefaultLogHandler; |
169 extern int cr_log_silencer_count_; | 167 static int log_silencer_count_ = 0; |
170 | 168 |
171 extern Mutex* cr_log_silencer_count_mutex_; | 169 static Mutex* log_silencer_count_mutex_ = NULL; |
172 extern ProtobufOnceType cr_log_silencer_count_init_; | 170 GOOGLE_PROTOBUF_DECLARE_ONCE(log_silencer_count_init_); |
173 | 171 |
174 void DeleteLogSilencerCount() { | 172 void DeleteLogSilencerCount() { |
175 delete cr_log_silencer_count_mutex_; | 173 delete log_silencer_count_mutex_; |
176 cr_log_silencer_count_mutex_ = NULL; | 174 log_silencer_count_mutex_ = NULL; |
177 } | 175 } |
178 void InitLogSilencerCount() { | 176 void InitLogSilencerCount() { |
179 cr_log_silencer_count_mutex_ = new Mutex; | 177 log_silencer_count_mutex_ = new Mutex; |
180 OnShutdown(&DeleteLogSilencerCount); | 178 OnShutdown(&DeleteLogSilencerCount); |
181 } | 179 } |
182 void InitLogSilencerCountOnce() { | 180 void InitLogSilencerCountOnce() { |
183 GoogleOnceInit(&cr_log_silencer_count_init_, &InitLogSilencerCount); | 181 GoogleOnceInit(&log_silencer_count_init_, &InitLogSilencerCount); |
184 } | 182 } |
185 | 183 |
186 LogMessage& LogMessage::operator<<(const string& value) { | 184 LogMessage& LogMessage::operator<<(const string& value) { |
187 message_ += value; | 185 message_ += value; |
188 return *this; | 186 return *this; |
189 } | 187 } |
190 | 188 |
191 LogMessage& LogMessage::operator<<(const char* value) { | 189 LogMessage& LogMessage::operator<<(const char* value) { |
192 message_ += value; | 190 message_ += value; |
193 return *this; | 191 return *this; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 | 239 |
242 LogMessage::LogMessage(LogLevel level, const char* filename, int line) | 240 LogMessage::LogMessage(LogLevel level, const char* filename, int line) |
243 : level_(level), filename_(filename), line_(line) {} | 241 : level_(level), filename_(filename), line_(line) {} |
244 LogMessage::~LogMessage() {} | 242 LogMessage::~LogMessage() {} |
245 | 243 |
246 void LogMessage::Finish() { | 244 void LogMessage::Finish() { |
247 bool suppress = false; | 245 bool suppress = false; |
248 | 246 |
249 if (level_ != LOGLEVEL_FATAL) { | 247 if (level_ != LOGLEVEL_FATAL) { |
250 InitLogSilencerCountOnce(); | 248 InitLogSilencerCountOnce(); |
251 MutexLock lock(cr_log_silencer_count_mutex_); | 249 MutexLock lock(log_silencer_count_mutex_); |
252 suppress = cr_log_silencer_count_ > 0; | 250 suppress = log_silencer_count_ > 0; |
253 } | 251 } |
254 | 252 |
255 if (!suppress) { | 253 if (!suppress) { |
256 (cr_log_handler_ ? cr_log_handler_ : DefaultLogHandler)(level_, filename_, | 254 log_handler_(level_, filename_, line_, message_); |
257 line_, message_); | |
258 } | 255 } |
259 | 256 |
260 if (level_ == LOGLEVEL_FATAL) { | 257 if (level_ == LOGLEVEL_FATAL) { |
261 #if PROTOBUF_USE_EXCEPTIONS | 258 #if PROTOBUF_USE_EXCEPTIONS |
262 throw FatalException(filename_, line_, message_); | 259 throw FatalException(filename_, line_, message_); |
263 #else | 260 #else |
264 abort(); | 261 abort(); |
265 #endif | 262 #endif |
266 } | 263 } |
267 } | 264 } |
268 | 265 |
269 void LogFinisher::operator=(LogMessage& other) { | 266 void LogFinisher::operator=(LogMessage& other) { |
270 other.Finish(); | 267 other.Finish(); |
271 } | 268 } |
272 | 269 |
273 } // namespace internal | 270 } // namespace internal |
274 | 271 |
275 LogHandler* SetLogHandler(LogHandler* new_func) { | 272 LogHandler* SetLogHandler(LogHandler* new_func) { |
276 LogHandler* old = internal::cr_log_handler_ ? internal::cr_log_handler_ | 273 LogHandler* old = internal::log_handler_; |
277 : internal::DefaultLogHandler; | |
278 if (old == &internal::NullLogHandler) { | 274 if (old == &internal::NullLogHandler) { |
279 old = NULL; | 275 old = NULL; |
280 } | 276 } |
281 if (new_func == NULL) { | 277 if (new_func == NULL) { |
282 internal::cr_log_handler_ = &internal::NullLogHandler; | 278 internal::log_handler_ = &internal::NullLogHandler; |
283 } else { | 279 } else { |
284 internal::cr_log_handler_ = new_func; | 280 internal::log_handler_ = new_func; |
285 } | 281 } |
286 return old; | 282 return old; |
287 } | 283 } |
288 | 284 |
289 LogSilencer::LogSilencer() { | 285 LogSilencer::LogSilencer() { |
290 internal::InitLogSilencerCountOnce(); | 286 internal::InitLogSilencerCountOnce(); |
291 MutexLock lock(internal::cr_log_silencer_count_mutex_); | 287 MutexLock lock(internal::log_silencer_count_mutex_); |
292 ++internal::cr_log_silencer_count_; | 288 ++internal::log_silencer_count_; |
293 }; | 289 }; |
294 | 290 |
295 LogSilencer::~LogSilencer() { | 291 LogSilencer::~LogSilencer() { |
296 internal::InitLogSilencerCountOnce(); | 292 internal::InitLogSilencerCountOnce(); |
297 MutexLock lock(internal::cr_log_silencer_count_mutex_); | 293 MutexLock lock(internal::log_silencer_count_mutex_); |
298 --internal::cr_log_silencer_count_; | 294 --internal::log_silencer_count_; |
299 }; | 295 }; |
300 | 296 |
301 // =================================================================== | 297 // =================================================================== |
302 // emulates google3/base/callback.cc | 298 // emulates google3/base/callback.cc |
303 | 299 |
304 Closure::~Closure() {} | 300 Closure::~Closure() {} |
305 | 301 |
306 namespace internal { FunctionClosure0::~FunctionClosure0() {} } | 302 namespace internal { FunctionClosure0::~FunctionClosure0() {} } |
307 | 303 |
308 void DoNothing() {} | 304 void DoNothing() {} |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 result_array[3] = static_cast<uint8>(x & 0xFF); | 400 result_array[3] = static_cast<uint8>(x & 0xFF); |
405 return result; | 401 return result; |
406 } | 402 } |
407 | 403 |
408 // =================================================================== | 404 // =================================================================== |
409 // Shutdown support. | 405 // Shutdown support. |
410 | 406 |
411 namespace internal { | 407 namespace internal { |
412 | 408 |
413 typedef void OnShutdownFunc(); | 409 typedef void OnShutdownFunc(); |
414 extern vector<void (*)()>* cr_shutdown_functions; | 410 vector<void (*)()>* shutdown_functions = NULL; |
415 extern Mutex* cr_shutdown_functions_mutex; | 411 Mutex* shutdown_functions_mutex = NULL; |
416 extern ProtobufOnceType cr_shutdown_functions_init; | 412 GOOGLE_PROTOBUF_DECLARE_ONCE(shutdown_functions_init); |
417 | 413 |
418 void InitShutdownFunctions() { | 414 void InitShutdownFunctions() { |
419 cr_shutdown_functions = new vector<void (*)()>; | 415 shutdown_functions = new vector<void (*)()>; |
420 cr_shutdown_functions_mutex = new Mutex; | 416 shutdown_functions_mutex = new Mutex; |
421 } | 417 } |
422 | 418 |
423 inline void InitShutdownFunctionsOnce() { | 419 inline void InitShutdownFunctionsOnce() { |
424 GoogleOnceInit(&cr_shutdown_functions_init, &InitShutdownFunctions); | 420 GoogleOnceInit(&shutdown_functions_init, &InitShutdownFunctions); |
425 } | 421 } |
426 | 422 |
427 void OnShutdown(void (*func)()) { | 423 void OnShutdown(void (*func)()) { |
428 InitShutdownFunctionsOnce(); | 424 InitShutdownFunctionsOnce(); |
429 MutexLock lock(cr_shutdown_functions_mutex); | 425 MutexLock lock(shutdown_functions_mutex); |
430 cr_shutdown_functions->push_back(func); | 426 shutdown_functions->push_back(func); |
431 } | 427 } |
432 | 428 |
433 } // namespace internal | 429 } // namespace internal |
434 | 430 |
435 void ShutdownProtobufLibrary() { | 431 void ShutdownProtobufLibrary() { |
436 internal::InitShutdownFunctionsOnce(); | 432 internal::InitShutdownFunctionsOnce(); |
437 | 433 |
438 // We don't need to lock cr_shutdown_functions_mutex because it's up to the | 434 // We don't need to lock shutdown_functions_mutex because it's up to the |
439 // caller to make sure that no one is using the library before this is | 435 // caller to make sure that no one is using the library before this is |
440 // called. | 436 // called. |
441 | 437 |
442 // Make it safe to call this multiple times. | 438 // Make it safe to call this multiple times. |
443 if (internal::cr_shutdown_functions == NULL) return; | 439 if (internal::shutdown_functions == NULL) return; |
444 | 440 |
445 for (int i = 0; i < internal::cr_shutdown_functions->size(); i++) { | 441 for (int i = 0; i < internal::shutdown_functions->size(); i++) { |
446 internal::cr_shutdown_functions->at(i)(); | 442 internal::shutdown_functions->at(i)(); |
447 } | 443 } |
448 delete internal::cr_shutdown_functions; | 444 delete internal::shutdown_functions; |
449 internal::cr_shutdown_functions = NULL; | 445 internal::shutdown_functions = NULL; |
450 delete internal::cr_shutdown_functions_mutex; | 446 delete internal::shutdown_functions_mutex; |
451 internal::cr_shutdown_functions_mutex = NULL; | 447 internal::shutdown_functions_mutex = NULL; |
452 } | 448 } |
453 | 449 |
454 #if PROTOBUF_USE_EXCEPTIONS | 450 #if PROTOBUF_USE_EXCEPTIONS |
455 FatalException::~FatalException() throw() {} | 451 FatalException::~FatalException() throw() {} |
456 | 452 |
457 const char* FatalException::what() const throw() { | 453 const char* FatalException::what() const throw() { |
458 return message_.c_str(); | 454 return message_.c_str(); |
459 } | 455 } |
460 #endif | 456 #endif |
461 | 457 |
462 } // namespace protobuf | 458 } // namespace protobuf |
463 } // namespace google | 459 } // namespace google |
OLD | NEW |