Newer
Older
Gigg, Martyn Anthony
committed
//-----------------------------------------------
// Includes
//-----------------------------------------------
#include "MantidKernel/ThreadSafeLogStream.h"
#include "MantidKernel/MultiThreaded.h"
using namespace Mantid::Kernel;
//************************************************************
// ThreadSafeLogStreamBuf
//************************************************************
/**
* Constructor
*/
ThreadSafeLogStreamBuf::ThreadSafeLogStreamBuf(Poco::Logger& logger, Poco::Message::Priority priority):
Poco::LogStreamBuf(logger, priority), m_messages()
{
}
/**
* Destructor
*/
ThreadSafeLogStreamBuf::~ThreadSafeLogStreamBuf()
{
}
/**
* If the character is an EOL character then write the buffered messsage to the chosen device(s). If
Gigg, Martyn Anthony
committed
* not, buffer the character
* @param c The input character
* @returns The ASCII code of the input character
*/
int ThreadSafeLogStreamBuf::writeToDevice(char c)
{
if (c == '\n' || c == '\r')
{
Poco::Message msg(logger().name(), m_messages[PARALLEL_THREAD_NUMBER], getPriority());
m_messages[PARALLEL_THREAD_NUMBER] = "";
Gigg, Martyn Anthony
committed
logger().log(msg);
}
else
{
PARALLEL_CRITICAL(logstream)
{
m_messages[PARALLEL_THREAD_NUMBER] += c;
Gigg, Martyn Anthony
committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
}
}
return static_cast<int>(c);
}
//************************************************************
// ThreadSafeLogIOS
//************************************************************
/**
* Constructor
* @param logger A reference to the logger associated with this stream
* @param priority The stream priority
*/
ThreadSafeLogIOS::ThreadSafeLogIOS(Poco::Logger& logger, Poco::Message::Priority priority):
m_buf(logger, priority)
{
poco_ios_init(&m_buf);
}
/**
* Destructor
*/
ThreadSafeLogIOS::~ThreadSafeLogIOS()
{
}
/**
* Return the underlying buffer for this stream
* @returns The thread-safe buffer associated with this stream
*/
Poco::LogStreamBuf* ThreadSafeLogIOS::rdbuf()
{
return &m_buf;
}
//************************************************************
// ThreadSafeLogStream
//************************************************************
/**
* Constructor
* @param logger A reference to the logger associated with this stream
* @param priority The stream priority
*/
ThreadSafeLogStream::ThreadSafeLogStream(Poco::Logger& logger, Poco::Message::Priority priority):
ThreadSafeLogIOS(logger, priority), std::ostream(&m_buf)
{
}
/**
* Constructor taking a name for a logger
Gigg, Martyn Anthony
committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
* @param loggerName A name for the logger stream
* @param priority The stream priority
*/
ThreadSafeLogStream::ThreadSafeLogStream(const std::string& loggerName, Poco::Message::Priority priority):
ThreadSafeLogIOS(Poco::Logger::get(loggerName), priority),
std::ostream(&m_buf)
{
}
/**
* Destructor
*/
ThreadSafeLogStream::~ThreadSafeLogStream()
{
}
/**
* Return a reference to the log stream with the priority set to fatal
* @returns A reference to the log stream with fatal priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::fatal()
{
return priority(Poco::Message::PRIO_FATAL);
}
/**
* Log a message as fatal and return a reference to the log stream with the priority set to fatal
* @param message The string to send to the logger
* @returns A reference to the log stream with fatal priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::fatal(const std::string& message)
{
m_buf.logger().fatal(message);
return priority(Poco::Message::PRIO_FATAL);
}
/**
* Return a reference to the log stream with the priority set to critical
* @returns A reference to the log stream with critical priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::critical()
{
return priority(Poco::Message::PRIO_CRITICAL);
}
/**
* Log a message as critical and return a reference to the log stream with the priority set to critical
* @param message The string to send to the logger
* @returns A reference to the log stream with critical priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::critical(const std::string& message)
{
m_buf.logger().critical(message);
return priority(Poco::Message::PRIO_CRITICAL);
}
/**
* Return a reference to the log stream with the priority set to error
* @returns A reference to the log stream with error priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::error()
{
return priority(Poco::Message::PRIO_ERROR);
}
/**
* Log a message as error and return a reference to the log stream with the priority set to error
* @param message The string to send to the logger
* @returns A reference to the log stream with error priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::error(const std::string& message)
{
m_buf.logger().error(message);
return priority(Poco::Message::PRIO_ERROR);
}
/**
* Return a reference to the log stream with the priority set to warning
* @returns A reference to the log stream with warning priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::warning()
{
return priority(Poco::Message::PRIO_WARNING);
}
/**
* Log a message as a warning and return a reference to the log stream with the priority set to warning
* @param message The string to send to the logger
* @returns A reference to the log stream with warning priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::warning(const std::string& message)
{
m_buf.logger().warning(message);
return priority(Poco::Message::PRIO_WARNING);
}
/**
* Return a reference tothe log stream with the priority set to notice
* @returns A reference to the log stream with notice priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::notice()
{
return priority(Poco::Message::PRIO_NOTICE);
}
/**
* Log a message as a notice and return a reference to the log stream with the priority set to notice
* @param message The string to send to the logger
* @returns A reference to the log stream with notice priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::notice(const std::string& message)
{
m_buf.logger().notice(message);
return priority(Poco::Message::PRIO_NOTICE);
}
/**
* Return a reference to the log stream with the priority set to information
* @returns A reference to the log stream with information priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::information()
{
return priority(Poco::Message::PRIO_INFORMATION);
}
/**
* Log a message as information and return a reference to the log stream with the priority set to information
* @param message The string to send to the logger
* @returns A reference to the log stream with information priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::information(const std::string& message)
{
m_buf.logger().information(message);
return priority(Poco::Message::PRIO_INFORMATION);
}
/**
* Return a reference to the log stream with the priority set to debug
* @returns A reference to the log stream with debug priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::debug()
{
return priority(Poco::Message::PRIO_DEBUG);
}
/**
* Log a message as debug and return a reference to the log stream with the priority set to debug
* @param message The string to send to the logger
* @returns A reference to the log stream with debug priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::debug(const std::string& message)
{
m_buf.logger().debug(message);
return priority(Poco::Message::PRIO_DEBUG);
}
/**
* Return a reference to the log stream with the priority set at the given level
* @param priority The priority level
* @returns A reference to the log stream with the given priority level
*/
ThreadSafeLogStream& ThreadSafeLogStream::priority(Poco::Message::Priority priority)
{
m_buf.setPriority(priority);
return *this;
}