libupnpp 0.16.0
A C++ wrapper for the Portable UPnP reference library
log.h
1/* Copyright (C) 2014-2019 J.F.Dockes
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation; either version 2.1 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this program; if not, write to the
14 * Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17#ifndef _LOG_H_X_INCLUDED_
18#define _LOG_H_X_INCLUDED_
19
20#include <string.h> // for strerror
21#include <fstream>
22#include <iostream>
23#include <string>
24
25#ifndef LOGGER_THREADSAFE
26#define LOGGER_THREADSAFE 1
27#endif
28
29#if LOGGER_THREADSAFE
30#include <mutex>
31#endif
32
33// Can't use the symbolic Logger::LLXX names in preproc. 6 is LLDEB1
34// STATICVERBOSITY is the level above which logging statements are
35// preproc'ed out (can't be dynamically turned on).
36#ifndef LOGGER_STATICVERBOSITY
37#define LOGGER_STATICVERBOSITY 5
38#endif
39
40#define LOGGER_DATESIZE 100
41
45class Logger {
46public:
47 Logger(const Logger&) = delete;
48 Logger& operator=(const Logger&) = delete;
49
54 static Logger *getTheLog(const std::string& fn = std::string());
55 static void cleanup();
56
59 bool reopen(const std::string& fn);
60
64 std::ostream& getstream() {
65 return m_tocerr ? std::cerr : m_stream;
66 }
67
71 enum LogLevel {LLNON=0, LLFAT=1, LLERR=2, LLINF=3, LLDEB=4,
72 LLDEB0=5, LLDEB1=6, LLDEB2=7};
73
75 void setLogLevel(LogLevel level) {
76 m_loglevel = level;
77 }
79 void setloglevel(LogLevel level) {
80 m_loglevel = level;
81 }
82
84 int getloglevel() const {
85 return m_loglevel;
86 }
88 const std::string& getlogfilename() const {
89 return m_fn;
90 }
92 bool logisstderr() const {
93 return m_tocerr;
94 }
95
97 void logthedate(bool onoff) {
98 m_logdate = onoff;
99 }
100
101 bool loggingdate() const {
102 return m_logdate;
103 }
104
107 void setdateformat(const std::string fmt) {
108 m_datefmt = fmt;
109 }
110
112 const char *datestring();
113
114#if LOGGER_THREADSAFE
115 std::recursive_mutex& getmutex() {
116 return m_mutex;
117 }
118#endif
119
120private:
121 bool m_tocerr{false};
122 bool m_logdate{false};
123 int m_loglevel{LLERR};
124 std::string m_datefmt{"%Y%m%d-%H%M%S"};
125 std::string m_fn;
126 std::ofstream m_stream;
127#if LOGGER_THREADSAFE
128 std::recursive_mutex m_mutex;
129#endif
130 char m_datebuf[LOGGER_DATESIZE];
131 Logger(const std::string& fn);
132 ~Logger();
133};
134
135#define LOGGER_PRT (Logger::getTheLog()->getstream())
136
137#if LOGGER_THREADSAFE
138#define LOGGER_LOCK \
139 std::unique_lock<std::recursive_mutex> lock(Logger::getTheLog()->getmutex())
140#else
141#define LOGGER_LOCK
142#endif
143
144#ifndef LOGGER_LOCAL_LOGINC
145#define LOGGER_LOCAL_LOGINC 0
146#endif
147
148#define LOGGER_LEVEL (Logger::getTheLog()->getloglevel() + \
149 LOGGER_LOCAL_LOGINC)
150
151#define LOGGER_DATE (Logger::getTheLog()->loggingdate() ? \
152 Logger::getTheLog()->datestring() : "")
153
154#define LOGGER_DOLOG(L,X) LOGGER_PRT << LOGGER_DATE << ":" << L << ":" << \
155 __FILE__ << ":" << __LINE__ << "::" << X \
156 << std::flush
157
158
159#if LOGGER_STATICVERBOSITY >= 7
160#define LOGDEB2(X) { \
161 if (LOGGER_LEVEL >= Logger::LLDEB2) { \
162 LOGGER_LOCK; \
163 LOGGER_DOLOG(Logger::LLDEB2, X); \
164 } \
165 }
166#else
167#define LOGDEB2(X)
168#endif
169
170#if LOGGER_STATICVERBOSITY >= 6
171#define LOGDEB1(X) { \
172 if (LOGGER_LEVEL >= Logger::LLDEB1) { \
173 LOGGER_LOCK; \
174 LOGGER_DOLOG(Logger::LLDEB1, X); \
175 } \
176 }
177#else
178#define LOGDEB1(X)
179#endif
180
181#if LOGGER_STATICVERBOSITY >= 5
182#define LOGDEB0(X) { \
183 if (LOGGER_LEVEL >= Logger::LLDEB0) { \
184 LOGGER_LOCK; \
185 LOGGER_DOLOG(Logger::LLDEB0, X); \
186 } \
187 }
188#else
189#define LOGDEB0(X)
190#endif
191
192#if LOGGER_STATICVERBOSITY >= 4
193#define LOGDEB(X) { \
194 if (LOGGER_LEVEL >= Logger::LLDEB) { \
195 LOGGER_LOCK; \
196 LOGGER_DOLOG(Logger::LLDEB, X); \
197 } \
198 }
199#else
200#define LOGDEB(X)
201#endif
202
203#if LOGGER_STATICVERBOSITY >= 3
208#define LOGINF(X) { \
209 if (LOGGER_LEVEL >= Logger::LLINF) { \
210 LOGGER_LOCK; \
211 LOGGER_DOLOG(Logger::LLINF, X); \
212 } \
213 }
214#else
215#define LOGINF(X)
216#endif
217#define LOGINFO LOGINF
218
219#if LOGGER_STATICVERBOSITY >= 2
220#define LOGERR(X) { \
221 if (LOGGER_LEVEL >= Logger::LLERR) { \
222 LOGGER_LOCK; \
223 LOGGER_DOLOG(Logger::LLERR, X); \
224 } \
225 }
226#else
227#define LOGERR(X)
228#endif
229
230#if LOGGER_STATICVERBOSITY >= 1
231#define LOGFAT(X) { \
232 if (LOGGER_LEVEL >= Logger::LLFAT) { \
233 LOGGER_LOCK; \
234 LOGGER_DOLOG(Logger::LLFAT, X); \
235 } \
236 }
237#else
238#define LOGFAT(X)
239#endif
240#define LOGFATAL LOGFAT
241
242#if defined(sun) || defined(_WIN32)
243#define LOGSYSERR(who, what, arg) { \
244 LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \
245 ": " << strerror(errno) << '\n'); \
246 }
247#else // not WINDOWS or sun
248
249inline char *_log_check_strerror_r(int, char *errbuf) {return errbuf;}
250inline char *_log_check_strerror_r(char *cp, char *){return cp;}
251
252#define LOGSYSERR(who, what, arg) { \
253 char buf[200]; buf[0] = 0; \
254 LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << ": " << \
255 _log_check_strerror_r(strerror_r(errno, buf, 200), buf) << '\n'); \
256 }
257
258#endif // not windows
259
260#endif /* _LOG_H_X_INCLUDED_ */
This is a singleton class.
Definition log.h:45
void setLogLevel(LogLevel level)
Set the log dynamic verbosity level.
Definition log.h:75
const char * datestring()
Call with log locked.
Definition log.cpp:67
void setloglevel(LogLevel level)
Set the log dynamic verbosity level.
Definition log.h:79
std::ostream & getstream()
Retrieve the output stream in case you need to write directly to it.
Definition log.h:64
static Logger * getTheLog(const std::string &fn=std::string())
Initialize logging to file name.
Definition log.cpp:80
int getloglevel() const
Retrieve the current log level.
Definition log.h:84
bool logisstderr() const
Logging to stderr ?
Definition log.h:92
bool reopen(const std::string &fn)
Close and reopen the output file.
Definition log.cpp:41
LogLevel
Log level values.
Definition log.h:71
void logthedate(bool onoff)
turn date logging on or off (default is off)
Definition log.h:97
void setdateformat(const std::string fmt)
Set the date format, as an strftime() format string.
Definition log.h:107
const std::string & getlogfilename() const
Retrieve current log file name.
Definition log.h:88