Feel++ 0.111.0-preview.14
Finite Element Embedded Library in C++
Feel::MasterStream Class Reference

Output Stream that outputs only on master rank of a worldcomm. More...

+ Collaboration diagram for Feel::MasterStream:

Public Types

using LogLevel = Logger::Level
 

Public Member Functions

void attachWorldComm (std::shared_ptr< WorldComm > &_wc)
 
void close ()
 provides close() from std::ofstream Only closes on master rank when MPI is active
 
void flush () const
 Explicit flush method.
 
 MasterStream (std::ostream &_out, std::shared_ptr< WorldComm > _wc=nullptr, LogLevel _log_level=Logger::Level::info)
 Construct a MasterStream from a std::ostream and optionally a WorldComm. More...
 
template<typename... Args>
void open (Args... args)
 variadic template function to provide open() from std::ofstream Only opens on master rank when MPI is active
 
template<typename T >
const MasterStream & operator<< (const T &v) const
 Output operator for generic types Buffers output and sends to both stream and logging backend on flush.
 
MasterStream const & operator<< (std::ostream &(*F)(std::ostream &)) const
 Output operator for stream manipulators (std::endl, std::flush, etc.) Flushes buffer to both output stream and logging backend.
 
void setLogLevel (LogLevel level)
 Set the log level for this stream.
 
std::string str () const
 provide an interface to str() for ostringstream More...
 
std::shared_ptr< WorldComm > worldCommPtr () const
 

Protected Attributes

std::ostringstream buffer
 Internal buffer for line-based output.
 
LogLevel log_level
 Log severity level.
 
std::ostream & out
 Underlying output stream.
 
std::shared_ptr< WorldComm > wc
 WorldComm for MPI rank checking.
 

Detailed Description

Output Stream that outputs only on master rank of a worldcomm.

The first application is to use the instantiation of MasterStream cout, cerr or clog to default output on master rank process of a Feel++ application

Environment env(...); // initialize Feel++ environment
// cout only on master rank process
cout << "Hello World from process " << Environment::rank() << std::endl;
static rank_type rank()
return the rank in global mpi communicator
Definition: environment.hpp:346

Behavior based on MPI state:

  • Before MPI initialization: outputs on all processes (typically just one)
  • After MPI initialization: outputs only on master rank
  • After MPI finalization: outputs on all processes (to avoid MPI crashes)
  • No MPI available: always outputs

Logging integration:

  • When logging is enabled (via spdlog or glog), output is also sent to LOG(INFO/WARNING/ERROR)
  • This provides automatic logging of all Feel::cout/cerr output
  • Logging happens on flush (std::endl, std::flush, or explicit flush())

This ensures MasterStream is safe to use throughout the entire program lifecycle, including during global object construction/destruction.

Constructor & Destructor Documentation

◆ MasterStream()

Feel::MasterStream::MasterStream ( std::ostream &  _out,
std::shared_ptr< WorldComm >  _wc = nullptr,
LogLevel  _log_level = Logger::Level::info 
)

Construct a MasterStream from a std::ostream and optionally a WorldComm.

If no WorldComm is provided, it will be retrieved from Environment when needed.

Parameters
_outthe underlying output stream (std::cout, std::cerr, etc.)
_wcoptional WorldComm pointer (defaults to nullptr, retrieved lazily)
_log_levelthe log level for logging backend (info, warn, err)

Member Function Documentation

◆ str()

std::string Feel::MasterStream::str ( ) const

provide an interface to str() for ostringstream

Returns
the current buffer content

◆ worldCommPtr()

std::shared_ptr< WorldComm > Feel::MasterStream::worldCommPtr ( ) const
Returns
the shared_ptr WorldComm