utils::Logger Class Reference

#include <logger.h>

List of all members.


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 75 of file logger.h.

Public Types

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

Static Public Member Functions

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

Static Public Attributes

Level mVerbosity = Logger::INFO
 Verbosity level.

Static Private Member Functions

void output (std::ostream &os, std::string const &msg, char const *prefix)
 Logs a generic message.

Static Private Attributes

bool mHasTimestamp = true
 Timestamp flag.
bool mTeeMode = false
 Tee mode flag.


Member Enumeration Documentation

enum utils::Logger::Level
 

Enumeration values:
FATAL 
ERROR 
WARN 
INFO 
DEBUG 

Definition at line 78 of file logger.h.


Member Function Documentation

void utils::Logger::output std::ostream &  os,
std::string const &  msg,
char const *  prefix
[static, private]
 

Logs a generic message.

Parameters:
os the output stream.
msg the message to log.
prefix the message prefix.
Exceptions:
std::ios::failure. 

Definition at line 70 of file logger.cpp.

void utils::Logger::output std::string const &  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 108 of file logger.cpp.

void utils::Logger::setLogFile std::string const &  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 85 of file logger.cpp.

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 114 of file logger.h.

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 105 of file logger.h.

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 122 of file logger.h.


Member Data Documentation

bool utils::Logger::mHasTimestamp = true [static, private]
 

Timestamp flag.

Definition at line 38 of file logger.cpp.

bool utils::Logger::mTeeMode = false [static, private]
 

Tee mode flag.

Definition at line 39 of file logger.cpp.

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

Verbosity level.

Definition at line 40 of file logger.cpp.


The documentation for this class was generated from the following files:
Generated on Fri Mar 30 15:39:19 2007 for TMW Server by  doxygen 1.3.9.1