dmlite  0.6
logger.h
Go to the documentation of this file.
1 #ifndef Logger_HH
2 #define Logger_HH
3 
4 #include <syslog.h>
5 
6 #include <sstream>
7 #include <string>
8 
9 #include <map>
10 #include <vector>
11 
12 #define Log(lvl, mymask, where, what) \
13 do{ \
14  if (Logger::get()->getLevel() >= lvl && Logger::get()->isLogged(mymask)) \
15  { \
16  std::ostringstream outs; \
17  outs << "[" << lvl << "] dmlite " << where << " " << __func__ << " : " << what; \
18  Logger::get()->log((Logger::Level)lvl, outs.str()); \
19  } \
20 }while(0) \
21 
22 
23 #define Err(where, what) \
24 do{ \
25  std::ostringstream outs; \
26  outs << "dmlite " << where << " !! " << __func__ << " : " << what; \
27  Logger::get()->log((Logger::Level)0, outs.str()); \
28 }while(0)
29 
30 /**
31  * A Logger class
32  */
33 class Logger
34 {
35 
36 public:
37  /// typedef for a bitmask (long long)
38  typedef unsigned long long bitmask;
39  /// typedef for a component name (std:string)
40  typedef std::string component;
41 
43  static char *unregisteredname;
44  /**
45  * Use the same values for log levels as syslog
46  */
47  enum Level
48  {
49  Lvl0, // The default?
54  };
55 
56  /// Destructor
57  ~Logger();
58 
59  static Logger *instance;
60 
61  /// @return the singleton instance
62  static Logger *get()
63  {
64  if (instance == 0)
65  instance = new Logger();
66  return instance;
67  }
68 
69  static void set(Logger *inst) {
70  Logger *old = instance;
71  instance = inst;
72  if (old) delete old;
73  }
74  /// @return the current debug level
75  short getLevel() const
76  {
77  return level;
78  }
79 
80  /// @param lvl : the logging level that will be set
81  void setLevel(Level lvl)
82  {
83  level = lvl;
84  }
85 
86  /// @return true if the given component is being logged, false otherwise
87  bool isLogged(bitmask m) const
88  {
89  if (mask == 0) return mask & unregistered;
90  return mask & m;
91  }
92 
93  /// @param comp : the component that will be registered for logging
94  void registerComponent(component const & comp);
95 
96  /// @param components : list of components that will be registered for logging
97  void registerComponents(std::vector<component> const & components);
98 
99  /// Sets if a component has to be logged or not
100  /// @param comp : the component name
101  /// @param tobelogged : true if we want to log this component
102  void setLogged(component const &comp, bool tobelogged);
103 
104  /**
105  * Logs the message
106  *
107  * @param lvl : log level of the message
108  * @param component : bitmask assignet to the given component
109  * @param msg : the message to be logged
110  */
111  void log(Level lvl, std::string const & msg) const;
112 
113  /**
114  * @param if true all unregistered components will be logged,
115  * if false only registered components will be logged
116  */
117  void logAll()
118  {
119  mask = ~0;
120  }
121 
122 
123  /**
124  * @param comp : component name
125  * @return respectiv bitmask assigned to given component
126  */
127  bitmask getMask(component const & comp);
128 
129  /**
130  * Build a printable stacktrace. Useful e.g. inside exceptions, to understand
131  * where they come from.
132  * Note: I don't think that the backtrace() function is thread safe, nor this function
133  * Returns the number of backtraces
134  * @param s : the string that will contain the printable stacktrace
135  * @return the number of stacktraces
136  */
137  static int getStackTrace(std::string &s);
138 
139 
140 private:
141 
142  ///Private constructor
143  Logger();
144  // Copy constructor (not implemented)
145  Logger(Logger const &);
146  // Assignment operator (not implemented)
147  Logger & operator=(Logger const &);
148 
149  /// current log level
150  short level;
151  /// number of components that were assigned with a bitmask
152  int size;
153  /// global bitmask with all registered components
155  /// component name to bitmask mapping
156  std::map<component, bitmask> mapping;
157 
158 
159 
160 };
161 
162 
163 // Specialized func to log configuration values. Filters out sensitive stuff.
164 void LogCfgParm(int lvl, Logger::bitmask mymask, std::string where, std::string key, std::string value);
165 
166 
167 
168 
169 #endif
~Logger()
Destructor.
static void set(Logger *inst)
Definition: logger.h:69
static bitmask unregistered
Definition: logger.h:42
void registerComponents(std::vector< component > const &components)
Definition: logger.h:51
std::map< component, bitmask > mapping
component name to bitmask mapping
Definition: logger.h:156
bitmask getMask(component const &comp)
Definition: logger.h:33
static Logger * instance
Definition: logger.h:59
bool isLogged(bitmask m) const
Definition: logger.h:87
int size
number of components that were assigned with a bitmask
Definition: logger.h:152
void setLogged(component const &comp, bool tobelogged)
unsigned long long bitmask
typedef for a bitmask (long long)
Definition: logger.h:38
short level
current log level
Definition: logger.h:150
void logAll()
Definition: logger.h:117
Logger & operator=(Logger const &)
void setLevel(Level lvl)
Definition: logger.h:81
bitmask mask
global bitmask with all registered components
Definition: logger.h:154
std::string component
typedef for a component name (std:string)
Definition: logger.h:40
void LogCfgParm(int lvl, Logger::bitmask mymask, std::string where, std::string key, std::string value)
void registerComponent(component const &comp)
Definition: logger.h:49
Definition: logger.h:50
void log(Level lvl, std::string const &msg) const
Level
Definition: logger.h:47
Definition: logger.h:52
Logger()
Private constructor.
short getLevel() const
Definition: logger.h:75
static char * unregisteredname
Definition: logger.h:43
Definition: logger.h:53
static int getStackTrace(std::string &s)