All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Logger.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/Logger.h
22 
23  \brief This class is designed to manage the log of information in TerraLib.
24  */
25 
26 #ifndef __TERRALIB_COMMON_INTERNAL_LOGGER_H
27 #define __TERRALIB_COMMON_INTERNAL_LOGGER_H
28 
29 // TerraLib
30 #include "Config.h"
31 
32 
33 /*!
34  \def TE_LOG_FATAL
35 
36  \brief Use this tag in order to log a message to a specified logger with the FATAL level.
37 
38  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
39 
40  \note The FATAL level designates very severe error events that will presumably lead the application to abort.
41 */
42 #if defined(TERRALIB_LOGGER_ENABLED) && defined(TERRALIB_LOGGER_FATAL_ENABLED)
43  #define TE_LOG_FATAL(msg) te::common::Logger::logFatal(TERRALIB_LOGGER_DEFAULT_NAME, msg)
44 #else
45  #define TE_LOG_FATAL(msg) ((void)0)
46 #endif
47 
48 /*!
49  \def TE_LOG_ASSERT
50 
51  \brief Use this tag in order to log a message to a specified logger with the ASSERT level.
52 
53  \param condition An expression (condition). If it is not true, the message will be logged.
54  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
55 
56  \note The ASSERT level can be used to check expressions that must be evaluated as true.
57 */
58 #if defined(TERRALIB_LOGGER_ENABLED) && defined(TERRALIB_LOGGER_ASSERT_ENABLED)
59  #define TE_LOG_ASSERT(condition, msg) te::common::Logger::logAssert(TERRALIB_LOGGER_DEFAULT_NAME, condition, msg)
60 #else
61  #define TE_LOG_ASSERT(condition, msg) ((void)0)
62 #endif
63 
64 /*!
65  \def TE_LOG_ERROR
66 
67  \brief Use this tag in order to log a message to a specified logger with the ERROR level.
68 
69  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
70 
71  \note The ERROR level designates error events that might still allow the application to continue running.
72 */
73 #if defined(TERRALIB_LOGGER_ENABLED) && defined(TERRALIB_LOGGER_ERROR_ENABLED)
74  #define TE_LOG_ERROR(msg) te::common::Logger::logError(TERRALIB_LOGGER_DEFAULT_NAME, msg)
75 #else
76  #define TE_LOG_ERROR(msg) ((void)0)
77 #endif
78 
79 /*!
80  \def TE_LOG_WARN
81 
82  \brief Use this tag in order to log a message to a specified logger with the WARN level.
83 
84  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
85 
86  \note The WARN level designates potentially harmful situations.
87 */
88 #if defined(TERRALIB_LOGGER_ENABLED) && defined(TERRALIB_LOGGER_WARN_ENABLED)
89  #define TE_LOG_WARN(msg) te::common::Logger::logWarning(TERRALIB_LOGGER_DEFAULT_NAME, msg)
90 #else
91  #define TE_LOG_WARN(msg) ((void)0)
92 #endif
93 
94 /*!
95  \def TE_LOG_INFO
96 
97  \brief Use this tag in order to log a message to a specified logger with the INFO level.
98 
99  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
100 
101  \note The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
102 */
103 #if defined(TERRALIB_LOGGER_ENABLED) && defined(TERRALIB_LOGGER_INFO_ENABLED)
104  #define TE_LOG_INFO(msg) te::common::Logger::logInfo(TERRALIB_LOGGER_DEFAULT_NAME, msg)
105 #else
106  #define TE_LOG_INFO(msg) ((void)0)
107 #endif
108 
109 /*!
110  \def TE_LOG_DEBUG
111 
112  \brief Use this tag in order to log a message to a specified logger with the DEBUG level.
113 
114  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
115 
116  \note The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
117 */
118 #if defined(TERRALIB_LOGGER_ENABLED) && defined(TERRALIB_LOGGER_DEBUG_ENABLED)
119  #define TE_LOG_DEBUG(msg) te::common::Logger::logDebug(TERRALIB_LOGGER_DEFAULT_NAME, msg)
120 #else
121  #define TE_LOG_DEBUG(msg) ((void)0)
122 #endif
123 
124 /*!
125  \def TE_LOG_TRACE
126 
127  \brief Use this tag in order to log a message to a specified logger with the TRACE level.
128 
129  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
130 
131  \note The TRACE Level designates finer-grained informational events than the DEBUG.
132 */
133 #if defined(TERRALIB_LOGGER_ENABLED) && defined(TERRALIB_LOGGER_TRACE_ENABLED)
134  #define TE_LOG_TRACE(msg) te::common::Logger::logTrace(TERRALIB_LOGGER_DEFAULT_NAME, msg)
135 #else
136  #define TE_LOG_TRACE(msg) ((void)0)
137 #endif
138 
139 /*!
140  \def TERRALIB_LOGGER_DEFAULT_NAME
141 
142  \brief This is the fully qualified TerraLib root logger.
143 
144  \note If you are developing a new module and want to have your
145  own log configuration file, please give it a name starting with "terralib.your-module-name". So it will
146  belong to TerraLib loggers tree.
147 */
148 #define TERRALIB_LOGGER_DEFAULT_NAME "terralib"
149 
150 /*!
151  \def TERRALIB_LOGGER_DEFAULT_CONFIG_FILE
152 
153  \brief If logger configuration is file based (text or XML), the file name (with its full path or relative path to TerraLib current directory).
154 */
155 #define TERRALIB_LOGGER_DEFAULT_CONFIGURATION_FILE "share/terralib/config/te-log.conf"
156 
157 /*!
158  \def TERRALIB_LOGGER_DEFAULT_CONFIG_FILE_TYPE
159 
160  \brief It sets the default type of file used to configure the logger.
161 
162  \note See LoggerConfigurationType enum for more information about possible values for this macro.
163 */
164 #define TERRALIB_LOGGER_DEFAULT_CONFIG_FILE_TYPE te::common::LOGGER_TXT_CONFIG
165 
166 #ifdef TERRALIB_LOGGER_ENABLED
167 
168 // TerraLib
169 #include "Enums.h"
170 #include "Static.h"
171 
172 // STL
173 #include <string>
174 
175 namespace te
176 {
177  namespace common
178  {
179  /*!
180  \class Logger
181 
182  \brief This class is designed to manage the log of information in TerraLib.
183 
184  You just need to register the name of your logger
185  and the configuration file and type used by it.
186  When ready, you call initLogger for setting up your logger.
187 
188  All loggers name should start with "terralib." in order to be
189  in the TerraLib logger tree. So, if you have an extension called Common,
190  the name of your logger could be "terralib.common". If the name
191  was WMS, you could use "terralib.wms" and so on.
192 
193  The use of log in TerraLib doesn't need to call methods of this class,
194  instead just use the macros to report to the log. It will allow you to turn the log off
195  when desired.
196 
197  Don't worry! Logging is used in TerraLib with so much care...
198  only in special places, in order to not slowdown its
199  performance. So we don't have any performance problem/penality by using a log.
200  And if you want, you can disable the logger so that no code is put into
201  the executable, see TERRALIB_LOGGER_ENABLED.
202 
203  \note The methods provided by this class must be used only after all static data has been initialized.
204 
205  \ingroup common
206  */
207  class TECOMMONEXPORT Logger : public Static
208  {
209  public:
210 
211  /** @name Configuration Methods
212  * Methods used to configure, initialize and finalize the logger.
213  */
214  //@{
215 
216  /*!
217  \brief It initializes a given logger based on its configuration properties.
218 
219  By default, if you not specify a logger configuration file, your new logger
220  will share the same configuration as the TerraLib tree of loggers.
221 
222  \param loggerName The fully qualified logger name. Example: terralib.common.
223  \param t The type of configuration to be used by the logger.
224  \param fileName If logger configuration is file based (text or XML), the file name (with its full path or the relative path to the TerraLib current directory).
225 
226  \exception Exception It raises an exception if the fileName is missing for a file based configuraton logger.
227  */
228  static void initialize(const std::string& loggerName,
229  const LoggerConfigurationType t,
230  const std::string& fileName);
231 
232  /*!
233  \brief It initializes a given logger based on its parent configuration.
234 
235  \param loggerName The fully qualified logger name. Example: terralib.common.
236 
237  \exception Exception It raises an exception if the fileName is missing.
238  */
239  static void initialize(const std::string& loggerName);
240 
241  /*!
242  \brief It removes the appenders and other stuffs from the logger and then eliminates them from the system.
243 
244  \param loggerName The fully qualified logger name. Example: terralib.common.
245  */
246  static void finalize(const std::string& loggerName);
247 
248  //@}
249 
250  /** @name Logging Methods
251  * Methods that can be used to send messages to the log. Please, see the logger macros defined in Config.h.
252  * There are a set of macros that can me used instead of using the methods of this class that can
253  * make things easier when disabling the logger support.
254  */
255  //@{
256 
257  /*!
258  \brief Use this class method in order to log a message to a specified logger with the FATAL level.
259 
260  \param logger The name of a logger. Example: "te.common".
261  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
262 
263  \note The FATAL level designates very severe error events that will presumably lead the application to abort.
264 
265  \note Avoid using this method. Try to use the macro TE_LOG_FATAL or TE_TASK_LOG_FATAL.
266  */
267  static void logFatal(const char* logger, const char* msg);
268 
269  /*!
270  \brief Use this class method in order to log a message to a specified logger with the FATAL level.
271 
272  \param logger The name of a logger. Example: "te.common".
273  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
274 
275  \note The FATAL level designates very severe error events that will presumably lead the application to abort.
276 
277  \note Avoid using this method. Try to use the macro TE_LOG_FATAL or TE_TASK_LOG_FATAL.
278  */
279  static void logFatal(const std::string& logger, const std::string& msg);
280 
281  /*!
282  \brief Use this class method in order to log a message to a specified logger with the ASSERT level.
283 
284  \param logger The name of a logger. Example: "te.common".
285  \param condition An expression (condition). If it is not true, the message will be logged.
286  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
287 
288  \note The ASSERT level can be used to check expressions that must be evaluated as true.
289 
290  \note Avoid using this method. Try to use the macro TE_LOG_ASSERT or TE_TASK_LOG_ASSERT.
291  */
292  static void logAssert(const char* logger, bool condition, const char* msg);
293 
294  /*!
295  \brief Use this class method in order to log a message to a specified logger with the ERROR level.
296 
297  \param logger The name of a logger. Example: "te.common".
298  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
299 
300  \note The ERROR level designates error events that might still allow the application to continue running.
301 
302  \note Avoid using this method. Try to use the macro TE_LOG_ERROR or TE_TASK_LOG_ERROR.
303  */
304  static void logError(const char* logger, const char* msg);
305 
306  /*!
307  \brief Use this class method in order to log a message to a specified logger with the WARNING level.
308 
309  \param logger The name of a logger. Example: "te.common".
310  \param msg The message to be logged. Example: "Exception raised because of a missing parameter!".
311 
312  \note The WARNING level designates potentially harmful situations.
313 
314  \note Avoid using this method. Try to use the macro TE_LOG_WARNING or TE_TASK_LOG_WARNING.
315  */
316  static void logWarning(const char* logger, const char* msg);
317 
318  /*!
319  \brief Use this class method in order to log a message to a specified logger with the INFO level.
320 
321  \param logger The name of a logger. Example: "te.common".
322  \param msg The message to be logged. Example: "Exception raised because of missing parameter!".
323 
324  \note The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
325 
326  \note Avoid using this method. Try to use the macro TE_LOG_INFO or TE_TASK_LOG_INFO.
327  */
328  static void logInfo(const char* logger, const char* msg);
329 
330  /*!
331  \brief Use this class method in order to log a message to a specified logger with the INFO level.
332 
333  \param logger The name of a logger. Example: "te.common".
334  \param msg The message to be logged. Example: "Exception raised because of missing parameter!".
335 
336  \note The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
337 
338  \note Avoid using this method. Try to use the macro TE_LOG_INFO or TE_TASK_LOG_INFO.
339  */
340  static void logInfo(const std::string& logger, const std::string& msg);
341 
342  /*!
343  \brief Use this clas smethod in order to log a message to a specified logger with the DEBUG level.
344 
345  \param logger The name of a logger. Example: "te.common".
346  \param msg The message to be logged. Example: "Exception raised because of missing parameter!".
347 
348  \note The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
349 
350  \note Avoid using this method. Try to use the macro TE_LOG_DEBUG or TE_TASK_LOG_DEBUG.
351  */
352  static void logDebug(const char* logger, const char* msg);
353 
354  /*!
355  \brief Use this class method in order to log a message to a specified logger with the TRACE level.
356 
357  \param logger The name of a logger. Example: "terralib".
358  \param msg The message to be logged. Example: "Exception raised because of missing parameter!".
359 
360  \note The TRACE Level designates finer-grained informational events than the DEBUG.
361 
362  \note Avoid using this method. Try to use the macro TE_LOG_TRACE or TE_TASK_LOG_TRACE.
363  */
364  static void logTrace(const char* logger, const char* msg);
365 
366  /*!
367  \brief Use this class method in order to log a message to a specified logger with the TRACE level.
368 
369  \param logger The name of a logger. Example: "terralib".
370  \param msg The message to be logged. Example: "Exception raised because of missing parameter!".
371 
372  \note The TRACE Level designates finer-grained informational events than the DEBUG.
373 
374  \note Avoid using this method. Try to use the macro TE_LOG_TRACE or TE_TASK_LOG_TRACE.
375  */
376  static void logTrace(const std::string& logger, const std::string& msg);
377 
378  //@}
379 
380  private:
381 
382  /** @name Instantiation of Objects
383  * Instantiation of objects don't allowed.
384  */
385  //@{
386 
387  /*! \brief As all private constructor, it doesn't allow direct instantiation of a Logger. */
388  //Logger();
389 
390  //@}
391  };
392 
393  } // end namespace common
394 } // end namespace te
395 
396 #endif // TERRALIB_LOGGER_ENABLED
397 
398 #endif // __TERRALIB_COMMON_INTERNAL_LOGGER_H
399 
Configuration flags for the TerraLib Common Runtime module.
A base type for static classes.
LoggerConfigurationType
Each enumerated type tells TerraLib how the configuration is done for a logger.
Definition: Enums.h:65
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:68