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
/// @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
Generated on Wed Oct 8 2014 02:52:35 for dmlite by
1.8.3.1