GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GLog Class Reference

Information logger interface definition. More...

#include <GLog.hpp>

Public Member Functions

 GLog (void)
 Void constructor. More...
 
 GLog (const GFilename &filename, const bool &clobber=false)
 Log file constructor. More...
 
 GLog (const GLog &log)
 Copy constructor. More...
 
virtual ~GLog (void)
 Destructor. More...
 
GLogoperator= (const GLog &log)
 Assignment operator. More...
 
void operator() (const char *msgFormat,...)
 Message logging operator. More...
 
GLogoperator<< (GLog &log)
 Insert logger into logger. More...
 
GLogoperator<< (const std::string &str)
 Insert C++ string into logger. More...
 
GLogoperator<< (const char *str)
 Insert C string into logger. More...
 
GLogoperator<< (const char &value)
 Insert character value into logger. More...
 
GLogoperator<< (const unsigned char &value)
 Insert unsigned character value into logger. More...
 
GLogoperator<< (const bool &value)
 Insert bool into logger. More...
 
GLogoperator<< (const int &value)
 Insert integer into logger. More...
 
GLogoperator<< (const unsigned int &value)
 Insert unsigned integer into logger. More...
 
GLogoperator<< (const double &value)
 Insert double precision value into logger. More...
 
GLogoperator<< (std::ostream &(*fn)(std::ostream &))
 Ostream function output operator. More...
 
void clear (void)
 Clear object. More...
 
long int size (void) const
 Return the number of characters in the log file. More...
 
long int written_size (void) const
 Return number of characters written through this logger. More...
 
std::string classname (void) const
 Return class name. More...
 
bool is_open (void) const
 Signal that log file is open. More...
 
bool is_empty (void) const
 Signal if log file is empty. More...
 
void open (const GFilename &filename, const bool &clobber=false)
 Open log file. More...
 
void close (void)
 Close log file. More...
 
void flush (const bool &force=false)
 Flush string buffer into log file. More...
 
void date (const bool &flag)
 Set date flag that controls date prefixing. More...
 
void cout (const bool &flag)
 Set standard output stream (cout) flag. More...
 
void cerr (const bool &flag)
 Enables/disables logging into standard error stream. More...
 
void name (const std::string &name)
 Set name to put into prefix. More...
 
void buffer_size (const int &size)
 Set the buffer size. More...
 
void indent (const int &indent)
 Set indentation. More...
 
void chatter (const GChatter &chatter)
 Set chattiness. More...
 
void header0 (const std::string &arg)
 Write string as centred header into logger. More...
 
void header1 (const std::string &arg)
 Write string as header framed by '=' characters into logger. More...
 
void header2 (const std::string &arg)
 Write string as header framed by '-' characters into logger. More...
 
void header3 (const std::string &arg)
 Write string as header enclosed by '===' into logger. More...
 
const bool & date (void) const
 Return date flag. More...
 
const bool & cout (void) const
 Return standard output stream (cout) flag. More...
 
const bool & cerr (void) const
 Return standard error stream (cerr) flag. More...
 
const std::string & name (void) const
 Return prefix name. More...
 
const int & buffer_size (void) const
 Return the buffer size. More...
 
const int & indent (void) const
 Return indentation. More...
 
const GChatterchatter (void) const
 Return chattiness. More...
 
const GFilenamefilename (void) const
 Return log filename. More...
 
const std::string & buffer (void) const
 Return logger buffer. More...
 

Protected Member Functions

void init_members (void)
 Initialise class members. More...
 
void copy_members (const GLog &log)
 Copy class members. More...
 
void free_members (void)
 Delete class members. More...
 
void header (const std::string &arg, int level)
 Write string as header into logger. More...
 
std::string prefix (void) const
 Return prefix. More...
 
void append (const std::string &string)
 Append string to the buffer. More...
 

Protected Attributes

int m_max_length
 Maximum buffer length. More...
 
int m_indent
 Indentation of text. More...
 
long int m_written_size
 Number of char's written though this logger. More...
 
bool m_stdout
 Dump in standard output. More...
 
bool m_stderr
 Dump in standard error. More...
 
bool m_use_date
 Dump date in prefix. More...
 
bool m_linestart
 Signals that buffer is at line start. More...
 
FILE * m_file
 Log file pointer. More...
 
GFilename m_filename
 Log file name. More...
 
std::string m_name
 Name for prefix. More...
 
std::string m_buffer
 Output string buffer. More...
 
GChatter m_chatter
 Chattiness for print() method. More...
 

Detailed Description

Information logger interface definition.

This class implements an interface for logging of text messages to the standard output and/or error streams and into an ASCII log file. It provides a C++ style interface (allowing to stream messages using code such as

log << "This is a message";

and a C style interface that allows making use of C style text formating using code such as

log("These are %d messages with %d items.", n, m);

The logger interface implements an internal character buffer that is flushed once a maximum size parameter is exceeded. The buffer limit can be modified using the buffer_size() method; by default it is set to 8192 characters. The logger allows prepending of the current date and the task name.

Definition at line 62 of file GLog.hpp.

Constructor & Destructor Documentation

GLog::GLog ( void  )

Void constructor.

Definition at line 66 of file GLog.cpp.

References init_members().

GLog::GLog ( const GFilename filename,
const bool &  clobber = false 
)

Log file constructor.

Parameters
[in]filenameName of log file.
[in]clobberIf true overwrite any existing file, otherwise append.

Construct a logger with an open log file.

Definition at line 84 of file GLog.cpp.

References init_members(), and open().

GLog::GLog ( const GLog log)

Copy constructor.

Parameters
[in]logObject from which the instance should be built.

Definition at line 102 of file GLog.cpp.

References copy_members(), and init_members().

GLog::~GLog ( void  )
virtual

Destructor.

Definition at line 118 of file GLog.cpp.

References free_members().

Member Function Documentation

void GLog::append ( const std::string &  string)
protected

Append string to the buffer.

Parameters
[in]stringString to append

This method appends a string to the buffer and prepends, if required, the current date and name at the beginning of each line.

Definition at line 808 of file GLog.cpp.

References flush(), m_buffer, m_linestart, m_stderr, m_stdout, m_written_size, and prefix().

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

const std::string & GLog::buffer ( void  ) const
inline

Return logger buffer.

Returns
Logger buffer.

Returns the buffer of the logger as string.

Definition at line 505 of file GLog.hpp.

References m_buffer.

Referenced by operator()().

void GLog::buffer_size ( const int &  size)
inline

Set the buffer size.

Parameters
[in]sizeBuffer size.

Set the number of characters allowed in the internal buffer before the buffer is flushed into the specified streams or file.

Definition at line 327 of file GLog.hpp.

References m_max_length, and size().

Referenced by GTestSuites::init_members(), and GTestSuite::init_members().

const int & GLog::buffer_size ( void  ) const
inline

Return the buffer size.

Returns
Buffer size.

Returns the number of characters that are allowed in the internal buffer before the buffer is flushed into the specified streams or file.

Definition at line 442 of file GLog.hpp.

References m_max_length.

void GLog::cerr ( const bool &  flag)
inline

Enables/disables logging into standard error stream.

Parameters
[in]flagEnable/disable logging (true/false).

The cerr flag specifies whether the logging should go in addition to the log file also into the standard error stream.

Definition at line 291 of file GLog.hpp.

References m_stderr.

const bool & GLog::cerr ( void  ) const
inline

Return standard error stream (cerr) flag.

Returns
True if cerr flag is set.

The cerr flag specifies whether the logging should go in addition to the log file also into the standard error stream.

Definition at line 412 of file GLog.hpp.

References m_stderr.

void GLog::chatter ( const GChatter chatter)
inline

Set chattiness.

Parameters
[in]chatterChattiness.

Sets the chattiness that is applied to print() methods that are piped into the logger. The following chattiness values are available:

  • SILENT: no logging will be performed
  • TERSE: minimum logging
  • NORMAL: normal level of logging
  • EXPLICIT: detailed information will be logged
  • VERBOSE: maximum information will be logged

Definition at line 366 of file GLog.hpp.

References chatter(), and m_chatter.

Referenced by operator<<(), GApplication::set_log_chatter(), and GDaemon::start().

const GChatter & GLog::chatter ( void  ) const
inline

Return chattiness.

Returns
Chattiness.

Returns the chattiness that is applied to print() methods that are piped into the logger. The following chattiness values are available:

  • SILENT: no logging will be performed
  • TERSE: minimum logging
  • NORMAL: normal level of logging
  • EXPLICIT: detailed information will be logged
  • VERBOSE: maximum information will be logged

Definition at line 477 of file GLog.hpp.

References m_chatter.

Referenced by chatter().

std::string GLog::classname ( void  ) const
inline

Return class name.

Returns
String containing the class name ("GLog").

Definition at line 148 of file GLog.hpp.

void GLog::clear ( void  )
void GLog::close ( void  )

Close log file.

Close log file. This method even works if no file has been opened. In any case flushing of the buffer is enforced.

Definition at line 538 of file GLog.cpp.

References flush(), and m_file.

Referenced by GTestSuites::free_members(), free_members(), GTestSuite::free_members(), GApplication::logFileClose(), open(), operator<<(), and GDaemon::start().

void GLog::copy_members ( const GLog log)
protected

Copy class members.

Parameters
[in]logLogger.

Copy all class members from log to the current logger instance. This method will flush the buffer of log before copying. It will duplicate the file pointer on the log file so that the copy can safely work on the file without having to worry that the file pointer goes out of scope.

Definition at line 600 of file GLog.cpp.

References flush(), log(), m_buffer, m_chatter, m_file, m_filename, m_indent, m_linestart, m_max_length, m_name, m_stderr, m_stdout, and m_use_date.

Referenced by GLog(), and operator=().

void GLog::cout ( const bool &  flag)
inline

Set standard output stream (cout) flag.

Parameters
[in]flagEnable/disable logging (true/false).

The cout flag specifies whether the logging should go in addition to the log file also into the standard output stream.

Definition at line 275 of file GLog.hpp.

References m_stdout.

Referenced by GTestSuite::cout(), GTestSuites::cout(), GTestSuites::init_members(), GApplication::logFileOpen(), and GCOMDris::vetorate_fit().

const bool & GLog::cout ( void  ) const
inline

Return standard output stream (cout) flag.

Returns
True if cout flag is set.

The cout flag specifies whether the logging should go in addition to the log file also into the standard output stream.

Definition at line 397 of file GLog.hpp.

References m_stdout.

void GLog::date ( const bool &  flag)
inline

Set date flag that controls date prefixing.

Parameters
[in]flagEnable/disable date flag (true/false).

The date flag specifies whether each new line will be prepended by the actual date when the writing of the line was started.

Definition at line 259 of file GLog.hpp.

References m_use_date.

Referenced by GDaemon::start().

const bool & GLog::date ( void  ) const
inline

Return date flag.

Returns
True if date flag is set.

The date flag specifies whether each new line will be prepended by the actual date when the writing of the line was started.

Definition at line 382 of file GLog.hpp.

References m_use_date.

const GFilename & GLog::filename ( void  ) const
inline

Return log filename.

Returns
Log filename.

Returns the filename of the log file.

Definition at line 491 of file GLog.hpp.

References m_filename.

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

void GLog::flush ( const bool &  force = false)

Flush string buffer into log file.

Parameters
[in]forceIf true, force flushing.

Flush string buffer if it is full or if flushing is enforced.

This method writes the output string in the relevant streams. It decomposes the buffer in lines that a separated by a '
' character. The following streams are currently implemented (and will be filled in parallel): stdout, stderr, and - if available - an ASCII file.

Definition at line 665 of file GLog.cpp.

References m_buffer, m_file, m_linestart, and m_max_length.

Referenced by append(), close(), copy_members(), free_members(), operator<<(), and GDaemon::start().

void GLog::free_members ( void  )
protected

Delete class members.

Deletes class member after flushing the buffer and closing the log file.

Definition at line 640 of file GLog.cpp.

References close(), and flush().

Referenced by clear(), operator=(), and ~GLog().

void GLog::header ( const std::string &  arg,
int  level 
)
protected

Write string as header into logger.

Parameters
[in]argHeader string.
[in]levelHeader level (0,1,2,3).

Writes a string as header into the logger. Various header levels exist: 0: 80 character wide centred header 1: Header framed by '=' characters 2: Header framed by '-' characters 3: Header enclosed by '===' string

Definition at line 725 of file GLog.cpp.

References append(), and gammalib::fill().

Referenced by header0(), header1(), header2(), and header3().

void GLog::header0 ( const std::string &  arg)
inline

Write string as centred header into logger.

Parameters
[in]argHeader string.

Writes a string as 80 character wide centred header.

Definition at line 198 of file GLog.hpp.

References header().

void GLog::header1 ( const std::string &  arg)
inline

Write string as header framed by '=' characters into logger.

Parameters
[in]argHeader string.

Writes a string as header framed by '=' characters into logger.

Definition at line 213 of file GLog.hpp.

References header().

Referenced by GApplication::log_header1(), and GApplication::log_parameters().

void GLog::header2 ( const std::string &  arg)
inline

Write string as header framed by '-' characters into logger.

Parameters
[in]argHeader string.

Writes a string as header framed by '-' characters into logger.

Definition at line 228 of file GLog.hpp.

References header().

Referenced by GApplication::log_header2().

void GLog::header3 ( const std::string &  arg)
inline

Write string as header enclosed by '===' into logger.

Parameters
[in]argHeader string.

Writes a string as header enclosed by '===' into logger.

Definition at line 243 of file GLog.hpp.

References header().

Referenced by GApplication::log_header3().

void GLog::indent ( const int &  indent)
inline

Set indentation.

Parameters
[in]indentNumber of indentation characters.

Sets the number of whitespace characters that are added after the prefix and before the log message content for each line.

Indentation allows for easy formatting of text in the log file.

Definition at line 345 of file GLog.hpp.

References indent(), and m_indent.

Referenced by GApplication::log_header(), and GApplication::log_trailer().

const int & GLog::indent ( void  ) const
inline

Return indentation.

Returns
Number of indentation characters.

Returns the number of whitespace characters that are added after the prefix and before the log message content for each line.

Definition at line 457 of file GLog.hpp.

References m_indent.

Referenced by indent().

void GLog::init_members ( void  )
protected

Initialise class members.

Definition at line 569 of file GLog.cpp.

References GFilename::clear(), m_buffer, m_chatter, m_file, m_filename, m_indent, m_linestart, m_max_length, m_name, m_stderr, m_stdout, m_use_date, m_written_size, and NORMAL.

Referenced by clear(), GLog(), and operator=().

bool GLog::is_empty ( void  ) const
inline

Signal if log file is empty.

Returns
True if log file is empty, false otherwise.

Definition at line 184 of file GLog.hpp.

References size().

bool GLog::is_open ( void  ) const
inline

Signal that log file is open.

Returns
True if log file is open, false otherwise.

Definition at line 172 of file GLog.hpp.

References m_file.

Referenced by GApplication::logFileClose().

void GLog::name ( const std::string &  name)
inline

Set name to put into prefix.

Parameters
[in]nameName to put into prefix.

Sets the name that will be prefixed to each new line. If the date flag is true, the name will be inserted after the date at the beginning of each line.

Specifying an empty string will suppress the insertion of the name into the logger.

Definition at line 311 of file GLog.hpp.

References m_name, and name().

const std::string & GLog::name ( void  ) const
inline

Return prefix name.

Returns
Name to put into prefix.

Returns the name that will be prefixed to each new line. If the name is empty, nothing will be prefixed.

Definition at line 427 of file GLog.hpp.

References m_name.

Referenced by name().

void GLog::open ( const GFilename filename,
const bool &  clobber = false 
)

Open log file.

Parameters
[in]filenameName of log file.
[in]clobberIf true overwrite any existing file, otherwise append.

Opens a file for logging. If a log file was already open it is closed before opening a new file.

Definition at line 505 of file GLog.cpp.

References close(), filename(), m_file, m_filename, and GFilename::url().

Referenced by GLog(), GApplication::logFileOpen(), operator<<(), and GDaemon::start().

void GLog::operator() ( const char *  msgFormat,
  ... 
)

Message logging operator.

Parameters
[in]msgFormatPointer to message format.
[in]...Message to put into log.

This operator provides message logging capabilities in C style. It expects a C-style format string followed by an arbitrary number of arguments that is used to fill the format.

Todo:
A static buffer of length 8192 is allocated. Although this is likely sufficient for any normal application there is nothing that prevents in principle the overflow of the buffer. A more sophisticated scheme should be implemented.

Definition at line 175 of file GLog.cpp.

References append(), and buffer().

GLog & GLog::operator<< ( GLog log)

Insert logger into logger.

Parameters
[in]logLogger to be inserted.

Insert a logger object into another logger object. This method handles information that has either been written into a file, or that is still present in the logger buffer.

Definition at line 204 of file GLog.cpp.

References close(), filename(), flush(), m_buffer, m_file, open(), and GFilename::url().

GLog & GLog::operator<< ( const std::string &  str)

Insert C++ string into logger.

Parameters
[in]strC++ string to be inserted.

This method insert a C++ string into the logger.

Definition at line 261 of file GLog.cpp.

References append().

GLog & GLog::operator<< ( const char *  str)

Insert C string into logger.

Parameters
[in]strC string to be inserted.

This method insert a C string into the logger.

Definition at line 278 of file GLog.cpp.

References append().

GLog & GLog::operator<< ( const char &  value)

Insert character value into logger.

Parameters
[in]valueCharacter value to be inserted.

This method inserts a character value into the logger.

Definition at line 295 of file GLog.cpp.

References append().

GLog & GLog::operator<< ( const unsigned char &  value)

Insert unsigned character value into logger.

Parameters
[in]valueUnsigned character value to be inserted.

This method inserts an unsigned character value into the logger.

Definition at line 318 of file GLog.cpp.

References append().

GLog & GLog::operator<< ( const bool &  value)

Insert bool into logger.

Parameters
[in]valueBoolean to be inserted.

Depending on the argument, this method inserts either the character string "true" or "false" into the logger.

Definition at line 342 of file GLog.cpp.

References append().

GLog & GLog::operator<< ( const int &  value)

Insert integer into logger.

Parameters
[in]valueInteger to be inserted.

This method inserts an integer into the logger.

Definition at line 364 of file GLog.cpp.

References append().

GLog & GLog::operator<< ( const unsigned int &  value)

Insert unsigned integer into logger.

Parameters
[in]valueUnsigned integer to be inserted.

This method inserts an unsigned integer into the logger.

Definition at line 387 of file GLog.cpp.

References append().

GLog & GLog::operator<< ( const double &  value)

Insert double precision value into logger.

Parameters
[in]valueDouble precision value to be inserted.

This method inserts a double precision value into the logger.

Definition at line 410 of file GLog.cpp.

References append().

GLog & GLog::operator<< ( std::ostream &(*)(std::ostream &)  fn)

Ostream function output operator.

Parameters
[in]fnOstream function.

The function output operator is interpreted as a line feed operator.

Todo:
This is a quick an dirty implementation of the std::endl operator. A clean GLog::endl operator should be implemented instead, yet this needs some deeper understanding of our the

Definition at line 437 of file GLog.cpp.

References append().

GLog & GLog::operator= ( const GLog log)

Assignment operator.

Parameters
[in]logObject which should be assigned.

Definition at line 139 of file GLog.cpp.

References copy_members(), free_members(), and init_members().

std::string GLog::prefix ( void  ) const
protected

Return prefix.

Returns the prefix for each line.

Definition at line 772 of file GLog.cpp.

References gammalib::fill(), m_indent, m_name, m_use_date, and gammalib::strdate().

Referenced by append().

long int GLog::size ( void  ) const

Return the number of characters in the log file.

Returns
Number of characters in log file.

Definition at line 474 of file GLog.cpp.

References m_buffer, and m_file.

Referenced by buffer_size(), and is_empty().

long int GLog::written_size ( void  ) const
inline

Return number of characters written through this logger.

Returns
Number of characters written.

Definition at line 160 of file GLog.hpp.

References m_written_size.

Referenced by GApplication::logFileClose().

Member Data Documentation

std::string GLog::m_buffer
protected

Output string buffer.

Definition at line 137 of file GLog.hpp.

Referenced by append(), buffer(), copy_members(), flush(), init_members(), operator<<(), and size().

GChatter GLog::m_chatter
protected

Chattiness for print() method.

Definition at line 138 of file GLog.hpp.

Referenced by chatter(), copy_members(), and init_members().

FILE* GLog::m_file
protected

Log file pointer.

Definition at line 134 of file GLog.hpp.

Referenced by close(), copy_members(), flush(), init_members(), is_open(), open(), operator<<(), and size().

GFilename GLog::m_filename
protected

Log file name.

Definition at line 135 of file GLog.hpp.

Referenced by copy_members(), filename(), init_members(), and open().

int GLog::m_indent
protected

Indentation of text.

Definition at line 128 of file GLog.hpp.

Referenced by copy_members(), indent(), init_members(), and prefix().

bool GLog::m_linestart
protected

Signals that buffer is at line start.

Definition at line 133 of file GLog.hpp.

Referenced by append(), copy_members(), flush(), and init_members().

int GLog::m_max_length
protected

Maximum buffer length.

Definition at line 127 of file GLog.hpp.

Referenced by buffer_size(), copy_members(), flush(), and init_members().

std::string GLog::m_name
protected

Name for prefix.

Definition at line 136 of file GLog.hpp.

Referenced by copy_members(), init_members(), name(), and prefix().

bool GLog::m_stderr
protected

Dump in standard error.

Definition at line 131 of file GLog.hpp.

Referenced by append(), cerr(), copy_members(), and init_members().

bool GLog::m_stdout
protected

Dump in standard output.

Definition at line 130 of file GLog.hpp.

Referenced by append(), copy_members(), cout(), and init_members().

bool GLog::m_use_date
protected

Dump date in prefix.

Definition at line 132 of file GLog.hpp.

Referenced by copy_members(), date(), init_members(), and prefix().

long int GLog::m_written_size
protected

Number of char's written though this logger.

Definition at line 129 of file GLog.hpp.

Referenced by append(), init_members(), and written_size().


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