Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
LoggerUtil.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_LOGGER_UTIL_H
4#define ICE_LOGGER_UTIL_H
5
6#include "CommunicatorF.h"
7#include "Config.h"
8#include "Exception.h"
9#include "Logger.h"
10#include "Plugin.h"
11#include "Proxy.h"
12
13#include <sstream>
14#include <utility>
15
16namespace Ice
17{
18 /// Base class for logger output utility classes.
19 /// @headerfile Ice/Ice.h
20 class ICE_API LoggerOutputBase
21 {
22 public:
23 LoggerOutputBase() = default;
24 LoggerOutputBase(const LoggerOutputBase&) = delete;
25
26 LoggerOutputBase& operator=(const LoggerOutputBase&) = delete;
27
28 /// Gets the collected output.
29 [[nodiscard]] std::string str() const;
30
31 /// @private
32 std::ostringstream& _stream(); // For internal use only. Don't use in your code.
33
34 private:
35 std::ostringstream _os;
36 };
37
38 /// @cond INTERNAL
39 ICE_API LoggerOutputBase& loggerInsert(LoggerOutputBase& out, const Ice::Exception& ex);
40
41 template<typename T> struct IsException
42 {
43 static char testex(Ice::Exception*) noexcept;
44 static long testex(...) noexcept;
45
46 static constexpr bool value = sizeof(testex(static_cast<T*>(nullptr))) == sizeof(char);
47 };
48
49 template<typename T, bool = false> struct LoggerOutputInserter
50 {
51 static inline LoggerOutputBase& insert(LoggerOutputBase& out, const T& val)
52 {
53 out._stream() << val;
54 return out;
55 }
56 };
57
58 // Partial specialization
59 template<typename T> struct LoggerOutputInserter<T, true>
60 {
61 static inline LoggerOutputBase& insert(LoggerOutputBase& out, const T& ex) { return loggerInsert(out, ex); }
62 };
63
64 template<typename T> inline LoggerOutputBase& operator<<(LoggerOutputBase& out, const T& val)
65 {
66 return LoggerOutputInserter<T, IsException<T>::value>::insert(out, val);
67 }
68
69 template<typename Prx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
70 inline LoggerOutputBase& operator<<(LoggerOutputBase& os, const std::optional<Prx>& p)
71 {
72 return os << (p ? p->ice_toString() : "");
73 }
74
75 inline LoggerOutputBase& operator<<(LoggerOutputBase& os, const ObjectPrx& p) { return os << p.ice_toString(); }
76
77 inline LoggerOutputBase& operator<<(LoggerOutputBase& out, const std::exception& ex)
78 {
79 out._stream() << ex.what();
80 return out;
81 }
82
83 ICE_API LoggerOutputBase& operator<<(LoggerOutputBase&, std::ios_base& (*)(std::ios_base&));
84 /// @endcond
85
86 /// Collects output and flushes it via a logger method.
87 /// @headerfile Ice/Ice.h
88 template<class L, class LPtr, void (L::*output)(const std::string&)> class LoggerOutput : public LoggerOutputBase
89 {
90 public:
91 /// Constructs a LoggerOutput object with the given logger.
92 /// @param logger The logger or logger-like object to log to.
93 LoggerOutput(LPtr logger) : _logger(std::move(logger)) {}
94
95 ~LoggerOutput() { flush(); }
96
97 /// Flushes the colleted output to the logger method.
98 void flush()
99 {
100 std::string s = _stream().str();
101 if (!s.empty())
102 {
103 L& ref = *_logger;
104 (ref.*output)(s);
105 }
106 _stream().str("");
107 }
108
109 private:
110 LPtr _logger;
111 };
112
113 /// Flushes output to Logger::print.
115
116 /// Flushes output to Logger::warning.
118
119 /// Flushes output to Logger::error.
121
122 /// Flushes output to Logger::trace.
123 /// @headerfile Ice/Ice.h
124 class ICE_API Trace : public LoggerOutputBase
125 {
126 public:
127 /// Constructs a Trace object with the given logger and category.
128 /// @param logger The logger to log to.
129 /// @param category The trace category.
130 Trace(LoggerPtr logger, std::string category);
131
132 ~Trace();
133
134 /// Calls Logger::trace with the collected output.
135 void flush();
136
137 private:
138 LoggerPtr _logger;
139 std::string _category;
140 };
141
142 /// A special plug-in that installs a logger during a communicator's initialization.
143 /// Both initialize and destroy are no-op. See Ice::InitializationData.
144 /// @headerfile Ice/Ice.h
145 class ICE_API LoggerPlugin : public Ice::Plugin
146 {
147 public:
148 /// Constructs the plug-in with a target communicator and a logger.
149 /// @param communicator The communicator in which to install the logger.
150 /// @param logger The logger to be installed.
151 LoggerPlugin(const CommunicatorPtr& communicator, const LoggerPtr& logger);
152
153 /// This method is a no-op.
154 void initialize() override;
155
156 /// This method is a no-op.
157 void destroy() override;
158 };
159}
160
161#endif
Abstract base class for all Ice exceptions.
Definition Exception.h:16
std::string str() const
Gets the collected output.
Base class for logger output utility classes.
Definition LoggerUtil.h:21
void flush()
Flushes the colleted output to the logger method.
Definition LoggerUtil.h:98
LoggerOutput(LPtr logger)
Constructs a LoggerOutput object with the given logger.
Definition LoggerUtil.h:93
Collects output and flushes it via a logger method.
Definition LoggerUtil.h:89
void destroy() override
This method is a no-op.
LoggerPlugin(const CommunicatorPtr &communicator, const LoggerPtr &logger)
Constructs the plug-in with a target communicator and a logger.
void initialize() override
This method is a no-op.
Represents a communicator plug-in.
Definition Plugin.h:18
Trace(LoggerPtr logger, std::string category)
Constructs a Trace object with the given logger and category.
void flush()
Calls Logger::trace with the collected output.
std::ostream & operator<<(std::ostream &os, const SampleEventSeq &types)
Converts the given sample type vector to a string and add it to the stream.
Definition DataStorm.h:89
LoggerOutput< Logger, LoggerPtr, &Logger::error > Error
Flushes output to Logger::error.
Definition LoggerUtil.h:120
std::shared_ptr< Communicator > CommunicatorPtr
A shared pointer to a Communicator.
std::shared_ptr< Logger > LoggerPtr
A shared pointer to a Logger.
Definition Logger.h:16
LoggerOutput< Logger, LoggerPtr, &Logger::warning > Warning
Flushes output to Logger::warning.
Definition LoggerUtil.h:117
LoggerOutput< Logger, LoggerPtr, &Logger::print > Print
Flushes output to Logger::print.
Definition LoggerUtil.h:114
The Ice RPC framework.
Definition SampleEvent.h:59