Skip to content
Snippets Groups Projects
ConfigService.cpp 71.2 KiB
Newer Older
      // get the system proxy
      Poco::URI uri(url);
      Mantid::Kernel::NetworkProxy proxyHelper;
      m_proxyInfo = proxyHelper.getHttpProxy(uri.toString());
    m_isProxySet = true;
  }
  return m_proxyInfo;
}

std::string ConfigServiceImpl::getFullPath(const std::string &filename,
                                           const bool ignoreDirs,
                                           Poco::Glob::Options options) const {
  std::string fName = Kernel::Strings::strip(filename);
  g_log.debug() << "getFullPath(" << fName << ")\n";
  // If this is already a full path, nothing to do
  if (Poco::Path(fName).isAbsolute())
    return fName;

  // First try the path relative to the current directory. Can throw in some
  // circumstances with extensions that have wild cards
  try {
    Poco::File fullPath(Poco::Path().resolve(fName));
    if (fullPath.exists() && (!ignoreDirs || !fullPath.isDirectory()))
      return fullPath.path();
  } catch (std::exception &) {
  }

  const std::vector<std::string> &searchPaths =
      Kernel::ConfigService::Instance().getDataSearchDirs();
  for (const auto &searchPath : searchPaths) {
    g_log.debug() << "Searching for " << fName << " in " << searchPath << "\n";
// On windows globbing is note working properly with network drives
// for example a network drive containing a $
// For this reason, and since windows is case insensitive anyway
// a special case is made for windows
#ifdef _WIN32
    if (fName.find("*") != std::string::npos) {
#endif
      Poco::Path path(searchPath, fName);
      std::set<std::string> files;
      Kernel::Glob::glob(path, files, options);
      if (!files.empty()) {
        Poco::File matchPath(*files.begin());
        if (ignoreDirs && matchPath.isDirectory()) {
          continue;
        }
        return *files.begin();
      }
#ifdef _WIN32
    } else {
      Poco::Path path(searchPath, fName);
      Poco::File file(path);
      if (file.exists() && !(ignoreDirs && file.isDirectory())) {
        return path.toString();
      }
    }
#endif
  }
  return "";
}

/** 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
Owen Arnold's avatar
Owen Arnold committed
* @param quiet do not emit log messages
* @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 int ConfigServiceImpl::getValue(const std::string &,
                                                   double &);
template DLLExport int ConfigServiceImpl::getValue(const std::string &,
                                                   std::string &);
template DLLExport int ConfigServiceImpl::getValue(const std::string &, int &);
template DLLExport int ConfigServiceImpl::getValue(const std::string &,
                                                   std::size_t &);

} // namespace Kernel
} // namespace Mantid