Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MCLogFile Class Reference

Implementation of a log file. More...

#include <mcllib/MCLogFile.h>

Inheritance diagram for MCLogFile:

MCRefCount List of all members.

Public Member Functions

bool isOutput (MCMsg::msglevel_t lvl) const
 Test to see if messages at a level will be output.
 MCLogFile (const MCString &fileName=MCString(0), MCMsg::msglevel_t lvl=MCMsg::MSGLVL_WARN, int maxSize=0, int maxRoll=MAX_ROLL, bool keepOpen=false)
 Construct a new log file.
void operator() (const MCMsg &msg)
 Output an MCMsg.
void operator() (MCMsg::msglevel_t lvl, const char *fmt,...)
 Output a message at the specified level.
void operator() (const char *fmt,...)
 Output a message at the default level.
void setDefaultLevel (MCMsg::msglevel_t lvl)
 Set the default level.
void setOutputFile (MCFile::EStdHandle hdl)
 Set the output file to a standard file handle.
void setOutputFile (const MCString &fileName)
 Set the output file.
void setOutputLevel (MCMsg::msglevel_t lvl)
 Set the output level.
 ~MCLogFile ()
 Close the log file.

Static Public Attributes

const int MAX_ROLL

Protected Member Functions

void init (const MCString &fileName=MCString(0), MCMsg::msglevel_t lvl=MCMsg::MSGLVL_WARN, int maxSize=0, int maxRoll=MAX_ROLL, bool keepOpen=false)
 Initialize for use in derived classes.
void operator() (MCMsg::msglevel_t lvl, const char *fmt, va_list ap)
 Output a message at the specified level.
void operator() (const char *fmt, va_list ap)
 Output a message at the default level.

Detailed Description

Implementation of a log file.

Log files allow messages to be output to them. To prevent disk space exhaustion, log files can have a maximum size specified when they are created. If output of a message causes the maximum size to be exceeded, the log file can be optionally "rolled". When a log file is rolled, the log file is closed and renamed to logfile.1. If logfile.1 exists it will be renamed to logfile.2 before being over-written, this is repeated the number of times specified when the log file was constructed. This mechanism frees the application from the need to worry about log messages exhausting disk space.

The log file has an output level associated with it which is specified at construction but can be changed later. Each message also has an output level associated with it. Only messages with a level less-than or equal to the output level of the log file will be output. All other messages will be silently discarded. This mechanism supports debug and other informational message output in the application that can be enabled or disabled at run-time.


Constructor & Destructor Documentation

MCLogFile const MCString fileName = MCString(0),
MCMsg::msglevel_t  lvl = MCMsg::MSGLVL_WARN,
int  maxSize = 0,
int  maxRoll = MAX_ROLL,
bool  keepOpen = false
 

Construct a new log file.

Parameters:
fileName if specified it indicates the name of the file to output messages to. The file will be created if it does not exist. If the file cannot be opened a message will be printed to the stderr of the application. If a fileName is not specified then output messages are directed to the stderr of the application.
lvl the output level of the log file
maxSize maximum size of the log file before rolling. Log rolling will not occur if maxSize is zero.
maxRoll maximum number of old log files to keep. maxRoll has no effect unless maxSize is non-zero. If maxRoll is zero then the log will be truncated when it is full.
keepOpen if false the log will be opened and closed each time a new message is output. If keepOpen is true the log will be opened once when the log file object is constructed. Some platforms (notably win platforms) prevent files being deleted if they are open. This is not a less of a problem if log rolling is being used. However the current log file can never be deleted in this case.

~MCLogFile  ) 
 

Close the log file.

The file is closed if it refers to something other than a standard handle (stderr or stdout).


Member Function Documentation

void init const MCString fileName = MCString(0),
MCMsg::msglevel_t  lvl = MCMsg::MSGLVL_WARN,
int  maxSize = 0,
int  maxRoll = MAX_ROLL,
bool  keepOpen = false
[protected]
 

Initialize for use in derived classes.

Arguments are as per the constructor.

bool isOutput MCMsg::msglevel_t  lvl  )  const
 

Test to see if messages at a level will be output.

Returns:
true if the level is enabled for output

void operator() MCMsg::msglevel_t  lvl,
const char *  fmt,
va_list  ap
[protected]
 

Output a message at the specified level.

A new-line is added at the end of the output if one is missing.

Parameters:
lvl the message level to output the message at.
fmt a format specification in the style of MCFile::printf()
ap va_list obtained from a varargs function

void operator() const char *  fmt,
va_list  ap
[protected]
 

Output a message at the default level.

The default level can be set with setDefaultLevel(). A new-line is added at the end of the output if one is missing.

Parameters:
fmt a format specification.
ap va_list obtained from a varargs function

void operator() const MCMsg msg  ) 
 

Output an MCMsg.

An MCMsg contains a message level. This is used as the level at which to output the message. A new-line is added at the end of the output if one is missing.

Parameters:
msg the MCMsg to output.

void operator() MCMsg::msglevel_t  lvl,
const char *  fmt,
  ...
 

Output a message at the specified level.

A new-line is added at the end of the output if one is missing.

Parameters:
lvl the message level to output the message at.
fmt a format specification in the style of MCFile::printf()
... variable arguments depending on the format specification

void operator() const char *  fmt,
  ...
 

Output a message at the default level.

The default level can be set with setDefaultLevel(). A new-line is added at the end of the output if one is missing.

Parameters:
fmt a format specification in the style of MCFile::printf()
... variable arguments depending on the format specification

void setDefaultLevel MCMsg::msglevel_t  lvl  ) 
 

Set the default level.

The default level is the level which will be used if the operater()(const char*, ...) method is used. The initial default level is the same as the log file output level.

Parameters:
lvl the new default message level.

void setOutputFile MCFile::EStdHandle  hdl  ) 
 

Set the output file to a standard file handle.

Parameters:
hdl should be one of stdout or stderr. The library does not trap use of stdin as an output handle.

void setOutputFile const MCString fileName  ) 
 

Set the output file.

The existing file (if any) is closed. A message will be printed to stderr if the file cannot be opened. In this case the log is directed to the stderr of the application.

Parameters:
fileName the new output file name.

void setOutputLevel MCMsg::msglevel_t  lvl  ) 
 

Set the output level.

This is the level at which output messages will be directed to the log file. Attempts to output messages with a level which is higher than this will be ignored.

Parameters:
lvl the new output level.


Generated on Wed Jan 12 19:05:49 2005 for MCLLIB by  doxygen 1.3.9.1