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>
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 
45 class Logger {
46 public:
51  static Logger *getTheLog(const std::string& fn = std::string());
52 
55  bool reopen(const std::string& fn);
56 
60  std::ostream& getstream() {
61  return m_tocerr ? std::cerr : m_stream;
62  }
63 
67  enum LogLevel {LLNON=0, LLFAT=1, LLERR=2, LLINF=3, LLDEB=4,
68  LLDEB0=5, LLDEB1=6, LLDEB2=7};
69 
71  void setLogLevel(LogLevel level) {
72  m_loglevel = level;
73  }
75  void setloglevel(LogLevel level) {
76  m_loglevel = level;
77  }
78 
80  int getloglevel() const {
81  return m_loglevel;
82  }
84  const std::string& getlogfilename() const {
85  return m_fn;
86  }
88  bool logisstderr() const {
89  return m_tocerr;
90  }
91 
93  void logthedate(bool onoff) {
94  m_logdate = onoff;
95  }
96 
97  bool loggingdate() const {
98  return m_logdate;
99  }
100 
103  void setdateformat(const std::string fmt) {
104  m_datefmt = fmt;
105  }
106 
108  const char *datestring();
109 
110 #if LOGGER_THREADSAFE
111  std::recursive_mutex& getmutex() {
112  return m_mutex;
113  }
114 #endif
115 
116 private:
117  bool m_tocerr{false};
118  bool m_logdate{false};
119  int m_loglevel{LLERR};
120  std::string m_datefmt{"%Y%m%d-%H%M%S"};
121  std::string m_fn;
122  std::ofstream m_stream;
123 #if LOGGER_THREADSAFE
124  std::recursive_mutex m_mutex;
125 #endif
126  char m_datebuf[LOGGER_DATESIZE];
127  Logger(const std::string& fn);
128  Logger(const Logger &);
129  Logger& operator=(const Logger &);
130 };
131 
132 #define LOGGER_PRT (Logger::getTheLog()->getstream())
133 
134 #if LOGGER_THREADSAFE
135 #define LOGGER_LOCK \
136  std::unique_lock<std::recursive_mutex> lock(Logger::getTheLog()->getmutex())
137 #else
138 #define LOGGER_LOCK
139 #endif
140 
141 #ifndef LOGGER_LOCAL_LOGINC
142 #define LOGGER_LOCAL_LOGINC 0
143 #endif
144 
145 #define LOGGER_LEVEL (Logger::getTheLog()->getloglevel() + \
146  LOGGER_LOCAL_LOGINC)
147 
148 #define LOGGER_DATE (Logger::getTheLog()->loggingdate() ? \
149  Logger::getTheLog()->datestring() : "")
150 
151 #define LOGGER_DOLOG(L,X) LOGGER_PRT << LOGGER_DATE << ":" << L << ":" << \
152  __FILE__ << ":" << __LINE__ << "::" << X \
153  << std::flush
154 
155 
156 #if LOGGER_STATICVERBOSITY >= 7
157 #define LOGDEB2(X) { \
158  if (LOGGER_LEVEL >= Logger::LLDEB2) { \
159  LOGGER_LOCK; \
160  LOGGER_DOLOG(Logger::LLDEB2, X); \
161  } \
162  }
163 #else
164 #define LOGDEB2(X)
165 #endif
166 
167 #if LOGGER_STATICVERBOSITY >= 6
168 #define LOGDEB1(X) { \
169  if (LOGGER_LEVEL >= Logger::LLDEB1) { \
170  LOGGER_LOCK; \
171  LOGGER_DOLOG(Logger::LLDEB1, X); \
172  } \
173  }
174 #else
175 #define LOGDEB1(X)
176 #endif
177 
178 #if LOGGER_STATICVERBOSITY >= 5
179 #define LOGDEB0(X) { \
180  if (LOGGER_LEVEL >= Logger::LLDEB0) { \
181  LOGGER_LOCK; \
182  LOGGER_DOLOG(Logger::LLDEB0, X); \
183  } \
184  }
185 #else
186 #define LOGDEB0(X)
187 #endif
188 
189 #if LOGGER_STATICVERBOSITY >= 4
190 #define LOGDEB(X) { \
191  if (LOGGER_LEVEL >= Logger::LLDEB) { \
192  LOGGER_LOCK; \
193  LOGGER_DOLOG(Logger::LLDEB, X); \
194  } \
195  }
196 #else
197 #define LOGDEB(X)
198 #endif
199 
200 #if LOGGER_STATICVERBOSITY >= 3
201 
205 #define LOGINF(X) { \
206  if (LOGGER_LEVEL >= Logger::LLINF) { \
207  LOGGER_LOCK; \
208  LOGGER_DOLOG(Logger::LLINF, X); \
209  } \
210  }
211 #else
212 #define LOGINF(X)
213 #endif
214 #define LOGINFO LOGINF
215 
216 #if LOGGER_STATICVERBOSITY >= 2
217 #define LOGERR(X) { \
218  if (LOGGER_LEVEL >= Logger::LLERR) { \
219  LOGGER_LOCK; \
220  LOGGER_DOLOG(Logger::LLERR, X); \
221  } \
222  }
223 #else
224 #define LOGERR(X)
225 #endif
226 
227 #if LOGGER_STATICVERBOSITY >= 1
228 #define LOGFAT(X) { \
229  if (LOGGER_LEVEL >= Logger::LLFAT) { \
230  LOGGER_LOCK; \
231  LOGGER_DOLOG(Logger::LLFAT, X); \
232  } \
233  }
234 #else
235 #define LOGFAT(X)
236 #endif
237 #define LOGFATAL LOGFAT
238 
239 #if defined(sun) || defined(_WIN32)
240 #define LOGSYSERR(who, what, arg) { \
241  LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \
242  ": " << strerror(errno) << std::endl); \
243  }
244 #else // not WINDOWS or sun
245 
246 inline char *_log_check_strerror_r(int, char *errbuf) {return errbuf;}
247 inline char *_log_check_strerror_r(char *cp, char *){return cp;}
248 
249 #define LOGSYSERR(who, what, arg) { \
250  char buf[200]; buf[0] = 0; \
251  LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \
252  ": " << _log_check_strerror_r( \
253  strerror_r(errno, buf, 200), buf) << std::endl); \
254  }
255 
256 #endif // not windows
257 
258 #endif /* _LOG_H_X_INCLUDED_ */
This is a singleton class.
Definition: log.h:45
void setdateformat(const std::string fmt)
Set the date format, as an strftime() format string.
Definition: log.h:103
LogLevel
Log level values.
Definition: log.h:67
int getloglevel() const
Retrieve the current log level.
Definition: log.h:80
std::ostream & getstream()
Retrieve the output stream in case you need to write directly to it.
Definition: log.h:60
void logthedate(bool onoff)
turn date logging on or off (default is off)
Definition: log.h:93
const std::string & getlogfilename() const
Retrieve current log file name.
Definition: log.h:84
bool reopen(const std::string &fn)
Close and reopen the output file.
Definition: log.cpp:36
void setLogLevel(LogLevel level)
Set the log dynamic verbosity level.
Definition: log.h:71
static Logger * getTheLog(const std::string &fn=std::string())
Initialize logging to file name.
Definition: log.cpp:75
bool logisstderr() const
Logging to stderr ?
Definition: log.h:88
void setloglevel(LogLevel level)
Set the log dynamic verbosity level.
Definition: log.h:75
const char * datestring()
Call with log locked.
Definition: log.cpp:62