-
Notifications
You must be signed in to change notification settings - Fork 0
/
envvars.hh
81 lines (66 loc) · 1.71 KB
/
envvars.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// -*- mode: c++; c-basic-offset: 2; -*-
#pragma once
/**
* @file envvars.hh
* @date January 21, 2021
* @brief Declare environment variables and access their values
*/
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
#include <string>
#endif
#define NVSL_DECL_ENV(name) \
static const char *name##_ENV __attribute__((unused)) = (char *)(#name)
NVSL_DECL_ENV(NVSL_NO_STACKTRACE);
NVSL_DECL_ENV(NVSL_LOG_WILDCARD);
NVSL_DECL_ENV(NVSL_GEN_STATS);
/**
* @brief Looks up a boolean like env var
* @details Behavior:
* 1. Env variable missing -> return false
* 2. 0 -> return false
* 1. 1 -> return true
*/
static inline bool get_env_val(const char *var) {
bool result = false;
const char *val = getenv(var);
if (val != NULL) {
if (strncmp(val, "1", 1) == 0) {
result = true;
}
}
return result;
}
/**
* @brief Looks up a string value from env var
* @details Behavior:
* 1. Env variable missing -> empty string
* @param[in] var Environment variable's name
* @param[in] def Default value to return if unset
*/
static inline char *get_env_str(const char *var, const char *def) {
char *result = (char *)def;
char *val = getenv(var);
if (val != NULL) {
result = val;
}
return result;
}
#ifdef __cplusplus
#include <string>
/**
* @brief Looks up a boolean like env var
* @details Behavior:
* 1. Env variable missing -> return false
* 2. 0 -> return false
* 1. 1 -> return true
*/
static inline bool get_env_val(const std::string var) {
return get_env_val(var.c_str());
}
static inline std::string get_env_str(const std::string var,
const std::string def = "") {
return std::string(get_env_str(var.c_str(), def.c_str()));
}
#endif