Flow123d  3.9.0-f06eab6ff
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Logger Class Reference

Class for storing logger messages. More...

#include <logger.hh>

Inheritance diagram for Logger:
Inheritance graph
[legend]
Collaboration diagram for Logger:
Collaboration graph
[legend]

Public Types

enum  MsgType {
  warning = 0, message = 1, log = 2, debug = 3,
  error = 4
}
 Enum of types of Logger messages. More...
 

Public Member Functions

 Logger (MsgType type)
 Constructor. More...
 
Loggerset_context (const char *file_name, const char *function, const int line)
 Stores values for printing out line number, function, etc. More...
 
Loggerevery_proc ()
 Set flag every_process_ to true. More...
 
template<class... T>
Loggerfmt (T &&... t)
 Allow use functionality of fmtlib for formating message. More...
 
 ~Logger ()
 Destructor. More...
 

Static Public Member Functions

static const std::string msg_type_string (MsgType msg_type, bool full_format=true)
 Return string value of given MsgType in full or shorter format (e.g. "WARNING" of "Wrn") More...
 

Private Member Functions

void set_mask ()
 Set streams_mask_ according to the type of message. More...
 
void print_to_screen (std::ostream &stream, std::stringstream &scr_stream, StreamMask mask)
 Print formated message to given screen stream if mask corresponds with streams_mask_. More...
 
void print_to_file (std::ofstream &stream, std::stringstream &file_stream, StreamMask mask)
 Print formated message to given file stream if mask corresponds with streams_mask_. More...
 
bool print_screen_header (std::stringstream &scr_stream)
 Print header to screen stream, helper method called from print_to_screen. More...
 
void print_file_header (std::stringstream &file_stream)
 Print header to file stream, helper method called from print_to_file. More...
 
std::string compact_file_name (std::string file_name)
 Return compact (relative) path to the given source file. More...
 

Private Attributes

std::stringstream cout_stream_
 Store messages printed to cout output stream. More...
 
std::stringstream cerr_stream_
 Store messages printed to cerr output stream. More...
 
std::stringstream file_stream_
 Store messages printed to file. More...
 
MsgType type_
 Type of message. More...
 
bool every_process_
 Flag marked if log message is printing for all processes or only for zero process. More...
 
std::string file_name_
 Actual file. More...
 
std::string function_
 Actual function. More...
 
int line_
 Actual line. More...
 
std::string date_time_
 Actual date and time. More...
 
int mpi_rank_
 Actual process (if MPI is supported) More...
 
StreamMask streams_mask_
 Mask of logger, specifies streams in actual time into which to be written. More...
 
StreamMask full_streams_mask_
 Mask of logger, specifies all streams into which to be written in logger message. More...
 

Friends

template<class T >
Loggeroperator<< (Logger &log, const T &x)
 
template<class T >
Loggeroperator<< (Logger &log, const std::vector< T > &vec)
 
Loggeroperator<< (Logger &log, std::ostream &(*pf)(std::ostream &))
 
Loggeroperator<< (Logger &log, StreamMask mask)
 

Detailed Description

Class for storing logger messages.

Allow define different levels of log messages and distinguish output streams for individual leves. These output streams are -

Logger distinguishes four type of levels -

File output is optional. See LoggerOptions for setting this output stream.

Example of Logger usage:

For individual levels are defined macros -

Logger message is created by using an operator << and allow to add each type that has override this operator. Message is terminated with manipulator std::endl. Implicitly logger message is printed only in processor with rank zero. If necessary printed message for all process, it provides a method every_proc().

Examples of logger messages formating:

MessageOut() << "End of simulation at time: " << secondary_eq->solved_time() << "\n";
WarningOut() << "Unprocessed keys '" << keys_vec << "'." << "\n";
LogOut() << "Write output to output stream: " << this->_base_filename << " for time: " << time << "\n";
DebugOut() << "Calling 'initialize' of empty equation '" << typeid(*this).name() << "'." << "\n";

See that output of vectors of printable objects is supported. Implementation of Logger does not support manipulator std::endl. Please, use "\n" instead. New line character "\n" can be used multiple times.

Logger allow using fmtlib functionality for simpler formatting of message:

MessageOut() << fmt::format("Start time: {}\nEnd time: {}\n", this->start_time(), this->end_time());
MessageOut().fmt("Start time: {}\nEnd time: {}\n", this->start_time(), this->end_time());

Messages are printed only on the zero MPI process by default. Parallel output on all processes must be required explicitly:

MessageOut().every_proc() << "Size distributed at process: " << distr->lsize() << "\n";

Definition at line 135 of file logger.hh.

Member Enumeration Documentation

◆ MsgType

Enum of types of Logger messages.

Rules:

  1. Warning and Error are the only messages that goes to CERR.
  2. All messages goes to the log file unless it is suppressed by the _no_log option if the user provides empty log file name.
  3. If the log file was not initialized YET all LOG output goes to COUT unless it already goes to CERR.
  4. 'message' goes to the COUT if LOG is suppressed.
  5. 'debug' goes to both COUT and LOG, but are suppresed by FLOW123D_DEBUG.
Enumerator
warning 
message 
log 
debug 
error 

Definition at line 150 of file logger.hh.

Constructor & Destructor Documentation

