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