MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
marley::Logger::Message Class Reference

Temporary object used for forming logger messages. More...

#include <Logger.hh>

Public Member Functions

 Message (Message &&other)=default
 
 Message (std::vector< std::ostream * > &vec, std::mutex *mtx)
 
 operator bool () const
 
template<typename OutputType>
Messageoperator<< (const OutputType &out)
 
Messageoperator<< (std::ios_base &(*manip)(std::ios_base &))
 Backup overload for extra output manipulators.
 
Messageoperator<< (std::ostream &(*manip)(std::ostream &))
 Allows the Message to accept standard stream manipulators (like std::endl) via operator<<.
 
Messageoperator= (Message &&other)=default
 

Protected Attributes

std::ostringstream buffer_
 
std::mutex * mtx_
 
std::vector< std::ostream * > osvec_
 Active output streams that should receive the Message.
 

Detailed Description

Temporary object used for forming logger messages.

Upon destruction, a newline is appended to the message. This is based on a trick from https://stackoverflow.com/a/57553824

Definition at line 172 of file Logger.hh.

Constructor & Destructor Documentation

◆ Message()

marley::Logger::Message::Message ( std::vector< std::ostream * > & vec,
std::mutex * mtx )
inline

Definition at line 176 of file Logger.hh.

177 : osvec_( vec ), mtx_( mtx ) {}
std::vector< std::ostream * > osvec_
Active output streams that should receive the Message.
Definition Logger.hh:231
std::mutex * mtx_
Definition Logger.hh:235

◆ ~Message()

marley::Logger::Message::~Message ( )
inline

Definition at line 190 of file Logger.hh.

190 {
191 // If we have no active OutStreams, then just destroy the Message
192 // without any output
193 if ( osvec_.empty() ) return;
194 // Otherwise, append a new line, apply a lock to protect against
195 // activity from other threads, and write to each active OutStream
196 buffer_ << '\n';
197 std::string msg = buffer_.str();
198 std::lock_guard< std::mutex > lock( *mtx_ );
199 for ( auto& s : osvec_ ) *s << msg;
200 }
std::ostringstream buffer_
Definition Logger.hh:228

Member Function Documentation

◆ operator bool()

marley::Logger::Message::operator bool ( ) const
inlineexplicit

Definition at line 188 of file Logger.hh.

188{ return true; }

◆ operator<<() [1/3]

template<typename OutputType>
Message & marley::Logger::Message::operator<< ( const OutputType & out)
inline

Definition at line 202 of file Logger.hh.

204 {
205 // If we have no active OutStreams, then don't both to store the
206 // streaming output (since it will not be sent anywhere by the
207 // destructor
208 if ( !osvec_.empty() ) buffer_ << out;
209 return *this;
210 }

◆ operator<<() [2/3]

marley::Logger::Message & marley::Logger::Message::operator<< ( std::ios_base &(* manip )(std::ios_base &))

Backup overload for extra output manipulators.

Definition at line 326 of file Logger.cc.

328{
329 buffer_ << manip;
330 return *this;
331}

References buffer_.

◆ operator<<() [3/3]

marley::Logger::Message & marley::Logger::Message::operator<< ( std::ostream &(* manip )(std::ostream &))

Allows the Message to accept standard stream manipulators (like std::endl) via operator<<.

Note
Code for this function is based on a trick discussed here: http://www.cplusplus.com/forum/general/54588/#msg294798

Definition at line 319 of file Logger.cc.

321{
322 buffer_ << manip;
323 return *this;
324}

References buffer_.

Member Data Documentation

◆ buffer_

std::ostringstream marley::Logger::Message::buffer_
protected

Used to cache output from multiple calls to operator<< until we have the full message. Then we just emit it in a single-step for each active OutStream, all at once. This together with a std::mutex prevents splitting logging messages due to race conditions between parallel threads.

Definition at line 228 of file Logger.hh.

Referenced by operator<<(), and operator<<().

◆ mtx_

std::mutex* marley::Logger::Message::mtx_
protected

std::mutex to use to lock streaming output when emitting the final message

Definition at line 235 of file Logger.hh.

◆ osvec_

std::vector< std::ostream* > marley::Logger::Message::osvec_
protected

Active output streams that should receive the Message.

Definition at line 231 of file Logger.hh.


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