The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PluginServiceV1.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2013-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 
13 
14 #define GAUDI_PLUGIN_SERVICE_V1
16 
17 #include <dirent.h>
18 #include <dlfcn.h>
19 
20 #include <cstdlib>
21 #include <fstream>
22 #include <iostream>
23 #include <memory>
24 #include <regex>
25 
26 #include <cxxabi.h>
27 #include <sys/stat.h>
28 
29 namespace {
30  std::mutex registrySingletonMutex;
31 }
32 
33 #include <algorithm>
34 
35 namespace {
36  // string trimming functions taken from
37  // http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
38 
39  constexpr struct is_space_t {
40  bool operator()( int i ) const { return std::isspace( i ); }
41  } is_space{};
42 
43  // trim from start
44  static inline std::string& ltrim( std::string& s ) {
45  s.erase( s.begin(), std::find_if_not( s.begin(), s.end(), is_space ) );
46  return s;
47  }
48 
49  // trim from end
50  static inline std::string& rtrim( std::string& s ) {
51  s.erase( std::find_if_not( s.rbegin(), s.rend(), is_space ).base(), s.end() );
52  return s;
53  }
54  // trim from both ends
55  static inline std::string& trim( std::string& s ) { return ltrim( rtrim( s ) ); }
56 } // namespace
57 
58 namespace {
62  inline void factoryInfoSetHelper( std::string& dest, const std::string& value, const std::string& desc,
63  const std::string& id ) {
64  if ( dest.empty() ) {
65  dest = value;
66  } else if ( dest != value ) {
67  Gaudi::PluginService::Details::logger().warning( "new factory loaded for '" + id + "' with different " + desc +
68  ": " + dest + " != " + value );
69  }
70  }
71 
72  struct OldStyleCnv {
74  void operator()( const char c ) {
75  switch ( c ) {
76  case '<':
77  case '>':
78  case ',':
79  case '(':
80  case ')':
81  case ':':
82  case '.':
83  name.push_back( '_' );
84  break;
85  case '&':
86  name.push_back( 'r' );
87  break;
88  case '*':
89  name.push_back( 'p' );
90  break;
91  case ' ':
92  break;
93  default:
94  name.push_back( c );
95  break;
96  }
97  }
98  };
100  std::string old_style_name( const std::string& name ) {
101  return std::for_each( name.begin(), name.end(), OldStyleCnv() ).name;
102  }
103 } // namespace
104 
105 namespace Gaudi {
106  namespace PluginService {
108  Exception::Exception( std::string msg ) : m_msg( std::move( msg ) ) {}
110  const char* Exception::what() const throw() { return m_msg.c_str(); }
111 
112  namespace Details {
113  void* getCreator( const std::string& id, const std::string& type ) {
114  return Registry::instance().get( id, type );
115  }
116 
118  int status;
120  abi::__cxa_demangle( id.c_str(), nullptr, nullptr, &status ), free );
121  if ( !realname ) return id;
122  return std::regex_replace(
123  realname.get(),
124  std::regex{ "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?" },
125  "std::string" );
126  }
127  std::string demangle( const std::type_info& id ) { return demangle( id.name() ); }
128 
130  auto _guard = std::scoped_lock{ ::registrySingletonMutex };
131  static Registry r;
132  return r;
133  }
134 
135  Registry::Registry() : m_initialized( false ) {}
136 
138  auto _guard = std::scoped_lock{ m_mutex };
139  if ( m_initialized ) return;
140  m_initialized = true;
141 #if defined( _WIN32 )
142  const char* envVar = "PATH";
143  const char sep = ';';
144 #else
145  const char* envVar = "LD_LIBRARY_PATH";
146  const char sep = ':';
147 #endif
148  char* search_path = ::getenv( envVar );
149  if ( search_path ) {
150  logger().debug( std::string( "searching factories in " ) + envVar );
151  std::string path( search_path );
152  std::string::size_type pos = 0;
153  std::string::size_type newpos = 0;
154  while ( pos != std::string::npos ) {
155  std::string dirName;
156  // get the next entry in the path
157  newpos = path.find( sep, pos );
158  if ( newpos != std::string::npos ) {
159  dirName = path.substr( pos, newpos - pos );
160  pos = newpos + 1;
161  } else {
162  dirName = path.substr( pos );
163  pos = newpos;
164  }
165  logger().debug( std::string( " looking into " ) + dirName );
166  // look for files called "*.components" in the directory
167  DIR* dir = opendir( dirName.c_str() );
168  if ( dir ) {
169  struct dirent* entry;
170  while ( ( entry = readdir( dir ) ) ) {
171  std::string name( entry->d_name );
172  // check if the file name ends with ".components"
173  std::string::size_type extpos = name.find( ".components" );
174  if ( ( extpos != std::string::npos ) && ( ( extpos + 11 ) == name.size() ) ) {
175  std::string fullPath = ( dirName + '/' + name );
176  { // check if it is a regular file
177  struct stat buf;
178  stat( fullPath.c_str(), &buf );
179  if ( !S_ISREG( buf.st_mode ) ) continue;
180  }
181  // read the file
182  logger().debug( std::string( " reading " ) + name );
183  std::ifstream factories{ fullPath };
185  int factoriesCount = 0;
186  int lineCount = 0;
187  while ( !factories.eof() ) {
188  ++lineCount;
190  trim( line );
191  // skip empty lines and lines starting with '#'
192  if ( line.empty() || line[0] == '#' ) continue;
193  // only accept "v1" factories
194  if ( line.substr( 0, 4 ) == "v1::" )
195  line = line.substr( 4 );
196  else
197  continue;
198  // look for the separator
199  auto pos = line.find( ':' );
200  if ( pos == std::string::npos ) {
201  logger().warning( "failed to parse line " + fullPath + ':' + std::to_string( lineCount ) );
202  continue;
203  }
204  const std::string lib( line, 0, pos );
205  const std::string fact( line, pos + 1 );
206  m_factories.emplace( fact, FactoryInfo( lib ) );
207 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
208  // add an alias for the factory using the Reflex convention
209  std::string old_name = old_style_name( fact );
210  if ( fact != old_name ) {
211  FactoryInfo old_info( lib );
212  old_info.properties["ReflexName"] = "true";
213  m_factories.emplace( old_name, old_info );
214  }
215 #endif
216  ++factoriesCount;
217  }
218  if ( logger().level() <= Logger::Debug ) {
219  logger().debug( " found " + std::to_string( factoriesCount ) + " factories" );
220  }
221  }
222  }
223  closedir( dir );
224  }
225  }
226  }
227  }
228 
229  Registry::FactoryInfo& Registry::add( const std::string& id, void* factory, const std::string& type,
230  const std::string& rtype, const std::string& className,
231  const Properties& props ) {
232  auto _guard = std::scoped_lock{ m_mutex };
233  FactoryMap& facts = factories();
234  auto entry = facts.find( id );
235  if ( entry == facts.end() ) {
236  // this factory was not known yet
237  entry = facts.emplace( id, FactoryInfo( "unknown", factory, type, rtype, className, props ) ).first;
238  } else {
239  // do not replace an existing factory with a new one
240  if ( !entry->second.ptr ) entry->second.ptr = factory;
241  factoryInfoSetHelper( entry->second.type, type, "type", id );
242  factoryInfoSetHelper( entry->second.rtype, rtype, "return type", id );
243  factoryInfoSetHelper( entry->second.className, className, "class", id );
244  }
245 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
246  // add an alias for the factory using the Reflex convention
247  std::string old_name = old_style_name( id );
248  if ( id != old_name )
249  add( old_name, factory, type, rtype, className, props ).properties["ReflexName"] = "true";
250 #endif
251  return entry->second;
252  }
253 
254  void* Registry::get( const std::string& id, const std::string& type ) const {
255  auto _guard = std::scoped_lock{ m_mutex };
256  const FactoryMap& facts = factories();
257  auto f = facts.find( id );
258  if ( f != facts.end() ) {
259 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
260  const Properties& props = f->second.properties;
261  if ( props.find( "ReflexName" ) != props.end() )
262  logger().warning( "requesting factory via old name '" + id +
263  "'"
264  "use '" +
265  f->second.className + "' instead" );
266 #endif
267  if ( !f->second.ptr ) {
268  if ( !dlopen( f->second.library.c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
269  logger().warning( "cannot load " + f->second.library + " for factory " + id );
270  char* dlmsg = dlerror();
271  if ( dlmsg ) logger().warning( dlmsg );
272  return nullptr;
273  }
274  f = facts.find( id ); // ensure that the iterator is valid
275  }
276  if ( f->second.type == type ) return f->second.ptr;
277  logger().warning( "found factory " + id + ", but of wrong type: " + demangle( f->second.type ) +
278  " instead of " + demangle( type ) );
279  }
280  return nullptr; // factory not found
281  }
282 
284  auto _guard = std::scoped_lock{ m_mutex };
285  static const FactoryInfo unknown( "unknown" );
286  const FactoryMap& facts = factories();
287  auto f = facts.find( id );
288  return ( f != facts.end() ) ? f->second : unknown;
289  }
290 
292  auto _guard = std::scoped_lock{ m_mutex };
293  FactoryMap& facts = factories();
294  auto f = facts.find( id );
295  if ( f != facts.end() ) f->second.properties[k] = v;
296  return *this;
297  }
298 
300  auto _guard = std::scoped_lock{ m_mutex };
302  for ( const auto& f : factories() ) {
303  if ( f.second.ptr ) l.insert( f.first );
304  }
305  return l;
306  }
307 
308  void Logger::report( Level lvl, const std::string& msg ) {
309  static const char* levels[] = { "DEBUG : ", "INFO : ", "WARNING: ", "ERROR : " };
310  if ( lvl >= level() ) { std::cerr << levels[lvl] << msg << std::endl; }
311  }
312 
313  static auto s_logger = std::make_unique<Logger>();
314  Logger& logger() { return *s_logger; }
315  void setLogger( Logger* logger ) { s_logger.reset( logger ); }
316 
317  } // namespace Details
318 
319  void SetDebug( int debugLevel ) {
320  using namespace Details;
321  Logger& l = logger();
322  if ( debugLevel > 1 )
323  l.setLevel( Logger::Debug );
324  else if ( debugLevel > 0 )
325  l.setLevel( Logger::Info );
326  else
327  l.setLevel( Logger::Warning );
328  }
329 
330  int Debug() {
331  using namespace Details;
332  switch ( logger().level() ) {
333  case Logger::Debug:
334  return 2;
335  case Logger::Info:
336  return 1;
337  default:
338  return 0;
339  }
340  }
341  }
342  } // namespace PluginService
343 } // namespace Gaudi
std::for_each
T for_each(T... args)
Gaudi::PluginService::v1::Details::Registry
In-memory database of the loaded factories.
Definition: PluginServiceDetailsV1.h:83
std::string
STL class.
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
GaudiPartProp.tests.v1
v1
Definition: tests.py:39
gaudirun.s
string s
Definition: gaudirun.py:346
std::find_if_not
T find_if_not(T... args)
std::type_info
check_ParticleID.props
props
Definition: check_ParticleID.py:21
Gaudi::PluginService::v1::Details::Registry::m_mutex
std::recursive_mutex m_mutex
Mutex used to control concurrent access to the internal data.
Definition: PluginServiceDetailsV1.h:182
Gaudi::PluginService::v1::Details::Logger::debug
void debug(const std::string &msg)
Definition: PluginServiceDetailsV1.h:194
Gaudi::PluginService::v1::Details::getCreator
GAUDIPS_API void * getCreator(const std::string &id, const std::string &type)
Function used to load a specific factory function.
Definition: PluginServiceV1.cpp:113
Gaudi::PluginService::v1::Details::Registry::Registry
Registry()
Private constructor for the singleton pattern.
Definition: PluginServiceV1.cpp:135
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
std::map::emplace
T emplace(T... args)
Gaudi::PluginService::v1::Exception::Exception
Exception(std::string msg)
Definition: PluginServiceV1.cpp:108
gaudirun.c
c
Definition: gaudirun.py:525
GaudiPartProp.tests.id
id
Definition: tests.py:111
Gaudi::PluginService::v1::Details::Registry::instance
static Registry & instance()
Retrieve the singleton instance of Registry.
Definition: PluginServiceV1.cpp:129
Gaudi::PluginService::v1::Details::Registry::m_factories
FactoryMap m_factories
Internal storage for factories.
Definition: PluginServiceDetailsV1.h:179
Gaudi::PluginService::v1::Details::Registry::m_initialized
bool m_initialized
Flag recording if the registry has been initialized or not.
Definition: PluginServiceDetailsV1.h:176
Gaudi::PluginService::v1::Details::Logger::Info
@ Info
Definition: PluginServiceDetailsV1.h:188
Properties
Definition: Properties.py:1
PluginService.h
std::cerr
Gaudi::PluginService::v1::Details::setLogger
GAUDIPS_API void setLogger(Logger *logger)
Set the logger instance to use.
Definition: PluginServiceV1.cpp:315
Gaudi::PluginService::v1::Details::Registry::getInfo
const FactoryInfo & getInfo(const std::string &id) const
Retrieve the FactoryInfo object for an id.
Definition: PluginServiceV1.cpp:283
std::string::c_str
T c_str(T... args)
Gaudi::PluginService::v1::Details::Registry::initialize
void initialize()
Initialize the registry loading the list of factories from the .component files in the library search...
Definition: PluginServiceV1.cpp:137
std::to_string
T to_string(T... args)
Gaudi::PluginService::v1::Details::Logger::Debug
@ Debug
Definition: PluginServiceDetailsV1.h:188
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::properties
Properties properties
Definition: PluginServiceDetailsV1.h:105
std::map< KeyType, FactoryInfo >
Gaudi::PluginService::v1::Details::Logger::report
virtual void report(Level lvl, const std::string &msg)
Definition: PluginServiceV1.cpp:308
Gaudi::PluginService::v1::Details::Registry::factories
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
Definition: PluginServiceDetailsV1.h:146
std::regex
gaudirun.level
level
Definition: gaudirun.py:364
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::PluginService::v1::Details::Logger::level
Level level() const
Definition: PluginServiceDetailsV1.h:191
Gaudi::PluginService::v1::Exception::what
const char * what() const override
Definition: PluginServiceV1.cpp:110
Gaudi::PluginService::v1::SetDebug
GAUDIPS_API void SetDebug(int debugLevel)
Backward compatibility with Reflex.
Definition: PluginServiceV1.cpp:319
GAUDI_PLUGIN_SERVICE_V1_INLINE
#define GAUDI_PLUGIN_SERVICE_V1_INLINE
Definition: PluginServiceCommon.h:18
Gaudi::PluginService::v1::Details::Registry::add
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
Definition: PluginServiceDetailsV1.h:121
gaudirun.dest
dest
Definition: gaudirun.py:224
gaudirun.type
type
Definition: gaudirun.py:160
Gaudi::PluginService::v1::Details::logger
GAUDIPS_API Logger & logger()
Return the current logger instance.
Definition: PluginServiceV1.cpp:314
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
std::endl
T endl(T... args)
gaudirun.l
dictionary l
Definition: gaudirun.py:583
Gaudi::PluginService::v1::Exception::m_msg
std::string m_msg
Definition: PluginServiceV1.h:80
std::getline
T getline(T... args)
std
STL namespace.
Gaudi::PluginService::v1::Details::Registry::addProperty
Registry & addProperty(const std::string &id, const std::string &k, const std::string &v)
Add a property to an already existing FactoryInfo object (via its id.)
Definition: PluginServiceV1.cpp:291
std::isspace
T isspace(T... args)
Gaudi::PluginService::v1::Details::Logger::Level
Level
Definition: PluginServiceDetailsV1.h:188
plotSpeedupsPyRoot.line
line
Definition: plotSpeedupsPyRoot.py:198
Properties.v
v
Definition: Properties.py:122
Gaudi::PluginService::v1::Debug
GAUDIPS_API int Debug()
Backward compatibility with Reflex.
Definition: PluginServiceV1.cpp:330
std::mutex
STL class.
Gaudi::PluginService::v1::Details::Registry::FactoryInfo::ptr
void * ptr
Definition: PluginServiceDetailsV1.h:101
std::map::end
T end(T... args)
Gaudi::PluginService::v1::Details::Registry::FactoryInfo
Definition: PluginServiceDetailsV1.h:90
Gaudi::PluginService::v1::Details::demangle
GAUDIPS_API std::string demangle(const std::type_info &id)
Return a canonical name for type_info object (implementation borrowed from GaudiKernel/System).
Definition: PluginServiceV1.cpp:127
Gaudi::PluginService::v1::Exception::~Exception
~Exception() override
Definition: PluginServiceV1.cpp:109
std::unique_ptr
STL class.
Gaudi::PluginService::v1::Details::Logger
Simple logging class, just to provide a default implementation.
Definition: PluginServiceDetailsV1.h:186
Gaudi::PluginService::v1::Details::Logger::Warning
@ Warning
Definition: PluginServiceDetailsV1.h:188
std::regex_replace
T regex_replace(T... args)
Gaudi::PluginService::v1::Details::Logger::warning
void warning(const std::string &msg)
Definition: PluginServiceDetailsV1.h:195
std::set
STL class.
Gaudi::PluginService::v1::Details::Registry::loadedFactoryNames
std::set< KeyType > loadedFactoryNames() const
Return a list of all the known and loaded factories.
Definition: PluginServiceV1.cpp:299
Gaudi::PluginService::v1::Details::Registry::get
void * get(const std::string &id, const std::string &type) const
Retrieve the factory for the given id.
Definition: PluginServiceV1.cpp:254
std::ifstream
STL class.