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