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