Mantid
Loading...
Searching...
No Matches
ThreadSafeLogStream.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//-----------------------------------------------
8// Includes
9//-----------------------------------------------
11
12#include <Poco/Logger.h>
13#include <Poco/StreamUtil.h>
14#include <Poco/UnbufferedStreamBuf.h>
15
16using namespace Mantid::Kernel;
17
18//************************************************************
19// ThreadSafeLogStreamBuf
20//************************************************************
21
25ThreadSafeLogStreamBuf::ThreadSafeLogStreamBuf(Poco::Logger &logger, Poco::Message::Priority priority)
26 : Poco::LogStreamBuf(logger, priority), m_messages() {}
27
28int ThreadSafeLogStreamBuf::overflow(char c) { return Poco::UnbufferedStreamBuf::overflow(c); }
29
38 if (c == '\n' || c == '\r') {
39 Poco::Message msg(logger().name(), m_messages[Poco::Thread::currentTid()], getPriority());
40 m_messages[Poco::Thread::currentTid()] = "";
41 logger().log(msg);
42 } else {
43 std::lock_guard<std::mutex> lock(m_mutex);
44 m_messages[Poco::Thread::currentTid()] += c;
45 }
46 return static_cast<int>(c);
47}
48
53void ThreadSafeLogStreamBuf::accumulate(const std::string &message) {
54 std::lock_guard<std::mutex> lock(m_mutex);
55 m_accumulator[Poco::Thread::currentTid()] += message;
56}
57
63 const std::string returnValue = m_accumulator[Poco::Thread::currentTid()];
64 std::lock_guard<std::mutex> lock(m_mutex);
65 m_accumulator[Poco::Thread::currentTid()] = "";
66 return returnValue;
67}
68
69//************************************************************
70// ThreadSafeLogIOS
71//************************************************************
77ThreadSafeLogIOS::ThreadSafeLogIOS(Poco::Logger &logger, Poco::Message::Priority priority) : m_buf(logger, priority) {
78 poco_ios_init(&m_buf);
79}
80
85Poco::LogStreamBuf *ThreadSafeLogIOS::rdbuf() { return &m_buf; }
86
87//************************************************************
88// ThreadSafeLogStream
89//************************************************************
95ThreadSafeLogStream::ThreadSafeLogStream(Poco::Logger &logger, Poco::Message::Priority priority)
96 : ThreadSafeLogIOS(logger, priority), std::ostream(&m_buf) {}
97
103ThreadSafeLogStream::ThreadSafeLogStream(const std::string &loggerName, Poco::Message::Priority priority)
104 : ThreadSafeLogIOS(Poco::Logger::get(loggerName), priority), std::ostream(&m_buf) {}
105
110ThreadSafeLogStream &ThreadSafeLogStream::fatal() { return priority(Poco::Message::PRIO_FATAL); }
111
119 m_buf.logger().fatal(message);
120 return priority(Poco::Message::PRIO_FATAL);
121}
122
127ThreadSafeLogStream &ThreadSafeLogStream::critical() { return priority(Poco::Message::PRIO_CRITICAL); }
128
136 m_buf.logger().critical(message);
137 return priority(Poco::Message::PRIO_CRITICAL);
138}
139
144ThreadSafeLogStream &ThreadSafeLogStream::error() { return priority(Poco::Message::PRIO_ERROR); }
145
153 m_buf.logger().error(message);
154 return priority(Poco::Message::PRIO_ERROR);
155}
156
161ThreadSafeLogStream &ThreadSafeLogStream::warning() { return priority(Poco::Message::PRIO_WARNING); }
162
170 m_buf.logger().warning(message);
171 return priority(Poco::Message::PRIO_WARNING);
172}
173
178ThreadSafeLogStream &ThreadSafeLogStream::notice() { return priority(Poco::Message::PRIO_NOTICE); }
179
187 m_buf.logger().notice(message);
188 return priority(Poco::Message::PRIO_NOTICE);
189}
190
195ThreadSafeLogStream &ThreadSafeLogStream::information() { return priority(Poco::Message::PRIO_INFORMATION); }
196
204 m_buf.logger().information(message);
205 return priority(Poco::Message::PRIO_INFORMATION);
206}
207
212ThreadSafeLogStream &ThreadSafeLogStream::debug() { return priority(Poco::Message::PRIO_DEBUG); }
213
221 m_buf.logger().debug(message);
222 return priority(Poco::Message::PRIO_DEBUG);
223}
224
230ThreadSafeLogStream &ThreadSafeLogStream::priority(Poco::Message::Priority priority) {
231 m_buf.setPriority(priority);
232 return *this;
233}
234
241 m_buf.accumulate(message);
242 return *this;
243}
244
249std::string ThreadSafeLogStream::flush() { return m_buf.flush(); }
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
The base class for ThreadSafeLogStream.
Poco::LogStreamBuf * rdbuf()
Return the underlying buffer for this stream.
ThreadSafeLogIOS(Poco::Logger &logger, Poco::Message::Priority priority)
Constructor.
ThreadSafeLogStreamBuf m_buf
The log stream buffer object.
std::map< Poco::Thread::TID, std::string > m_messages
Store a map of thread indices to messages.
int writeToDevice(char c) override
Overridden from base to write to the device in a thread-safe manner.
std::map< Poco::Thread::TID, std::string > m_accumulator
Store a map of thread indices to accummulators of messages.
std::string flush()
Returns and flushes the accumulated message.
ThreadSafeLogStreamBuf(Poco::Logger &logger, Poco::Message::Priority priority)
Constructor.
void accumulate(const std::string &message)
accumulate a message to the thread safe accummulator.
std::mutex m_mutex
mutex protecting logstream
The main log stream class implementing an ostream interface to a Logger.
ThreadSafeLogStream & warning()
Sets the priority for log messages to Poco::Message::PRIO_WARNING.
ThreadSafeLogStream & fatal()
Sets the priority for log messages to Poco::Message::PRIO_FATAL.
ThreadSafeLogStream & error()
Sets the priority for log messages to Poco::Message::PRIO_ERROR.
std::string flush()
Returns and flushes the accumulated messages.
ThreadSafeLogStream & accumulate(const std::string &message)
accumulates the message to the accummulator buffer
ThreadSafeLogStream & notice()
Sets the priority for log messages to Poco::Message::PRIO_NOTICE.
ThreadSafeLogStream & critical()
Sets the priority for log messages to Poco::Message::PRIO_CRITICAL.
ThreadSafeLogStream & information()
Sets the priority for log messages to Poco::Message::PRIO_INFORMATION.
ThreadSafeLogStream & priority(Poco::Message::Priority priority)
Sets the priority for log messages.
ThreadSafeLogStream(Poco::Logger &logger, Poco::Message::Priority priority=Poco::Message::PRIO_INFORMATION)
Creates the ThreadSafeLogStream, using the given logger and priority.
ThreadSafeLogStream & debug()
Sets the priority for log messages to Poco::Message::PRIO_DEBUG.
Definition: Algorithm.h:30
STL namespace.