rivet is hosted by Hepforge, IPPP Durham

#include <Logging.hh>

List of all members.

Public Types

enum  Level {
  TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30,
  WARNING = 30, ERROR = 40, CRITICAL = 50, ALWAYS = 50
}
 Log priority levels. More...
typedef std::map< std::string,
Log
LogMap
 Typedef for a collection of named logs.
typedef std::map< std::string,
int > 
LevelMap
 Typedef for a collection of named log levels.
typedef std::map< int,
std::string > 
ColorCodes
 Typedef for a collection of shell color codes, accessed by log level.

Public Member Functions

int getLevel () const
 Get the priority level of this logger.
LogsetLevel (int level)
 Set the priority level of this logger.
std::string getName () const
 Get the name of this logger.
LogsetName (const std::string &name)
 Set the name of this logger.
bool isActive (int level) const
 Will this log level produce output on this logger at the moment?
Explicit log methods
void trace (const std::string &message)
void debug (const std::string &message)
void info (const std::string &message)
void warn (const std::string &message)
void error (const std::string &message)

Static Public Member Functions

static void setLevel (const std::string &name, int level)
 Set the log levels.
static void setLevels (const LevelMap &logLevels)
static void setShowTimestamp (bool showTime=true)
static void setShowLevel (bool showLevel=true)
static void setShowLoggerName (bool showName=true)
static void setUseColors (bool useColors=true)
static LoggetLog (const std::string &name)
static Level getLevelFromName (const std::string &level)
 Get a log level enum from a string.
static std::string getLevelName (int level)
 Get the std::string representation of a log level.

Protected Member Functions

void log (int level, const std::string &message)
 Write a message at a particular level.
std::string formatMessage (int level, const std::string &message)
 Turn a message string into the current log format.
Hidden constructors etc.
 Log (const std::string &name)
 Constructor 1.
 Log (const std::string &name, int level)
 Constructor 2.

Static Protected Member Functions

static std::string getColorCode (int level)

Private Attributes

std::string _name
 This logger's name.
int _level
 Threshold level for this logger.

Static Private Attributes

static LogMap existingLogs
 A static map of existing logs: we don't make more loggers than necessary.
static LevelMap defaultLevels
 A static map of default log levels.
static ColorCodes colorCodes
 A static map of shell color codes for the log levels.
static std::string endColorCode
 Shell color code for the end of the log levels.
static bool showTimestamp = false
 Show timestamp?
static bool showLogLevel = true
 Show log level?
static bool showLoggerName = true
 Show logger name?
static bool useShellColors = true
 Use shell colour escape codes?

Friends

std::ostream & operator<< (Log &log, int level)
 The streaming operator can use Log's internals.

Detailed Description

Definition at line 9 of file Logging.hh.


Member Typedef Documentation

typedef std::map<int, std::string> ColorCodes

Typedef for a collection of shell color codes, accessed by log level.

Definition at line 24 of file Logging.hh.

typedef std::map<std::string, int> LevelMap

Typedef for a collection of named log levels.

Definition at line 21 of file Logging.hh.

typedef std::map<std::string, Log> LogMap

Typedef for a collection of named logs.

Definition at line 18 of file Logging.hh.


Member Enumeration Documentation

enum Level

Log priority levels.

Enumerator:
TRACE 
DEBUG 
INFO 
WARN 
WARNING 
ERROR 
CRITICAL 
ALWAYS 

Definition at line 13 of file Logging.hh.

               {
      TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, WARNING = 30, ERROR = 40, CRITICAL = 50, ALWAYS = 50
    };

Constructor & Destructor Documentation

Log ( const std::string &  name) [protected]

Constructor 1.

Definition at line 19 of file Logging.cc.

    : _name(name), _level(INFO) { }
Log ( const std::string &  name,
int  level 
) [protected]

Constructor 2.

Definition at line 23 of file Logging.cc.

    : _name(name), _level(level) { }

Member Function Documentation

void debug ( const std::string &  message) [inline]

Definition at line 131 of file Logging.hh.

{ log(DEBUG, message); }
void error ( const std::string &  message) [inline]

Definition at line 137 of file Logging.hh.

{ log(ERROR, message); }
string formatMessage ( int  level,
const std::string &  message 
) [protected]

