Ice 3.7 C++11 API Reference
LoggerUtil.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_LOGGER_UTIL_H
6 #define ICE_LOGGER_UTIL_H
7 
8 #include <Ice/Logger.h>
9 #include <Ice/CommunicatorF.h>
10 #include <Ice/Plugin.h>
11 #include <Ice/Exception.h>
12 
13 namespace Ice
14 {
15 
21 {
22 public:
23 
25  std::string str() const;
26 
28  std::ostringstream& _stream(); // For internal use only. Don't use in your code.
30 
31 private:
32 
33  std::ostringstream _os;
34 };
35 
37 ICE_API LoggerOutputBase& loggerInsert(LoggerOutputBase& out, const IceUtil::Exception& ex);
38 
39 template<typename T>
40 struct IsException
41 {
42  static char testex(IceUtil::Exception*);
43  static long testex(...);
44 
45  static const bool value = sizeof(testex(static_cast<T*>(0))) == sizeof(char);
46 };
47 
48 template<typename T, bool = false>
49 struct LoggerOutputInserter
50 {
51  static inline LoggerOutputBase&
52  insert(LoggerOutputBase& out, const T& val)
53  {
54  out._stream() << val;
55  return out;
56  }
57 };
58 
59 // Partial specialization
60 template<typename T>
61 struct LoggerOutputInserter<T, true>
62 {
63  static inline LoggerOutputBase&
64  insert(LoggerOutputBase& out, const T& ex)
65  {
66  return loggerInsert(out, ex);
67  }
68 };
69 
70 template<typename T>
71 inline LoggerOutputBase&
72 operator<<(LoggerOutputBase& out, const T& val)
73 {
74  return LoggerOutputInserter<T, IsException<T>::value>::insert(out, val);
75 }
76 
77 #ifdef ICE_CPP11_MAPPING
78 template<typename T, typename ::std::enable_if<::std::is_base_of<::Ice::ObjectPrx, T>::value>::type* = nullptr>
79 inline LoggerOutputBase&
80 operator<<(LoggerOutputBase& os, const ::std::shared_ptr<T>& p)
81 #else
82 template<typename T>
83 inline LoggerOutputBase&
84 operator<<(LoggerOutputBase& os, const ::IceInternal::ProxyHandle<T>& p)
85 #endif
86 {
87  return os << (p ? p->ice_toString() : "");
88 }
89 
90 inline LoggerOutputBase&
91 operator<<(LoggerOutputBase& out, const ::std::exception& ex)
92 {
93  out._stream() << ex.what();
94  return out;
95 }
96 
97 ICE_API LoggerOutputBase& operator<<(LoggerOutputBase&, std::ios_base& (*)(std::ios_base&));
99 
104 template<class L, class LPtr, void (L::*output)(const std::string&)>
106 {
107 public:
108  inline LoggerOutput(const LPtr& lptr) :
109  _logger(lptr)
110  {}
111 
112  inline ~LoggerOutput()
113  {
114  flush();
115  }
116 
118  inline void flush()
119  {
120  std::string s = _stream().str();
121  if(!s.empty())
122  {
123  L& ref = *_logger;
124  (ref.*output)(s);
125  }
126  _stream().str("");
127  }
128 
129 private:
130 
131  LPtr _logger;
132 };
133 
136 
139 
142 
148 {
149 public:
150  Trace(const LoggerPtr&, const std::string&);
152  void flush();
153 
154 private:
155 
156  LoggerPtr _logger;
157  std::string _category;
158 };
159 
166 {
167 public:
168 
174  LoggerPlugin(const CommunicatorPtr& communicator, const LoggerPtr& logger);
175 
177  virtual void initialize();
178 
180  virtual void destroy();
181 };
182 
183 }
184 
185 #endif
Ice::LoggerOutput::~LoggerOutput
~LoggerOutput()
Definition: LoggerUtil.h:112
Ice::LoggerOutputBase::str
std::string str() const
Obtains the collected output.
CommunicatorF.h
Ice::Plugin
A communicator plug-in.
Definition: Plugin.h:78
Ice::LoggerOutput::LoggerOutput
LoggerOutput(const LPtr &lptr)
Definition: LoggerUtil.h:108
Ice::LoggerPlugin::initialize
virtual void initialize()
This method is a no-op.
Ice::operator<<
std::ostream & operator<<(std::ostream &out, const ProtocolVersion &version)
Definition: Protocol.h:179
Ice::Trace
Flushes output to Logger::trace.
Definition: LoggerUtil.h:148
Ice::LoggerOutput
Collects output and flushes it via a logger method.
Definition: LoggerUtil.h:106
Ice::LoggerPlugin::LoggerPlugin
LoggerPlugin(const CommunicatorPtr &communicator, const LoggerPtr &logger)
Constructs the plug-in with a target communicator and a logger.
ICE_API
#define ICE_API
Definition: Config.h:197
Ice::Trace::~Trace
~Trace()
Ice::Trace::flush
void flush()
Ice::LoggerPlugin
A special plug-in that installs a logger during a communicator's initialization.
Definition: LoggerUtil.h:166
Ice::LoggerPlugin::destroy
virtual void destroy()
This method is a no-op.
Logger.h
Plugin.h
IceUtil::noncopyable
Definition: Config.h:313
Ice::Trace::Trace
Trace(const LoggerPtr &, const std::string &)
Ice::Print
LoggerOutput< Logger, LoggerPtr, &Logger::print > Print
Flushes output to Logger::print.
Definition: LoggerUtil.h:135
IceUtil::Exception
Abstract base class for all Ice exceptions.
Definition: Exception.h:22
Ice
Definition: BuiltinSequences.h:56
Ice::Error
LoggerOutput< Logger, LoggerPtr, &Logger::error > Error
Flushes output to Logger::error.
Definition: LoggerUtil.h:141
Exception.h
Ice::Warning
LoggerOutput< Logger, LoggerPtr, &Logger::warning > Warning
Flushes output to Logger::warning.
Definition: LoggerUtil.h:138
Ice::LoggerOutputBase
Base class for logger output utility classes.
Definition: LoggerUtil.h:21
Ice::LoggerOutput::flush
void flush()
Flushes the colleted output to the logger method.
Definition: LoggerUtil.h:118