NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/plugin/detail/dynamic_load_unix.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <dlfcn.h>
7 #include <algorithm>
8 #include <memory>
9 
10 #include "opentelemetry/plugin/detail/dynamic_library_handle.h"
11 #include "opentelemetry/plugin/detail/loader_info.h"
12 #include "opentelemetry/plugin/detail/utility.h"
13 #include "opentelemetry/plugin/factory.h"
14 #include "opentelemetry/plugin/hook.h"
15 #include "opentelemetry/version.h"
16 
18 namespace plugin
19 {
21 {
22 public:
23  explicit DynamicLibraryHandleUnix(void *handle) noexcept : handle_{handle} {}
24 
25  ~DynamicLibraryHandleUnix() override { ::dlclose(handle_); }
26 
27 private:
28  void *handle_;
29 };
30 
31 inline std::unique_ptr<Factory> LoadFactory(const char *plugin, std::string &error_message) noexcept
32 {
33  dlerror(); // Clear any existing error.
34 
35  auto handle = ::dlopen(plugin, RTLD_NOW | RTLD_LOCAL);
36  if (handle == nullptr)
37  {
38  detail::CopyErrorMessage(dlerror(), error_message);
39  return nullptr;
40  }
41 
42  std::shared_ptr<DynamicLibraryHandle> library_handle{new (std::nothrow)
43  DynamicLibraryHandleUnix{handle}};
44  if (library_handle == nullptr)
45  {
46  return nullptr;
47  }
48 
49  auto make_factory_impl =
50  reinterpret_cast<OpenTelemetryHook *>(::dlsym(handle, "OpenTelemetryMakeFactoryImpl"));
51  if (make_factory_impl == nullptr)
52  {
53  detail::CopyErrorMessage(dlerror(), error_message);
54  return nullptr;
55  }
56  if (*make_factory_impl == nullptr)
57  {
58  detail::CopyErrorMessage("Invalid plugin hook", error_message);
59  return nullptr;
60  }
61  LoaderInfo loader_info;
62  nostd::unique_ptr<char[]> plugin_error_message;
63  auto factory_impl = (**make_factory_impl)(loader_info, plugin_error_message);
64  if (factory_impl == nullptr)
65  {
66  detail::CopyErrorMessage(plugin_error_message.get(), error_message);
67  return nullptr;
68  }
69  return std::unique_ptr<Factory>{new (std::nothrow)
70  Factory{std::move(library_handle), std::move(factory_impl)}};
71 }
72 } // namespace plugin
plugin::DynamicLibraryHandleUnix
Definition: includes/opentelemetry/plugin/detail/dynamic_load_unix.h:20
plugin::OpenTelemetryHook
nostd::unique_ptr< Factory::FactoryImpl >(*)(const LoaderInfo &loader_info, nostd::unique_ptr< char[]> &error_message) OpenTelemetryHook
Definition: includes/opentelemetry/plugin/hook.h:49
nostd::unique_ptr< char[]>
nostd::unique_ptr::get
pointer get() const noexcept
Definition: includes/opentelemetry/nostd/unique_ptr.h:107
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
plugin::LoadFactory
std::unique_ptr< Factory > LoadFactory(const char *plugin, std::string &error_message) noexcept
Load an OpenTelemetry implementation as a plugin.
Definition: includes/opentelemetry/plugin/detail/dynamic_load_unix.h:31
plugin::Factory
Factory creates OpenTelemetry objects from configuration strings.
Definition: includes/opentelemetry/plugin/factory.h:19
plugin::DynamicLibraryHandleUnix::DynamicLibraryHandleUnix
DynamicLibraryHandleUnix(void *handle) noexcept
Definition: includes/opentelemetry/plugin/detail/dynamic_load_unix.h:23
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
plugin
Definition: includes/opentelemetry/plugin/dynamic_load.h:19
absl::move
constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > && move(T &&t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:199
plugin::LoaderInfo
LoaderInfo describes the versioning of the loader.
Definition: includes/opentelemetry/plugin/detail/loader_info.h:18
plugin::DynamicLibraryHandleUnix::~DynamicLibraryHandleUnix
~DynamicLibraryHandleUnix() override
Definition: includes/opentelemetry/plugin/detail/dynamic_load_unix.h:25
plugin::detail::CopyErrorMessage
void CopyErrorMessage(const char *source, std::string &destination) noexcept
Definition: includes/opentelemetry/plugin/detail/utility.h:19
plugin::DynamicLibraryHandle
Manage the ownership of a dynamically loaded library.
Definition: includes/opentelemetry/plugin/detail/dynamic_library_handle.h:14