Thea
Public Types | Public Member Functions | List of all members
LockedOutputStream< StreamT > Class Template Reference

A temporary object that locks an output stream on construction and unlocks it (after optionally writing a newline) on destruction. More...

#include <Log.hpp>

Inheritance diagram for LockedOutputStream< StreamT >:
Noncopyable

Public Types

typedef StreamT StreamType
 The type of wrapped output stream. More...
 

Public Member Functions

StreamT & getStream ()
 Get the locked stream. More...
 
 LockedOutputStream (StreamT &stream_, bool append_newline_=false)
 Constructor. More...
 
 LockedOutputStream (StreamT &stream_, std::string const &prefix, bool append_newline_=false)
 Constructor. More...
 
 ~LockedOutputStream ()
 Destructor. More...
 

Detailed Description

template<typename StreamT = std::basic_ostream<char>>
class Thea::LockedOutputStream< StreamT >

A temporary object that locks an output stream on construction and unlocks it (after optionally writing a newline) on destruction.

All objects piped to the object in a single line are written atomically. Useful e.g. for writing log messages to the same file/console from multiple threads.

Example:

LockedOutputStream<>(std::cout).getStream() << "This " << " line " << " will " << " be " << " written " << " atomically";
Note
Currently, all objects of this class share the same lock, regardless of the wrapped stream.

Definition at line 52 of file Log.hpp.

Member Typedef Documentation

typedef StreamT StreamType

The type of wrapped output stream.

Definition at line 55 of file Log.hpp.

Constructor & Destructor Documentation

LockedOutputStream ( StreamT &  stream_,
bool  append_newline_ = false 
)

Constructor.

Definition at line 58 of file Log.hpp.

LockedOutputStream ( StreamT &  stream_,
std::string const &  prefix,
bool  append_newline_ = false 
)

Constructor.

Writes a prefix to the stream after acquiring the output lock.

Definition at line 65 of file Log.hpp.

Destructor.

Writes a newline to the output stream if the object was constructed with append_newline = true.

Definition at line 73 of file Log.hpp.

Member Function Documentation

StreamT& getStream ( )

Get the locked stream.

Definition at line 82 of file Log.hpp.


The documentation for this class was generated from the following file: