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

Generated on 4 May 2016 for dmlite by  doxygen 1.4.7