Initialization.cpp
Go to the documentation of this file.
2 
3 // std
4 #include <memory>
5 
6 // project
7 #include "build/config.hpp"
8 #include "build/version.hpp"
10 #include "utility/Logging.hpp"
11 #include "utility/Resources.hpp"
13 
14 // libraries
15 #include "XLink/XLink.h"
16 #include "spdlog/cfg/env.h"
17 #include "spdlog/cfg/helpers.h"
18 #include "spdlog/details/os.h"
19 #include "spdlog/spdlog.h"
20 #include "utility/Logging.hpp"
21 extern "C" {
22 #include "XLink/XLinkLog.h"
23 }
24 
25 #ifdef DEPTHAI_ENABLE_BACKWARD
26  #include "backward.hpp"
27 #endif
28 
29 // For easier access to dai namespaced symbols
30 namespace dai {
31 
32 // Anonymous namespace to hide 'Preloader' symbol and variable as its not needed to be visible to other compilation units
33 namespace {
34 
35 // Doing early static initialization hits this stage faster than some libraries initialize their global static members
36 
37 // Preloader uses static global object constructor (works only for shared libraries)
38 // to execute some code upon final executable launch or library import
39 // Preloader
40 // struct Preloader {
41 // Preloader(){
42 // initialize();
43 // }
44 // } preloader;
45 
46 } // namespace
47 
48 // Backward library stacktrace handling
49 #ifdef DEPTHAI_ENABLE_BACKWARD
50 static std::unique_ptr<backward::SignalHandling> signalHandler;
51 #endif
52 
53 bool initialize() {
54  return initialize(nullptr, false, nullptr);
55 }
56 
57 bool initialize(void* javavm) {
58  return initialize(nullptr, false, javavm);
59 }
60 
61 bool initialize(std::string additionalInfo, bool installSignalHandler, void* javavm) {
62  return initialize(additionalInfo.c_str(), installSignalHandler, javavm);
63 }
64 
65 bool initialize(const char* additionalInfo, bool installSignalHandler, void* javavm) {
66  // singleton for checking whether depthai was already initialized
67  static const bool initialized = [&]() {
68  // Initialize logging
70 
71 #ifdef DEPTHAI_ENABLE_BACKWARD
72  // install backward if specified
73  auto envSignalHandler = utility::getEnv("DEPTHAI_INSTALL_SIGNAL_HANDLER");
74  if(installSignalHandler && envSignalHandler != "0") {
75  signalHandler = std::make_unique<backward::SignalHandling>();
76  }
77 #else
78  (void)installSignalHandler;
79 #endif
80 
81  // Read JavaVM pointer from env if present
82  {
83  auto javavmEnvStr = utility::getEnv("DEPTHAI_LIBUSB_ANDROID_JAVAVM");
84  if(javavm == nullptr && !javavmEnvStr.empty()) {
85  // Read the uintptr_t value from the decimal string
86  sscanf(javavmEnvStr.c_str(), "%" SCNuPTR, reinterpret_cast<uintptr_t*>(&javavm));
87  }
88  }
89 
90  // Print core commit and build datetime
91  if(additionalInfo != nullptr && additionalInfo[0] != '\0') {
92  logger::debug("{}", additionalInfo);
93  }
94  logger::debug("Library information - version: {}, commit: {} from {}, build: {}, libusb enabled: {}",
95  build::VERSION,
96  build::COMMIT,
97  build::COMMIT_DATETIME,
98  build::BUILD_DATETIME,
99  build::HAVE_LIBUSB_SUPPORT);
100 
101  // Executed at library load time
102 
103  // Preload Resources (getting instance causes some internal lazy loading to start)
105 
106  // Static global handler
107  static XLinkGlobalHandler_t xlinkGlobalHandler = {};
108  xlinkGlobalHandler.protocol = X_LINK_USB_VSC;
109  xlinkGlobalHandler.options = javavm;
110  auto status = XLinkInitialize(&xlinkGlobalHandler);
111  const auto ERROR_MSG_USB_TIP = fmt::format("If running in a container, make sure that the following is set: \"{}\"",
112  "-v /dev/bus/usb:/dev/bus/usb --device-cgroup-rule='c 189:* rmw'");
113  if(X_LINK_SUCCESS != status) {
114  std::string errorMsg = fmt::format("Couldn't initialize XLink: {}. ", XLinkErrorToStr(status));
115  if(status == X_LINK_INIT_USB_ERROR) {
116  errorMsg += ERROR_MSG_USB_TIP;
117  }
118  logger::debug("Initialize failed - {}", errorMsg);
119  throw std::runtime_error(errorMsg);
120  }
121 
122  // Check that USB protocol is available, IFF libusb is enabled
123 #ifdef DEPTHAI_ENABLE_LIBUSB
124  if(!XLinkIsProtocolInitialized(X_LINK_USB_VSC)) {
125  logger::warn("USB protocol not available - {}", ERROR_MSG_USB_TIP);
126  }
127 #endif
128 
129  // Enable Global XLink profiling
130  XLinkProfStart();
131  auto profilingEnvLevel = utility::getEnv("DEPTHAI_PROFILING");
132  if(profilingEnvLevel == "1") {
134  }
135 
136  // TODO(themarpe), move into XLink library
137  auto xlinkEnvLevel = utility::getEnv("XLINK_LEVEL");
138  if(xlinkEnvLevel == "debug") {
139  mvLogDefaultLevelSet(MVLOG_DEBUG);
140  } else if(xlinkEnvLevel == "info") {
141  mvLogDefaultLevelSet(MVLOG_INFO);
142  } else if(xlinkEnvLevel == "warn") {
143  mvLogDefaultLevelSet(MVLOG_WARN);
144  } else if(xlinkEnvLevel == "error") {
145  mvLogDefaultLevelSet(MVLOG_ERROR);
146  } else if(xlinkEnvLevel == "fatal") {
147  mvLogDefaultLevelSet(MVLOG_FATAL);
148  } else if(xlinkEnvLevel == "off") {
149  mvLogDefaultLevelSet(MVLOG_LAST);
150  } else {
151  // Suppress XLink related errors by default
152  mvLogDefaultLevelSet(MVLOG_FATAL);
153  }
154 
155  logger::debug("Initialize - finished");
156 
157  return true;
158  }();
159  return initialized;
160 }
161 
162 } // namespace dai
dai::XLinkGlobalProfilingLogger::enable
void enable(bool enable)
Definition: XLinkGlobalProfilingLogger.cpp:15
Environment.hpp
Resources.hpp
dai::logger::debug
void debug(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:72
dai::logger::warn
void warn(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:84
dai::Resources::getInstance
static Resources & getInstance()
Definition: Resources.cpp:248
dai::utility::getEnv
std::string getEnv(const std::string &var)
Definition: Environment.cpp:18
Initialization.hpp
XLinkGlobalProfilingLogger.hpp
dai::initialize
bool initialize()
Definition: Initialization.cpp:53
nanorpc::core::detail::pack::meta::status
status
Definition: pack_meta.h:33
dai::XLinkGlobalProfilingLogger::getInstance
static XLinkGlobalProfilingLogger & getInstance()
Definition: XLinkGlobalProfilingLogger.cpp:56
dai::Logging::getInstance
static Logging & getInstance()
Definition: Logging.hpp:32
Logging.hpp
dai
Definition: CameraExposureOffset.hpp:6


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19