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) 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 #ifdef TERRALIB_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 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
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 void te::common::Logger::initialize(const std::string& loggerName,
55  const std::string& fileName)
56 {
57 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
58  finalize(loggerName);
59 
60  if(fileName.empty())
61  return;
62 
63  //if(fileName.empty())
64  // throw Exception(TE_TR("You must specify a logger configuration file!"));
65 
66  log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger(loggerName));
67 
68  if(logger == 0)
69  throw Exception(TE_TR("It was not possible to initialize the logger!"));
70 
71  if(t == LOGGER_XML_CONFIG)
72  {
73  log4cxx::xml::DOMConfigurator::configure(fileName);
74  }
75  else if(t == LOGGER_TXT_CONFIG)
76  {
77  log4cxx::PropertyConfigurator::configure(fileName);
78  }
79  else
80  {
81  throw Exception(TE_TR("Invalid logger configuration type!"));
82  }
83 #endif
84 }
85 
86 void te::common::Logger::initialize(const std::string& loggerName)
87 {
88 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
89  finalize(loggerName);
90 
91  log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger(loggerName));
92 
93  if(logger == 0)
94  throw Exception(TE_TR("It was not possible to initialize the the logger!"));
95 #endif
96 }
97 
98 void te::common::Logger::finalize(const std::string& loggerName)
99 {
100 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
101  if(loggerName.empty())
102  throw Exception(TE_TR("The logger name is empty!"));
103 
104  log4cxx::LoggerPtr logger = log4cxx::LogManager::exists(loggerName);
105 
106  if(logger == 0)
107  return; // the logger doesn't exist in the hierarchy, so we don't need to finalize anything!
108 
109  logger->removeAllAppenders();
110 #endif
111 }
112 
113 void te::common::Logger::logFatal(const char* logger, const char* msg)
114 {
115 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
116  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
117  LOG4CXX_FATAL(lo, msg);
118 #endif
119 }
120 
121 void te::common::Logger::logFatal(const std::string& logger, const std::string& msg)
122 {
123  logFatal(logger.c_str(), msg.c_str());
124 }
125 
126 void te::common::Logger::logAssert(const char* logger, bool condition, const char* msg)
127 {
128 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
129  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
130  LOG4CXX_ASSERT(lo, condition, msg);
131 #endif
132 }
133 
134 void te::common::Logger::logError(const char* logger, const char* msg)
135 {
136 #if TE_USE_APACHE_LOG4CXX
137  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
138  LOG4CXX_ERROR(lo, msg);
139 #endif
140 }
141 
142 void te::common::Logger::logWarning(const char* logger, const char* msg)
143 {
144 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
145  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
146  LOG4CXX_WARN(lo, msg);
147 #endif
148 }
149 
150 void te::common::Logger::logInfo(const char* logger, const char* msg)
151 {
152 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
153  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
154  LOG4CXX_INFO(lo, msg);
155 #endif
156 }
157 
158 void te::common::Logger::logInfo(const std::string& logger, const std::string& msg)
159 {
160  logInfo(logger.c_str(), msg.c_str());
161 }
162 
163 void te::common::Logger::logDebug(const char* logger, const char* msg)
164 {
165 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
166  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
167  LOG4CXX_DEBUG(lo, msg);
168 #endif
169 }
170 
171 void te::common::Logger::logTrace(const char* logger, const char* msg)
172 {
173 #ifdef TERRALIB_APACHE_LOG4CXX_ENABLED
174  log4cxx::LoggerPtr lo(log4cxx::Logger::getLogger(logger));
175  LOG4CXX_TRACE(lo, msg);
176 #endif
177 }
178 
179 void te::common::Logger::logTrace(const std::string& logger, const std::string& msg)
180 {
181  logTrace(logger.c_str(), msg.c_str());
182 }
183 
184 #endif // TERRALIB_LOGGER_ENABLED
185 
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
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.