GammaLib
2.0.0
|
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... | |
GLog & | operator= (const GLog &log) |
Assignment operator. More... | |
void | operator() (const char *msgFormat,...) |
Message logging operator. More... | |
GLog & | operator<< (GLog &log) |
Insert logger into logger. More... | |
GLog & | operator<< (const std::string &str) |
Insert C++ string into logger. More... | |
GLog & | operator<< (const char *str) |
Insert C string into logger. More... | |
GLog & | operator<< (const char &value) |
Insert character value into logger. More... | |
GLog & | operator<< (const unsigned char &value) |
Insert unsigned character value into logger. More... | |
GLog & | operator<< (const bool &value) |
Insert bool into logger. More... | |
GLog & | operator<< (const int &value) |
Insert integer into logger. More... | |
GLog & | operator<< (const unsigned int &value) |
Insert unsigned integer into logger. More... | |
GLog & | operator<< (const double &value) |
Insert double precision value into logger. More... | |
GLog & | operator<< (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 GChatter & | chatter (void) const |
Return chattiness. More... | |
const GFilename & | filename (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... | |
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.
GLog::GLog | ( | void | ) |
GLog::GLog | ( | const GFilename & | filename, |
const bool & | clobber = false |
||
) |
Log file constructor.
[in] | filename | Name of log file. |
[in] | clobber | If 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.
[in] | log | Object from which the instance should be built. |
Definition at line 102 of file GLog.cpp.
References copy_members(), and init_members().
|
virtual |
|
protected |
Append string to the buffer.
[in] | string | String 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<<().
|
inline |
Return logger buffer.
Returns the buffer of the logger as string.
Definition at line 505 of file GLog.hpp.
References m_buffer.
Referenced by operator()().
|
inline |
Set the buffer size.
[in] | size | Buffer 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().
|
inline |
Return the 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.
|
inline |
|
inline |
|
inline |
Set chattiness.
[in] | chatter | Chattiness. |
Sets the chattiness that is applied to print() methods that are piped into the logger. The following chattiness values are available:
Definition at line 366 of file GLog.hpp.
References chatter(), and m_chatter.
Referenced by operator<<(), GApplication::set_log_chatter(), and GDaemon::start().
|
inline |
Return chattiness.
Returns the chattiness that is applied to print() methods that are piped into the logger. The following chattiness values are available:
Definition at line 477 of file GLog.hpp.
References m_chatter.
Referenced by chatter().
|
inline |
void GLog::clear | ( | void | ) |
Clear object.
Definition at line 456 of file GLog.cpp.
References free_members(), and init_members().
Referenced by GDaemon::init_members(), GTestSuites::init_members(), GTestSuite::init_members(), and GApplication::init_members().
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().
|
protected |
Copy class members.
[in] | log | Logger. |
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=().
|
inline |
Set standard output stream (cout) flag.
[in] | flag | Enable/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(), and GApplication::logFileOpen().
|
inline |
|
inline |
Set date flag that controls date prefixing.
[in] | flag | Enable/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().
|
inline |
Return date flag.
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.
|
inline |
Return 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.
[in] | force | If 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().
|
protected |
|
protected |
Write string as header into logger.
[in] | arg | Header string. |
[in] | level | Header 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().
|
inline |
|
inline |
Write string as header framed by '=' characters into logger.
[in] | arg | Header 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().
|
inline |
Write string as header framed by '-' characters into logger.
[in] | arg | Header 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().
|
inline |
Write string as header enclosed by '===' into logger.
[in] | arg | Header 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().
|
inline |
Set indentation.
[in] | indent | Number 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().
|
inline |
|
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=().
|
inline |
|
inline |
Signal that log file is open.
Definition at line 172 of file GLog.hpp.
References m_file.
Referenced by GApplication::logFileClose().
|
inline |
Set name to put into prefix.
[in] | name | Name 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.
|
inline |
void GLog::open | ( | const GFilename & | filename, |
const bool & | clobber = false |
||
) |
Open log file.
[in] | filename | Name of log file. |
[in] | clobber | If 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.
[in] | msgFormat | Pointer 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.
Insert logger into logger.
[in] | log | Logger 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 | ) |
GLog & GLog::operator<< | ( | const char * | str | ) |
GLog & GLog::operator<< | ( | const char & | value | ) |
GLog & GLog::operator<< | ( | const unsigned char & | value | ) |
GLog & GLog::operator<< | ( | const bool & | value | ) |
GLog & GLog::operator<< | ( | const int & | value | ) |
GLog & GLog::operator<< | ( | const unsigned int & | value | ) |
GLog & GLog::operator<< | ( | const double & | value | ) |
GLog & GLog::operator<< | ( | std::ostream &(*)(std::ostream &) | fn | ) |
Ostream function output operator.
[in] | fn | Ostream function. |
The function output operator is interpreted as a line feed operator.
Definition at line 437 of file GLog.cpp.
References append().
Assignment operator.
[in] | log | Object which should be assigned. |
Definition at line 139 of file GLog.cpp.
References copy_members(), free_members(), and init_members().
|
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.
Definition at line 474 of file GLog.cpp.
References m_buffer, and m_file.
Referenced by buffer_size(), and is_empty().
|
inline |
Return number of characters written through this logger.
Definition at line 160 of file GLog.hpp.
References m_written_size.
Referenced by GApplication::logFileClose().
|
protected |
Output string buffer.
Definition at line 137 of file GLog.hpp.
Referenced by append(), buffer(), copy_members(), flush(), init_members(), operator<<(), and size().
|
protected |
Chattiness for print() method.
Definition at line 138 of file GLog.hpp.
Referenced by chatter(), copy_members(), and init_members().
|
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().
|
protected |
Log file name.
Definition at line 135 of file GLog.hpp.
Referenced by copy_members(), filename(), init_members(), and open().
|
protected |
Indentation of text.
Definition at line 128 of file GLog.hpp.
Referenced by copy_members(), indent(), init_members(), and prefix().
|
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().
|
protected |
Maximum buffer length.
Definition at line 127 of file GLog.hpp.
Referenced by buffer_size(), copy_members(), flush(), and init_members().
|
protected |
Name for prefix.
Definition at line 136 of file GLog.hpp.
Referenced by copy_members(), init_members(), name(), and prefix().
|
protected |
Dump in standard error.
Definition at line 131 of file GLog.hpp.
Referenced by append(), cerr(), copy_members(), and init_members().
|
protected |
Dump in standard output.
Definition at line 130 of file GLog.hpp.
Referenced by append(), copy_members(), cout(), and init_members().
|
protected |
Dump date in prefix.
Definition at line 132 of file GLog.hpp.
Referenced by copy_members(), date(), init_members(), and prefix().
|
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().