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 <filesystem>
14
15namespace Mantid::Kernel {
16namespace {
18Logger g_log("LibraryManager");
19} // namespace
20
22LibraryManagerImpl::LibraryManagerImpl() : m_openedLibs() { g_log.debug("LibraryManager created."); }
23
33int LibraryManagerImpl::openLibraries(const std::string &filepath, LoadLibraries loadingBehaviour,
34 const std::vector<std::string> &excludes) {
35 g_log.debug("Opening all libraries in " + filepath + "\n");
36 try {
37 return openLibraries(std::filesystem::path(filepath), loadingBehaviour, excludes);
38 } catch (std::exception &exc) {
39 g_log.debug() << "Error occurred while opening libraries: " << exc.what() << "\n";
40 return 0;
41 } catch (...) {
42 g_log.error("An unknown error occurred while opening libraries.");
43 return 0;
44 }
45}
46
47//-------------------------------------------------------------------------
48// Private members
49//-------------------------------------------------------------------------
60int LibraryManagerImpl::openLibraries(const std::filesystem::path &libpath,
61 LibraryManagerImpl::LoadLibraries loadingBehaviour,
62 const std::vector<std::string> &excludes) {
63 int libCount(0);
64 if (std::filesystem::exists(libpath) && std::filesystem::is_directory(libpath)) {
65 // Iterate over the available files
66 for (const auto &file : std::filesystem::directory_iterator(libpath)) {
67 const auto &path = file.path();
68 if (std::filesystem::is_regular_file(path)) {
69 if (shouldBeLoaded(path.filename().string(), excludes))
70 libCount += openLibrary(path.string(), path.filename().string());
71 else
72 continue;
73 } else if (loadingBehaviour == LoadLibraries::Recursive) {
74 // it must be a directory
75 libCount += openLibraries(path, LoadLibraries::Recursive, excludes);
76 }
77 }
78 } else {
79 g_log.error("In OpenAllLibraries: " + libpath.string() + " must be a directory.");
80 }
81 return libCount;
82}
83
92bool LibraryManagerImpl::shouldBeLoaded(const std::string &filename, const std::vector<std::string> &excludes) const {
93 return !isLoaded(filename) && DllOpen::isValidFilename(filename) && !isExcluded(filename, excludes);
94}
95
101bool LibraryManagerImpl::isLoaded(const std::string &filename) const {
102 return m_openedLibs.find(filename) != m_openedLibs.cend();
103}
104
115bool LibraryManagerImpl::isExcluded(const std::string &filename, const std::vector<std::string> &excludes) const {
116 return std::any_of(excludes.cbegin(), excludes.cend(),
117 [&filename](const auto exclude) { return filename.find(exclude) != std::string::npos; });
118}
119
126int LibraryManagerImpl::openLibrary(const std::filesystem::path &filepath, const std::string &cacheKey) {
127 // Try to open the library. The wrapper will unload the library when it
128 // is deleted
129 LibraryWrapper dlwrap;
130 if (dlwrap.openLibrary(filepath.string())) {
131 // Successfully opened, so add to map
132 if (g_log.is(Poco::Message::PRIO_DEBUG)) {
133 g_log.debug("Opened library: " + filepath.string() + ".\n");
134 }
135 m_openedLibs.emplace(cacheKey, std::move(dlwrap));
136 return 1;
137 } else
138 return 0;
139}
140
141} // 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:86
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 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.
int openLibrary(const std::filesystem::path &filepath, const std::string &cacheKey)
Load a given library.
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:145
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
bool is(int level) const
Returns true if at least the given log level is set.
Definition Logger.cpp:177