Turn a message string into the current log format.

Definition at line 150 of file Logging.cc.

                                                            {
    string out;
    if (Log::useShellColors) {
      out += getColorCode(level);
    }

    if (Log::showLoggerName) {
      out += getName();
      out += ": ";
    }

    if (Log::showLogLevel) {
      out += Log::getLevelName(level);
      out += " ";
    }

    if (Log::showTimestamp) {
      time_t rawtime;
      time(&rawtime);
      char* timestr = ctime(&rawtime);
      timestr[24] = ' ';
      out += timestr;
      out += " ";
    }

    if (Log::useShellColors) {
      out += endColorCode;
    }

    out += " ";
    out += message;
 
    return out;
  }
string getColorCode ( int  level) [static, protected]
Todo:
Test for VT100 compliance?
Todo:
Do the map::upper_limit thing to find nearest level...

Definition at line 113 of file Logging.cc.

                                    {
    if (!Log::useShellColors) return "";
    // If the codes haven't been initialized, do so now.
    if (Log::colorCodes.empty()) {
      // If stdout is a valid tty, try to use the appropriate codes.
      if (isatty(1)) {
        /// @todo Test for VT100 compliance?
        Log::colorCodes[TRACE] = "\033[0;36m";
        Log::colorCodes[DEBUG] = "\033[0;34m";
        Log::colorCodes[INFO]  = "\033[0;32m";
        Log::colorCodes[WARN]  = "\033[0;33m";
        Log::colorCodes[ERROR] = "\033[0;31m";
        Log::endColorCode      = "\033[0m";
      } else {
        Log::colorCodes[TRACE] = "";
        Log::colorCodes[DEBUG] = "";
        Log::colorCodes[INFO] = "";
        Log::colorCodes[WARN] = "";
        Log::colorCodes[ERROR] = "";
      }
    }
    // Return the appropriate code from the colour map.
    /// @todo Do the map::upper_limit thing to find nearest level...
    return colorCodes[level];
  }
int getLevel ( ) const [inline]

Get the priority level of this logger.

Definition at line 95 of file Logging.hh.

                         {
      return _level;
    }
Log::Level getLevelFromName ( const std::string &  level) [static]

Get a log level enum from a string.

Definition at line 140 of file Logging.cc.

                                                    {
    if (level == "TRACE") return TRACE;
    if (level == "DEBUG") return DEBUG;
    if (level == "INFO") return INFO;
    if (level == "WARN") return WARN;
    if (level == "ERROR") return ERROR;
    throw Error("Couldn't create a log level from string '" + level + "'");
  }
string getLevelName ( int  level) [static]

Get the std::string representation of a log level.

Todo:
Do the map::upper_limit thing to find nearest level...

Definition at line 93 of file Logging.cc.

                                    {
    /// @todo Do the map::upper_limit thing to find nearest level...
    switch(level) {
    case TRACE:
      return "TRACE";
    case DEBUG:
      return "DEBUG";
    case INFO:
      return "INFO";
    case WARN:
      return "WARN";
    case ERROR:
      return "ERROR";
    default:
      return "";
    }
    //throw Error("Enum value was not a valid log level. How did that happen?");
  }
Log & getLog ( const std::string &  name) [static]

Get a logger with the given name. The level will be taken from the "requestedLevels" static map or will be INFO by default.

Definition at line 55 of file Logging.cc.

                                     {
    auto theLog = existingLogs.find(name);
    if (theLog == existingLogs.end()) {
      int level = INFO;
      // Try running through all parent classes to find an existing level
      string tmpname = name;
      bool triedAllParents = false;
      while (! triedAllParents) {
        // Is there a default level?
        if (defaultLevels.find(tmpname) != defaultLevels.end()) {
          level = defaultLevels.find(tmpname)->second;
          break;
        }
        // Is there already such a logger? (NB. tmpname != name in later iterations)
        if (existingLogs.find(tmpname) != existingLogs.end()) {
          level = existingLogs.find(tmpname)->second.getLevel();
          break;
        }
        // Crop the string back to the next parent level
        size_t lastDot = tmpname.find_last_of(".");
        if (lastDot != string::npos) {
          tmpname = tmpname.substr(0, lastDot);
        } else {
          triedAllParents = true;
        }
      }
      // for (LevelMap::const_iterator l = defaultLevels.begin(); l != defaultLevels.end(); ++l) {
      // 
      // }

      // emplace returns pair<iterator,bool>
      auto result = existingLogs.emplace(name, Log(name, level));
      theLog = result.first;
    }
    return theLog->second;
  }
