gphoto2pp
A C++ Wrapper for libgphoto2
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Enumerations
Enumerator
Friends
Macros
log.h
Go to the documentation of this file.
1
#ifndef __LOG_H__
2
#define __LOG_H__
3
4
// from here http://stackoverflow.com/questions/5028302/small-logger-class
5
6
#include <sstream>
7
#include <string>
8
#include <stdio.h>
9
10
inline
std::string
NowTime
();
11
12
enum
TLogLevel
{
logEMERGENCY
,
logALERT
,
logCRITICAL
,
logERROR
,
logWARN
,
logWARN1
,
logWARN2
,
logWARN3
,
logINFO
,
logDEBUG
};
13
14
template
<
typename
T>
15
class
Log
16
{
17
public
:
18
Log
();
19
virtual
~Log
();
20
std::ostringstream&
Get
(
TLogLevel
level =
logINFO
);
21
public
:
22
static
TLogLevel
&
ReportingLevel
();
23
static
std::string
ToString
(
TLogLevel
level);
24
static
TLogLevel
FromString
(
const
std::string& level);
25
protected
:
26
std::ostringstream
os
;
27
private
:
28
Log
(
const
Log
&);
29
Log
& operator =(
const
Log
&);
30
};
31
32
template
<
typename
T>
33
Log<T>::Log
()
34
{
35
}
36
37
template
<
typename
T>
38
std::ostringstream&
Log<T>::Get
(
TLogLevel
level)
39
{
40
os <<
"- "
<<
NowTime
();
41
os <<
" "
<< ToString(level) <<
": "
;
42
os << std::string(level >
logWARN
&& level <=
logWARN3
? level -
logWARN
: 0,
'\t'
);
43
return
os;
44
}
45
46
template
<
typename
T>
47
Log<T>::~Log
()
48
{
49
os << std::endl;
50
T::Output(os.str());
51
}
52
53
template
<
typename
T>
54
TLogLevel
&
Log<T>::ReportingLevel
()
55
{
56
static
TLogLevel
reportingLevel =
logERROR
;
57
return
reportingLevel;
58
}
59
60
template
<
typename
T>
61
std::string
Log<T>::ToString
(
TLogLevel
level)
62
{
63
static
const
char
*
const
buffer[] = {
"EMERGENCY"
,
"ALERT"
,
"CRITICAL"
,
"ERROR"
,
"WARN"
,
"WARN1"
,
"WARN2"
,
"WARN3"
,
"INFO"
,
"DEBUG"
};
64
return
buffer[level];
65
}
66
67
template
<
typename
T>
68
TLogLevel
Log<T>::FromString
(
const
std::string& level)
69
{
70
if
(level ==
"DEBUG"
)
71
return
logDEBUG
;
72
if
(level ==
"INFO"
)
73
return
logINFO
;
74
if
(level ==
"WARN3"
)
75
return
logWARN3
;
76
if
(level ==
"WARN2"
)
77
return
logWARN2
;
78
if
(level ==
"WARN1"
)
79
return
logWARN1
;
80
if
(level ==
"WARN"
)
81
return
logWARN
;
82
if
(level ==
"ERROR"
)
83
return
logERROR
;
84
if
(level ==
"CRITICAL"
)
85
return
logCRITICAL
;
86
if
(level ==
"ALERT"
)
87
return
logALERT
;
88
if
(level ==
"EMERGENCY"
)
89
return
logEMERGENCY
;
90
Log<T>
().Get(
logWARN
) <<
"Unknown logging level '"
<< level <<
"'. Using INFO level as default."
;
91
return
logINFO
;
92
}
93
94
class
Output2FILE
95
{
96
public
:
97
static
FILE*&
Stream
();
98
static
void
Output
(
const
std::string& msg);
99
};
100
101
inline
FILE*&
Output2FILE::Stream
()
102
{
103
static
FILE* pStream = stderr;
104
return
pStream;
105
}
106
107
inline
void
Output2FILE::Output
(
const
std::string& msg)
108
{
109
FILE* pStream =
Stream
();
110
if
(!pStream)
111
return
;
112
fprintf(pStream,
"%s"
, msg.c_str());
113
fflush(pStream);
114
}
115
116
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
117
# if defined (BUILDING_FILELOG_DLL)
118
# define FILELOG_DECLSPEC __declspec (dllexport)
119
# elif defined (USING_FILELOG_DLL)
120
# define FILELOG_DECLSPEC __declspec (dllimport)
121
# else
122
# define FILELOG_DECLSPEC
123
# endif // BUILDING_DBSIMPLE_DLL
124
#else
125
# define FILELOG_DECLSPEC
126
#endif // _WIN32
127
128
class
FILELOG_DECLSPEC
FILELog
:
public
Log
<Output2FILE> {};
129
//typedef Log<Output2FILE> FILELog;
130
131
#ifndef FILELOG_MAX_LEVEL
132
#define FILELOG_MAX_LEVEL logDEBUG
133
#endif
134
135
#define FILE_LOG(level) \
136
if (level > FILELOG_MAX_LEVEL) ;\
137
else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \
138
else FILELog().Get(level)
139
140
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
141
142
#include <windows.h>
143
144
inline
std::string
NowTime
()
145
{
146
const
int
MAX_LEN = 200;
147
char
buffer[MAX_LEN];
148
if
(GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0,
149
"HH':'mm':'ss"
, buffer, MAX_LEN) == 0)
150
return
"Error in NowTime()"
;
151
152
char
result[100] = {0};
153
static
DWORD first = GetTickCount();
154
std::sprintf(result,
"%s.%03ld"
, buffer, (
long
)(GetTickCount() - first) % 1000);
155
return
result;
156
}
157
158
#else
159
160
#include <sys/time.h>
161
162
inline
std::string
NowTime
()
163
{
164
char
buffer[11];
165
time_t t;
166
time(&t);
167
tm r = {0};
168
strftime(buffer,
sizeof
(buffer),
"%X"
, localtime_r(&t, &r));
169
struct
timeval tv;
170
gettimeofday(&tv, 0);
171
char
result[100] = {0};
172
std::sprintf(result,
"%s.%03ld"
, buffer, (
long
)tv.tv_usec / 1000);
173
return
result;
174
}
175
176
#endif //WIN32
177
178
#endif //__LOG_H__
include
log.h
Generated on Wed May 7 2014 20:06:38 for gphoto2pp by
1.8.1.2