Logging.hh

Go to the documentation of this file.
00001 // $Id: $
00002 #ifndef RIVET_LOGGING_HH 
00003 #define RIVET_LOGGING_HH 1
00004 
00005 #include "Rivet/Rivet.hh"
00006 
00007 
00008 namespace Rivet {
00009 
00010   class Log {
00011   public:
00012 
00013     /// Log priority levels.
00014     enum Level {
00015       TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, ERROR = 40 
00016     };
00017 
00018     /// Typedef for a collection of named logs.
00019     typedef map<const string, Log*> LogMap;
00020 
00021     /// Typedef for a collection of named log levels.
00022     typedef map<const string, Level> LevelMap;
00023 
00024   private:
00025     /// A static map of existing logs: we don't make more loggers than necessary.
00026     static LogMap existingLogs;
00027 
00028     /// A static map of default log levels.
00029     static LevelMap defaultLevels;
00030 
00031     /// Show timestamp?
00032     static bool showTimestamp;
00033 
00034     /// Show log level?
00035     static bool showLogLevel;
00036 
00037     /// Show logger name?
00038     static bool showLoggerName;
00039 
00040   public:
00041     /// Set the default log levels
00042     inline static void setDefaultLevels(const LevelMap& logLevels) {
00043       defaultLevels = logLevels;
00044     }
00045 
00046     inline static void setShowTimestamp(const bool showTime=true) {
00047       showTimestamp = showTime;
00048     }
00049 
00050     inline static void setShowLevel(const bool showLevel=true) {
00051       showLogLevel = showLevel;
00052     }
00053 
00054     inline static void setShowLoggerName(const bool showName=true) {
00055       showLoggerName = showName;
00056     }
00057 
00058 
00059   protected:
00060     /// @name Hidden constructors etc.
00061     //@{
00062     /// Constructor 1
00063     Log(const string& name);
00064 
00065     /// Constructor 2
00066     Log(const string& name, const Level& level);
00067 
00068     /// Copy constructor
00069     //Log(const Log&);
00070 
00071     /// Copy assignment operator
00072     //Log& operator=(const Log&);
00073     //@}
00074 
00075   public:
00076     /// Get a logger with the given name. The level will be taken from the 
00077     /// "requestedLevels" static map or will be INFO by default.
00078     static Log& getLog(const string& name);
00079 
00080     /// Get a logger with the given name and level.
00081     static Log& getLog(const string& name, const Level& level);
00082 
00083   public:
00084     /// Get the priority level of this logger.
00085     inline Level getLevel() const {
00086       return _level;
00087     }
00088 
00089     /// Set the priority level of this logger.
00090     inline Log& setLevel(const Level& level) {
00091       _level = level;
00092       return *this;
00093     }
00094 
00095     /// Get a log level enum from a string.
00096     static Level getLevelFromName(const string& level);
00097 
00098     /// Get the string representation of a log level.
00099     static string getLevelName(const Level& level);
00100 
00101     /// Get the name of this logger.
00102     inline string getName() const {
00103       return _name;
00104     }
00105 
00106     /// Set the name of this logger.
00107     inline Log& setName(const string& name) {
00108       _name = name;
00109       return *this;
00110     }
00111 
00112     /// Will this log level produce output on this logger at the moment?
00113     inline bool isActive(const Level& level) const {
00114       return (level >= _level);
00115     }
00116 
00117     /// @name Explicit log methods
00118     //@{
00119     inline void trace(const string& message) { log(TRACE, message); }
00120 
00121     inline void debug(const string& message) { log(DEBUG, message); }
00122     
00123     inline void info(const string& message) { log(INFO, message); }
00124 
00125     inline void warn(const string& message) { log(WARN, message); }
00126 
00127     inline void error(const string& message) { log(ERROR, message); }
00128     //@}
00129 
00130   private:
00131     /// This logger's name
00132     string _name;
00133     
00134     /// Threshold level for this logger.
00135     Level _level;
00136     
00137   protected:
00138     /// Write a message at a particular level.
00139     void log(const Level& level, const string& message);
00140 
00141     /// Turn a message string into the current log format.
00142     string formatMessage(const Level& level, const string& message);
00143 
00144   public:
00145 
00146     /// A null output stream, used for piping discarded output to nowhere.
00147     /// @todo Hide this...
00148     ostream* const _nostream;
00149 
00150     /// The streaming operator can use Log's internals.
00151     friend ostream& operator<<(Log& log, const Log::Level& level);
00152 
00153   };
00154   
00155   /// Streaming output to a logger must have a Log::Level as its first argument.
00156   ostream& operator<<(Log& log, const Log::Level& level);
00157   
00158 }
00159 
00160 
00161 #endif