Skip to content
Snippets Groups Projects
ConfigService.cpp 70.4 KiB
Newer Older
void ConfigServiceImpl::addObserver(
    const Poco::AbstractObserver &observer) const {
  m_notificationCenter.addObserver(observer);
}

/**  Remove an observer
 @param observer :: Reference to the observer to remove
 */
void ConfigServiceImpl::removeObserver(
    const Poco::AbstractObserver &observer) const {
  m_notificationCenter.removeObserver(observer);
/*
Gets the system proxy information
@url A url to match the proxy to
@return the proxy information.
*/
Kernel::ProxyInfo &ConfigServiceImpl::getProxy(const std::string &url) {
  if (!m_isProxySet) {
    // set the proxy
    // first check if the proxy is defined in the properties file
	  auto proxyHost = getValue<std::string>("proxy.host");
	  auto proxyPort = getValue<int>("proxy.port");



    if ( proxyHost.is_initialized() && proxyPort.is_initialized()) {
      // set it from the config values
      m_proxyInfo = ProxyInfo(proxyHost.get(), proxyPort.get(), true);
    } else {
      // get the system proxy
      Poco::URI uri(url);
      Mantid::Kernel::NetworkProxy proxyHelper;
      m_proxyInfo = proxyHelper.getHttpProxy(uri.toString());
    m_isProxySet = true;
  }
  return m_proxyInfo;
}

/** Sets the log level priority for the File log channel
* @param logLevel the integer value of the log level to set, 1=Critical, 7=Debug
*/
void ConfigServiceImpl::setFileLogLevel(int logLevel) {
  setFilterChannelLogLevel(m_filterChannels[0], logLevel);
}
/** Sets the log level priority for the Console log channel
* @param logLevel the integer value of the log level to set, 1=Critical, 7=Debug
*/
void ConfigServiceImpl::setConsoleLogLevel(int logLevel) {
  setFilterChannelLogLevel(m_filterChannels[1], logLevel);
}

/** Sets the Log level for a filter channel
* @param filterChannelName the channel name of the filter channel to change
* @param logLevel the integer value of the log level to set, 1=Critical, 7=Debug
* @param quiet If true then no message regarding the level change is emitted
* @throws std::invalid_argument if the channel name is incorrect or it is not a
* filterChannel
*/
void ConfigServiceImpl::setFilterChannelLogLevel(
    const std::string &filterChannelName, int logLevel, bool quiet) {
  Poco::Channel *channel = nullptr;
  try {
    channel = Poco::LoggingRegistry::defaultRegistry().channelForName(
        filterChannelName);
  } catch (Poco::NotFoundException &) {
    throw std::invalid_argument(filterChannelName +
                                " not found in the Logging Registry");
  auto *filterChannel = dynamic_cast<Poco::FilterChannel *>(channel);
  if (filterChannel) {
    filterChannel->setPriority(logLevel);
    int lowestLogLevel = FindLowestFilterLevel();
    int rootLevel = Poco::Logger::root().getLevel();
    if (rootLevel != lowestLogLevel) {
      Mantid::Kernel::Logger::setLevelForAll(lowestLogLevel);
    if (!quiet) {
      g_log.log(filterChannelName + " log channel set to " +
                    Logger::PriorityNames[logLevel] + " priority",
                static_cast<Logger::Priority>(logLevel));
    }
  } else {
    throw std::invalid_argument(filterChannelName +
                                " was not a filter channel");
/** Finds the lowest Log level for all registered filter channels
int ConfigServiceImpl::FindLowestFilterLevel() const {
  int lowestPriority = Logger::Priority::PRIO_FATAL;
  // Find the lowest level of all of the filter channels
  for (const auto &filterChannelName : m_filterChannels) {
Nick Draper's avatar
Nick Draper committed
      auto *channel = Poco::LoggingRegistry::defaultRegistry().channelForName(
          filterChannelName);
      auto *filterChannel = dynamic_cast<Poco::FilterChannel *>(channel);
      if (filterChannel) {
        int filterPriority = filterChannel->getPriority();
        if (filterPriority > lowestPriority) {
          lowestPriority = filterPriority;
        }
      }
    } catch (Poco::NotFoundException &) {
      g_log.warning(filterChannelName +
                    " registered log filter channel not found");
    }
  }

  return lowestPriority;
}
template DLLExport boost::optional<double> ConfigServiceImpl::getValue(const std::string &);
template DLLExport boost::optional<std::string> ConfigServiceImpl::getValue(const std::string &);
template DLLExport boost::optional<int> ConfigServiceImpl::getValue(const std::string &);
template DLLExport boost::optional<size_t> ConfigServiceImpl::getValue(const std::string &);
template DLLExport boost::optional<bool> ConfigServiceImpl::getValue(const std::string &);

} // namespace Kernel
} // namespace Mantid