1 // Copyright (C) 2011 The Libphonenumber Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Author: Philippe Liard 16 17 #ifndef I18N_PHONENUMBERS_LOGGER_H_ 18 #define I18N_PHONENUMBERS_LOGGER_H_ 19 20 #include <cstdio> 21 #include <string> 22 23 namespace i18n { 24 namespace phonenumbers { 25 26 using std::string; 27 28 enum { 29 LOG_FATAL = 1, 30 LOG_ERROR, 31 LOG_WARNING, 32 LOG_INFO, 33 LOG_DEBUG, 34 }; 35 36 enum { 37 DFATAL = LOG_FATAL, 38 // ERROR seems to be defined on MSVC, therefore don't overwrite it. 39 #ifndef ERROR 40 ERROR = LOG_ERROR, 41 #endif 42 WARNING = LOG_WARNING, 43 }; 44 45 // Subclass this abstract class to override the way logging is handled in the 46 // library. You can then call the PhoneNumberUtil::SetLogger() method. 47 class Logger { 48 public: Logger()49 Logger() : level_(LOG_ERROR) {} ~Logger()50 virtual ~Logger() {} 51 52 // Writes the message level to the underlying output stream. WriteLevel()53 virtual void WriteLevel() {} 54 // Writes the provided message to the underlying output stream. 55 virtual void WriteMessage(const string& msg) = 0; 56 57 // Note that if set_verbosity_level has been used to set the level to a value 58 // that is not represented by an enum, the result here will be a log 59 // level that is higher than LOG_DEBUG. level()60 inline int level() const { 61 return level_; 62 } 63 set_level(int level)64 inline void set_level(int level) { 65 level_ = level; 66 } 67 68 // If you want to see verbose logs in addition to other logs, use this method. 69 // This will result in all log messages at the levels above being shown, along 70 // with calls to VLOG with the verbosity level set to this level or lower. 71 // For example, set_verbosity_level(2) will show calls of VLOG(1) and VLOG(2) 72 // but not VLOG(3), along with all calls to LOG(). set_verbosity_level(int verbose_logs_level)73 inline void set_verbosity_level(int verbose_logs_level) { 74 set_level(LOG_DEBUG + verbose_logs_level); 75 } 76 set_logger_impl(Logger * logger)77 static inline Logger* set_logger_impl(Logger* logger) { 78 impl_ = logger; 79 return logger; 80 } 81 mutable_logger_impl()82 static inline Logger* mutable_logger_impl() { 83 return impl_; 84 } 85 86 private: 87 static Logger* impl_; 88 int level_; 89 }; 90 91 // Logger that does not log anything. It could be useful to "mute" the 92 // phonenumber library. 93 class NullLogger : public Logger { 94 public: ~NullLogger()95 virtual ~NullLogger() {} 96 WriteMessage(const string &)97 virtual void WriteMessage(const string& /* msg */) {} 98 }; 99 100 } // namespace phonenumbers 101 } // namespace i18n 102 103 #endif // I18N_PHONENUMBERS_LOGGER_ADAPTER_H_ 104