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