utils::Logger Class Reference

A very simple logger that writes messages to a log file. More...

#include <logger.h>

List of all members.

Public Types

enum  Level {
  FATAL = 0, ERROR, WARN, INFO,
  DEBUG
}

Static Public Member Functions

static void setLogFile (const std::string &logFile)
 Sets the log file.
static void setTimestamp (bool flag=true)
 Add/removes the timestamp.
static void setTeeMode (bool flag=true)
 Sets tee mode.
static void setVerbosity (Level verbosity)
 Sets the verbosity level of the logger.
static void output (const std::string &msg, Level atVerbosity)
 Logs a generic message.

Static Public Attributes

static Level mVerbosity = Logger::INFO
 Verbosity level.


Detailed Description

A very simple logger that writes messages to a log file.

If the log file is not set, the messages are routed to the standard output or the standard error depending on the level of the message. By default, the messages will be timestamped but the logger can be configured to not prefix the messages with a timestamp.

Limitations:

Example of use:

 #include "logger.h"

 int main(void)
 {
     using namespace utils;

     Logger::setLogFile("/path/to/logfile");

     // log messages using helper macros.
     LOG_DEBUG("level: " << 3)
     LOG_INFO("init sound")
     LOG_WARN("not implemented")
     LOG_ERROR("resource not found")
     LOG_FATAL("unable to init graphics")

     // log messages using APIs.
     std::ostringstream os;
     os << "level: " << 3;
     Logger::output(os.str(), Logger::DEBUG);

     Logger::output("init sound", Logger::INFO);
     Logger::output("not implemented", Logger::WARN);
     Logger::output("resource not found", Logger::ERROR);
     Logger::output("unable to init graphics", Logger::FATAL);

     return 0;
 }
 

Definition at line 72 of file logger.h.


Member Function Documentation

void utils::Logger::setLogFile ( const std::string &  logFile  )  [static]

Sets the log file.

This method will open the log file for writing, the former file contents are removed.

Parameters:
logFile the log file name (may include path).
Exceptions:
std::ios::failure if the log file could not be opened.

Definition at line 83 of file logger.cpp.

static void utils::Logger::setTimestamp ( bool  flag = true  )  [inline, static]

Add/removes the timestamp.

Parameters:
flag if true, a log message will always be timestamped (default = true).

Definition at line 102 of file logger.h.

static void utils::Logger::setTeeMode ( bool  flag = true  )  [inline, static]

Sets tee mode.

Parameters:
flag if true, write messages to both the standard (or error) output and the log file (if set) (default = true).

Definition at line 111 of file logger.h.

static void utils::Logger::setVerbosity ( Level  verbosity  )  [inline, static]

Sets the verbosity level of the logger.

Parameters:
verbosity is the level of verbosity.

Definition at line 119 of file logger.h.

References mVerbosity.

void utils::Logger::output ( const std::string &  msg,
Level  atVerbosity 
) [static]

Logs a generic message.

Parameters:
msg the message to log.
atVerbosity the minimum verbosity level to log this
Exceptions:
std::ios::failure. 

Definition at line 106 of file logger.cpp.

References mVerbosity.


Member Data Documentation

Logger::Level utils::Logger::mVerbosity = Logger::INFO [static]

Verbosity level.

Definition at line 134 of file logger.h.

Referenced by output(), and setVerbosity().


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

Generated on Sun Apr 26 17:30:47 2009 for TMW Server Collection by  doxygen 1.5.5