25 |
//! Logging facilities |
//! Logging facilities |
26 |
namespace log |
namespace log |
27 |
{ |
{ |
28 |
//! Logging levels |
//! \brief Logging levels |
29 |
|
//! \note This struct acts as a namespace for the enumeration. |
30 |
struct Level |
struct Level |
31 |
{ |
{ |
|
// NOTE: Using a struct to put the enumerations in a |
|
|
// 'namespace' - descender |
|
32 |
static enum |
static enum |
33 |
{ |
{ |
34 |
DEBUG, //!< Debug messages |
DEBUG, //!< Debug messages |
57 |
{ |
{ |
58 |
public: |
public: |
59 |
|
|
60 |
//! Default constructor |
//! \brief Default constructor |
61 |
Server(); |
Server(); |
62 |
|
|
63 |
//! Destructor |
//! \brief Destructor |
64 |
~Server(); |
~Server(); |
65 |
|
|
66 |
//! Add a logging target |
//! \brief Add a logging target |
67 |
//! \param[in] target Logging target to add. |
//! \param[in] target Logging target to add. |
68 |
void |
void |
69 |
add_target(Target *target); |
add_target(Target *target); |
70 |
|
|
71 |
//! Sets threshold logging level |
//! \brief Sets threshold logging level |
72 |
//! \param[in] level Threshold level. |
//! \param[in] level Threshold level. |
73 |
void |
void |
74 |
set_level(int level); |
set_level(int level); |
75 |
|
|
76 |
//! Sets the message prefix for a level (overwrites |
//! \brief Sets the message prefix for a level |
77 |
//! existing). |
//! \note Overwrites existing prefixes. |
78 |
//! \param[in] level Logging level. |
//! \param[in] level Logging level. |
79 |
//! \param[in] prefix Message prefix. |
//! \param[in] prefix Message prefix. |
80 |
void |
void |
81 |
set_prefix(int level, const std::string& prefix); |
set_prefix(int level, const std::string& prefix); |
82 |
|
|
83 |
//! Logs a message at the specified level |
//! \brief Logs a message at the specified level |
84 |
//! \param[in] level Logging level. |
//! \param[in] level Logging level. |
85 |
//! \param[in] message Log message. |
//! \param[in] message Log message. |
86 |
void |
void |
87 |
log(int level, const std::string& message); |
log(int level, const std::string& message); |
88 |
|
|
89 |
//! Writes all buffered messages to targets |
//! \brief Writes all buffered messages to targets |
90 |
void |
void |
91 |
commit(); |
commit(); |
92 |
|
|
95 |
boost::scoped_ptr<ServerImpl> impl; |
boost::scoped_ptr<ServerImpl> impl; |
96 |
}; |
}; |
97 |
|
|
98 |
//! \brief Logging target abstract class |
//! \brief Logging target abstract class |
99 |
|
|
100 |
|
//! \note This class does not have a public destructor and therefore |
101 |
|
//! may not be allocated statically or on the stack. Call die() to |
102 |
|
//! destroy. |
103 |
class Target |
class Target |
104 |
{ |
{ |
105 |
public: |
public: |
106 |
|
|
107 |
//! Default constructor |
//! \brief Default constructor |
108 |
Target() |
Target() |
109 |
{} |
{} |
110 |
|
|
111 |
//! Opens target for writing |
//! \brief Opens target for writing |
112 |
virtual void |
virtual void |
113 |
open() = 0; |
open() = 0; |
114 |
|
|
115 |
//! Writes message to target |
//! \brief Writes message to target |
116 |
virtual void |
virtual void |
117 |
write(const std::string& message) = 0; |
write(const std::string& message) = 0; |
118 |
|
|
119 |
//! Commits buffered messages |
//! \brief Commits buffered messages |
120 |
virtual void |
virtual void |
121 |
commit() = 0; |
commit() = 0; |
122 |
|
|
123 |
//! Closes target |
//! \brief Closes target |
124 |
virtual void |
virtual void |
125 |
close() = 0; |
close() = 0; |
126 |
|
|
127 |
// This is for convenience, easily pluggable into |
//! \brief Destructor |
|
// for_each() |
|
|
//! Destructor |
|
128 |
virtual void |
virtual void |
129 |
die() |
die() |
130 |
{ |
{ |
136 |
protected: |
protected: |
137 |
|
|
138 |
// NOTE: This has been made protected because we want the |
// NOTE: This has been made protected because we want the |
139 |
// user to use die() instead and also to avoid mistakes in |
// user to use die() instead (which is pluggable into |
140 |
|
// std::for_each()) and also to avoid mistakes in |
141 |
// allocating this on the stack |
// allocating this on the stack |
142 |
|
//! \brief Destructor |
143 |
|
//! \note Do not use this as it does not clean up. Use die() instead. |
144 |
virtual ~Target() |
virtual ~Target() |
145 |
{} |
{} |
146 |
}; |
}; |
147 |
|
|
148 |
//! Log stream |
//! \brief Log stream |
149 |
//! |
//! |
150 |
//! Helper class that implements various convenience functions |
//! Helper class that implements various convenience functions |
151 |
//! to talk to the server. |
//! to talk to the server. |
153 |
{ |
{ |
154 |
public: |
public: |
155 |
|
|
156 |
//! Default constructor |
//! \brief Default constructor |
157 |
//! \param[in] level Logging level (defaults to INFO). |
//! \param[in] level Logging level (defaults to INFO). |
158 |
//! \param[in] server Server to connect to. |
//! \param[in] server Server to connect to. |
159 |
Stream(int level = Level::INFO, Server *server = &usata::log::server) |
Stream(int level = Level::INFO, Server *server = &usata::log::server) |
160 |
: m_server(server), m_level(level) |
: m_server(server), m_level(level) |
161 |
{} |
{} |
162 |
|
|
163 |
//! Destructor |
//! \brief Destructor |
164 |
~Stream() |
~Stream() |
165 |
{} |
{} |
166 |
|
|
167 |
//! Sets logging level |
//! \brief Sets logging level |
168 |
//! \param[in] level Logging level. |
//! \param[in] level Logging level. |
169 |
void |
void |
170 |
set_level(int level) |
set_level(int level) |
172 |
m_level = level; |
m_level = level; |
173 |
} |
} |
174 |
|
|
175 |
//! Output stream operator |
//! \brief Output stream operator |
176 |
template <typename T> |
template <typename T> |
177 |
friend Stream& |
friend Stream& |
178 |
operator << (Stream& log, T data) |
operator << (Stream& log, T data) |
196 |
|
|
197 |
public: |
public: |
198 |
|
|
199 |
//! Default constructor |
//! \brief Default constructor |
200 |
//! \param[in] level Logging level (defaults to INFO). |
//! \param[in] level Logging level (defaults to INFO). |
201 |
//! \param[in] server Server to connect to. |
//! \param[in] server Server to connect to. |
202 |
BufferedStream(int level = Level::INFO, Server *server = &usata::log::server) |
BufferedStream(int level = Level::INFO, Server *server = &usata::log::server) |
203 |
: m_stream(level, server) |
: m_stream(level, server) |
204 |
{} |
{} |
205 |
|
|
206 |
//! Destructor |
//! \brief Destructor |
207 |
~BufferedStream() |
~BufferedStream() |
208 |
{ |
{ |
209 |
if (!m_buffer.str().empty()) |
if (!m_buffer.str().empty()) |
210 |
commit(); |
commit(); |
211 |
} |
} |
212 |
|
|
213 |
//! Sets logging level |
//! \brief Sets logging level |
214 |
//! \param[in] level Logging level. |
//! \param[in] level Logging level. |
215 |
void |
void |
216 |
set_level(int level) |
set_level(int level) |
218 |
m_stream.set_level(level); |
m_stream.set_level(level); |
219 |
} |
} |
220 |
|
|
221 |
//! Output stream operator |
//! \brief Output stream operator |
222 |
template <typename T> |
template <typename T> |
223 |
friend BufferedStream& |
friend BufferedStream& |
224 |
operator << (BufferedStream& log, T data) |
operator << (BufferedStream& log, T data) |
227 |
return log; |
return log; |
228 |
} |
} |
229 |
|
|
230 |
//! Commits buffered messages |
//! \brief Commits buffered messages |
231 |
void |
void |
232 |
commit(); |
commit(); |
233 |
|
|
241 |
// Stream manipulators |
// Stream manipulators |
242 |
|
|
243 |
struct StreamSetLevel { int level; }; |
struct StreamSetLevel { int level; }; |
|
|
|
244 |
struct BufferedStreamCommit {}; |
struct BufferedStreamCommit {}; |
245 |
|
|
246 |
extern BufferedStreamCommit commit; |
extern BufferedStreamCommit commit; |
247 |
|
|
248 |
StreamSetLevel set_level(int level); |
StreamSetLevel set_level(int level); |