Mantid
Loading...
Searching...
No Matches
LibraryManager.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11#include "MantidKernel/Logger.h"
12
13#include <Poco/DirectoryIterator.h>
14#include <Poco/File.h>
15#include <Poco/Path.h>
16#include <boost/algorithm/string.hpp>
17
18namespace Mantid::Kernel {
19namespace {
21Logger g_log("LibraryManager");
22} // namespace
23
25LibraryManagerImpl::LibraryManagerImpl() : m_openedLibs() { g_log.debug("LibraryManager created."); }
26
36int LibraryManagerImpl::openLibraries(const std::string &filepath, LoadLibraries loadingBehaviour,
37 const std::vector<std::string> &excludes) {
38 g_log.debug("Opening all libraries in " + filepath + "\n");
39 try {
40 return openLibraries(Poco::File(filepath), loadingBehaviour, excludes);
41 } catch (std::exception &exc) {
42 g_log.debug() << "Error occurred while opening libraries: " << exc.what() << "\n";
43 return 0;
44 } catch (...) {
45 g_log.error("An unknown error occurred while opening libraries.");
46 return 0;
47 }
48}
49
50//-------------------------------------------------------------------------
51// Private members
52//-------------------------------------------------------------------------
63int LibraryManagerImpl::openLibraries(const Poco::File &libpath, LibraryManagerImpl::LoadLibraries loadingBehaviour,
64 const std::vector<std::string> &excludes) {
65 int libCount(0);
66 if (libpath.exists() && libpath.isDirectory()) {
67 // Iterate over the available files
68 Poco::DirectoryIterator end_itr;
69 for (Poco::DirectoryIterator itr(libpath); itr != end_itr; ++itr) {
70 const Poco::File &item = *itr;
71 if (item.isFile()) {
72 if (shouldBeLoaded(itr.path().getFileName(), excludes))
73 libCount += openLibrary(itr.path(), itr.path().getFileName());
74 else
75 continue;
76 } else if (loadingBehaviour == LoadLibraries::Recursive) {
77 // it must be a directory
78 libCount += openLibraries(item, LoadLibraries::Recursive, excludes);
79 }
80 }
81 } else {
82 g_log.error("In OpenAllLibraries: " + libpath.path() + " must be a directory.");
83 }
84 return libCount;
85}
86
95bool LibraryManagerImpl::shouldBeLoaded(const std::string &filename, const std::vector<std::string> &excludes) const {
96 return !isLoaded(filename) && DllOpen::isValidFilename(filename) && !isExcluded(filename, excludes);
97}
98
104bool LibraryManagerImpl::isLoaded(const std::string &filename) const {
105 return m_openedLibs.find(filename) != m_openedLibs.cend();
106}
107
118bool LibraryManagerImpl::isExcluded(const std::string &filename, const std::vector<std::string> &excludes) const {
119 for (const auto &exclude : excludes) {
120 if (filename.find(exclude) != std::string::npos) {
121 return true;
122 }
123 }
124 return false;
125}
126
133int LibraryManagerImpl::openLibrary(const Poco::File &filepath, const std::string &cacheKey) {
134 // Try to open the library. The wrapper will unload the library when it
135 // is deleted
136 LibraryWrapper dlwrap;
137 if (dlwrap.openLibrary(filepath.path())) {
138 // Successfully opened, so add to map
139 if (g_log.is(Poco::Message::PRIO_DEBUG)) {
140 g_log.debug("Opened library: " + filepath.path() + ".\n");
141 }
142 m_openedLibs.emplace(cacheKey, std::move(dlwrap));
143 return 1;
144 } else
145 return 0;
146}
147
148} // namespace Mantid::Kernel
static bool isValidFilename(const std::string &filename)
Check if the filename conforms to the expected style for this platform.
Definition: DllOpen.cpp:88
bool isExcluded(const std::string &filename, const std::vector< std::string > &excludes) const
Returns true if the library has been requested to be excluded.
bool isLoaded(const std::string &filename) const
Check if the library has already been loaded.
int openLibrary(const Poco::File &filepath, const std::string &cacheKey)
Load a given library.
int openLibraries(const std::string &libpath, LoadLibraries loadingBehaviour, const std::vector< std::string > &excludes)
Opens suitable DLLs on a given path.
std::unordered_map< std::string, LibraryWrapper > m_openedLibs
Storage for the LibraryWrappers.
bool shouldBeLoaded(const std::string &filename, const std::vector< std::string > &excludes) const
Check if the library should be loaded.
LibraryManagerImpl()
Private Constructor.
Class for wrapping a shared library.
bool openLibrary(const std::string &filepath)
Opens a DLL.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
bool is(int level) const
Returns true if at least the given log level is set.
Definition: Logger.cpp:146