blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00038 #include "blocxx/BLOCXX_config.h" 00039 #include "blocxx/Logger.hpp" 00040 #include "blocxx/ExceptionIds.hpp" 00041 #include "blocxx/LogMessage.hpp" 00042 #include "blocxx/Assertion.hpp" 00043 #include "blocxx/Array.hpp" 00044 #include "blocxx/LogMessagePatternFormatter.hpp" 00045 #include "blocxx/AppenderLogger.hpp" 00046 #include "blocxx/LogAppender.hpp" 00047 #include "blocxx/Format.hpp" 00048 00049 namespace BLOCXX_NAMESPACE 00050 { 00051 00052 BLOCXX_DEFINE_EXCEPTION_WITH_ID(Logger); 00053 00054 const GlobalString Logger::STR_NONE_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("NONE"); 00055 const GlobalString Logger::STR_FATAL_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("FATAL"); 00056 const GlobalString Logger::STR_ERROR_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("ERROR"); 00057 const GlobalString Logger::STR_WARNING_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("WARNING"); 00058 const GlobalString Logger::STR_INFO_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("INFO"); 00059 const GlobalString Logger::STR_DEBUG_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("DEBUG"); 00060 const GlobalString Logger::STR_DEBUG2_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("DEBUG2"); 00061 const GlobalString Logger::STR_DEBUG3_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("DEBUG3"); 00062 const GlobalString Logger::STR_ALL_CATEGORY = BLOCXX_GLOBAL_STRING_INIT("ALL"); 00063 const GlobalString Logger::STR_DEFAULT_COMPONENT = BLOCXX_GLOBAL_STRING_INIT("none"); 00064 00066 Logger::Logger(const String& defaultComponent, const LogAppenderRef& appender) 00067 : m_defaultComponent(defaultComponent) 00068 , m_appender(appender ? appender : LogAppender::getCurrentLogAppender()) 00069 , m_logLevel(m_appender->getLogLevel()) 00070 { 00071 BLOCXX_ASSERT(m_defaultComponent.length()); 00072 } 00073 00075 Logger::Logger(const String& defaultComponent, const ELogLevel logLevel) 00076 : m_defaultComponent(defaultComponent) 00077 , m_appender(LogAppender::getCurrentLogAppender()) 00078 , m_logLevel(logLevel) 00079 { 00080 BLOCXX_ASSERT(m_defaultComponent.length()); 00081 } 00082 00084 Logger::Logger(const Logger& x) 00085 : IntrusiveCountableBase(x) 00086 , m_defaultComponent(x.m_defaultComponent) 00087 , m_appender(x.m_appender) 00088 , m_logLevel(x.m_logLevel) 00089 { 00090 } 00091 00093 Logger& 00094 Logger::operator=(const Logger& x) 00095 { 00096 m_defaultComponent = x.m_defaultComponent; 00097 m_appender = x.m_appender; 00098 m_logLevel = x.m_logLevel; 00099 00100 return *this; 00101 } 00102 00104 void 00105 Logger::swap(Logger& x) 00106 { 00107 m_defaultComponent.swap(x.m_defaultComponent); 00108 m_appender.swap(x.m_appender); 00109 std::swap(m_logLevel, x.m_logLevel); 00110 } 00111 00113 Logger::~Logger() 00114 { 00115 } 00116 00118 LoggerRef 00119 Logger::clone() const 00120 { 00121 return LoggerRef(new Logger(*this)); 00122 } 00123 00125 void 00126 Logger::logFatalError(const String& message, const char* filename, int fileline, const char* methodname) const 00127 { 00128 if (m_logLevel >= E_FATAL_ERROR_LEVEL) 00129 { 00130 processLogMessage( LogMessage(m_defaultComponent, STR_FATAL_CATEGORY, message, filename, fileline, methodname) ); 00131 } 00132 } 00133 00135 void 00136 Logger::logError(const String& message, const char* filename, int fileline, const char* methodname) const 00137 { 00138 if (m_logLevel >= E_ERROR_LEVEL) 00139 { 00140 processLogMessage( LogMessage(m_defaultComponent, STR_ERROR_CATEGORY, message, filename, fileline, methodname) ); 00141 } 00142 } 00143 00145 void 00146 Logger::logWarning(const String& message, const char* filename, int fileline, const char* methodname) const 00147 { 00148 if (m_logLevel >= E_WARNING_LEVEL) 00149 { 00150 processLogMessage( LogMessage(m_defaultComponent, STR_WARNING_CATEGORY, message, filename, fileline, methodname) ); 00151 } 00152 } 00153 00155 void 00156 Logger::logInfo(const String& message, const char* filename, int fileline, const char* methodname) const 00157 { 00158 if (m_logLevel >= E_INFO_LEVEL) 00159 { 00160 processLogMessage( LogMessage(m_defaultComponent, STR_INFO_CATEGORY, message, filename, fileline, methodname) ); 00161 } 00162 } 00163 00165 void 00166 Logger::logDebug(const String& message, const char* filename, int fileline, const char* methodname) const 00167 { 00168 if (m_logLevel >= E_DEBUG_LEVEL) 00169 { 00170 processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG_CATEGORY, message, filename, fileline, methodname) ); 00171 } 00172 } 00173 00175 void 00176 Logger::logDebug2(const String& message, const char* filename, int fileline, const char* methodname) const 00177 { 00178 if (m_logLevel >= E_DEBUG2_LEVEL) 00179 { 00180 processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG2_CATEGORY, message, filename, fileline, methodname) ); 00181 } 00182 } 00183 00185 void 00186 Logger::logDebug3(const String& message, const char* filename, int fileline, const char* methodname) const 00187 { 00188 if (m_logLevel >= E_DEBUG3_LEVEL) 00189 { 00190 processLogMessage( LogMessage(m_defaultComponent, STR_DEBUG3_CATEGORY, message, filename, fileline, methodname) ); 00191 } 00192 } 00193 00195 void 00196 Logger::logMessage(const String& component, const String& category, const String& message) const 00197 { 00198 processLogMessage(LogMessage(component, category, message, 0, -1, 0)); 00199 } 00200 00202 void 00203 Logger::logMessage(const String& component, const String& category, const String& message, const char* filename, int fileline, const char* methodname) const 00204 { 00205 processLogMessage(LogMessage(component, category, message, filename, fileline, methodname)); 00206 } 00207 00209 void 00210 Logger::logMessage(const String& category, const String& message) const 00211 { 00212 processLogMessage(LogMessage(m_defaultComponent, category, message, 0, -1, 0)); 00213 } 00214 00216 void 00217 Logger::logMessage(const String& category, const String& message, const char* filename, int fileline, const char* methodname) const 00218 { 00219 processLogMessage(LogMessage(m_defaultComponent, category, message, filename, fileline, methodname)); 00220 } 00221 00223 void 00224 Logger::logMessage(const LogMessage& message) const 00225 { 00226 processLogMessage(message); 00227 } 00228 00230 void 00231 Logger::setDefaultComponent(const String& component) 00232 { 00233 BLOCXX_ASSERT(component != ""); 00234 m_defaultComponent = component; 00235 } 00236 00238 String 00239 Logger::getDefaultComponent() const 00240 { 00241 return m_defaultComponent; 00242 } 00243 00245 void 00246 Logger::setLogLevel(ELogLevel logLevel) 00247 { 00248 m_logLevel = logLevel; 00249 } 00250 00252 void 00253 Logger::setLogLevel(const String& l) 00254 { 00255 setLogLevel(stringToLogLevel(l)); 00256 } 00257 00259 ELogLevel 00260 Logger::stringToLogLevel(const String& l) 00261 { 00262 if (l.equalsIgnoreCase(STR_INFO_CATEGORY)) 00263 { 00264 return E_INFO_LEVEL; 00265 } 00266 else if (l.equalsIgnoreCase(STR_DEBUG_CATEGORY)) 00267 { 00268 return E_DEBUG_LEVEL; 00269 } 00270 else if (l.equalsIgnoreCase(STR_DEBUG2_CATEGORY)) 00271 { 00272 return E_DEBUG2_LEVEL; 00273 } 00274 else if (l.equalsIgnoreCase(STR_DEBUG3_CATEGORY)) 00275 { 00276 return E_DEBUG3_LEVEL; 00277 } 00278 else if (l.equalsIgnoreCase(STR_ERROR_CATEGORY)) 00279 { 00280 return E_ERROR_LEVEL; 00281 } 00282 else if (l.equalsIgnoreCase(STR_WARNING_CATEGORY)) 00283 { 00284 return E_WARNING_LEVEL; 00285 } 00286 else if (l.equalsIgnoreCase(STR_ALL_CATEGORY)) 00287 { 00288 return E_ALL_LEVEL; 00289 } 00290 else if (l.equalsIgnoreCase(STR_NONE_CATEGORY)) 00291 { 00292 return E_NONE_LEVEL; 00293 } 00294 else 00295 { 00296 return E_FATAL_ERROR_LEVEL; 00297 } 00298 } 00299 00301 String 00302 Logger::logLevelToString(ELogLevel logLevel) 00303 { 00304 switch (logLevel) 00305 { 00306 case E_ALL_LEVEL: 00307 return STR_ALL_CATEGORY; 00308 case E_DEBUG3_LEVEL: 00309 return STR_DEBUG3_CATEGORY; 00310 case E_DEBUG2_LEVEL: 00311 return STR_DEBUG2_CATEGORY; 00312 case E_DEBUG_LEVEL: 00313 return STR_DEBUG_CATEGORY; 00314 case E_WARNING_LEVEL: 00315 return STR_WARNING_CATEGORY; 00316 case E_INFO_LEVEL: 00317 return STR_INFO_CATEGORY; 00318 case E_ERROR_LEVEL: 00319 return STR_ERROR_CATEGORY; 00320 case E_FATAL_ERROR_LEVEL: 00321 return STR_FATAL_CATEGORY; 00322 default: 00323 return STR_NONE_CATEGORY; 00324 } 00325 } 00326 00328 bool 00329 Logger::categoryIsEnabled(const String& category) const 00330 { 00331 return m_appender->categoryIsEnabled(category); 00332 } 00333 00335 bool 00336 Logger::levelIsEnabled(const ELogLevel level) const 00337 { 00338 return (getLogLevel() >= level); 00339 } 00340 00342 bool 00343 Logger::componentAndCategoryAreEnabled(const String& component, const String& category) const 00344 { 00345 return m_appender->componentAndCategoryAreEnabled(component, category); 00346 } 00347 00349 void 00350 Logger::processLogMessage(const LogMessage& message) const 00351 { 00352 BLOCXX_ASSERT(!message.component.empty()); 00353 BLOCXX_ASSERT(!message.category.empty()); 00354 BLOCXX_ASSERT(!message.message.empty()); 00355 00356 m_appender->logMessage(message); 00357 } 00358 00359 } // end namespace BLOCXX_NAMESPACE 00360