std::string getName ( ) const [inline]

Get the name of this logger.

Definition at line 112 of file Logging.hh.

                              {
      return _name;
    }
void info ( const std::string &  message) [inline]

Definition at line 133 of file Logging.hh.

{ log(INFO, message); }
bool isActive ( int  level) const [inline]

Will this log level produce output on this logger at the moment?

Definition at line 123 of file Logging.hh.

                                   {
      return (level >= _level);
    }
void log ( int  level,
const std::string &  message 
) [protected]

Write a message at a particular level.

Definition at line 186 of file Logging.cc.

                                                {
    if (isActive(level)) {
      cout << formatMessage(level, message) << endl;
    }
  }
void setLevel ( const std::string &  name,
int  level 
) [static]

Set the log levels.

Definition at line 40 of file Logging.cc.

                                                  {
    defaultLevels[name] = level;
    //cout << name << " -> " << level << endl;
    _updateLevels(defaultLevels, existingLogs);
  }
Log& setLevel ( int  level) [inline]

Set the priority level of this logger.

Definition at line 100 of file Logging.hh.

                             {
      _level = level;
      return *this;
    }
void setLevels ( const LevelMap logLevels) [static]

Definition at line 47 of file Logging.cc.

                                               {
    for (LevelMap::const_iterator lev = logLevels.begin(); lev != logLevels.end(); ++lev) {
      defaultLevels[lev->first] = lev->second;
    }
    _updateLevels(defaultLevels, existingLogs);
  }
Log& setName ( const std::string &  name) [inline]

Set the name of this logger.

Definition at line 117 of file Logging.hh.

                                        {
      _name = name;
      return *this;
    }
static void setShowLevel ( bool  showLevel = true) [inline, static]

Definition at line 60 of file Logging.hh.

                                                  {
      showLogLevel = showLevel;
    }
static void setShowLoggerName ( bool  showName = true) [inline, static]

Definition at line 64 of file Logging.hh.

                                                      {
      showLoggerName = showName;
    }
static void setShowTimestamp ( bool  showTime = true) [inline, static]

Definition at line 56 of file Logging.hh.

                                                     {
      showTimestamp = showTime;
    }
static void setUseColors ( bool  useColors = true) [inline, static]

Definition at line 68 of file Logging.hh.

                                                  {
      useShellColors = useColors;
    }
void trace ( const std::string &  message) [inline]

Definition at line 129 of file Logging.hh.

{ log(TRACE, message); }
void warn ( const std::string &  message) [inline]

Definition at line 135 of file Logging.hh.

{ log(WARN, message); }

Friends And Related Function Documentation

std::ostream& operator<< ( Log log,
int  level 
) [friend]

The streaming operator can use Log's internals.


Member Data Documentation

int _level [private]

Threshold level for this logger.

Definition at line 145 of file Logging.hh.

std::string _name [private]

This logger's name.

Definition at line 142 of file Logging.hh.

Log::ColorCodes colorCodes [static, private]

A static map of shell color codes for the log levels.

Definition at line 34 of file Logging.hh.

Log::LevelMap defaultLevels [static, private]

A static map of default log levels.

Definition at line 31 of file Logging.hh.

string endColorCode [static, private]

Shell color code for the end of the log levels.

Definition at line 37 of file Logging.hh.

Log::LogMap existingLogs [static, private]

A static map of existing logs: we don't make more loggers than necessary.

Definition at line 28 of file Logging.hh.

bool showLoggerName = true [static, private]

Show logger name?

Definition at line 46 of file Logging.hh.

bool showLogLevel = true [static, private]

Show log level?

Definition at line 43 of file Logging.hh.

bool showTimestamp = false [static, private]

Show timestamp?

Definition at line 40 of file Logging.hh.

bool useShellColors = true [static, private]

Use shell colour escape codes?

Definition at line 49 of file Logging.hh.


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