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