All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Logger.cpp
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.cpp
22 
23  \brief This class is designed to manage the log of information in TerraLib.
24  */
25 
26 // TerraLib
27 #include "Logger.h"
28 
29 #ifdef TERRALIB_LOGGER_ENABLED
30 
31 // TerraLib
32 #include "Exception.h"
33 #include "Translator.h"
34 
35 #include "terralib_config.h"
36 
37 // STL
38 #include <cassert>
39 #include <cstdlib>
40 
41 // Boost
42 #include <boost/filesystem.hpp>
43 
44 // Apache Log4CXX
45 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
46 #include <log4cxx/basicconfigurator.h>
47 #include <log4cxx/consoleappender.h>
48 #include <log4cxx/logger.h>
49 #include <log4cxx/logmanager.h>
50 #include <log4cxx/patternlayout.h>
51 #include <log4cxx/propertyconfigurator.h>
52 #include <log4cxx/xml/domconfigurator.h>
53 #endif
54 
55 void te::common::Logger::initialize(const std::string& loggerName,
57  const std::string& fileName)
58 {
59 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
60  finalize(loggerName);
61 
62  if(fileName.empty())
63  return;
64 
65  //if(fileName.empty())
66  // throw Exception(TE_TR("You must specify a logger configuration file!"));
67 
68  log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger(loggerName));
69 
70  if(logger == 0)
71  throw Exception(TE_TR("It was not possible to initialize the logger!"));
72 
73  if(t == LOGGER_XML_CONFIG)
74  {
75  log4cxx::xml::DOMConfigurator::configure(fileName);
76  }
77  else if(t == LOGGER_TXT_CONFIG)
78  {
79  log4cxx::PropertyConfigurator::configure(fileName);
80  }
81  else
82  {
83  throw Exception(TE_TR("Invalid logger configuration type!"));
84  }
85 #endif
86 }
87 
88 void te::common::Logger::initialize(const std::string& loggerName)
89 {
90 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
91  finalize(loggerName);
92 
93  log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger(loggerName));
94 
95  if(logger == 0)
96  throw Exception(TE_TR("It was not possible to initialize the the logger!"));
97 #endif
98 }
99 
100 void te::common::Logger::finalize(const std::string& loggerName)
101 {
102 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
103  if(loggerName.empty())
104  throw Exception(TE_TR("The logger name is empty!"));
105 
106  log4cxx::LoggerPtr logger = log4cxx::LogManager::exists(loggerName);
107 
108  if(logger == 0)
109  return; // the logger doesn't exist in the hierarchy, so we don't need to finalize anything!
110 
111  logger->removeAllAppenders();
112 #endif
113 }
114 
115 void te::common::Logger::logFatal(const char* logger, const char* msg)
116 {
117 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
118  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
119  LOG4CXX_FATAL(lo, msg);
120 #endif
121 }
122 
123 void te::common::Logger::logFatal(const std::string& logger, const std::string& msg)
124 {
125  logFatal(logger.c_str(), msg.c_str());
126 }
127 
128 void te::common::Logger::logAssert(const char* logger, bool condition, const char* msg)
129 {
130 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
131  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
132  LOG4CXX_ASSERT(lo, condition, msg);
133 #endif
134 }
135 
136 void te::common::Logger::logError(const char* logger, const char* msg)
137 {
138 #if TE_USE_APACHE_LOG4CXX
139  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
140  LOG4CXX_ERROR(lo, msg);
141 #endif
142 }
143 
144 void te::common::Logger::logWarning(const char* logger, const char* msg)
145 {
146 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
147  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
148  LOG4CXX_WARN(lo, msg);
149 #endif
150 }
151 
152 void te::common::Logger::logInfo(const char* logger, const char* msg)
153 {
154 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
155  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
156  LOG4CXX_INFO(lo, msg);
157 #endif
158 }
159 
160 void te::common::Logger::logInfo(const std::string& logger, const std::string& msg)
161 {
162  logInfo(logger.c_str(), msg.c_str());
163 }
164 
165 void te::common::Logger::logDebug(const char* logger, const char* msg)
166 {
167 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
168  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
169  LOG4CXX_DEBUG(lo, msg);
170 #endif
171 }
172 
173 void te::common::Logger::logTrace(const char* logger, const char* msg)
174 {
175 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
176  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
177  LOG4CXX_TRACE(lo, msg);
178 #endif
179 }
180 
181 void te::common::Logger::logTrace(const std::string& logger, const std::string& msg)
182 {
183  logTrace(logger.c_str(), msg.c_str());
184 }
185 
186 #endif // TERRALIB_LOGGER_ENABLED
187 
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
LoggerConfigurationType
Each enumerated type tells TerraLib how the configuration is done for a logger.
Definition: Enums.h:65
This class is designed for dealing with multi-language text translation in TerraLib.
This class is designed to manage the log of information in TerraLib.