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