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