55bool environment::local_debug =
false;
57static const std::string
s_true(
"true");
61 const char*
value = ::getenv(
name.c_str());
62 if (
nullptr !=
value ) {
64 return std::string(
value);
66 if ( std::string::npos !=
name.find(
'.', 0) ) {
68 std::string alt_name(
name);
69 std::replace(alt_name.begin(), alt_name.end(),
'.',
'_');
70 value = ::getenv(alt_name.c_str());
71 if (
nullptr !=
value ) {
72 COND_PRINT(local_debug,
"env::getProperty0 '%s' -> '%s': '%s'",
name.c_str(), alt_name.c_str(),
value);
73 return std::string(
value);
75 COND_PRINT(local_debug,
"env::getProperty0 '%s' -> '%s': NOT FOUND",
name.c_str(), alt_name.c_str());
77 COND_PRINT(local_debug,
"env::getProperty0 '%s': NOT FOUND",
name.c_str());
85 if ( 0 ==
value.length() ) {
86 COND_PRINT(local_debug,
"env::getProperty1 %s: null -> %s (default)",
name.c_str(), default_value.c_str());
89 COND_PRINT(local_debug,
"env::getProperty1 %s (default %s): %s",
name.c_str(), default_value.c_str(),
value.c_str());
96 if ( 0 ==
value.length() ) {
97 COND_PRINT(local_debug,
"env::getBooleanProperty %s: null -> %d (default)",
name.c_str(), default_value);
100 const bool res =
"true" ==
value;
101 COND_PRINT(local_debug,
"env::getBooleanProperty %s (default %d): %d/%s",
name.c_str(), default_value, res,
value.c_str());
109 const int32_t min_allowed,
const int32_t max_allowed)
noexcept {
111 if ( 0 ==
value.length() ) {
112 COND_PRINT(local_debug,
"env::getInt32Property %s: null -> %" PRId32
" (default)",
name.c_str(), default_value);
113 return default_value;
115 int32_t res = default_value;
116 char* endptr =
nullptr;
117 const long int res0 = strtol(
value.c_str(), &endptr, 10);
118 if ( *endptr ==
'\0' ) {
120 if ( INT32_MIN <= res0 && res0 <= INT32_MAX ) {
122 const int32_t res1 = (int32_t)res0;
123 if ( min_allowed <= res1 && res1 <= max_allowed ) {
126 COND_PRINT(local_debug,
"env::getInt32Property %s (default %" PRId32
"): %" PRId32
"/%s",
127 name.c_str(), default_value, res,
value.c_str());
130 ERR_PRINT(
"env::getInt32Property %s: %" PRId32
"/%s (invalid user range [% " PRId32
"..%" PRId32
"]) -> %" PRId32
" (default)",
131 name.c_str(), res1,
value.c_str(), min_allowed, max_allowed, res);
135 ERR_PRINT(
"env::getInt32Property %s: %" PRIu64
"/%s (invalid int32_t range) -> %" PRId32
" (default)",
136 name.c_str(), (uint64_t)res0,
value.c_str(), res);
140 ERR_PRINT(
"env::getInt32Property %s: %s (invalid string) -> %" PRId32
" (default)",
148 const uint32_t min_allowed,
const uint32_t max_allowed)
noexcept {
150 if ( 0 ==
value.length() ) {
151 COND_PRINT(local_debug,
"env::getUint32Property %s: null -> %" PRIu32
" (default)",
name.c_str(), default_value);
152 return default_value;
154 uint32_t res = default_value;
155 char* endptr =
nullptr;
156 unsigned long int res0 = strtoul(
value.c_str(), &endptr, 10);
157 if ( *endptr ==
'\0' ) {
159 if ( res0 <= UINT32_MAX ) {
161 const uint32_t res1 = (uint32_t)res0;
162 if ( min_allowed <= res1 && res1 <= max_allowed ) {
165 COND_PRINT(local_debug,
"env::getUint32Property %s (default %" PRIu32
"): %" PRIu32
"/%s",
166 name.c_str(), default_value, res,
value.c_str());
169 ERR_PRINT(
"env::getUint32Property %s: %" PRIu32
"/%s (invalid user range [% " PRIu32
"..%" PRIu32
"]) -> %" PRIu32
" (default)",
170 name.c_str(), res1,
value.c_str(), min_allowed, max_allowed, res);
174 ERR_PRINT(
"env::getUint32Property %s: %" PRIu64
"/%s (invalid uint32_t range) -> %" PRIu32
" (default)",
175 name.c_str(), (uint64_t)res0,
value.c_str(), res);
179 ERR_PRINT(
"env::getUint32Property %s: %s (invalid string) -> %" PRIu32
" (default)",
189 if ( 0 ==
value.length() ) {
190 COND_PRINT(local_debug,
"env::getFractionProperty %s: null -> %s (default)",
name.c_str(), default_value.toString().c_str());
191 return default_value;
195 ERR_PRINT(
"env::getFractionProperty %s: value %s not valid or in range[%s .. %s] -> %s (default)",
196 name.c_str(),
value.c_str(), min_allowed.toString().c_str(), max_allowed.toString().c_str(), default_value.toString().c_str());
202void environment::envSet(
const std::string& prefix_domain, std::string basepair)
noexcept {
204 if ( basepair.length() > 0 ) {
205 size_t pos = 0, start = 0;
206 if ( (pos = basepair.find(
'=', start)) != std::string::npos ) {
207 const size_t elem_len = pos - start;
208 std::string
name = prefix_domain +
"." + basepair.substr(start, elem_len);
209 std::string
value = basepair.substr(pos + 1, std::string::npos);
212 if (
name.length() > 0 ) {
213 if (
value.length() > 0 ) {
214 COND_PRINT(local_debug,
"env::setProperty %s -> %s (explode)",
name.c_str(),
value.c_str());
215 ::setenv(
name.c_str(),
value.c_str(), 1 );
217 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default-1)",
name.c_str());
218 ::setenv(
name.c_str(),
"true", 1 );
222 const std::string
name = prefix_domain +
"." + basepair;
223 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default-0)",
name.c_str());
224 ::setenv(
name.c_str(),
"true", 1 );
229void environment::envExplodeProperties(
const std::string& prefix_domain,
const std::string& list)
noexcept {
230 size_t pos = 0,
start = 0;
231 while ( (pos = list.find(
',', start)) != std::string::npos ) {
232 const size_t elem_len = pos -
start;
233 envSet(prefix_domain, list.substr(start, elem_len));
236 const size_t elem_len = list.length() -
start;
237 if ( elem_len > 0 ) {
238 envSet(prefix_domain, list.substr(start, elem_len));
240 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default)", prefix_domain.c_str());
241 ::setenv(prefix_domain.c_str(),
"true", 1 );
244bool environment::getExplodingPropertiesImpl(
const std::string& root_prefix_domain,
const std::string& prefix_domain)
noexcept {
252 if ( root_prefix_domain.length() > 0 && root_prefix_domain +
".debug" == prefix_domain ) {
255 envExplodeProperties(prefix_domain,
value);
259environment::environment(
const std::string& root_prefix_domain_) noexcept
260: root_prefix_domain(root_prefix_domain_),
261 debug(getExplodingPropertiesImpl(root_prefix_domain_, root_prefix_domain_ +
".debug")),
262 debug_jni(getBooleanProperty(root_prefix_domain_ +
".debug.jni",
false)),
static std::string getProperty(const std::string &name) noexcept
Returns the value of the environment's variable 'name'.
const bool debug
Debug logging enabled or disabled.
static int32_t getInt32Property(const std::string &name, const int32_t default_value, const int32_t min_allowed=INT32_MIN, const int32_t max_allowed=INT32_MAX) noexcept
Returns the int32_t value of the environment's variable 'name', or the 'default_value' if the environ...
static fraction_i64 getFractionProperty(const std::string &name, const fraction_i64 &default_value, const fraction_i64 &min_allowed, const fraction_i64 &max_allowed) noexcept
Returns the fraction_i64 value of the environment's variable 'name' in format <num>/<denom>,...
static bool getBooleanProperty(const std::string &name, const bool default_value) noexcept
Returns the boolean value of the environment's variable 'name', or the 'default_value' if the environ...
static const fraction_timespec startupTimeMonotonic
Module startup time t0 in monotonic time using high precision and range of fraction_timespec.
static const uint64_t startupTimeMilliseconds
Module startup time t0 in monotonic time in milliseconds.
static uint32_t getUint32Property(const std::string &name, const uint32_t default_value, const uint32_t min_allowed=0, const uint32_t max_allowed=UINT32_MAX) noexcept
Returns the uint32_t value of the environment's variable 'name', or the 'default_value' if the enviro...
static void set_terminating() noexcept
Optional path to signal early termination, i.e.
static bool is_terminating() noexcept
Returns true if program is terminating as detected via atexit() callback or set_terminating() has bee...
#define ERR_PRINT(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
#define COND_PRINT(C,...)
Use for conditional plain messages, prefix '[elapsed_time] '.
static const std::string s_false("false")
static const std::string s_true("true")
static jau::sc_atomic_bool is_terminating_
static bool atexit_cockie
static void at_exit_func() noexcept
static int install_atexit() noexcept
ordered_atomic< bool, std::memory_order_seq_cst > sc_atomic_bool
SC atomic integral scalar boolean.
constexpr bool value(const Bool rhs) noexcept
constexpr std::string_view name(const Bool v) noexcept
@ verbose
Enable verbosity mode, potentially used by a path_visitor implementation like remove().
fraction_timespec getMonotonicTime() noexcept
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
FracI64SizeBoolTuple to_fraction_i64(const std::string &value, const fraction_i64 &min_allowed, const fraction_i64 &max_allowed) noexcept
Returns the fraction_i64 of the given string in format <num>/<denom>, which may contain whitespace.
fraction< int64_t > fraction_i64
fraction using int64_t as integral type
void trimInPlace(std::string &s) noexcept
trim in place
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
uint64_t getCurrentMilliseconds() noexcept
Returns current monotonic time in milliseconds.
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...