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