include/dmlite/cpp/utils/logger.h

Go to the documentation of this file.
00001 #ifndef Logger_HH
00002 #define Logger_HH
00003 
00004 #include <syslog.h>
00005 
00006 #include <sstream>
00007 #include <string>
00008 
00009 #include <map>
00010 #include <vector>
00011 
00012 
00013 #define Log(lvl, mymask, where, what)                                                                                           \
00014 do{                                                                                                                     \
00015         if (Logger::get()->getLevel() >= lvl && Logger::get()->isLogged(mymask))        \
00016         {                                                                                                                                       \
00017                 std::ostringstream outs;                                                        \
00018                 outs << "dmlite " << where << " " << __func__ << " : " << what;                                         \
00019                 Logger::get()->log((Logger::Level)lvl, outs.str());                             \
00020         }                                                                               \
00021 }while(0)                                                                                       \
00022 
00023 
00024 #define Err(where, what)                                                                                                \
00025 do{                                                                                                                     \
00026                 std::ostringstream outs;                                                        \
00027                 outs << "dmlite " << where << " !! " << __func__ << " : " << what;                                              \
00028                 Logger::get()->log((Logger::Level)0, outs.str());                               \
00029 }while(0)       
00030 
00031 /**
00032  * A Logger class
00033  */
00034 class Logger
00035 {
00036 
00037 public:
00038         /// typedef for a bitmask (long long)
00039         typedef unsigned long long bitmask;
00040         /// typedef for a component name (std:string)
00041         typedef std::string component;
00042 
00043         static bitmask unregistered;
00044 
00045     /**
00046      * Use the same values for log levels as syslog
00047      */
00048     enum Level
00049     {
00050         Lvl0,       // The default?
00051         Lvl1,
00052         Lvl2,
00053         Lvl3,
00054         Lvl4
00055     };
00056 
00057     /// Destructor
00058     ~Logger();
00059 
00060     static Logger *instance;
00061     
00062     /// @return the singleton instance
00063     static Logger *get()
00064     {
00065         if (instance == 0)
00066           instance = new Logger();
00067         return instance;
00068     }
00069 
00070     static void set(Logger *inst) {
00071       Logger *old = instance;
00072       instance = inst;
00073       if (old) delete old;
00074     }
00075     /// @return the current debug level
00076     short getLevel() const
00077     {
00078         return level;
00079     }
00080 
00081     /// @param lvl : the logging level that will be set
00082     void setLevel(Level lvl)
00083     {
00084         level = lvl;
00085     }
00086 
00087     /// @return true if the given component is being logged, false otherwise
00088     bool isLogged(bitmask m) const
00089     {
00090         if (mask == 0) return mask & unregistered;
00091         return mask & m;
00092     }
00093     
00094     /// @param comp : the component that will be registered for logging
00095     void registerComponent(component const &  comp);
00096     
00097     /// @param components : list of components that will be registered for logging
00098     void registerComponents(std::vector<component> const & components);
00099 
00100     /// Sets if a component has to be logged or not
00101     /// @param comp : the component name
00102     /// @param tobelogged : true if we want to log this component
00103     void setLogged(component const &comp, bool tobelogged);
00104     
00105     /**
00106      * Logs the message
00107      *
00108      * @param lvl : log level of the message
00109      * @param component : bitmask assignet to the given component
00110      * @param msg : the message to be logged
00111      */
00112     void log(Level lvl, std::string const & msg) const;
00113 
00114     /**
00115      * @param if true all unregistered components will be logged,
00116      *                  if false only registered components will be logged
00117      */
00118     void logAll()
00119     {
00120         mask = ~0;
00121     }
00122     
00123         
00124     /**
00125      * @param comp : component name
00126      * @return respectiv bitmask assigned to given component
00127      */
00128     bitmask getMask(component const & comp);
00129 
00130     /**
00131      * Build a printable stacktrace. Useful e.g. inside exceptions, to understand
00132      * where they come from.
00133      * Note: I don't think that the backtrace() function is thread safe, nor this function
00134      * Returns the number of backtraces
00135      * @param s : the string that will contain the printable stacktrace
00136      * @return the number of stacktraces
00137      */
00138     static int getStackTrace(std::string &s);
00139 
00140 
00141 private:
00142 
00143     ///Private constructor
00144     Logger();
00145     // Copy constructor (not implemented)
00146     Logger(Logger const &);
00147     // Assignment operator (not implemented)
00148     Logger & operator=(Logger const &);
00149 
00150     /// current log level
00151     short level;
00152     /// number of components that were assigned with a bitmask
00153     int size;
00154     /// global bitmask with all registered components
00155     bitmask mask;
00156     /// component name to bitmask mapping
00157     std::map<component, bitmask> mapping;
00158 
00159 
00160 
00161 };
00162 
00163 
00164 #endif

Generated on 18 Nov 2014 for dmlite by  doxygen 1.4.7