Chaste  Release::2018.1
LogFile Class Reference

#include <LogFile.hpp>

+ Collaboration diagram for LogFile:

Public Member Functions

void Set (unsigned level, const std::string &rDirectory, const std::string &rFileName="log.txt")
 
void SetPrecision (unsigned precision)
 
void WriteHeader (std::string simulationType="")
 
void WriteElapsedTime (std::string pre="")
 
bool IsFileSet ()
 
template<class T >
LogFileoperator<< (T message)
 

Static Public Member Functions

static LogFileInstance ()
 
static unsigned Level ()
 
static unsigned MaxLoggingLevel ()
 
static void Close ()
 

Private Member Functions

 LogFile ()
 

Private Attributes

bool mFileSet
 
out_stream mpOutStream
 
time_t mInitTime
 
unsigned mLevel
 
unsigned mPrecision
 

Static Private Attributes

static LogFilempInstance = nullptr
 
static const unsigned mMaxLoggingLevel = 2
 

Detailed Description

A singleton log file class. Allows the user to define log file in the test, which can be written to (without being passed around in the code).

Usage (in test):

 // beginning of test
 LogFile* p_log = LogFile::Instance();
 p_log->Set(level, "dir","file");
 p_log->WriteHeader("type_of_sim"); // optional

 // at end of simulation
 LogFile::Close();
*

Here 'level' is a number between 0 and LogFile::MaxLoggingLevel, with zero meaning no logging and MaxLoggingLevel meaning full logging.

Usage (in source) - use the macro 'LOG'

 LOG(1, "Info to be written to the log file\n" << "More info\n");
 LogFile::Instance()->WriteElapsedTime(); // optional
* 

This checks whether the given level (here '1') is greater or equal to the given logging level, in which case it writes to the current log file. If there is no log file set up it does nothing.

Note the log file can be written to directly, without any level-checking, using

 (*LogFile::Instance()) << "Info to be written to the log file\n";
* 

Definition at line 80 of file LogFile.hpp.

Constructor & Destructor Documentation

LogFile::LogFile ( )
private

Constructor. Should never be called directly, call LogFile::Instance() instead.

Definition at line 44 of file LogFile.cpp.

Referenced by Instance().

Member Function Documentation

void LogFile::Close ( )
static

Close the currently open file, and delete the single LogFile instance.

Definition at line 101 of file LogFile.cpp.

References mpInstance, and mpOutStream.

Referenced by CardiacElectroMechanicsProblem< DIM, ELEC_PROB_DIM >::~CardiacElectroMechanicsProblem().

LogFile * LogFile::Instance ( )
static
Returns
the single instance of the LogFile object.

Definition at line 52 of file LogFile.cpp.

References LogFile(), and mpInstance.

Referenced by CardiacElectroMechanicsProblem< DIM, ELEC_PROB_DIM >::Initialise(), and CardiacElectroMechanicsProblem< DIM, ELEC_PROB_DIM >::Solve().

bool LogFile::IsFileSet ( )
Returns
true if Set() has been called.

Definition at line 129 of file LogFile.cpp.

References mFileSet.

unsigned LogFile::Level ( )
static
Returns
the logging level.

Definition at line 61 of file LogFile.cpp.

References mLevel, and mpInstance.

unsigned LogFile::MaxLoggingLevel ( )
static
Returns
the maximum allowed logging level.

Definition at line 96 of file LogFile.cpp.

References mMaxLoggingLevel.

template<class T >
LogFile& LogFile::operator<< ( message)
inline
Returns
reference to this object (as convention) Overloaded << operator, to write to the log file, if one has been set, and does nothing if not.
Parameters
messagethe message to write to the log file

Definition at line 187 of file LogFile.hpp.

References mFileSet, and mPrecision.

void LogFile::Set ( unsigned  level,
const std::string &  rDirectory,
const std::string &  rFileName = "log.txt" 
)

Set the logging level, the directory (relative to TEST_OUTPUT) and the file the log should be written to (file defaults to "log.txt").

The level should be a number between 0 and LogFile::MaxLoggingLevel() (which is the same as LogFile::mMaxLoggingLevel)

Note: we intentionally do NOT check or throw an exception if a file has already been set (i.e. Close() wasn't called the last time a log was used).

The directory is never cleaned.

Parameters
levelthe logging level
rDirectorythe directory in which to write the data to file
rFileNamethe name of the file to write to, relative to the output directory

Definition at line 73 of file LogFile.cpp.

References EXCEPTION, mFileSet, mLevel, mMaxLoggingLevel, mpOutStream, and OutputFileHandler::OpenOutputFile().

Referenced by CardiacElectroMechanicsProblem< DIM, ELEC_PROB_DIM >::Initialise().

void LogFile::SetPrecision ( unsigned  precision)

Set the precision to write data (the 'decimal precision', look up documentation for std::setprecision()).

Parameters
precisionthe precision

Definition at line 134 of file LogFile.cpp.

References mPrecision.

void LogFile::WriteElapsedTime ( std::string  pre = "")

Write the elapsed time since the simulation began (since the log file was created).

Parameters
prea string (eg spacings) to write before the elapsed time line.

Definition at line 116 of file LogFile.cpp.

References mInitTime.

Referenced by CardiacElectroMechanicsProblem< DIM, ELEC_PROB_DIM >::Solve().

void LogFile::WriteHeader ( std::string  simulationType = "")

Write a header in the log file, stating the (given) type of simulation and the date and time.

Parameters
simulationTypeThe type of simulation, eg "Bidomain" or "Crypt" or "Cardiac Electromechanics". Defaults to empty.

Definition at line 111 of file LogFile.cpp.

References mInitTime.

Referenced by CardiacElectroMechanicsProblem< DIM, ELEC_PROB_DIM >::Initialise().

Member Data Documentation

bool LogFile::mFileSet
private

Whether a directory and filename has been set.

Definition at line 88 of file LogFile.hpp.

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

time_t LogFile::mInitTime
private

The current current calendar time.

Definition at line 94 of file LogFile.hpp.

Referenced by WriteElapsedTime(), and WriteHeader().

unsigned LogFile::mLevel
private

The level of logging required for this particular log file.

Definition at line 97 of file LogFile.hpp.

Referenced by Level(), and Set().

const unsigned LogFile::mMaxLoggingLevel = 2
staticprivate

The max level of logging.

Definition at line 100 of file LogFile.hpp.

Referenced by MaxLoggingLevel(), and Set().

LogFile * LogFile::mpInstance = nullptr
staticprivate

The static single instance.

Definition at line 85 of file LogFile.hpp.

Referenced by Close(), Instance(), and Level().

out_stream LogFile::mpOutStream
private

The file to be written to.

Definition at line 91 of file LogFile.hpp.

Referenced by Close(), and Set().

unsigned LogFile::mPrecision
private

The precision with which to output data.

Definition at line 103 of file LogFile.hpp.

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


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