libupnpp  0.16.0
A C++ wrapper for the Portable UPnP reference library
log.h
1 /* Copyright (C) 2014 J.F.Dockes
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 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 General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the
14  * Free Software Foundation, Inc.,
15  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17 /* Copyright (C) 2006-2016 J.F.Dockes
18  *
19  * This library is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU Lesser General Public
21  * License as published by the Free Software Foundation; either
22  * version 2.1 of the License, or (at your option) any later version.
23  *
24  * This library is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27  * Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public
30  * License along with this library; if not, write to the Free Software
31  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32  * 02110-1301 USA
33  */
34 #ifndef _LOG_H_X_INCLUDED_
35 #define _LOG_H_X_INCLUDED_
36 
37 #include <fstream>
38 #include <iostream>
39 #include <string>
40 #include <mutex>
41 
42 #ifndef LOGGER_THREADSAFE
43 #define LOGGER_THREADSAFE 1
44 #endif
45 
46 #if LOGGER_THREADSAFE
47 #include <mutex>
48 #endif
49 
50 // Can't use the symbolic Logger::LLXX names in preproc. 6 is LLDEB1
51 #ifndef LOGGER_STATICVERBOSITY
52 #define LOGGER_STATICVERBOSITY 5
53 #endif
54 
55 class Logger {
56 public:
59  static Logger *getTheLog(const std::string& fn);
60 
61  bool reopen(const std::string& fn);
62 
63  std::ostream& getstream() {
64  return m_tocerr ? std::cerr : m_stream;
65  }
66  enum LogLevel {LLNON=0, LLFAT=1, LLERR=2, LLINF=3, LLDEB=4,
67  LLDEB0=5, LLDEB1=6, LLDEB2=7};
68  void setLogLevel(LogLevel level) {
69  m_loglevel = level;
70  }
71  int getloglevel() {
72  return m_loglevel;
73  }
74 
75 #if LOGGER_THREADSAFE
76  std::recursive_mutex& getmutex() {
77  return m_mutex;
78  }
79 #endif
80 
81 private:
82  bool m_tocerr;
83  int m_loglevel;
84  std::string m_fn;
85  std::ofstream m_stream;
86 #if LOGGER_THREADSAFE
87  std::recursive_mutex m_mutex;
88 #endif
89 
90  Logger(const std::string& fn);
91  Logger(const Logger &);
92  Logger& operator=(const Logger &);
93 };
94 
95 #define LOGGER_PRT (Logger::getTheLog("")->getstream())
96 
97 #if LOGGER_THREADSAFE
98 #define LOGGER_LOCK \
99  std::unique_lock<std::recursive_mutex> lock(Logger::getTheLog("")->getmutex())
100 #else
101 #define LOGGER_LOCK
102 #endif
103 
104 #ifndef LOGGER_LOCAL_LOGINC
105 #define LOGGER_LOCAL_LOGINC 0
106 #endif
107 
108 #define LOGGER_LEVEL (Logger::getTheLog("")->getloglevel() + \
109  LOGGER_LOCAL_LOGINC)
110 
111 #define LOGGER_DOLOG(L,X) LOGGER_PRT << ":" << L << ":" << \
112  __FILE__ << ":" << __LINE__ << "::" << X \
113  << std::flush
114 
115 #if LOGGER_STATICVERBOSITY >= 7
116 #define LOGDEB2(X) { \
117  if (LOGGER_LEVEL >= Logger::LLDEB2) { \
118  LOGGER_LOCK; \
119  LOGGER_DOLOG(Logger::LLDEB2, X); \
120  } \
121  }
122 #else
123 #define LOGDEB2(X)
124 #endif
125 
126 #if LOGGER_STATICVERBOSITY >= 6
127 #define LOGDEB1(X) { \
128  if (LOGGER_LEVEL >= Logger::LLDEB1) { \
129  LOGGER_LOCK; \
130  LOGGER_DOLOG(Logger::LLDEB1, X); \
131  } \
132  }
133 #else
134 #define LOGDEB1(X)
135 #endif
136 
137 #if LOGGER_STATICVERBOSITY >= 5
138 #define LOGDEB0(X) { \
139  if (LOGGER_LEVEL >= Logger::LLDEB0) { \
140  LOGGER_LOCK; \
141  LOGGER_DOLOG(Logger::LLDEB0, X); \
142  } \
143  }
144 #else
145 #define LOGDEB0(X)
146 #endif
147 
148 #if LOGGER_STATICVERBOSITY >= 4
149 #define LOGDEB(X) { \
150  if (LOGGER_LEVEL >= Logger::LLDEB) { \
151  LOGGER_LOCK; \
152  LOGGER_DOLOG(Logger::LLDEB, X); \
153  } \
154  }
155 #else
156 #define LOGDEB(X)
157 #endif
158 
159 #if LOGGER_STATICVERBOSITY >= 3
160 #define LOGINF(X) { \
161  if (LOGGER_LEVEL >= Logger::LLINF) { \
162  LOGGER_LOCK; \
163  LOGGER_DOLOG(Logger::LLINF, X); \
164  } \
165  }
166 #else
167 #define LOGINF(X)
168 #endif
169 #define LOGINFO LOGINF
170 
171 #if LOGGER_STATICVERBOSITY >= 2
172 #define LOGERR(X) { \
173  if (LOGGER_LEVEL >= Logger::LLERR) { \
174  LOGGER_LOCK; \
175  LOGGER_DOLOG(Logger::LLERR, X); \
176  } \
177  }
178 #else
179 #define LOGERR(X)
180 #endif
181 
182 #if LOGGER_STATICVERBOSITY >= 1
183 #define LOGFAT(X) { \
184  if (LOGGER_LEVEL >= Logger::LLFAT) { \
185  LOGGER_LOCK; \
186  LOGGER_DOLOG(Logger::LLFAT, X); \
187  } \
188  }
189 #else
190 #define LOGFAT(X)
191 #endif
192 #define LOGFATAL LOGFAT
193 
194 #endif /* _LOG_H_X_INCLUDED_ */
Definition: log.h:55
static Logger * getTheLog(const std::string &fn)
Initialize logging to file name.
Definition: log.cpp:59