virtual LoggerPtr getLogger(const std::string& name) { #if 1 thread_local LoggerPtr lastResult = nullptr; if (lastResult) { if (lastResult->getName() == name) { return lastResult; } } #endif LoggerPtr result; mutex.lock_shared(); auto findIt = loggers.find(name); if (findIt != loggers.end()) { result = findIt->second; } mutex.unlock_shared(); if (!result) { throw std::runtime_error("Logger not found"); } #if 1 lastResult = result; #endif return result; }
void Hierarchy::emitNoAppenderWarning(const LoggerPtr& logger) { // No appenders in hierarchy, warn user only once. if(!this->emittedNoAppenderWarning) { LogLog::warn(_T("No appenders could be found for logger (") + logger->getName() + _T(").")); LogLog::warn(_T("Please initialize the log4cxx system properly.")); this->emittedNoAppenderWarning = true; } }
virtual void registerLogger(LoggerPtr logger) { assert(logger); mutex.lock(); const auto& name = logger->getName(); if (loggers.count(name)) { throw std::runtime_error("Logger already register"); } loggers[name] = logger; mutex.unlock(); }
void Hierarchy::emitNoAppenderWarning(const LoggerPtr& logger) { bool emitWarning = false; { synchronized sync(mutex); emitWarning = !emittedNoAppenderWarning; emittedNoAppenderWarning = true; } // No appender in hierarchy, warn user only once. if(emitWarning) { LogLog::warn(((LogString) LOG4CXX_STR("No appender could be found for logger (")) + logger->getName() + LOG4CXX_STR(").")); LogLog::warn(LOG4CXX_STR("Please initialize the log4cxx system properly.")); } }
void Hierarchy::updateParents(LoggerPtr logger) { synchronized sync(mutex); const LogString name(logger->getName()); int length = name.size(); bool parentFound = false; // if name = "w.x.y.z", loop thourgh "w.x.y", "w.x" and "w", but not "w.x.y.z" for(size_t i = name.find_last_of(0x2E /* '.' */, length-1); i != LogString::npos; i = name.find_last_of(0x2E /* '.' */, i-1)) { LogString substr = name.substr(0, i); LoggerMap::iterator it = loggers->find(substr); if(it != loggers->end()) { parentFound = true; logger->parent = it->second; break; // no need to update the ancestors of the closest ancestor } else { ProvisionNodeMap::iterator it2 = provisionNodes->find(substr); if (it2 != provisionNodes->end()) { it2->second.push_back(logger); } else { ProvisionNode node(1, logger); provisionNodes->insert( ProvisionNodeMap::value_type(substr, node)); } } } // If we could not find any existing parents, then link with root. if(!parentFound) { logger->parent = root; } }
void FallbackErrorHandler::setLogger(const LoggerPtr& logger) { LogLog::debug(_T("FB: Adding logger [") + logger->getName() + _T("].")); loggers.push_back(logger); }