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