Newer
Older
Gigg, Martyn Anthony
committed
#ifndef MANTID_KERNEL_THREADSAFELOGSTREAM
#define MANTID_KERNEL_THREADSAFELOGSTREAM
//--------------------------------------------
// Includes
//--------------------------------------------
Gigg, Martyn Anthony
committed
#include "MantidKernel/DllConfig.h"
#include <map>
#include <string>
#include <iosfwd>
Gigg, Martyn Anthony
committed
namespace Mantid {
namespace Kernel {
Gigg, Martyn Anthony
committed
/**
This class implements a threadsafe version of the POCO buffer interface to a
Logger's stream object. The
buffer uses OpenMP API calls to both protect shared memory from access by
multiple threads and updates
Gigg, Martyn Anthony
committed
log messages is such a way that they are not mangled by separate threads.
@author Martyn Gigg, Tessella Support Services plc
@date 13/04/2010
Copyright © 2010 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
National Laboratory & European Spallation Source
Gigg, Martyn Anthony
committed
This file is part of Mantid.
Gigg, Martyn Anthony
committed
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Gigg, Martyn Anthony
committed
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Gigg, Martyn Anthony
committed
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Gigg, Martyn Anthony
committed
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_KERNEL_DLL ThreadSafeLogStreamBuf : public Poco::LogStreamBuf {
Gigg, Martyn Anthony
committed
public:
/// Constructor
ThreadSafeLogStreamBuf(Poco::Logger &logger,
Poco::Message::Priority priority);
int overflow(char c);
using Poco::LogStreamBuf::overflow;
Gigg, Martyn Anthony
committed
private:
/// Overridden fron base to write to the device in a thread-safe manner.
int writeToDevice(char c) override;
Gigg, Martyn Anthony
committed
private:
/// Store a map of thread indices to messages
std::map<Poco::Thread::TID, std::string> m_messages;
/// mutex protecting logstream
Gigg, Martyn Anthony
committed
};
/**
The base class for ThreadSafeLogStream.
From Poco/Foundation/LogStream.h - This class is needed to ensure the correct
initialization
Gigg, Martyn Anthony
committed
order of the stream buffer and base classes.
*/
class MANTID_KERNEL_DLL ThreadSafeLogIOS : public virtual std::ios {
Gigg, Martyn Anthony
committed
public:
/// Constructor
ThreadSafeLogIOS(Poco::Logger &logger, Poco::Message::Priority priority);
Gigg, Martyn Anthony
committed
// Return a pointer to the stream buffer object
Gigg, Martyn Anthony
committed
protected:
/// The log stream buffer object
ThreadSafeLogStreamBuf m_buf;
};
/**
The main log stream class implementing an ostream interface to a Logger.
Nearly identical to the Poco::LogStream class but instead uses thread-safe
buffering required for Mantid
Gigg, Martyn Anthony
committed
The stream's buffer appends all characters written to it
to a string. As soon as a CR or LF (std::endl) is written,
the string is sent to the Poco::Logger, with the current
priority.
Gigg, Martyn Anthony
committed
Usage example:
ThreadSafeLogStream ls(somePoco::Logger);
ls << "Some informational message\n";
ls.error() << "Some error message\n";
Gigg, Martyn Anthony
committed
*/
class MANTID_KERNEL_DLL ThreadSafeLogStream : public ThreadSafeLogIOS,
public std::ostream {
Gigg, Martyn Anthony
committed
public:
/// Creates the ThreadSafeLogStream, using the given logger and priority.
ThreadSafeLogStream(
Poco::Logger &logger,
Poco::Message::Priority priority = Poco::Message::PRIO_INFORMATION);
Gigg, Martyn Anthony
committed
/// Creates the ThreadSafeLogStream, using the logger identified
/// by loggerName, and sets the priority.
ThreadSafeLogStream(
const std::string &loggerName,
Poco::Message::Priority priority = Poco::Message::PRIO_INFORMATION);
/// Sets the priority for log messages to Poco::Message::PRIO_FATAL.
ThreadSafeLogStream &fatal();
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_FATAL
/// and writes the given message.
ThreadSafeLogStream &fatal(const std::string &message);
/// Sets the priority for log messages to Poco::Message::PRIO_CRITICAL.
ThreadSafeLogStream &critical();
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_CRITICAL
/// and writes the given message.
ThreadSafeLogStream &critical(const std::string &message);
/// Sets the priority for log messages to Poco::Message::PRIO_ERROR.
ThreadSafeLogStream &error();
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_ERROR
/// and writes the given message.
ThreadSafeLogStream &error(const std::string &message);
/// Sets the priority for log messages to Poco::Message::PRIO_WARNING.
ThreadSafeLogStream &warning();
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_WARNING
/// and writes the given message.
ThreadSafeLogStream &warning(const std::string &message);
/// Sets the priority for log messages to Poco::Message::PRIO_NOTICE.
ThreadSafeLogStream ¬ice();
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_NOTICE
/// and writes the given message.
ThreadSafeLogStream ¬ice(const std::string &message);
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_INFORMATION.
ThreadSafeLogStream &information();
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_INFORMATION
/// and writes the given message.
ThreadSafeLogStream &information(const std::string &message);
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_DEBUG.
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages to Poco::Message::PRIO_DEBUG
/// and writes the given message.
ThreadSafeLogStream &debug(const std::string &message);
Gigg, Martyn Anthony
committed
/// Sets the priority for log messages.
ThreadSafeLogStream &priority(Poco::Message::Priority priority);
Gigg, Martyn Anthony
committed
};
}
}
#endif // MANTID_KERNEL_THREADSAFELOGSTREAM