Logger.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file terralib/core/logger/Logger.h
23 
24  \brief This class is designed to manage the log of information in TerraLib.
25 
26  \author Matheus Cavassan Zaglia
27  \author Gilberto Ribeiro de Queiroz
28 */
29 
30 #ifndef __TERRALIB_CORE_LOGGER_LOGGER_H__
31 #define __TERRALIB_CORE_LOGGER_LOGGER_H__
32 
33 
34 // TerraLib
35 #include "../Config.h"
36 #include "../utils/Platform.h"
37 
38 // Boost
39 #include <boost/log/attributes/attribute_set.hpp>
40 #include <boost/log/trivial.hpp>
41 
42 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
43  #define CURRENT_FUNCTION std::string(__FUNCTION__)
44 #else
45  #define CURRENT_FUNCTION std::string(__PRETTY_FUNCTION__)
46 #endif
47 
48 /*!
49  \brief The default name of the log file if none is informed.
50  */
51 const std::string TERRALIB_DEFAULT_LOGGER = "terralib";
52 
53 /*!
54  \brief The default message format if none is informed.
55  */
56 const std::string TERRALIB_DEFAULT_LOGGER_FORMAT = "[%TimeStamp%] <%Severity%> %Message%" ;
57 
58 
59 namespace te
60 {
61  namespace core
62  {
64  {
65  /*!
66  \class Logger
67 
68  \brief This class is a singleton designed to manage log messages in TerraLib
69  */
70  public:
71 
73  {
79  fatal
80  };
81 
82  /*!
83  \brief It returns a reference to the singleton instance.
84 
85  \return A reference to the singleton instance.
86  */
87  static Logger& instance();
88 
89  /*!
90  \brief It logs a given string message, channel and severity.
91 
92  \param message The string message to be logged.
93  \param channel The channel name that will receive the message.
94  \param severity The severity of the logged message.
95  */
96  void log(const std::string& message, const std::string &channel, severity_level severity);
97 
98  /*!
99  \brief It sets the logger configuration from a given file.
100 
101  \param filename The name of the configuration file.
102 
103  \exception std::exception If the configuration file is doesn't load.
104  */
105  void addLoggerFromFile(const std::string &filename);
106 
107  /*!
108  \brief It sets the logger using a default implementation.
109 
110  \param name The name of the logger.
111  \param filename The name of the log file.
112  \param format The format string of the logger.
113 
114  \exception te::InvalidArgumentException If the logger name is empty or already registered
115 
116  \note The logs will be stored in the given file name. If the file already
117  exists, the logs will be appended to the end of the file.
118  */
119  void addLogger(const std::string &name, const std::string &filename, std::string format);
120 
121  /*!
122  \brief Checks if exists a logger registered with the given name.
123 
124  \param name The name to be checked.
125 
126  \return True if a logger with the given name, false if not.
127  */
128  bool exists(const std::string &name);
129 
130  /*!
131  \brief It removes all added loggers.
132  */
134 
135  /*!
136  \brief Sets the severity filter for the logger
137  */
139 
140  private:
141 
142  /*! \brief Singleton constructor must be private or protected. */
144 
145  /*! \brief Singleton destructor must be private or protected. */
147 
148  /*! \brief Singleton copy constructor must be private or protected. */
149  Logger(Logger const&); // No copy allowed
150 
151  /*! \brief Singleton copy assignment operator must be private or protected. */
152  Logger& operator=(Logger const&); // No copy allowed
153 
154  /*!
155  \brief Convert te::security_level to boost::security_level
156  */
157  boost::log::trivial::severity_level toBoostSecurityLevel(te::core::Logger::severity_level severity);
158  /*!
159  \brief Convert te::security_level to human readable name
160  */
162 
163  private:
164 
165  struct Impl;
166 
167  Impl* m_pimpl;
168 
169  std::pair< boost::log::attribute_set::iterator, bool > m_process;
170  std::pair< boost::log::attribute_set::iterator, bool > m_processId;
171  std::pair< boost::log::attribute_set::iterator, bool > m_threadId;
172  };
173  } //end namespace core
174 } //end namespace te
175 
176 
177 /*!
178  \def TE_ADD_LOGGER
179 
180  \brief Use this tag to init a logger using a default implementation without a configuration file.
181 
182  \param name The name of the logger.
183  \param filename The name of the log file.
184  \param format The format string of the logger.
185  */
186 #ifdef TERRALIB_LOGGER_ENABLED
187  #define TE_ADD_LOGGER(name, filename, format) te::core::Logger::instance().addLogger(name, filename, format)
188 #else
189  #define TE_ADD_LOGGER(name, filename, format) ((void)0)
190 #endif
191 
192 /*!
193  \def TE_ADD_LOGGER_FROM_FILE
194 
195  \brief Use this tag to init a logger using a configuration file.
196 
197  \param name The name of the logger.
198  \param filename The name of the configuration file.
199 
200  \exception std::exception If the configuration file is doesn't load.
201  */
202 #ifdef TERRALIB_LOGGER_ENABLED
203  #define TE_ADD_LOGGER_FROM_FILE(filename) te::core::Logger::instance().addLoggerFromFile(filename)
204 #else
205  #define TE_ADD_LOGGER_FROM_FILE(filename) ((void)0)
206 #endif
207 
208 /*!
209  \def TE_INIT_DEFAULT_LOGGER
210 
211  \brief Use this tag in order to initialize the default TerraLib logger.
212 
213  \param filename The name of the log file.
214  */
215 #ifdef TERRALIB_LOGGER_ENABLED
216  #define TE_INIT_DEFAULT_LOGGER(filename) te::core::Logger::instance().addLogger(TERRALIB_DEFAULT_LOGGER, filename, TERRALIB_DEFAULT_LOGGER_FORMAT)
217 #else
218  #define TE_INIT_DEFAULT_LOGGER(filename) ((void)0)
219 #endif
220 
221 /*!
222  \def TE_CORE_LOG_TRACE
223 
224  \brief Use this tag in order to log a message to a specified logger with the TRACE level.
225 
226  \param channel The name of your logger.
227  \param message The message to be logged.
228  */
229 #ifdef TERRALIB_LOGGER_TRACE_ENABLED
230  #define TE_CORE_LOG_TRACE(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::trace)
231 #else
232  #define TE_CORE_LOG_TRACE(channel, message) ((void)0)
233 #endif
234 
235 /*!
236  \def TE_CORE_LOG_DEBUG
237 
238  \brief Use this tag in order to log a message to a specified logger with the DEBUG level.
239 
240  \param channel The name of your logger.
241  \param message The message to be logged.
242  */
243 #ifdef TERRALIB_LOGGER_DEBUG_ENABLED
244  #define TE_CORE_LOG_DEBUG(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::debug)
245 #else
246  #define TE_CORE_LOG_DEBUG(channel, message) ((void)0)
247 #endif
248 
249 /*!
250  \def TE_CORE_LOG_INFO
251 
252  \brief Use this tag in order to log a message to a specified logger with the INFO level.
253 
254  \param channel The name of your logger.
255  \param message The message to be logged.
256  */
257 #ifdef TERRALIB_LOGGER_INFO_ENABLED
258  #define TE_CORE_LOG_INFO(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::info)
259 #else
260  #define TE_CORE_LOG_INFO(channel, message) ((void)0)
261 #endif
262 
263 /*!
264  \def TE_CORE_LOG_WARN
265 
266  \brief Use this tag in order to log a message to a specified logger with the WARN level.
267 
268  \param channel The name of your logger.
269  \param message The message to be logged.
270  */
271 #ifdef TERRALIB_LOGGER_WARN_ENABLED
272  #define TE_CORE_LOG_WARN(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::warning)
273 #else
274  #define TE_CORE_LOG_WARN(channel, message) ((void)0)
275 #endif
276 
277 /*!
278  \def TE_CORE_LOG_ERROR
279 
280  \brief Use this tag in order to log a message to a specified logger with the ERROR level.
281 
282  \param channel The name of your logger.
283  \param message The message to be logged.
284  */
285 #ifdef TERRALIB_LOGGER_ERROR_ENABLED
286  #define TE_CORE_LOG_ERROR(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::error)
287 #else
288  #define TE_CORE_LOG_ERROR(channel, message) ((void)0)
289 #endif
290 
291 /*!
292  \def TE_CORE_LOG_FATAL
293 
294  \brief Use this tag in order to log a message to a specified logger with the FATAL level.
295 
296  \param channel The name of your logger.
297  \param message The message to be logged.
298  */
299 #ifdef TERRALIB_LOGGER_FATAL_ENABLED
300  #define TE_CORE_LOG_FATAL(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::fatal)
301 #else
302  #define TE_CORE_LOG_FATAL(channel, message) ((void)0)
303 #endif
304 
305 /*!
306  \def TE_LOG_TRACE
307 
308  \brief Use this tag in order to log a message to the TerraLib default logger with the TRACE level.
309 
310  \param message The message to be logged.
311 
312  \note The TRACE Level designates finer-grained informational events than the DEBUG.
313 */
314 #define TE_LOG_TRACE(message) TE_CORE_LOG_TRACE(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
315 
316 /*!
317  \def TE_LOG_DEBUG
318 
319  \brief Use this tag in order to log a message to the TerraLib default logger with the DEBUG level.
320 
321  \param message The message to be logged.
322 
323  \note The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
324 */
325 #define TE_LOG_DEBUG(message) TE_CORE_LOG_DEBUG(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
326 
327 /*!
328  \def TE_LOG_INFO
329 
330  \brief Use this tag in order to log a message to the TerraLib default logger with the INFO level.
331 
332  \param message The message to be logged.
333 
334  \note The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
335 */
336 #define TE_LOG_INFO(message) TE_CORE_LOG_INFO(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
337 
338 /*!
339  \def TE_LOG_WARN
340 
341  \brief Use this tag in order to log a message to the TerraLib default logger with the WARN level.
342 
343  \param message The message to be logged.
344 
345  \note The WARN level designates potentially harmful situations.
346 */
347 #define TE_LOG_WARN(message)TE_CORE_LOG_WARN(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
348 
349 /*!
350  \def TE_LOG_ERROR
351 
352  \brief Use this tag in order to log a message to the TerraLib default logger with the ERROR level.
353 
354  \param message The message to be logged.
355 
356  \note The ERROR level designates error events that might still allow the application to continue running.
357 */
358 #define TE_LOG_ERROR(message) TE_CORE_LOG_ERROR(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
359 
360 /*!
361  \def TE_LOG_FATAL
362 
363  \brief Use this tag in order to log a message to the TerraLib default logger with the FATAL level.
364 
365  \param message The message to be logged.
366 
367  \note The FATAL level designates very severe error events that will presumably lead the application to abort.
368 */
369 #define TE_LOG_FATAL(message) TE_CORE_LOG_FATAL(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
370 
371 /*!
372  \def TE_SET_LOGGER_FILTER
373 
374  \brief Sets the logger severity level filter. All levels with lesser severity then the givin one are discarded. The default severity level is Warning.
375 
376  \param severity The base severity to be logged. All levels with lesser severity are discarded
377  */
378 #ifdef TERRALIB_LOGGER_ENABLED
379 #define TE_SET_LOGGER_FILTER(severity) te::core::Logger::instance().setFilter(severity);
380 #else
381 #define TE_SET_LOGGER_FILTER(severity) ((void)0)
382 #endif
383 
384 #endif // __TERRALIB_CORE_LOGGER_LOGGER_H__
te::core::Logger::log
void log(const std::string &message, const std::string &channel, severity_level severity)
It logs a given string message, channel and severity.
te::core::Logger::getLoggingLevelName
std::string getLoggingLevelName(te::core::Logger::severity_level severity)
Convert te::security_level to human readable name.
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::core::Logger::warning
@ warning
Definition: Logger.h:77
te::core::Logger::Logger
Logger()
Singleton constructor must be private or protected.
te::core::Logger::Logger
Logger(Logger const &)
Singleton copy constructor must be private or protected.
TECOREEXPORT
#define TECOREEXPORT
Definition: Config.h:52
te::core::Logger::m_processId
std::pair< boost::log::attribute_set::iterator, bool > m_processId
Definition: Logger.h:170
te::core::Logger::instance
static Logger & instance()
It returns a reference to the singleton instance.
te::core::Logger::exists
bool exists(const std::string &name)
Checks if exists a logger registered with the given name.
te::core::Logger::~Logger
~Logger()
Singleton destructor must be private or protected.
te::core::Logger::trace
@ trace
Definition: Logger.h:74
te::core::Logger::m_process
std::pair< boost::log::attribute_set::iterator, bool > m_process
Definition: Logger.h:169
te::core::Logger::toBoostSecurityLevel
boost::log::trivial::severity_level toBoostSecurityLevel(te::core::Logger::severity_level severity)
Convert te::security_level to boost::security_level.
te::core::Logger::severity_level
severity_level
Definition: Logger.h:73
te::core::Logger::addLoggerFromFile
void addLoggerFromFile(const std::string &filename)
It sets the logger configuration from a given file.
TERRALIB_DEFAULT_LOGGER
const std::string TERRALIB_DEFAULT_LOGGER
The default name of the log file if none is informed.
Definition: Logger.h:51
te::core::Logger::removeAllLoggers
void removeAllLoggers()
It removes all added loggers.
te::core::Logger::debug
@ debug
Definition: Logger.h:75
te::core::Logger::addLogger
void addLogger(const std::string &name, const std::string &filename, std::string format)
It sets the logger using a default implementation.
te::core::Logger::m_threadId
std::pair< boost::log::attribute_set::iterator, bool > m_threadId
Definition: Logger.h:171
TERRALIB_DEFAULT_LOGGER_FORMAT
const std::string TERRALIB_DEFAULT_LOGGER_FORMAT
The default message format if none is informed.
Definition: Logger.h:56
te::core::Logger::setFilter
void setFilter(te::core::Logger::severity_level severity)
Sets the severity filter for the logger.
te::core::Logger::info
@ info
Definition: Logger.h:76
te::core::Logger
Definition: Logger.h:64
te::core::Logger::m_pimpl
Impl * m_pimpl
Definition: Logger.h:165
te::core::Logger::error
@ error
Definition: Logger.h:78
te::core::Logger::operator=
Logger & operator=(Logger const &)
Singleton copy assignment operator must be private or protected.