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