cpp-common
envvars.hh
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 2; -*-
2 
3 #pragma once
4 
11 #include <string>
12 
13 #define NVSL_DECL_ENV(name) \
14  static const char *name##_ENV __attribute__((unused)) = (char *)(#name)
15 
16 NVSL_DECL_ENV(NVSL_NO_STACKTRACE);
17 NVSL_DECL_ENV(NVSL_LOG_WILDCARD);
18 NVSL_DECL_ENV(NVSL_GEN_STATS);
19 
27 static inline bool get_env_val(const std::string var) {
28  bool result = false;
29 
30  const char *val = std::getenv(var.c_str());
31  if (val != nullptr) {
32  const std::string val_str = std::string(val);
33 
34  if (val_str == "1") {
35  result = true;
36  }
37  }
38 
39  return result;
40 }
41 
49 static inline std::string get_env_str(const std::string var,
50  const std::string def = "") {
51  std::string result = def;
52 
53  const char *val = std::getenv(var.c_str());
54  if (val != nullptr) {
55  result = std::string(val);
56  }
57 
58  return result;
59 }