Logging.cpp
Go to the documentation of this file.
1 #include "Logging.hpp"
2 
3 namespace dai {
4 
5 Logging::Logging() : logger("depthai", {std::make_shared<spdlog::sinks::stdout_color_sink_mt>()}) {
6  // Default global logging level set to WARN; override with ENV variable 'DEPTHAI_LEVEL'
7  // Taken from spdlog, to replace with DEPTHAI_LEVEL instead of SPDLOG_LEVEL
8  // spdlog::cfg::load_env_levels();
9  auto level = spdlog::level::warn;
10  auto envLevel = utility::getEnv("DEPTHAI_LEVEL", logger);
11  if(!envLevel.empty()) {
12  level = parseLevel(envLevel);
13  }
14  logger.set_level(level);
15 
16  auto debugStr = utility::getEnv("DEPTHAI_DEBUG", logger);
17  if(!debugStr.empty()) {
18  // Try parsing the string as a number
19  try {
20  int debug{std::stoi(debugStr)};
21  if(debug && (level > spdlog::level::debug)) {
22  logger.set_level(spdlog::level::debug);
23  logger.info("DEPTHAI_DEBUG enabled, lowered DEPTHAI_LEVEL to 'debug'");
24  }
25  } catch(const std::invalid_argument& e) {
26  logger.warn("DEPTHAI_DEBUG value invalid: {}, should be a number (non-zero to enable)", e.what());
27  }
28  }
29 }
30 
31 spdlog::level::level_enum Logging::parseLevel(std::string lvl) {
32  std::transform(lvl.begin(), lvl.end(), lvl.begin(), [](char ch) { return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); });
33 
34  if(lvl == "trace") {
35  return spdlog::level::trace;
36  } else if(lvl == "debug") {
37  return spdlog::level::debug;
38  } else if(lvl == "info") {
39  return spdlog::level::info;
40  } else if(lvl == "warn") {
41  return spdlog::level::warn;
42  } else if(lvl == "error") {
43  return spdlog::level::err;
44  } else if(lvl == "off") {
45  return spdlog::level::off;
46  } else {
47  throw std::invalid_argument(fmt::format("Cannot parse logging level: {}", lvl));
48  }
49 }
50 
51 } // namespace dai
dai::logger::info
void info(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:78
dai::Logging::Logging
Logging()
Definition: Logging.cpp:5
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::utility::getEnv
std::string getEnv(const std::string &var)
Definition: Environment.cpp:18
transform
static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms)
Definition: sha1.hpp:129
dai::logger::trace
void trace(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:66
Logging.hpp
dai
Definition: CameraExposureOffset.hpp:6
dai::Logging::parseLevel
spdlog::level::level_enum parseLevel(std::string lvl)
Definition: Logging.cpp:31


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