OLD | NEW |
| (Empty) |
1 // Copyright (C) 2011 Google Inc. | |
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 <stdio.h> | |
21 | |
22 #include <string> | |
23 | |
24 #include "logger.h" | |
25 | |
26 using std::string; | |
27 | |
28 // Make the logging functions private (not declared in logger.h) as the client | |
29 // should not have any reason to use them. | |
30 namespace { | |
31 | |
32 using i18n::phonenumbers::Logger; | |
33 | |
34 // Class template used to inline the right implementation for the T -> string | |
35 // conversion. | |
36 template <typename T> | |
37 struct ConvertToString; | |
38 | |
39 template <typename T> | |
40 struct ConvertToString { | |
41 static inline string DoWork(const T& s) { | |
42 return string(s); | |
43 } | |
44 }; | |
45 | |
46 template <> | |
47 struct ConvertToString<int> { | |
48 static inline string DoWork(const int& n) { | |
49 char buffer[16]; | |
50 #if defined(OS_WIN) | |
51 _itoa_s(n, buffer, sizeof(buffer), 10); | |
52 #else | |
53 snprintf(buffer, sizeof(buffer), "%d", n); | |
54 #endif | |
55 return string(buffer); | |
56 } | |
57 }; | |
58 | |
59 class LoggerHandler { | |
60 public: | |
61 LoggerHandler(Logger* impl) : impl_(impl) {} | |
62 | |
63 ~LoggerHandler() { | |
64 if (impl_) { | |
65 impl_->WriteMessage("\n"); | |
66 } | |
67 } | |
68 | |
69 template <typename T> | |
70 LoggerHandler& operator<<(const T& value) { | |
71 if (impl_) { | |
72 impl_->WriteMessage(ConvertToString<T>::DoWork(value)); | |
73 } | |
74 return *this; | |
75 } | |
76 | |
77 private: | |
78 Logger* const impl_; | |
79 }; | |
80 | |
81 } // namespace | |
82 | |
83 namespace i18n { | |
84 namespace phonenumbers { | |
85 | |
86 // Default logger implementation used by PhoneNumberUtil class. It outputs the | |
87 // messages to the standard output. | |
88 class StdoutLogger : public Logger { | |
89 public: | |
90 virtual ~StdoutLogger() {} | |
91 | |
92 virtual void WriteLevel(); | |
93 virtual void WriteMessage(const string& msg); | |
94 }; | |
95 | |
96 } // namespace phonenumbers | |
97 } // namespace i18n | |
98 | |
99 #endif // I18N_PHONENUMBERS_DEFAULT_LOGGER_H_ | |
OLD | NEW |