dmlite
0.6
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
include
dmlite
cpp
utils
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?
51
Lvl1
,
52
Lvl2
,
53
Lvl3
,
54
Lvl4
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
static
void
set
(
Logger
*inst) {
71
Logger
*old =
instance
;
72
instance
= inst;
73
if
(old)
delete
old;
74
}
75
/// @return the current debug level
76
short
getLevel
()
const
77
{
78
return
level
;
79
}
80
81
/// @param lvl : the logging level that will be set
82
void
setLevel
(
Level
lvl)
83
{
84
level
= lvl;
85
}
86
87
/// @return true if the given component is being logged, false otherwise
88
bool
isLogged
(
bitmask
m)
const
89
{
90
if
(
mask
== 0)
return
mask
&
unregistered
;
91
return
mask
& m;
92
}
93
94
/// @param comp : the component that will be registered for logging
95
void
registerComponent
(
component
const
& comp);
96
97
/// @param components : list of components that will be registered for logging
98
void
registerComponents
(std::vector<component>
const
& components);
99
100
/// Sets if a component has to be logged or not
101
/// @param comp : the component name
102
/// @param tobelogged : true if we want to log this component
103
void
setLogged
(
component
const
&comp,
bool
tobelogged);
104
105
/**
106
* Logs the message
107
*
108
* @param lvl : log level of the message
109
* @param component : bitmask assignet to the given component
110
* @param msg : the message to be logged
111
*/
112
void
log
(
Level
lvl, std::string
const
& msg)
const
;
113
114
/**
115
* @param if true all unregistered components will be logged,
116
* if false only registered components will be logged
117
*/
118
void
logAll
()
119
{
120
mask
= ~0;
121
}
122
123
124
/**
125
* @param comp : component name
126
* @return respectiv bitmask assigned to given component
127
*/
128
bitmask
getMask
(
component
const
& comp);
129
130
/**
131
* Build a printable stacktrace. Useful e.g. inside exceptions, to understand
132
* where they come from.
133
* Note: I don't think that the backtrace() function is thread safe, nor this function
134
* Returns the number of backtraces
135
* @param s : the string that will contain the printable stacktrace
136
* @return the number of stacktraces
137
*/
138
static
int
getStackTrace
(std::string &s);
139
140
141
private
:
142
143
///Private constructor
144
Logger
();
145
// Copy constructor (not implemented)
146
Logger
(
Logger
const
&);
147
// Assignment operator (not implemented)
148
Logger
&
operator=
(
Logger
const
&);
149
150
/// current log level
151
short
level
;
152
/// number of components that were assigned with a bitmask
153
int
size
;
154
/// global bitmask with all registered components
155
bitmask
mask
;
156
/// component name to bitmask mapping
157
std::map<component, bitmask>
mapping
;
158
159
160
161
};
162
163
164
#endif
Generated on Tue Nov 18 2014 16:48:49 for dmlite by
1.8.3.1