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