◆ Logger()

Logger::Logger ( MsgType  type)

Constructor.

Definition at line 64 of file logger.cc.

◆ ~Logger()

Logger::~Logger ( )

Destructor.

Definition at line 75 of file logger.cc.

Member Function Documentation

◆ compact_file_name()

std::string Logger::compact_file_name ( std::string  file_name)
private

Return compact (relative) path to the given source file.

Definition at line 230 of file logger.cc.

Here is the caller graph for this function:

◆ every_proc()

Logger & Logger::every_proc ( )

Set flag every_process_ to true.

Definition at line 110 of file logger.cc.

◆ fmt()

template<class... T>
Logger& Logger::fmt ( T &&...  t)
inline

Allow use functionality of fmtlib for formating message.

See examples in description of Logger class.

Definition at line 176 of file logger.hh.

◆ msg_type_string()

const std::string Logger::msg_type_string ( MsgType  msg_type,
bool  full_format = true 
)
static

Return string value of given MsgType in full or shorter format (e.g. "WARNING" of "Wrn")

Definition at line 85 of file logger.cc.

Here is the caller graph for this function:

◆ print_file_header()

void Logger::print_file_header ( std::stringstream &  file_stream)
private

Print header to file stream, helper method called from print_to_file.

Definition at line 259 of file logger.cc.

Here is the caller graph for this function:

◆ print_screen_header()

bool Logger::print_screen_header ( std::stringstream &  scr_stream)
private

Print header to screen stream, helper method called from print_to_screen.

Definition at line 240 of file logger.cc.

Here is the caller graph for this function:

◆ print_to_file()

void Logger::print_to_file ( std::ofstream &  stream,
std::stringstream &  file_stream,
StreamMask  mask 
)
private

Print formated message to given file stream if mask corresponds with streams_mask_.

Definition at line 200 of file logger.cc.

Here is the caller graph for this function:

◆ print_to_screen()

void Logger::print_to_screen ( std::ostream &  stream,
std::stringstream &  scr_stream,
StreamMask  mask 
)
private

Print formated message to given screen stream if mask corresponds with streams_mask_.

Definition at line 172 of file logger.cc.

Here is the caller graph for this function:

◆ set_context()

Logger & Logger::set_context ( const char *  file_name,
const char *  function,
const int  line 
)

Stores values for printing out line number, function, etc.

Definition at line 99 of file logger.cc.

◆ set_mask()

void Logger::set_mask ( )
private

Set streams_mask_ according to the type of message.

Definition at line 118 of file logger.cc.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator<< [1/4]

template<class T >
Logger& operator<< ( Logger log,
const std::vector< T > &  vec 
)
friend

Definition at line 242 of file logger.hh.

◆ operator<< [2/4]

template<class T >
Logger& operator<< ( Logger log,
const T &  x 
)
friend

Definition at line 259 of file logger.hh.

◆ operator<< [3/4]

Logger& operator<< ( Logger log,
std::ostream &(*)(std::ostream &)  pf 
)
friend

Definition at line 291 of file logger.cc.

◆ operator<< [4/4]

Logger& operator<< ( Logger log,
StreamMask  mask 
)
friend

implementation of operators

Definition at line 281 of file logger.cc.

Member Data Documentation

◆ cerr_stream_

std::stringstream Logger::cerr_stream_
private

Store messages printed to cerr output stream.

Definition at line 209 of file logger.hh.

◆ cout_stream_

std::stringstream Logger::cout_stream_
private

Store messages printed to cout output stream.

Definition at line 208 of file logger.hh.

◆ date_time_

std::string Logger::date_time_
private

Actual date and time.

Definition at line 217 of file logger.hh.

◆ every_process_

bool Logger::every_process_
private

Flag marked if log message is printing for all processes or only for zero process.

Definition at line 213 of file logger.hh.

◆ file_name_

std::string Logger::file_name_
private

Actual file.

Definition at line 214 of file logger.hh.

◆ file_stream_

std::stringstream Logger::file_stream_
private

Store messages printed to file.

Definition at line 210 of file logger.hh.

◆ full_streams_mask_

StreamMask Logger::full_streams_mask_
private

Mask of logger, specifies all streams into which to be written in logger message.

Definition at line 220 of file logger.hh.

◆ function_

std::string Logger::function_
private

Actual function.

Definition at line 215 of file logger.hh.

◆ line_

int Logger::line_
private

Actual line.

Definition at line 216 of file logger.hh.

◆ mpi_rank_

int Logger::mpi_rank_
private

Actual process (if MPI is supported)

Definition at line 218 of file logger.hh.

◆ streams_mask_

StreamMask Logger::streams_mask_
private

Mask of logger, specifies streams in actual time into which to be written.

Definition at line 219 of file logger.hh.

◆ type_

MsgType Logger::type_
private

Type of message.

Definition at line 212 of file logger.hh.


The documentation for this class was generated from the following files:
fmt::format
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
LogOut
#define LogOut()
Macro defining 'log' record of log.
Definition: logger.hh:281
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
DebugOut
#define DebugOut()
Macro defining 'debug' record of log.
Definition: logger.hh:284
MessageOut
#define MessageOut()
Macro defining 'message' record of log.
Definition: logger.hh:275