RSC  0.17.1
Logger.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of RSC project
4  *
5  * Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <string>
30 #include <ostream>
31 #include <sstream>
32 
33 #include <boost/noncopyable.hpp>
34 #include <boost/shared_ptr.hpp>
35 
36 #include "rsc/rscexports.h"
37 
38 namespace rsc {
39 namespace logging {
40 
41 class Logger;
42 typedef boost::shared_ptr<Logger> LoggerPtr;
43 
54 class RSC_EXPORT Logger: public boost::noncopyable {
55 public:
56 
63  enum Level {
64  LEVEL_ALL = 0x11111111,
65  LEVEL_TRACE = 60,
66  LEVEL_DEBUG = 50,
67  LEVEL_INFO = 40,
68  LEVEL_WARN = 30,
69  LEVEL_ERROR = 20,
70  LEVEL_FATAL = 10,
71  LEVEL_OFF = 0
72  };
73 
74  virtual ~Logger();
75 
87  static LoggerPtr getLogger(const std::string& name);
88 
93 
99  virtual Level getLevel() const = 0;
100 
106  virtual void setLevel(const Level& level) = 0;
107 
113  virtual std::string getName() const = 0;
114 
120  virtual void setName(const std::string& name) = 0;
121 
123 
130 
136  virtual void trace(const std::string& msg);
137 
143  virtual void debug(const std::string& msg);
144 
150  virtual void info(const std::string& msg);
151 
157  virtual void warn(const std::string& msg);
158 
164  virtual void error(const std::string& msg);
165 
171  virtual void fatal(const std::string& msg);
172 
174 
181  virtual void log(const Level& level, const std::string& msg) = 0;
182 
189  virtual bool isTraceEnabled() const;
190  virtual bool isDebugEnabled() const;
191  virtual bool isInfoEnabled() const;
192  virtual bool isWarnEnabled() const;
193  virtual bool isErrorEnabled() const;
194  virtual bool isFatalEnabled() const;
195  virtual bool isEnabledFor(const Level& level) const;
197 
198 };
199 
200 RSC_EXPORT std::ostream& operator<<(std::ostream& stream, const Logger::Level& level);
201 
202 }
203 }
204 
209 
210 #define RSCTRACE(logger, msg) \
211  if (logger->isTraceEnabled()) { \
212  std::stringstream iShouldNeverBeMatchedByClientCode; \
213  iShouldNeverBeMatchedByClientCode << msg; \
214  logger->trace(iShouldNeverBeMatchedByClientCode.str()); \
215  }
216 
217 #define RSCDEBUG(logger, msg) \
218  if (logger->isDebugEnabled()) { \
219  std::stringstream iShouldNeverBeMatchedByClientCode; \
220  iShouldNeverBeMatchedByClientCode << msg; \
221  logger->debug(iShouldNeverBeMatchedByClientCode.str()); \
222  }
223 
224 #define RSCINFO(logger, msg) \
225  if (logger->isInfoEnabled()) { \
226  std::stringstream iShouldNeverBeMatchedByClientCode; \
227  iShouldNeverBeMatchedByClientCode << msg; \
228  logger->info(iShouldNeverBeMatchedByClientCode.str()); \
229  }
230 
231 #define RSCWARN(logger, msg) \
232  if (logger->isWarnEnabled()) { \
233  std::stringstream iShouldNeverBeMatchedByClientCode; \
234  iShouldNeverBeMatchedByClientCode << msg; \
235  logger->warn(iShouldNeverBeMatchedByClientCode.str()); \
236  }
237 
238 #define RSCERROR(logger, msg) \
239  if (logger->isErrorEnabled()) { \
240  std::stringstream iShouldNeverBeMatchedByClientCode; \
241  iShouldNeverBeMatchedByClientCode << msg; \
242  logger->error(iShouldNeverBeMatchedByClientCode.str()); \
243  }
244 
245 #define RSCFATAL(logger, msg) \
246  if (logger->isFatalEnabled()) { \
247  std::stringstream iShouldNeverBeMatchedByClientCode; \
248  iShouldNeverBeMatchedByClientCode << msg; \
249  logger->fatal(iShouldNeverBeMatchedByClientCode.str()); \
250  }
251 
253 
261 
262 #define RSCTRACE_EXPECT(condition, logger, msg) \
263  if (!(condition) && logger->isTraceEnabled()) { \
264  std::stringstream iShouldNeverBeMatchedByClientCode; \
265  iShouldNeverBeMatchedByClientCode << msg << "\nfailed condition: " << #condition; \
266  logger->trace(iShouldNeverBeMatchedByClientCode.str()); \
267  }
268 
269 #define RSCDEBUG_EXPECT(condition, logger, msg) \
270  if (!(condition) && logger->isDebugEnabled()) { \
271  std::stringstream iShouldNeverBeMatchedByClientCode; \
272  iShouldNeverBeMatchedByClientCode << msg << "\nfailed condition: " << #condition; \
273  logger->debug(iShouldNeverBeMatchedByClientCode.str()); \
274  }
275 
276 #define RSCINFO_EXPECT(condition, logger, msg) \
277  if (!(condition) && logger->isInfoEnabled()) { \
278  std::stringstream iShouldNeverBeMatchedByClientCode; \
279  iShouldNeverBeMatchedByClientCode << msg << "\nfailed condition: " << #condition; \
280  logger->info(iShouldNeverBeMatchedByClientCode.str()); \
281  }
282 
283 #define RSCWARN_EXPECT(condition, logger, msg) \
284  if (!(condition) && logger->isWarnEnabled()) { \
285  std::stringstream iShouldNeverBeMatchedByClientCode; \
286  iShouldNeverBeMatchedByClientCode << msg << "\nfailed condition: " << #condition; \
287  logger->warn(iShouldNeverBeMatchedByClientCode.str()); \
288  }
289 
290 #define RSCERROR_EXPECT(condition, logger, msg) \
291  if (!(condition) && logger->isErrorEnabled()) { \
292  std::stringstream iShouldNeverBeMatchedByClientCode; \
293  iShouldNeverBeMatchedByClientCode << msg << "\nfailed condition: " << #condition; \
294  logger->error(iShouldNeverBeMatchedByClientCode.str()); \
295  }
296 
297 #define RSCFATAL_EXPECT(condition, logger, msg) \
298  if (!(condition) && logger->isFatalEnabled()) { \
299  std::stringstream iShouldNeverBeMatchedByClientCode; \
300  iShouldNeverBeMatchedByClientCode << msg << "\nfailed condition: " << #condition; \
301  logger->fatal(iShouldNeverBeMatchedByClientCode.str()); \
302  }
303 
Level
Possible logging levels.
Definition: Logger.h:63
LoggerPtr getLogger()
ostream & operator<<(ostream &stream, const Logger::Level &level)
Definition: Logger.cpp:95
Interface for logging adapters that can be used with RSC.
Definition: Logger.h:54
boost::shared_ptr< Logger > LoggerPtr
Definition: Logger.h:41