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  */
131  void removeAllLoggers();
132 
133  private:
134 
135  /*! \brief Singleton constructor must be private or protected. */
136  Logger();
137 
138  /*! \brief Singleton destructor must be private or protected. */
139  ~Logger();
140 
141  /*! \brief Singleton copy constructor must be private or protected. */
142  Logger(Logger const&); // No copy allowed
143 
144  /*! \brief Singleton copy assignment operator must be private or protected. */
145  Logger& operator=(Logger const&); // No copy allowed
146 
147  private:
148 
149  struct Impl;
150 
151  Impl* m_pimpl;
152 
153  std::pair< boost::log::attribute_set::iterator, bool > m_process;
154  std::pair< boost::log::attribute_set::iterator, bool > m_processId;
155  std::pair< boost::log::attribute_set::iterator, bool > m_threadId;
156  };
157  } //end namespace core
158 } //end namespace te
159 
160 
161 /*!
162  \def TE_ADD_LOGGER
163 
164  \brief Use this tag to init a logger using a default implementation without a configuration file.
165 
166  \param name The name of the logger.
167  \param filename The name of the log file.
168  \param format The format string of the logger.
169  */
170 #ifdef TERRALIB_LOGGER_ENABLED
171  #define TE_ADD_LOGGER(name, filename, format) te::core::Logger::instance().addLogger(name, filename, format)
172 #else
173  #define TE_ADD_LOGGER(name, filename, format) ((void)0)
174 #endif
175 
176 /*!
177  \def TE_ADD_LOGGER_FROM_FILE
178 
179  \brief Use this tag to init a logger using a configuration file.
180 
181  \param name The name of the logger.
182  \param filename The name of the configuration file.
183 
184  \exception std::exception If the configuration file is doesn't load.
185  */
186 #ifdef TERRALIB_LOGGER_ENABLED
187  #define TE_ADD_LOGGER_FROM_FILE(filename) te::core::Logger::instance().addLoggerFromFile(filename)
188 #else
189  #define TE_ADD_LOGGER_FROM_FILE(filename) ((void)0)
190 #endif
191 
192 /*!
193  \def TE_INIT_DEFAULT_LOGGER
194 
195  \brief Use this tag in order to initialize the default TerraLib logger.
196 
197  \param filename The name of the log file.
198  */
199 #ifdef TERRALIB_LOGGER_ENABLED
200  #define TE_INIT_DEFAULT_LOGGER(filename) te::core::Logger::instance().addLogger(TERRALIB_DEFAULT_LOGGER, filename, TERRALIB_DEFAULT_LOGGER_FORMAT)
201 #else
202  #define TE_INIT_DEFAULT_LOGGER(filename) ((void)0)
203 #endif
204 
205 /*!
206  \def TE_CORE_LOG_TRACE
207 
208  \brief Use this tag in order to log a message to a specified logger with the TRACE level.
209 
210  \param channel The name of your logger.
211  \param message The message to be logged.
212  */
213 #ifdef TERRALIB_LOGGER_TRACE_ENABLED
214  #define TE_CORE_LOG_TRACE(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::trace)
215 #else
216  #define TE_CORE_LOG_TRACE(channel, message) ((void)0)
217 #endif
218 
219 /*!
220  \def TE_CORE_LOG_DEBUG
221 
222  \brief Use this tag in order to log a message to a specified logger with the DEBUG level.
223 
224  \param channel The name of your logger.
225  \param message The message to be logged.
226  */
227 #ifdef TERRALIB_LOGGER_DEBUG_ENABLED
228  #define TE_CORE_LOG_DEBUG(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::debug)
229 #else
230  #define TE_CORE_LOG_DEBUG(channel, message) ((void)0)
231 #endif
232 
233 /*!
234  \def TE_CORE_LOG_INFO
235 
236  \brief Use this tag in order to log a message to a specified logger with the INFO level.
237 
238  \param channel The name of your logger.
239  \param message The message to be logged.
240  */
241 #ifdef TERRALIB_LOGGER_INFO_ENABLED
242  #define TE_CORE_LOG_INFO(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::info)
243 #else
244  #define TE_CORE_LOG_INFO(channel, message) ((void)0)
245 #endif
246 
247 /*!
248  \def TE_CORE_LOG_WARN
249 
250  \brief Use this tag in order to log a message to a specified logger with the WARN level.
251 
252  \param channel The name of your logger.
253  \param message The message to be logged.
254  */
255 #ifdef TERRALIB_LOGGER_WARN_ENABLED
256  #define TE_CORE_LOG_WARN(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::warning)
257 #else
258  #define TE_CORE_LOG_WARN(channel, message) ((void)0)
259 #endif
260 
261 /*!
262  \def TE_CORE_LOG_ERROR
263 
264  \brief Use this tag in order to log a message to a specified logger with the ERROR level.
265 
266  \param channel The name of your logger.
267  \param message The message to be logged.
268  */
269 #ifdef TERRALIB_LOGGER_ERROR_ENABLED
270  #define TE_CORE_LOG_ERROR(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::error)
271 #else
272  #define TE_CORE_LOG_ERROR(channel, message) ((void)0)
273 #endif
274 
275 /*!
276  \def TE_CORE_LOG_FATAL
277 
278  \brief Use this tag in order to log a message to a specified logger with the FATAL level.
279 
280  \param channel The name of your logger.
281  \param message The message to be logged.
282  */
283 #ifdef TERRALIB_LOGGER_FATAL_ENABLED
284  #define TE_CORE_LOG_FATAL(channel, message) te::core::Logger::instance().log(message, channel ,te::core::Logger::severity_level::fatal)
285 #else
286  #define TE_CORE_LOG_FATAL(channel, message) ((void)0)
287 #endif
288 
289 /*!
290  \def TE_LOG_TRACE
291 
292  \brief Use this tag in order to log a message to the TerraLib default logger with the TRACE level.
293 
294  \param message The message to be logged.
295 
296  \note The TRACE Level designates finer-grained informational events than the DEBUG.
297 */
298 #define TE_LOG_TRACE(message) TE_CORE_LOG_TRACE(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
299 
300 /*!
301  \def TE_LOG_DEBUG
302 
303  \brief Use this tag in order to log a message to the TerraLib default logger with the DEBUG level.
304 
305  \param message The message to be logged.
306 
307  \note The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
308 */
309 #define TE_LOG_DEBUG(message) TE_CORE_LOG_DEBUG(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
310 
311 /*!
312  \def TE_LOG_INFO
313 
314  \brief Use this tag in order to log a message to the TerraLib default logger with the INFO level.
315 
316  \param message The message to be logged.
317 
318  \note The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
319 */
320 #define TE_LOG_INFO(message) TE_CORE_LOG_INFO(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
321 
322 /*!
323  \def TE_LOG_WARN
324 
325  \brief Use this tag in order to log a message to the TerraLib default logger with the WARN level.
326 
327  \param message The message to be logged.
328 
329  \note The WARN level designates potentially harmful situations.
330 */
331 #define TE_LOG_WARN(message)TE_CORE_LOG_WARN(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
332 
333 /*!
334  \def TE_LOG_ERROR
335 
336  \brief Use this tag in order to log a message to the TerraLib default logger with the ERROR level.
337 
338  \param message The message to be logged.
339 
340  \note The ERROR level designates error events that might still allow the application to continue running.
341 */
342 #define TE_LOG_ERROR(message) TE_CORE_LOG_ERROR(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
343 
344 /*!
345  \def TE_LOG_FATAL
346 
347  \brief Use this tag in order to log a message to the TerraLib default logger with the FATAL level.
348 
349  \param message The message to be logged.
350 
351  \note The FATAL level designates very severe error events that will presumably lead the application to abort.
352 */
353 #define TE_LOG_FATAL(message) TE_CORE_LOG_FATAL(TERRALIB_DEFAULT_LOGGER, CURRENT_FUNCTION + " : " + message)
354 
355 #endif // __TERRALIB_CORE_LOGGER_LOGGER_H__
Impl * m_pimpl
Definition: Logger.h:149
const std::string TERRALIB_DEFAULT_LOGGER
The default name of the log file if none is informed.
Definition: Logger.h:50
const std::string TERRALIB_DEFAULT_LOGGER_FORMAT
The default message format if none is informed.
Definition: Logger.h:55
std::pair< boost::log::attribute_set::iterator, bool > m_processId
Definition: Logger.h:154
#define TECOREEXPORT
Definition: Config.h:52
TerraLib.
std::pair< boost::log::attribute_set::iterator, bool > m_process
Definition: Logger.h:153
std::pair< boost::log::attribute_set::iterator, bool > m_threadId
Definition: Logger.h:155