Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
Logger Class Reference

#include <logger.h>

Public Member Functions

 Logger ()
 ~Logger ()
void clear ()
bool close ()
bool flush (bool verbose)
std::stringstream & getStream ()
bool open (MPI_Comm comm, const int &MASTERRANK, const std::string &fname, const bool &append=false)
 Open a logfile.
bool print (const std::string &s)
std::string str ()
template<typename T>
Loggeroperator<< (const T &value)
Loggeroperator<< (Logger &(*pf)(Logger &))
Loggeroperator<< (std::ostream &(*pf)(std::ostream &))

Private Attributes

bool fileOpen
int mpiRank
int masterRank
std::stringstream outStream
std::fstream * masterStream

Detailed Description

A class for writing log messages in parallel. Logger functions in the same way as standard C++ input streams, i.e. you insert values by using the stream input operator <<.

There is one major difference between Logger and standard C++ input streams: the contents of Logger are not flushed/written into the output file when endl or flush are inserted. Instead, one has to insert manipulator write, defined in logger.h, into the stream.

This is because Logger actually uses a stringstream buffer internally, and C++ standard manipulators are just passed on to the stringstream as-is. It is not possible to detect when endl or flush are inserted, because they have the same function signature as other manipulators (such as showpos). In principle it is possible to define new manipulators endl and flush here in the same manner as write, but then wrapper functions would need to be written for all standard C++ manipulators because compilers think that std::endl and user-defined endl as ambiguous, i.e. compilers cannot decide which function should be linked.

Definition at line 49 of file logger.h.

Constructor & Destructor Documentation

◆ Logger()

Logger::Logger ( )

Default constructor for class Logger.

Definition at line 32 of file logger.cpp.

Here is the caller graph for this function:

◆ ~Logger()

Logger::~Logger ( )

Destructor for class Logger. Destructor only calls Logger::close.

Definition at line 39 of file logger.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ clear()

void Logger::clear ( )
inline

Definition at line 54 of file logger.h.

◆ close()

bool Logger::close ( )

Close a logfile which has been previously opened with Logger::open.

Returns
If true, the logfile was closed successfully.

Definition at line 51 of file logger.cpp.

Here is the caller graph for this function:

◆ flush()

bool Logger::flush ( bool verbose)

Write the Logger stream buffer into the output file. The stream buffer is emptied if the write succeeded. The rank of the writing MPI process and current time and date, as given by ctime, are written before the user-defined message.

Returns
If true, the buffer was written successfully and buffer was emptied.

Definition at line 67 of file logger.cpp.

Here is the caller graph for this function:

◆ getStream()

std::stringstream & Logger::getStream ( )
inline

Definition at line 57 of file logger.h.

◆ open()

bool Logger::open ( MPI_Comm comm,
const int & MASTERRANK,
const std::string & fname,
const bool & append = false )

Open a logfile.

This class can be used to write out logfiles. Only master rank writes out anythin, all other processes will not do anything when they attempt to use the logger, but it is still perfectly legal for them to call it.

Parameters
[in]commMPI communicator for processes that are able to call this logger
[in]MASTERRANKThe rank that does all communication.
[in]fnameThe name of the logfile.
[in]appendIs the file appended (true), or overwritten (false)
Returns
If true, the file was opened successfully and Logger is ready for use.

Definition at line 117 of file logger.cpp.

◆ operator<<() [1/3]

template<typename T>
Logger & Logger::operator<< ( const T & value)
inline

Stream insertion operator for inserting values to the input stream. The given value is passed on to internal stream buffer as-is.

Parameters
valueThe value to be inserted into the stream.
Returns
A reference to Logger.

Definition at line 84 of file logger.h.

Here is the call graph for this function:

◆ operator<<() [2/3]

Logger & Logger::operator<< ( Logger &(* pf )(Logger &))

Function which allows one to insert C++-style stream manipulators, defined inside Logger, to be used in the input stream. This function gets mainly called when manipulator write is inserted to the stream.

Parameters
pfA pointer to a stream manipulator.
Returns
A reference to Logger.

Definition at line 151 of file logger.cpp.

Here is the call graph for this function:

◆ operator<<() [3/3]

Logger & Logger::operator<< ( std::ostream &(* pf )(std::ostream &))

Function which allows one to use C++ stream manipulators such as endl or showpos, with Logger. This is just a wrapper function which passes the manipulators to internal stream buffer as-is.

Parameters
pfFunction pointer to C++ stream manipulator.
Returns
Reference to Logger.

Definition at line 162 of file logger.cpp.

Here is the call graph for this function:

◆ print()

bool Logger::print ( const std::string & s)

Definition at line 135 of file logger.cpp.

◆ str()

std::string Logger::str ( )
inline

Definition at line 60 of file logger.h.

Member Data Documentation

◆ fileOpen

bool Logger::fileOpen
private

If true, the class has an open MPIFile.

Definition at line 71 of file logger.h.

◆ masterRank

int Logger::masterRank
private

MPI rank of the master process.

Definition at line 73 of file logger.h.

◆ masterStream

std::fstream* Logger::masterStream
private

Output stream for master process only.

Definition at line 75 of file logger.h.

◆ mpiRank

int Logger::mpiRank
private

The rank of the process using Logger within a user-defined communicator.

Definition at line 72 of file logger.h.

◆ outStream

std::stringstream Logger::outStream
private

Output buffer.

Definition at line 74 of file logger.h.


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