Skip to content

Commit

Permalink
Improve the output of the SIOBlocks lib loading (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener authored Dec 19, 2022
1 parent 0a3bb19 commit 579e69c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
12 changes: 9 additions & 3 deletions include/podio/SIOBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <memory>
#include <string>
#include <string_view>
#include <tuple>

namespace podio {

Expand Down Expand Up @@ -185,15 +186,20 @@ class SIOBlockFactory {
class SIOBlockLibraryLoader {
private:
SIOBlockLibraryLoader();

/// Status code for loading shared SIOBlocks libraries
enum class LoadStatus : short { Success = 0, AlreadyLoaded = 1, Error = 2 };

/**
* Load a library with the given name via dlopen
*/
void loadLib(const std::string& libname);
LoadStatus loadLib(const std::string& libname);

/**
* Get all files that are found on LD_LIBRARY_PATH and that have "SioBlocks"
* in their name
* in their name together with the directory they are in
*/
static std::vector<std::string> getLibNames();
static std::vector<std::tuple<std::string, std::string>> getLibNames();

std::map<std::string, void*> _loadedLibs{};

Expand Down
33 changes: 21 additions & 12 deletions src/SIOBlock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,33 +140,42 @@ std::shared_ptr<SIOBlock> SIOBlockFactory::createBlock(const podio::CollectionBa
}

SIOBlockLibraryLoader::SIOBlockLibraryLoader() {
for (const auto& lib : getLibNames()) {
loadLib(lib);
for (const auto& [lib, dir] : getLibNames()) {
const auto status = loadLib(lib);
switch (status) {
case LoadStatus::Success:
std::cout << "Loaded SIOBlocks library \'" << lib << "\' (from " << dir << ")" << std::endl;
break;
case LoadStatus::AlreadyLoaded:
std::cerr << "SIOBlocks library \'" << lib << "\' already loaded. Not loading again from " << dir << std::endl;
break;
case LoadStatus::Error:
std::cerr << "ERROR while loading SIOBlocks library \'" << lib << "\' (from " << dir << ")" << std::endl;
break;
}
}
}

void SIOBlockLibraryLoader::loadLib(const std::string& libname) {
SIOBlockLibraryLoader::LoadStatus SIOBlockLibraryLoader::loadLib(const std::string& libname) {
if (_loadedLibs.find(libname) != _loadedLibs.end()) {
std::cerr << "SIOBlocks library \'" << libname << "\' already loaded. Not loading it again" << std::endl;
return;
return LoadStatus::AlreadyLoaded;
}

void* libhandle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if (libhandle) {
std::cout << "Loading SIOBlocks library \'" << libname << "\'" << std::endl;
_loadedLibs.insert({libname, libhandle});
} else {
std::cerr << "ERROR while loading SIOBlocks library \'" << libname << "\'" << std::endl;
return LoadStatus::Success;
}

return LoadStatus::Error;
}

std::vector<std::string> SIOBlockLibraryLoader::getLibNames() {
std::vector<std::tuple<std::string, std::string>> SIOBlockLibraryLoader::getLibNames() {
#ifdef USE_BOOST_FILESYSTEM
namespace fs = boost::filesystem;
#else
namespace fs = std::filesystem;
#endif
std::vector<std::string> libs;
std::vector<std::tuple<std::string, std::string>> libs;

std::string dir;
const auto ldLibPath = std::getenv("LD_LIBRARY_PATH");
Expand All @@ -182,7 +191,7 @@ std::vector<std::string> SIOBlockLibraryLoader::getLibNames() {
for (auto& lib : fs::directory_iterator(dir)) {
const auto filename = lib.path().filename().string();
if (filename.find("SioBlocks") != std::string::npos) {
libs.emplace_back(std::move(filename));
libs.emplace_back(std::move(filename), dir);
}
}
}
Expand Down

0 comments on commit 579e69c

Please sign in to comment.