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 
41 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
42  #define CURRENT_FUNCTION std::string(__FUNCTION__)
43 #else
44  #define CURRENT_FUNCTION std::string(__PRETTY_FUNCTION__)
45 #endif
46 
47 /*!
48  \brief The default name of the log file if none is informed.
49  */
50 const std::string TERRALIB_DEFAULT_LOGGER = "terralib";
51 
52 /*!
53  \brief The default message format if none is informed.
54  */
55 const std::string TERRALIB_DEFAULT_LOGGER_FORMAT = "[%TimeStamp%] <%Severity%> %Message%" ;
56 
57 namespace te
58 {
59  namespace core
60  {
62  {
63  /*!
64  \class Logger
65 
66  \brief This class is a singleton designed to manage log messages in TerraLib
67  */
68  public:
69 
71  {
77  fatal
78  };
79 
80  /*!
81  \brief It returns a reference to the singleton instance.
82 
83  \return A reference to the singleton instance.
84  */
85  static Logger& instance();
86 
87  /*!
88  \brief It logs a given string message, channel and severity.
89 
90  \param message The string message to be logged.
91  \param channel The channel name that will receive the message.
92  \param severity The severity of the logged message.
93  */
94  void log(const std::string& message, const std::string &channel, severity_level severity);
95 
96  /*!
97  \brief It sets the logger configuration from a given file.
98 
99  \param filename The name of the configuration file.
100 
101  \exception std::exception If the configuration file is doesn't load.
102  */
103  void addLoggerFromFile(const std::string &filename);
104 
105  /*!
106  \brief It sets the logger using a default implementation.
107 
108  \param name The name of the logger.
109  \param filename The name of the log file.
110  \param format The format string of the logger.
111 
112  \exception te::InvalidArgumentException If the logger name is empty or already registered
113 
114  \note The logs will be stored in the given file name. If the file already
115  exists, the logs will be appended to the end of the file.
116  */
117  void addLogger(const std::string &name, const std::string &filename, std::string format);
118 
119  /*!
120  \brief Checks if exists a logger registered with the given name.
121 
122  \param name The name to be checked.
123 
124  \return True if a logger with the given name, false if not.
125  */
126  bool exists(const std::string &name);
127 
128  /*!
129  \brief It removes all added loggers.
130  */
132 
133  /*!
134  \brief Sets the severity filter for the logger
135  */
137 
138  private:
139 
140  /*! \brief Singleton constructor must be private or protected. */
142 
143  /*! \brief Singleton destructor must be private or protected. */
145 
146  /*! \brief Singleton copy constructor must be private or protected. */
147  Logger(Logger const&); // No copy allowed
148 
149  /*! \brief Singleton copy assignment operator must be private or protected. */
150  Logger& operator=(Logger const&); // No copy allowed
151 
152  private:
153 
154  struct Impl;
155 
156  Impl* m_pimpl;
157 
158  std::pair< boost::log::attribute_set::iterator, bool > m_process;
159  std::pair< boost::log::attribute_set::iterator, bool > m_processId;
160  std::pair< boost::log::attribute_set::iterator, bool > m_threadId;
161  };
162  } //end namespace core
163 } //end namespace te
164 
165 
166 /*!
167  \def TE_ADD_LOGGER
168 
169  \brief Use this tag to init a logger using a default implementation without a configuration file.
170 
171  \param name The name of the logger.
172  \param filename The name of the log file.
173  \param format The format string of the logger.
174  */
175 #ifdef TERRALIB_LOGGER_ENABLED
176  #define TE_ADD_LOGGER(name, filename, format) te::core::Logger::instance().addLogger(name, filename, format)
177 #else
178  #define TE_ADD_LOGGER(name, filename, format) ((void)0)
179 #endif
180 
181 /*!
182  \def TE_ADD_LOGGER_FROM_FILE
183 
184  \brief Use this tag to init a logger using a configuration file.
185 
186  \param name The name of the logger.
187  \param filename The name of the configuration file.
188 
189  \exception std::exception If the configuration file is doesn't load.
190  */
191 #ifdef TERRALIB_LOGGER_ENABLED
192  #define TE_ADD_LOGGER_FROM_FILE(filename) te::core::Logger::instance().addLoggerFromFile(filename)
193 #else
194  #define TE_ADD_LOGGER_FROM_FILE(filename) ((void)0)
195 #endif
196 
197 /*!
198  \def TE_INIT_DEFAULT_LOGGER
199 
200  \brief Use this tag in order to initialize the default TerraLib logger.
201 
202  \param filename The name of the log file.
203  */
204 #ifdef TERRALIB_LOGGER_ENABLED
205  #define TE_INIT_DEFAULT_LOGGER(filename) te::core::Logger::instance().addLogger(TERRALIB_DEFAULT_LOGGER, filename, TERRALIB_DEFAULT_LOGGER_FORMAT)
206 #else
207  #define TE_INIT_DEFAULT_LOGGER(filename) ((void)0)
208 #endif
209 
210 /*!
211  \def TE_CORE_LOG_TRACE
212 
213  \brief Use this tag in order to log a message to a specified logger with the TRACE level.
214 
215  \param channel The name of your logger.
216  \param message The message to be logged.
217  */
218 #ifdef TERRALIB_LOGGER_TRACE_ENABLED
219  #define TE_CORE_LOG_TRACE(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::trace)
220 #else
221  #define TE_CORE_LOG_TRACE(channel, message) ((void)0)
222 #endif
223 
224 /*!
225  \def TE_CORE_LOG_DEBUG
226 
227  \brief Use this tag in order to log a message to a specified logger with the DEBUG level.
228 
229  \param channel The name of your logger.
230  \param message The message to be logged.
231  */
232 #ifdef TERRALIB_LOGGER_DEBUG_ENABLED
233  #define TE_CORE_LOG_DEBUG(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::debug)
234 #else
235  #define TE_CORE_LOG_DEBUG(channel, message) ((void)0)
236 #endif
237 
238 /*!
239  \def TE_CORE_LOG_INFO
240 
241  \brief Use this tag in order to log a message to a specified logger with the INFO level.
242 
243  \param channel The name of your logger.
244  \param message The message to be logged.
245  */
246 #ifdef TERRALIB_LOGGER_INFO_ENABLED
247  #define TE_CORE_LOG_INFO(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::info)
248 #else
249  #define TE_CORE_LOG_INFO(channel, message) ((void)0)
250 #endif
251 
252 /*!
253  \def TE_CORE_LOG_WARN
254 
255  \brief Use this tag in order to log a message to a specified logger with the WARN level.
256 
257  \param channel The name of your logger.
258  \param message The message to be logged.
259  */
260 #ifdef TERRALIB_LOGGER_WARN_ENABLED
261  #define TE_CORE_LOG_WARN(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::warning)
262 #else
263  #define TE_CORE_LOG_WARN(channel, message) ((void)0)
264 #endif
265 
266 /*!
267  \def TE_CORE_LOG_ERROR
268 
269  \brief Use this tag in order to log a message to a specified logger with the ERROR level.
270 
271  \param channel The name of your logger.
272  \param message The message to be logged.
273  */
274 #ifdef TERRALIB_LOGGER_ERROR_ENABLED
275  #define TE_CORE_LOG_ERROR(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::error)
276 #else
277  #define TE_CORE_LOG_ERROR(channel, message) ((void)0)
278 #endif
279 
280 /*!
281  \def TE_CORE_LOG_FATAL
282 
283  \brief Use this tag in order to log a message to a specified logger with the FATAL level.
284 
285  \param channel The name of your logger.
286  \param message The message to be logged.
287  */
288 #ifdef TERRALIB_LOGGER_FATAL_ENABLED
289  #define TE_CORE_LOG_FATAL(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::fatal)
290 #else
291  #define TE_CORE_LOG_FATAL(channel, message) ((void)0)
292 #endif
293 
294 /*!
295  \def TE_LOG_TRACE
296 
297  \brief Use this tag in order to log a message to the TerraLib default logger with the TRACE level.
298 
299  \param message The message to be logged.
300 
301  \note The TRACE Level designates finer-grained informational events than the DEBUG.
302 */
303 #define TE_LOG_TRACE(message) TE_CORE_LOG_TRACE(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
304 
305 /*!
306  \def TE_LOG_DEBUG
307 
308  \brief Use this tag in order to log a message to the TerraLib default logger with the DEBUG level.
309 
310  \param message The message to be logged.
311 
312  \note The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
313 */
314 #define TE_LOG_DEBUG(message) TE_CORE_LOG_DEBUG(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
315 
316 /*!
317  \def TE_LOG_INFO
318 
319  \brief Use this tag in order to log a message to the TerraLib default logger with the INFO level.
320 
321  \param message The message to be logged.
322 
323  \note The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
324 */
325 #define TE_LOG_INFO(message) TE_CORE_LOG_INFO(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
326 
327 /*!
328  \def TE_LOG_WARN
329 
330  \brief Use this tag in order to log a message to the TerraLib default logger with the WARN level.
331 
332  \param message The message to be logged.
333 
334  \note The WARN level designates potentially harmful situations.
335 */
336 #define TE_LOG_WARN(message)TE_CORE_LOG_WARN(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
337 
338 /*!
339  \def TE_LOG_ERROR
340 
341  \brief Use this tag in order to log a message to the TerraLib default logger with the ERROR level.
342 
343  \param message The message to be logged.
344 
345  \note The ERROR level designates error events that might still allow the application to continue running.
346 */
347 #define TE_LOG_ERROR(message) TE_CORE_LOG_ERROR(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
348 
349 /*!
350  \def TE_LOG_FATAL
351 
352  \brief Use this tag in order to log a message to the TerraLib default logger with the FATAL level.
353 
354  \param message The message to be logged.
355 
356  \note The FATAL level designates very severe error events that will presumably lead the application to abort.
357 */
358 #define TE_LOG_FATAL(message) TE_CORE_LOG_FATAL(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
359 
360 /*!
361  \def TE_SET_LOGGER_FILTER
362 
363  \brief Sets the logger severity level filter. All levels with lesser severity then the givin one are discarded. The default severity level is Warning.
364 
365  \param severity The base severity to be logged. All levels with lesser severity are discarded
366  */
367 #ifdef TERRALIB_LOGGER_ENABLED
368 #define TE_SET_LOGGER_FILTER(severity) te::core::Logger::instance().setFilter(severity);
369 #else
370 #define TE_SET_LOGGER_FILTER(severity) ((void)0)
371 #endif
372 
373 #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
TerraLib.
Definition: AddressGeocodingOp.h:52
te::core::Logger::warning
@ warning
Definition: Logger.h:75
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:159
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:72
te::core::Logger::m_process
std::pair< boost::log::attribute_set::iterator, bool > m_process
Definition: Logger.h:158
te::core::Logger::severity_level
severity_level
Definition: Logger.h:71
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:50
te::core::Logger::removeAllLoggers
void removeAllLoggers()
It removes all added loggers.
te::core::Logger::debug
@ debug
Definition: Logger.h:73
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:160
TERRALIB_DEFAULT_LOGGER_FORMAT
const std::string TERRALIB_DEFAULT_LOGGER_FORMAT
The default message format if none is informed.
Definition: Logger.h:55
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:74
te::core::Logger
Definition: Logger.h:62
te::core::Logger::m_pimpl
Impl * m_pimpl
Definition: Logger.h:154
te::core::Logger::error
@ error
Definition: Logger.h:76
te::core::Logger::operator=
Logger & operator=(Logger const &)
Singleton copy assignment operator must be private or protected.