diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 1eca57f0f..1acf742a4 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -157,7 +157,7 @@ set ( SOURCES "src/base/string_piece.cc" "src/default_logger.cc" - "src/logger_adapter.cc" + "src/logger.cc" "src/metadata.h" # Generated by build tools. "src/phonemetadata.pb.cc" # Generated by Protocol Buffers. "src/phonenumber.cc" @@ -288,6 +288,7 @@ target_link_libraries (phonenumber_testing ${LIBRARY_DEPS}) add_dependencies (phonenumber_testing generate-sources ${TEST_METADATA_TARGET}) set (TEST_SOURCES + "src/logger_test.cc" "src/phonenumberutil_test.cc" "src/regexp_adapter_test.cc" "src/regexp_cache_test.cc" @@ -304,13 +305,17 @@ target_link_libraries ( ) # Install rules. -install (FILES src/phonenumber.pb.h DESTINATION include/phonenumbers/) -install (FILES src/phonenumberutil.h DESTINATION include/phonenumbers/) +install (FILES + "src/logger.h" + "src/phonenumber.pb.h" + "src/phonenumberutil.h" + DESTINATION include/phonenumbers/ +) install (FILES - src/base/basictypes.h - src/base/scoped_ptr.h - src/base/singleton.h + "src/base/basictypes.h" + "src/base/scoped_ptr.h" + "src/base/singleton.h" DESTINATION include/phonenumbers/base/ ) install (FILES src/base/synchronization/lock.h diff --git a/cpp/src/base/logging.h b/cpp/src/base/logging.h index f43ecc6a1..7c876a16c 100644 --- a/cpp/src/base/logging.h +++ b/cpp/src/base/logging.h @@ -29,8 +29,5 @@ # define DCHECK_EQ(X, Y) CHECK_EQ((X), (Y)) # define NOTREACHED() std::cerr -# define LOG(Level) std::cerr - -# define FATAL 1 #endif diff --git a/cpp/src/default_logger.cc b/cpp/src/default_logger.cc index 11eb59f04..fa8780d59 100644 --- a/cpp/src/default_logger.cc +++ b/cpp/src/default_logger.cc @@ -18,45 +18,31 @@ #include "default_logger.h" -using std::cerr; -using std::cout; -using std::endl; - namespace i18n { namespace phonenumbers { -DefaultLogger::DefaultLogger(LogLevel level) : level_(level) {} - -DefaultLogger::~DefaultLogger() {} - -void DefaultLogger::Fatal(const string& msg) const { - if (level_ >= LOG_FATAL) { - cerr << "FATAL libphonenumber " << msg << endl; - } -} - -void DefaultLogger::Error(const string& msg) const { - if (level_ >= LOG_ERROR) { - cerr << "ERROR libphonenumber " << msg << endl; - } -} - -void DefaultLogger::Warning(const string& msg) const { - if (level_ >= LOG_WARNING) { - cerr << "WARNING libphonenumber " << msg << endl; - } -} +using std::cout; +using std::string; -void DefaultLogger::Info(const string& msg) const { - if (level_ >= LOG_INFO) { - cout << "INFO libphonenumber " << msg << endl; - } +void StdoutLogger::WriteMessage(const string& msg) { + cout << " " << msg; } -void DefaultLogger::Debug(const string& msg) const { - if (level_ >= LOG_DEBUG) { - cout << "DEBUG libphonenumber " << msg << endl; +void StdoutLogger::WriteLevel() { + LogLevel log_level = level(); + cout << "["; + + switch (log_level) { + case LOG_FATAL: cout << "FATAL"; break; +#ifdef ERROR // In case ERROR is defined by MSVC (i.e not set to LOG_ERROR). + case ERROR: +#endif + case LOG_ERROR: cout << "ERROR"; break; + case LOG_WARNING: cout << "WARNING"; break; + case LOG_INFO: cout << "INFO"; break; + case LOG_DEBUG: cout << "DEBUG"; break; } + cout << "]"; } } // namespace phonenumbers diff --git a/cpp/src/default_logger.h b/cpp/src/default_logger.h index d1ebc1f10..d9c7cd781 100644 --- a/cpp/src/default_logger.h +++ b/cpp/src/default_logger.h @@ -17,40 +17,90 @@ #ifndef I18N_PHONENUMBERS_DEFAULT_LOGGER_H_ #define I18N_PHONENUMBERS_DEFAULT_LOGGER_H_ -#include "logger_adapter.h" +#include -namespace i18n { -namespace phonenumbers { +#include "logger.h" + +using std::string; + +// Make the logging functions private (not declared in logger.h) as the client +// should not have any reason to use them. +namespace { + +using i18n::phonenumbers::Logger; + +// Class template used to inline the right implementation for the T -> string +// conversion. +template +struct ConvertToString; + +template +struct ConvertToString { + static inline string DoWork(const T& s) { + return string(s); + } +}; -enum LogLevel { - LOG_FATAL, - LOG_ERROR, - LOG_WARNING, - LOG_INFO, - LOG_DEBUG, +template <> +struct ConvertToString { + static inline string DoWork(const int& n) { + char buffer[16]; + std::snprintf(buffer, sizeof(buffer), "%d", n); + return string(buffer); + } }; -class DefaultLogger : public LoggerAdapter { +class LoggerHandler { public: - virtual ~DefaultLogger(); + LoggerHandler(Logger* impl) : impl_(impl) {} - DefaultLogger(LogLevel level = LOG_WARNING); + ~LoggerHandler() { + if (impl_) { + impl_->WriteMessage("\n"); + } + } - virtual void Fatal(const string& msg) const; + template + LoggerHandler& operator<<(const T& value) { + if (impl_) { + impl_->WriteMessage(ConvertToString::DoWork(value)); + } + return *this; + } - virtual void Error(const string& msg) const; + private: + Logger* const impl_; +}; + +} // namespace + +namespace i18n { +namespace phonenumbers { - virtual void Warning(const string& msg) const; +inline LoggerHandler VLOG(int n) { + Logger* const logger_impl = Logger::mutable_logger_impl(); + if (logger_impl->level() < n) { + return LoggerHandler(NULL); + } + logger_impl->WriteLevel(); + return LoggerHandler(logger_impl); +} - virtual void Info(const string& msg) const; +inline LoggerHandler LOG(int n) { + return VLOG(n); +} - virtual void Debug(const string& msg) const; +// Default logger implementation used by PhoneNumberUtil class. It outputs the +// messages to the standard output. +class StdoutLogger : public Logger { + public: + virtual ~StdoutLogger() {} - private: - LogLevel level_; + virtual void WriteLevel(); + virtual void WriteMessage(const string& msg); }; } // namespace phonenumbers } // namespace i18n -# endif // I18N_PHONENUMBERS_DEFAULT_LOGGER_H_ +#endif // I18N_PHONENUMBERS_DEFAULT_LOGGER_H_ diff --git a/cpp/src/logger_adapter.cc b/cpp/src/logger.cc similarity index 91% rename from cpp/src/logger_adapter.cc rename to cpp/src/logger.cc index edfaa0416..be68d179a 100644 --- a/cpp/src/logger_adapter.cc +++ b/cpp/src/logger.cc @@ -14,12 +14,14 @@ // Author: Philippe Liard -# include "logger_adapter.h" +#include "logger.h" + +#include namespace i18n { namespace phonenumbers { -LoggerAdapter::~LoggerAdapter() {} +Logger* Logger::impl_ = NULL; } // namespace phonenumbers } // namespace i18n diff --git a/cpp/src/logger.h b/cpp/src/logger.h new file mode 100644 index 000000000..e4cca5a2c --- /dev/null +++ b/cpp/src/logger.h @@ -0,0 +1,92 @@ +// Copyright (C) 2011 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Author: Philippe Liard + +#ifndef I18N_PHONENUMBERS_LOGGER_H_ +#define I18N_PHONENUMBERS_LOGGER_H_ + +#include +#include + +#include + +namespace i18n { +namespace phonenumbers { + +using std::string; + +enum LogLevel { + LOG_FATAL = 1, + LOG_ERROR, + LOG_WARNING, + LOG_INFO, + LOG_DEBUG, +}; + +enum { + DFATAL = LOG_FATAL, +// ERROR seems to be defined on MSVC, therefore don't overwrite it. +#ifndef ERROR + ERROR = LOG_ERROR, +#endif + WARNING = LOG_WARNING, +}; + +// Subclass this abstract class to override the way logging is handled in the +// library. You can then call the PhoneNumberUtil::SetLogger() method. +class Logger { + public: + Logger() : level_(LOG_ERROR) {} + virtual ~Logger() {} + + // Writes the message level to the underlying output stream. + virtual void WriteLevel() {} + // Writes the provided message to the underlying output stream. + virtual void WriteMessage(const string& msg) = 0; + + inline LogLevel level() const { + return level_; + } + + inline void set_level(LogLevel level) { + level_ = level; + } + + static inline void set_logger_impl(Logger* logger) { + impl_ = logger; + } + + static inline Logger* mutable_logger_impl() { + return impl_; + } + + private: + static Logger* impl_; + LogLevel level_; +}; + +// Logger that does not log anything. It could be useful to "mute" the +// phonenumber library. +class NullLogger : public Logger { + public: + virtual ~NullLogger() {} + + virtual void WriteMessage(const string& /* msg */) {} +}; + +} // namespace phonenumbers +} // namespace i18n + +#endif // I18N_PHONENUMBERS_LOGGER_ADAPTER_H_ diff --git a/cpp/src/logger_adapter.h b/cpp/src/logger_adapter.h deleted file mode 100644 index 37122bc3b..000000000 --- a/cpp/src/logger_adapter.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (C) 2011 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Author: Philippe Liard - -#ifndef I18N_PHONENUMBERS_LOGGER_ADAPTER_H_ -#define I18N_PHONENUMBERS_LOGGER_ADAPTER_H_ - -#include - -using std::string; - -namespace i18n { -namespace phonenumbers { - -// Implement this 'interface' to override the way logging is handled -// in the library. -class LoggerAdapter { - public: - virtual ~LoggerAdapter(); - - // Logging methods - virtual void Fatal(const string& msg) const = 0; - - virtual void Error(const string& msg) const = 0; - - virtual void Warning(const string& msg) const = 0; - - virtual void Info(const string& msg) const = 0; - - virtual void Debug(const string& msg) const = 0; -}; - -} // namespace phonenumbers -} // namespace i18n - -#endif // I18N_PHONENUMBERS_LOGGER_ADAPTER_H_ diff --git a/cpp/src/logger_test.cc b/cpp/src/logger_test.cc new file mode 100644 index 000000000..bd3d19b07 --- /dev/null +++ b/cpp/src/logger_test.cc @@ -0,0 +1,104 @@ +// Copyright (C) 2011 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Author: Philippe Liard + +#include + +#include + +#include "default_logger.h" + +namespace i18n { +namespace phonenumbers { + +using std::string; + +// String logger implementation used for testing. Messages are output to a +// string for convenience. +class StringLogger : public Logger { + public: + virtual ~StringLogger() {} + + const string& message() const { + return msg_; + } + + virtual void WriteMessage(const string& msg) { + msg_ += msg; + } + + private: + string msg_; +}; + +class LoggerTest : public ::testing::Test { + protected: + virtual void SetUp() { + test_logger_.reset(new StringLogger()); + test_logger_->set_level(LOG_INFO); + // Save the current logger implementation and restore it when the test is + // done to avoid side-effects in other tests (including phonenumberutil + // tests) as the logger implementation is global. + old_logger_ = Logger::mutable_logger_impl(); + Logger::set_logger_impl(test_logger_.get()); + } + + virtual void TearDown() { + // Restore the previous logger implementation to avoid side-effects in other + // tests as mentioned above. + Logger::set_logger_impl(old_logger_); + } + + scoped_ptr test_logger_; + Logger* old_logger_; +}; + +TEST_F(LoggerTest, LoggerIgnoresHigherVerbosity) { + // The logger verbosity is set to LOG_INFO, therefore LOG_DEBUG messages + // should be ignored. + VLOG(LOG_DEBUG) << "Hello"; + EXPECT_EQ("", test_logger_->message()); +} + +TEST_F(LoggerTest, LoggerOutputsNewline) { + VLOG(LOG_INFO) << "Hello"; + EXPECT_EQ("Hello\n", test_logger_->message()); +} + +TEST_F(LoggerTest, LoggerLogsEqualVerbosity) { + VLOG(LOG_INFO) << "Hello"; + EXPECT_EQ("Hello\n", test_logger_->message()); +} + +TEST_F(LoggerTest, LoggerLogsLowerVerbosity) { + VLOG(LOG_WARNING) << "Hello"; + EXPECT_EQ("Hello\n", test_logger_->message()); +} + +TEST_F(LoggerTest, LoggerConcatenatesMessages) { + VLOG(LOG_INFO) << "Hello"; + ASSERT_EQ("Hello\n", test_logger_->message()); + + VLOG(LOG_INFO) << " World"; + EXPECT_EQ("Hello\n World\n", test_logger_->message()); +} + +TEST_F(LoggerTest, LoggerHandlesDifferentTypes) { + VLOG(LOG_INFO) << "Hello " << 42; + EXPECT_EQ("Hello 42\n", test_logger_->message()); +} + +} // namespace phonenumbers +} // namespace i18n diff --git a/cpp/src/phonenumberutil.cc b/cpp/src/phonenumberutil.cc index a5b28ddf4..ec590c0b0 100644 --- a/cpp/src/phonenumberutil.cc +++ b/cpp/src/phonenumberutil.cc @@ -36,7 +36,6 @@ #include "base/singleton.h" #include "default_logger.h" #include "encoding_utils.h" -#include "logger_adapter.h" #include "metadata.h" #include "normalize_utf8.h" #include "phonemetadata.pb.h" @@ -75,8 +74,7 @@ const char PhoneNumberUtil::kValidPunctuation[] = namespace { -scoped_ptr logger; - +scoped_ptr logger_; scoped_ptr regexp_cache; // These objects are created in the function InitializeStaticMapsAndSets. @@ -374,36 +372,36 @@ PhoneNumberUtil::PhoneNumberType GetNumberTypeHelper( const PhoneNumberDesc& general_desc = metadata.general_desc(); if (!general_desc.has_national_number_pattern() || !IsNumberMatchingDesc(national_number, general_desc)) { - logger->Debug("Number type unknown - " - "doesn't match general national number pattern."); + VLOG(4) << "Number type unknown - doesn't match general national number" + << " pattern."; return PhoneNumberUtil::UNKNOWN; } if (IsNumberMatchingDesc(national_number, metadata.premium_rate())) { - logger->Debug("Number is a premium number."); + VLOG(4) << "Number is a premium number."; return PhoneNumberUtil::PREMIUM_RATE; } if (IsNumberMatchingDesc(national_number, metadata.toll_free())) { - logger->Debug("Number is a toll-free number."); + VLOG(4) << "Number is a toll-free number."; return PhoneNumberUtil::TOLL_FREE; } if (IsNumberMatchingDesc(national_number, metadata.shared_cost())) { - logger->Debug("Number is a shared cost number."); + VLOG(4) << "Number is a shared cost number."; return PhoneNumberUtil::SHARED_COST; } if (IsNumberMatchingDesc(national_number, metadata.voip())) { - logger->Debug("Number is a VOIP (Voice over IP) number."); + VLOG(4) << "Number is a VOIP (Voice over IP) number."; return PhoneNumberUtil::VOIP; } if (IsNumberMatchingDesc(national_number, metadata.personal_number())) { - logger->Debug("Number is a personal number."); + VLOG(4) << "Number is a personal number."; return PhoneNumberUtil::PERSONAL_NUMBER; } if (IsNumberMatchingDesc(national_number, metadata.pager())) { - logger->Debug("Number is a pager number."); + VLOG(4) << "Number is a pager number."; return PhoneNumberUtil::PAGER; } if (IsNumberMatchingDesc(national_number, metadata.uan())) { - logger->Debug("Number is a UAN."); + VLOG(4) << "Number is a UAN."; return PhoneNumberUtil::UAN; } @@ -411,26 +409,26 @@ PhoneNumberUtil::PhoneNumberType GetNumberTypeHelper( IsNumberMatchingDesc(national_number, metadata.fixed_line()); if (is_fixed_line) { if (metadata.same_mobile_and_fixed_line_pattern()) { - logger->Debug("Fixed-line and mobile patterns equal, " - "number is fixed-line or mobile"); + VLOG(4) << "Fixed-line and mobile patterns equal, number is fixed-line" + << " or mobile"; return PhoneNumberUtil::FIXED_LINE_OR_MOBILE; } else if (IsNumberMatchingDesc(national_number, metadata.mobile())) { - logger->Debug("Fixed-line and mobile patterns differ, but number is " - "still fixed-line or mobile"); + VLOG(4) << "Fixed-line and mobile patterns differ, but number is " + << "still fixed-line or mobile"; return PhoneNumberUtil::FIXED_LINE_OR_MOBILE; } - logger->Debug("Number is a fixed line number."); + VLOG(4) << "Number is a fixed line number."; return PhoneNumberUtil::FIXED_LINE; } // Otherwise, test to see if the number is mobile. Only do this if certain // that the patterns for mobile and fixed line aren't the same. if (!metadata.same_mobile_and_fixed_line_pattern() && IsNumberMatchingDesc(national_number, metadata.mobile())) { - logger->Debug("Number is a mobile number."); + VLOG(4) << "Number is a mobile number."; return PhoneNumberUtil::MOBILE; } - logger->Debug("Number type unknown - doesn't match any specific number type" - " pattern."); + VLOG(4) << "Number type unknown - doesn\'t match any specific number type" + << " pattern."; return PhoneNumberUtil::UNKNOWN; } @@ -617,8 +615,8 @@ PhoneNumberUtil::ValidationResult TestNumberLengthAgainstPattern( } // namespace -void PhoneNumberUtil::SetLoggerAdapter(LoggerAdapter* logger_adapter) { - logger.reset(logger_adapter); +void PhoneNumberUtil::SetLogger(Logger* logger) { + Logger::set_logger_impl(logger); } // Private constructor. Also takes care of initialisation. @@ -626,12 +624,11 @@ PhoneNumberUtil::PhoneNumberUtil() : country_calling_code_to_region_code_map_(new vector()), nanpa_regions_(new set()), region_to_metadata_map_(new map()) { - if (logger == NULL) { - SetLoggerAdapter(new DefaultLogger()); - } + logger_.reset(new StdoutLogger()); + Logger::set_logger_impl(logger_.get()); PhoneMetadataCollection metadata_collection; if (!LoadCompiledInMetadata(&metadata_collection)) { - logger->Fatal("Could not parse compiled-in metadata"); + LOG(DFATAL) << "Could not parse compiled-in metadata."; return; } // Storing data in a temporary map to make it easier to find other regions @@ -773,7 +770,7 @@ void PhoneNumberUtil::GetNddPrefixForRegion(const string& region_code, string* national_prefix) const { DCHECK(national_prefix); if (!IsValidRegionCode(region_code)) { - logger->Error("Invalid region code provided."); + LOG(ERROR) << "Invalid region code provided."; return; } const PhoneMetadata* metadata = GetMetadataForRegion(region_code); @@ -794,10 +791,10 @@ bool PhoneNumberUtil::HasValidRegionCode(const string& region_code, int country_calling_code, const string& number) const { if (!IsValidRegionCode(region_code)) { - logger->Info(string("Number ") + number + - " has invalid or missing country code (" + - country_calling_code + - ")"); + VLOG(1) << "Number " << number + << " has invalid or missing country calling code (" + << country_calling_code + << ")"; return false; } return true; @@ -959,8 +956,8 @@ void PhoneNumberUtil::FormatOutOfCountryCallingNumber( string* formatted_number) const { DCHECK(formatted_number); if (!IsValidRegionCode(calling_from)) { - logger->Info("Trying to format number from invalid region. International" - " formatting applied."); + VLOG(1) << "Trying to format number from invalid region. International" + << " formatting applied."; Format(number, INTERNATIONAL, formatted_number); return; } @@ -1257,8 +1254,9 @@ void PhoneNumberUtil::GetRegionCodeForNumber(const PhoneNumber& number, if (region_codes.size() == 0) { string number_string; GetNationalSignificantNumber(number, &number_string); - logger->Warning(string("Missing/invalid country code (") + - SimpleItoa(country_calling_code) + ") for number " + number_string); + LOG(WARNING) << "Missing/invalid country calling code (" + << country_calling_code + << ") for number " << number_string; *region_code = RegionCode::GetUnknown(); return; } @@ -1296,7 +1294,7 @@ void PhoneNumberUtil::GetRegionCodeForNumberFromRegionList( int PhoneNumberUtil::GetCountryCodeForRegion(const string& region_code) const { if (!IsValidRegionCode(region_code)) { - logger->Error("Invalid or unknown country code provided."); + LOG(ERROR) << "Invalid or unknown region code provided."; return 0; } const PhoneMetadata* metadata = GetMetadataForRegion(region_code); @@ -1321,7 +1319,7 @@ bool PhoneNumberUtil::GetExampleNumberForType( PhoneNumber* number) const { DCHECK(number); if (!IsValidRegionCode(region_code)) { - logger->Warning("Invalid or unknown region code provided."); + LOG(WARNING) << "Invalid or unknown region code provided."; return false; } const PhoneMetadata* region_metadata = GetMetadataForRegion(region_code); @@ -1378,13 +1376,13 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseHelper( string national_number; ExtractPossibleNumber(number_to_parse, &national_number); if (!IsViablePhoneNumber(national_number)) { - logger->Debug("The string supplied did not seem to be a phone number."); + VLOG(2) << "The string supplied did not seem to be a phone number."; return NOT_A_NUMBER; } if (check_region && !CheckRegionForParsing(national_number, default_region)) { - logger->Info("Missing or invalid default country."); + VLOG(1) << "Missing or invalid default country."; return INVALID_COUNTRY_CODE_ERROR; } PhoneNumber temp_number; @@ -1422,7 +1420,7 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseHelper( country_code = country_metadata->country_code(); } if (normalized_national_number.length() < kMinLengthForNsn) { - logger->Debug("The string supplied is too short to be a phone number."); + VLOG(2) << "The string supplied is too short to be a phone number."; return TOO_SHORT_NSN; } if (country_metadata) { @@ -1435,11 +1433,11 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::ParseHelper( size_t normalized_national_number_length = normalized_national_number.length(); if (normalized_national_number_length < kMinLengthForNsn) { - logger->Debug("The string supplied is too short to be a phone number."); + VLOG(2) << "The string supplied is too short to be a phone number."; return TOO_SHORT_NSN; } if (normalized_national_number_length > kMaxLengthForNsn) { - logger->Debug("The string supplied is too long to be a phone number."); + VLOG(2) << "The string supplied is too long to be a phone number."; return TOO_LONG_NSN; } temp_number.set_country_code(country_code); @@ -1496,8 +1494,8 @@ void PhoneNumberUtil::ExtractPossibleNumber(const string& number, return; } - logger->Debug("After stripping starting and trailing characters," - " left with: " + *extracted_number); + VLOG(3) << "After stripping starting and trailing characters, left with: " + << *extracted_number; // Now remove any extra numbers at the end. capture_up_to_second_number_start_pattern->PartialMatch(*extracted_number, @@ -1606,7 +1604,7 @@ bool PhoneNumberUtil::IsValidNumberForRegion(const PhoneNumber& number, // is between the minimum and maximum lengths defined by ITU for a national // significant number. if (!general_desc.has_national_number_pattern()) { - logger->Info("Validating number with incomplete metadata."); + VLOG(3) << "Validating number with incomplete metadata."; size_t number_length = national_number.length(); return number_length > kMinLengthForNsn && number_length <= kMaxLengthForNsn; @@ -1765,7 +1763,7 @@ void PhoneNumberUtil::Normalize(string* number) const { // method ExtractPossibleNumber. bool PhoneNumberUtil::IsViablePhoneNumber(const string& number) const { if (number.length() < kMinLengthForNsn) { - logger->Debug("Number too short to be viable:" + number); + VLOG(2) << "Number too short to be viable:" << number; return false; } return valid_phone_number_pattern->FullMatch(number); @@ -1865,7 +1863,7 @@ void PhoneNumberUtil::MaybeStripNationalPrefixAndCarrierCode( number_copy_without_transform.get(), &carrier_code_temp) || possible_national_prefix_pattern.Consume( number_copy_without_transform.get())) { - logger->Debug("Parsed the first digits as a national prefix."); + VLOG(4) << "Parsed the first digits as a national prefix."; // If captured_part_of_prefix is empty, this implies nothing was captured by // the capturing groups in possible_national_prefix; therefore, no // transformation is necessary, and we just remove the national prefix. @@ -1878,7 +1876,7 @@ void PhoneNumberUtil::MaybeStripNationalPrefixAndCarrierCode( } } } else { - logger->Debug("The first digits did not match the national prefix."); + VLOG(4) << "The first digits did not match the national prefix."; } } @@ -1903,10 +1901,11 @@ bool PhoneNumberUtil::MaybeStripExtension(string* number, string* extension) &possible_extension_three)) { // Replace the extensions in the original string here. extn_pattern->Replace(&number_copy, ""); - logger->Debug("Found an extension. Possible extension one: " - + possible_extension_one - + ". Possible extension two: " + possible_extension_two - + ". Remaining number: " + number_copy); + VLOG(4) << "Found an extension. Possible extension one: " + << possible_extension_one + << ". Possible extension two: " << possible_extension_two + << ". Possible extension three: " << possible_extension_three + << ". Remaining number: " << number_copy; // If we find a potential extension, and the number preceding this is a // viable number, we assume it is an extension. if ((!possible_extension_one.empty() || !possible_extension_two.empty() || @@ -1985,8 +1984,8 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::MaybeExtractCountryCode( } if (country_code_source != PhoneNumber::FROM_DEFAULT_COUNTRY) { if (national_number->length() < kMinLengthForNsn) { - logger->Debug("Phone number had an IDD, but after this was not " - "long enough to be a viable phone number."); + VLOG(2) << "Phone number had an IDD, but after this was not " + << "long enough to be a viable phone number."; return TOO_SHORT_AFTER_IDD; } int potential_country_code = ExtractCountryCode(national_number); @@ -2003,7 +2002,7 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::MaybeExtractCountryCode( // checks on the validity of the number before and after. int default_country_code = default_region_metadata->country_code(); string default_country_code_string(SimpleItoa(default_country_code)); - logger->Debug("Possible country code: " + default_country_code_string); + VLOG(4) << "Possible country calling code: " << default_country_code_string; string potential_national_number; if (TryStripPrefixString(*national_number, default_country_code_string, @@ -2015,8 +2014,8 @@ PhoneNumberUtil::ErrorType PhoneNumberUtil::MaybeExtractCountryCode( MaybeStripNationalPrefixAndCarrierCode(*default_region_metadata, &potential_national_number, NULL); - logger->Debug("Number without country code prefix: " - + potential_national_number); + VLOG(4) << "Number without country calling code prefix: " + << potential_national_number; const RegExp& possible_number_pattern = regexp_cache->GetRegExp( StrCat("(", general_num_desc.possible_number_pattern(), ")")); // If the number was not valid before but is valid now, or if it was too diff --git a/cpp/src/phonenumberutil.h b/cpp/src/phonenumberutil.h index 26f8bcfa3..bb5031556 100644 --- a/cpp/src/phonenumberutil.h +++ b/cpp/src/phonenumberutil.h @@ -46,7 +46,7 @@ using std::vector; using google::protobuf::RepeatedPtrField; -class LoggerAdapter; +class Logger; class NumberFormat; class PhoneMetadata; class PhoneMetadataCollection; @@ -494,9 +494,9 @@ class PhoneNumberUtil : public Singleton { MatchType IsNumberMatchWithOneString(const PhoneNumber& first_number, const string& second_number) const; - // Override the default logging system. The provided adapter destruction is - // handled by this class (don't delete it). - static void SetLoggerAdapter(LoggerAdapter* logger_adapter); + // Overrides the default logging system. The provided logger destruction is + // handled by this class (i.e don't delete it). + static void SetLogger(Logger* logger); friend bool ConvertFromTelephoneNumberProto( const TelephoneNumber& proto_to_convert, diff --git a/cpp/src/regexp_adapter_icu.cc b/cpp/src/regexp_adapter_icu.cc index 5ffd3a4f9..933f455d8 100644 --- a/cpp/src/regexp_adapter_icu.cc +++ b/cpp/src/regexp_adapter_icu.cc @@ -94,7 +94,7 @@ class IcuRegExp : public RegExp { UnicodeString::fromUTF8(utf8_regexp), 0, parse_error, status)); if (U_FAILURE(status)) { // The provided regular expressions should compile correctly. - logger_.Error("Error compiling regular expression: " + utf8_regexp); + LOG(ERROR) << "Error compiling regular expression: " << utf8_regexp; utf8_regexp_.reset(NULL); } } @@ -192,7 +192,6 @@ class IcuRegExp : public RegExp { } private: - DefaultLogger logger_; scoped_ptr utf8_regexp_; DISALLOW_COPY_AND_ASSIGN(IcuRegExp);