| 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 // http://code.google.com/p/protobuf/ |
| 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. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
| 32 | 32 |
| 33 #include <google/protobuf/stubs/common.h> | 33 #include <google/protobuf/stubs/common.h> |
| 34 #include <google/protobuf/stubs/once.h> | 34 #include <google/protobuf/stubs/once.h> |
| 35 #include <google/protobuf/stubs/status.h> | |
| 36 #include <google/protobuf/stubs/stringpiece.h> | |
| 37 #include <google/protobuf/stubs/strutil.h> | |
| 38 #include <stdio.h> | 35 #include <stdio.h> |
| 39 #include <errno.h> | 36 #include <errno.h> |
| 40 #include <vector> | 37 #include <vector> |
| 41 | 38 |
| 39 #include "config.h" |
| 40 |
| 42 #ifdef _WIN32 | 41 #ifdef _WIN32 |
| 43 #define WIN32_LEAN_AND_MEAN // We only need minimal includes | 42 #define WIN32_LEAN_AND_MEAN // We only need minimal includes |
| 44 #include <windows.h> | 43 #include <windows.h> |
| 45 #define snprintf _snprintf // see comment in strutil.cc | 44 #define snprintf _snprintf // see comment in strutil.cc |
| 46 #elif defined(HAVE_PTHREAD) | 45 #elif defined(HAVE_PTHREAD) |
| 47 #include <pthread.h> | 46 #include <pthread.h> |
| 48 #else | 47 #else |
| 49 #error "No suitable threading library available." | 48 #error "No suitable threading library available." |
| 50 #endif | 49 #endif |
| 51 | 50 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 const string& message) { | 108 const string& message) { |
| 110 static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" }; | 109 static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" }; |
| 111 | 110 |
| 112 // We use fprintf() instead of cerr because we want this to work at static | 111 // We use fprintf() instead of cerr because we want this to work at static |
| 113 // initialization time. | 112 // initialization time. |
| 114 fprintf(stderr, "[libprotobuf %s %s:%d] %s\n", | 113 fprintf(stderr, "[libprotobuf %s %s:%d] %s\n", |
| 115 level_names[level], filename, line, message.c_str()); | 114 level_names[level], filename, line, message.c_str()); |
| 116 fflush(stderr); // Needed on MSVC. | 115 fflush(stderr); // Needed on MSVC. |
| 117 } | 116 } |
| 118 | 117 |
| 119 void NullLogHandler(LogLevel /* level */, const char* /* filename */, | 118 void NullLogHandler(LogLevel level, const char* filename, int line, |
| 120 int /* line */, const string& /* message */) { | 119 const string& message) { |
| 121 // Nothing. | 120 // Nothing. |
| 122 } | 121 } |
| 123 | 122 |
| 124 static LogHandler* log_handler_ = &DefaultLogHandler; | 123 static LogHandler* log_handler_ = &DefaultLogHandler; |
| 125 static int log_silencer_count_ = 0; | 124 static int log_silencer_count_ = 0; |
| 126 | 125 |
| 127 static Mutex* log_silencer_count_mutex_ = NULL; | 126 static Mutex* log_silencer_count_mutex_ = NULL; |
| 128 GOOGLE_PROTOBUF_DECLARE_ONCE(log_silencer_count_init_); | 127 GOOGLE_PROTOBUF_DECLARE_ONCE(log_silencer_count_init_); |
| 129 | 128 |
| 130 void DeleteLogSilencerCount() { | 129 void DeleteLogSilencerCount() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 142 LogMessage& LogMessage::operator<<(const string& value) { | 141 LogMessage& LogMessage::operator<<(const string& value) { |
| 143 message_ += value; | 142 message_ += value; |
| 144 return *this; | 143 return *this; |
| 145 } | 144 } |
| 146 | 145 |
| 147 LogMessage& LogMessage::operator<<(const char* value) { | 146 LogMessage& LogMessage::operator<<(const char* value) { |
| 148 message_ += value; | 147 message_ += value; |
| 149 return *this; | 148 return *this; |
| 150 } | 149 } |
| 151 | 150 |
| 152 LogMessage& LogMessage::operator<<(const StringPiece& value) { | |
| 153 message_ += value.ToString(); | |
| 154 return *this; | |
| 155 } | |
| 156 | |
| 157 LogMessage& LogMessage::operator<<(long long value) { | |
| 158 message_ += SimpleItoa(value); | |
| 159 return *this; | |
| 160 } | |
| 161 | |
| 162 LogMessage& LogMessage::operator<<(unsigned long long value) { | |
| 163 message_ += SimpleItoa(value); | |
| 164 return *this; | |
| 165 } | |
| 166 | |
| 167 LogMessage& LogMessage::operator<<( | |
| 168 const ::google::protobuf::util::Status& status) { | |
| 169 message_ += status.ToString(); | |
| 170 return *this; | |
| 171 } | |
| 172 | |
| 173 // Since this is just for logging, we don't care if the current locale changes | 151 // Since this is just for logging, we don't care if the current locale changes |
| 174 // the results -- in fact, we probably prefer that. So we use snprintf() | 152 // the results -- in fact, we probably prefer that. So we use snprintf() |
| 175 // instead of Simple*toa(). | 153 // instead of Simple*toa(). |
| 176 #undef DECLARE_STREAM_OPERATOR | 154 #undef DECLARE_STREAM_OPERATOR |
| 177 #define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \ | 155 #define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \ |
| 178 LogMessage& LogMessage::operator<<(TYPE value) { \ | 156 LogMessage& LogMessage::operator<<(TYPE value) { \ |
| 179 /* 128 bytes should be big enough for any of the primitive */ \ | 157 /* 128 bytes should be big enough for any of the primitive */ \ |
| 180 /* values which we print with this, but well use snprintf() */ \ | 158 /* values which we print with this, but well use snprintf() */ \ |
| 181 /* anyway to be extra safe. */ \ | 159 /* anyway to be extra safe. */ \ |
| 182 char buffer[128]; \ | 160 char buffer[128]; \ |
| 183 snprintf(buffer, sizeof(buffer), FORMAT, value); \ | 161 snprintf(buffer, sizeof(buffer), FORMAT, value); \ |
| 184 /* Guard against broken MSVC snprintf(). */ \ | 162 /* Guard against broken MSVC snprintf(). */ \ |
| 185 buffer[sizeof(buffer)-1] = '\0'; \ | 163 buffer[sizeof(buffer)-1] = '\0'; \ |
| 186 message_ += buffer; \ | 164 message_ += buffer; \ |
| 187 return *this; \ | 165 return *this; \ |
| 188 } | 166 } |
| 189 | 167 |
| 190 DECLARE_STREAM_OPERATOR(char , "%c" ) | 168 DECLARE_STREAM_OPERATOR(char , "%c" ) |
| 191 DECLARE_STREAM_OPERATOR(int , "%d" ) | 169 DECLARE_STREAM_OPERATOR(int , "%d" ) |
| 192 DECLARE_STREAM_OPERATOR(unsigned int , "%u" ) | 170 DECLARE_STREAM_OPERATOR(uint , "%u" ) |
| 193 DECLARE_STREAM_OPERATOR(long , "%ld") | 171 DECLARE_STREAM_OPERATOR(long , "%ld") |
| 194 DECLARE_STREAM_OPERATOR(unsigned long, "%lu") | 172 DECLARE_STREAM_OPERATOR(unsigned long, "%lu") |
| 195 DECLARE_STREAM_OPERATOR(double , "%g" ) | 173 DECLARE_STREAM_OPERATOR(double , "%g" ) |
| 196 DECLARE_STREAM_OPERATOR(void* , "%p" ) | |
| 197 #undef DECLARE_STREAM_OPERATOR | 174 #undef DECLARE_STREAM_OPERATOR |
| 198 | 175 |
| 199 LogMessage::LogMessage(LogLevel level, const char* filename, int line) | 176 LogMessage::LogMessage(LogLevel level, const char* filename, int line) |
| 200 : level_(level), filename_(filename), line_(line) {} | 177 : level_(level), filename_(filename), line_(line) {} |
| 201 LogMessage::~LogMessage() {} | 178 LogMessage::~LogMessage() {} |
| 202 | 179 |
| 203 void LogMessage::Finish() { | 180 void LogMessage::Finish() { |
| 204 bool suppress = false; | 181 bool suppress = false; |
| 205 | 182 |
| 206 if (level_ != LOGLEVEL_FATAL) { | 183 if (level_ != LOGLEVEL_FATAL) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 #if PROTOBUF_USE_EXCEPTIONS | 386 #if PROTOBUF_USE_EXCEPTIONS |
| 410 FatalException::~FatalException() throw() {} | 387 FatalException::~FatalException() throw() {} |
| 411 | 388 |
| 412 const char* FatalException::what() const throw() { | 389 const char* FatalException::what() const throw() { |
| 413 return message_.c_str(); | 390 return message_.c_str(); |
| 414 } | 391 } |
| 415 #endif | 392 #endif |
| 416 | 393 |
| 417 } // namespace protobuf | 394 } // namespace protobuf |
| 418 } // namespace google | 395 } // namespace google |
| OLD | NEW |