"Framework/API/git@code.ornl.gov:mantidproject/mantid.git" did not exist on "5d34f7bf653a2ed480fe65e7e74bccc0c4c8e97c"
Newer
Older
Gigg, Martyn Anthony
committed
//-----------------------------------------------
// Includes
//-----------------------------------------------
#include "MantidKernel/ThreadSafeLogStream.h"
using namespace Mantid::Kernel;
//************************************************************
// ThreadSafeLogStreamBuf
//************************************************************
/**
* Constructor
*/
ThreadSafeLogStreamBuf::ThreadSafeLogStreamBuf(Poco::Logger &logger,
Poco::Message::Priority priority)
: Poco::LogStreamBuf(logger, priority), m_messages() {}
Gigg, Martyn Anthony
committed
/**
* Destructor
*/
ThreadSafeLogStreamBuf::~ThreadSafeLogStreamBuf() {}
Gigg, Martyn Anthony
committed
int ThreadSafeLogStreamBuf::overflow(char c) {
return Poco::UnbufferedStreamBuf::overflow(c);
Gigg, Martyn Anthony
committed
/**
* 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
Janik Zikovsky
committed
* @param c :: The input character
Gigg, Martyn Anthony
committed
* @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[Poco::Thread::currentTid()],
getPriority());
m_messages[Poco::Thread::currentTid()] = "";
Gigg, Martyn Anthony
committed
logger().log(msg);
Poco::FastMutex::ScopedLock lock(m_mutex);
m_messages[Poco::Thread::currentTid()] += c;
Gigg, Martyn Anthony
committed
}
return static_cast<int>(c);
}
//************************************************************
// ThreadSafeLogIOS
//************************************************************
/**
* Constructor
Janik Zikovsky
committed
* @param logger :: A reference to the logger associated with this stream
* @param priority :: The stream priority
Gigg, Martyn Anthony
committed
*/
ThreadSafeLogIOS::ThreadSafeLogIOS(Poco::Logger &logger,
Poco::Message::Priority priority)
: m_buf(logger, priority) {
Gigg, Martyn Anthony
committed
poco_ios_init(&m_buf);
}
/**
* Destructor
*/
ThreadSafeLogIOS::~ThreadSafeLogIOS() {}
Gigg, Martyn Anthony
committed
/**
* Return the underlying buffer for this stream
* @returns The thread-safe buffer associated with this stream
*/
Poco::LogStreamBuf *ThreadSafeLogIOS::rdbuf() { return &m_buf; }
Gigg, Martyn Anthony
committed
//************************************************************
// ThreadSafeLogStream
//************************************************************
/**
* Constructor
Janik Zikovsky
committed
* @param logger :: A reference to the logger associated with this stream
* @param priority :: The stream priority
Gigg, Martyn Anthony
committed
*/
ThreadSafeLogStream::ThreadSafeLogStream(Poco::Logger &logger,
Poco::Message::Priority priority)
: ThreadSafeLogIOS(logger, priority), std::ostream(&m_buf) {}
Gigg, Martyn Anthony
committed
/**
* Constructor taking a name for a logger
Janik Zikovsky
committed
* @param loggerName :: A name for the logger stream
* @param priority :: The stream priority
Gigg, Martyn Anthony
committed
*/
ThreadSafeLogStream::ThreadSafeLogStream(const std::string &loggerName,
Poco::Message::Priority priority)
: ThreadSafeLogIOS(Poco::Logger::get(loggerName), priority),
std::ostream(&m_buf) {}
Gigg, Martyn Anthony
committed
/**
* Destructor
*/
ThreadSafeLogStream::~ThreadSafeLogStream() {}
Gigg, Martyn Anthony
committed
/**
* 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() {
Gigg, Martyn Anthony
committed
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
Janik Zikovsky
committed
* @param message :: The string to send to the logger
Gigg, Martyn Anthony
committed
* @returns A reference to the log stream with fatal priority level
*/
ThreadSafeLogStream &ThreadSafeLogStream::fatal(const std::string &message) {
Gigg, Martyn Anthony
committed
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() {
Gigg, Martyn Anthony
committed
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
Janik Zikovsky
committed
* @param message :: The string to send to the logger
Gigg, Martyn Anthony
committed
* @returns A reference to the log stream with critical priority level
*/
ThreadSafeLogStream &ThreadSafeLogStream::critical(const std::string &message) {
Gigg, Martyn Anthony
committed
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() {
Gigg, Martyn Anthony
committed
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
Janik Zikovsky
committed
* @param message :: The string to send to the logger
Gigg, Martyn Anthony
committed
* @returns A reference to the log stream with error priority level
*/
ThreadSafeLogStream &ThreadSafeLogStream::error(const std::string &message) {
Gigg, Martyn Anthony
committed
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() {
Gigg, Martyn Anthony
committed
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
Janik Zikovsky
committed
* @param message :: The string to send to the logger
Gigg, Martyn Anthony
committed
* @returns A reference to the log stream with warning priority level
*/
ThreadSafeLogStream &ThreadSafeLogStream::warning(const std::string &message) {
Gigg, Martyn Anthony
committed
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() {
Gigg, Martyn Anthony
committed
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
Janik Zikovsky
committed
* @param message :: The string to send to the logger
Gigg, Martyn Anthony
committed
* @returns A reference to the log stream with notice priority level
*/
ThreadSafeLogStream &ThreadSafeLogStream::notice(const std::string &message) {
Gigg, Martyn Anthony
committed
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() {
Gigg, Martyn Anthony
committed
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
Janik Zikovsky
committed
* @param message :: The string to send to the logger
Gigg, Martyn Anthony
committed
* @returns A reference to the log stream with information priority level
*/
ThreadSafeLogStream &
ThreadSafeLogStream::information(const std::string &message) {
Gigg, Martyn Anthony
committed
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() {
Gigg, Martyn Anthony
committed
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
Janik Zikovsky
committed
* @param message :: The string to send to the logger
Gigg, Martyn Anthony
committed
* @returns A reference to the log stream with debug priority level
*/
ThreadSafeLogStream &ThreadSafeLogStream::debug(const std::string &message) {
Gigg, Martyn Anthony
committed
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
Janik Zikovsky
committed
* @param priority :: The priority level
Gigg, Martyn Anthony
committed
* @returns A reference to the log stream with the given priority level
*/
ThreadSafeLogStream &
ThreadSafeLogStream::priority(Poco::Message::Priority priority) {
Gigg, Martyn Anthony
committed
m_buf.setPriority(priority);
return *this;
}