14 #define GAUDI_PLUGIN_SERVICE_V2
17 #include <boost/algorithm/string.hpp>
37 #ifdef USE_BOOST_FILESYSTEM
38 # include <boost/filesystem.hpp>
39 namespace fs = boost::filesystem;
41 # include <filesystem>
42 namespace fs = std::filesystem;
43 #endif // USE_BOOST_FILESYSTEM
45 #include <string_view>
47 #define REG_SCOPE_LOCK std::lock_guard<std::recursive_mutex> _guard( m_mutex );
50 std::mutex registrySingletonMutex;
52 #define SINGLETON_LOCK std::lock_guard<std::mutex> _guard( ::registrySingletonMutex );
59 void operator()(
const char c ) {
68 name.push_back(
'_' );
71 name.push_back(
'r' );
74 name.push_back(
'p' );
85 std::string old_style_name(
const std::string& name ) {
86 return std::for_each( name.begin(), name.end(), OldStyleCnv() ).name;
91 namespace PluginService {
94 std::string
demangle(
const std::string&
id ) {
96 auto realname = std::unique_ptr<char, decltype( free )*>(
97 abi::__cxa_demangle(
id.c_str(),
nullptr,
nullptr, &status ), free );
98 if ( !realname )
return id;
99 #if _GLIBCXX_USE_CXX11_ABI
100 return std::regex_replace(
102 std::regex{
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?"},
105 return std::string{realname.get()};
110 Registry& Registry::instance() {
118 std::stringstream msg;
119 const auto&
info = Registry::instance().getInfo(
id );
120 msg <<
"bad any_cast: requested factory " <<
id <<
" of type " <<
demangle( factory_type ) <<
", got ";
125 logger().debug( msg.str() );
129 Registry::Properties::mapped_type Registry::FactoryInfo::getprop(
const Properties::key_type& name )
const {
130 auto p = properties.find( name );
131 return ( p != end( properties ) ) ? p->second : Properties::mapped_type{};
134 Registry::Registry() {}
136 void Registry::initialize() {
138 #if defined( _WIN32 )
139 const std::string envVar =
"PATH";
140 const std::string sep =
";";
141 #elif defined( __APPLE__ )
142 const std::string envVar =
"DYLD_LIBRARY_PATH";
143 const std::string sep =
":";
145 const std::string envVar =
"LD_LIBRARY_PATH";
146 const std::string sep =
":";
149 std::regex line_format{
"^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$"};
152 std::string search_path;
153 const char* envPtr = std::getenv( envVar.c_str() );
154 search_path = envPtr ? envPtr :
"/usr/lib64:/usr/lib:/usr/local/lib";
155 if ( search_path.empty() ) {
159 logger().debug(
"searching factories in " + envVar);
160 logger().debug(
"searching factories in " + search_path);
162 std::vector<std::string> directories;
163 boost::split(directories, search_path, boost::is_any_of(sep));
165 for(fs::path dirName: directories) {
166 if ( not is_directory( dirName ) ) {
169 logger().debug(
" looking into " + dirName.string() );
170 for (
auto& p : fs::directory_iterator( dirName ) ) {
172 if ( p.path().extension() ==
".components" && is_regular_file( p.path() ) ) {
174 const auto& fullPath = p.path().string();
175 logger().debug(
" reading " + p.path().filename().string() );
176 std::ifstream factories{fullPath};
178 int factoriesCount = 0;
180 while ( !factories.eof() ) {
182 std::getline( factories, line );
183 if ( regex_match( line, matches, line_format ) ) {
184 if ( matches[1] ==
"v2" ) {
185 const std::string lib{matches[2]};
186 const std::string fact{matches[3]};
187 m_factories.emplace( fact, FactoryInfo{lib, {}, {{
"ClassName", fact}}} );
188 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
190 std::string old_name = old_style_name( fact );
191 if ( fact != old_name ) {
192 m_factories.emplace( old_name,
193 FactoryInfo{lib, {}, {{
"ReflexName",
"true"}, {
"ClassName", fact}}} );
199 logger().debug(
"failed to parse line " + fullPath +
':' + std::to_string( lineCount ) );
203 logger().debug(
" found " + std::to_string( factoriesCount ) +
" factories" );
210 const Registry::FactoryMap& Registry::factories()
const {
211 std::call_once( m_initialized, &Registry::initialize,
const_cast<Registry*
>(
this ) );
215 Registry::FactoryMap& Registry::factories() {
216 std::call_once( m_initialized, &Registry::initialize,
this );
220 Registry::FactoryInfo& Registry::add(
const KeyType&
id, FactoryInfo
info ) {
222 FactoryMap& facts = factories();
224 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
226 const auto old_name = old_style_name(
id );
227 if (
id != old_name ) {
228 auto new_info =
info;
230 new_info.properties[
"ReflexName"] =
"true";
232 add( old_name, new_info );
236 auto entry = facts.find(
id );
237 if ( entry == facts.end() ) {
239 entry = facts.emplace(
id, std::move(
info ) ).first;
242 if ( !entry->second.is_set() ) entry->second = std::move(
info );
244 return entry->second;
247 const Registry::FactoryInfo& Registry::getInfo(
const KeyType&
id,
const bool load )
const {
249 static const FactoryInfo unknown = {
"unknown"};
250 const FactoryMap& facts = factories();
251 auto f = facts.find(
id );
253 if ( f != facts.end() ) {
254 if ( load && !f->second.is_set() ) {
255 const std::string library = f->second.library;
256 if ( !dlopen( library.c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
257 logger().warning(
"cannot load " + library +
" for factory " +
id );
258 char* dlmsg = dlerror();
259 if ( dlmsg )
logger().warning( dlmsg );
262 f = facts.find(
id );
270 Registry& Registry::addProperty(
const KeyType&
id,
const KeyType& k,
const std::string&
v ) {
272 FactoryMap& facts = factories();
273 auto f = facts.find(
id );
275 if ( f != facts.end() ) f->second.properties[k] =
v;
279 std::set<Registry::KeyType> Registry::loadedFactoryNames()
const {
282 for (
const auto& f : factories() ) {
283 if ( f.second.is_set() ) l.insert( f.first );
288 void Logger::report( Level lvl,
const std::string& msg ) {
289 static const char* levels[] = {
"DEBUG : ",
"INFO : ",
"WARNING: ",
"ERROR : "};
290 if ( lvl >= level() ) { std::cerr << levels[lvl] << msg << std::endl; }
293 static auto s_logger = std::make_unique<Logger>();
301 if ( dladdr( fptr, &
info ) == 0 )
return "";
303 auto pos = std::strrchr(
info.dli_fname,
'/' );
307 return info.dli_fname;
316 using namespace Details;
318 if ( debugLevel > 1 )
320 else if ( debugLevel > 0 )
321 l.setLevel( Logger::Info );
323 l.setLevel( Logger::Warning );
327 using namespace Details;
328 switch (
logger().level() ) {