• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DEFAULT_LOGGER_H_
18 #define I18N_PHONENUMBERS_DEFAULT_LOGGER_H_
19 
20 #include "phonenumbers/logger.h"
21 
22 #include <sstream>
23 #include <string>
24 
25 namespace i18n {
26 namespace phonenumbers {
27 
28 using i18n::phonenumbers::Logger;
29 using std::string;
30 using std::stringstream;
31 
32 // Class template used to inline the right implementation for the T -> string
33 // conversion.
34 template <typename T>
35 struct ConvertToString;
36 
37 template <typename T>
38 struct ConvertToString {
DoWorkConvertToString39   static inline string DoWork(const T& s) {
40     return string(s);
41   }
42 };
43 
44 template <>
45 struct ConvertToString<int> {
46   static inline string DoWork(int n) {
47     stringstream stream;
48     stream << n;
49     string result;
50     stream >> result;
51     return result;
52   }
53 };
54 
55 class LoggerHandler {
56  public:
57   LoggerHandler(Logger* impl) : impl_(impl) {}
58 
59   ~LoggerHandler() {
60     if (impl_) {
61       impl_->WriteMessage("\n");
62     }
63   }
64 
65   template <typename T>
66   LoggerHandler& operator<<(const T& value) {
67     if (impl_) {
68       impl_->WriteMessage(ConvertToString<T>::DoWork(value));
69     }
70     return *this;
71   }
72 
73  private:
74   Logger* const impl_;
75 };
76 
77 inline LoggerHandler LOG(int n) {
78   Logger* const logger_impl = Logger::mutable_logger_impl();
79   if (logger_impl->level() < n) {
80     return LoggerHandler(NULL);
81   }
82   logger_impl->WriteLevel();
83   return LoggerHandler(logger_impl);
84 }
85 
86 inline LoggerHandler VLOG(int n) {
87   // VLOG(1) is the next logging level after LOG(DEBUG).
88   n += LOG_DEBUG;
89   return LOG(n);
90 }
91 
92 // Default logger implementation used by PhoneNumberUtil class. It outputs the
93 // messages to the standard output.
94 class StdoutLogger : public Logger {
95  public:
96   virtual ~StdoutLogger() {}
97 
98   virtual void WriteLevel();
99   virtual void WriteMessage(const string& msg);
100 };
101 
102 }  // namespace phonenumbers
103 }  // namespace i18n
104 
105 #endif  // I18N_PHONENUMBERS_DEFAULT_LOGGER_H_
106