55bool environment::local_debug =
false;
57static const std::string
s_true(
"true");
61 const char * value = ::getenv(name.c_str());
62 if(
nullptr != value ) {
63 COND_PRINT(local_debug,
"env::getProperty0 '%s': '%s'", name.c_str(), 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());
84 std::string value = getProperty(name);
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());
95 const std::string value = getProperty(name);
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 const std::string value = getProperty(name);
112 if( 0 == value.length() ) {
113 COND_PRINT(local_debug,
"env::getInt32Property %s: null -> %" PRId32
" (default)", name.c_str(), default_value);
114 return default_value;
116 int32_t res = default_value;
117 char *endptr =
nullptr;
118 const long int res0 = strtol(value.c_str(), &endptr, 10);
119 if( *endptr ==
'\0' ) {
121 if( INT32_MIN <= res0 && res0 <= INT32_MAX ) {
123 const int32_t res1 = (int32_t)res0;
124 if( min_allowed <= res1 && res1 <= max_allowed ) {
127 COND_PRINT(local_debug,
"env::getInt32Property %s (default %" PRId32
"): %" PRId32
"/%s",
128 name.c_str(), default_value, res, value.c_str());
131 ERR_PRINT(
"env::getInt32Property %s: %" PRId32
"/%s (invalid user range [% " PRId32
"..%" PRId32
"]) -> %" PRId32
" (default)",
132 name.c_str(), res1, value.c_str(), min_allowed, max_allowed, res);
136 ERR_PRINT(
"env::getInt32Property %s: %" PRIu64
"/%s (invalid int32_t range) -> %" PRId32
" (default)",
137 name.c_str(), (uint64_t)res0, value.c_str(), res);
141 ERR_PRINT(
"env::getInt32Property %s: %s (invalid string) -> %" PRId32
" (default)",
142 name.c_str(), value.c_str(), res);
149 const uint32_t min_allowed,
const uint32_t max_allowed)
noexcept
151 const std::string value = getProperty(name);
152 if( 0 == value.length() ) {
153 COND_PRINT(local_debug,
"env::getUint32Property %s: null -> %" PRIu32
" (default)", name.c_str(), default_value);
154 return default_value;
156 uint32_t res = default_value;
157 char *endptr =
nullptr;
158 unsigned long int res0 = strtoul(value.c_str(), &endptr, 10);
159 if( *endptr ==
'\0' ) {
161 if( res0 <= UINT32_MAX ) {
163 const uint32_t res1 = (uint32_t)res0;
164 if( min_allowed <= res1 && res1 <= max_allowed ) {
167 COND_PRINT(local_debug,
"env::getUint32Property %s (default %" PRIu32
"): %" PRIu32
"/%s",
168 name.c_str(), default_value, res, value.c_str());
171 ERR_PRINT(
"env::getUint32Property %s: %" PRIu32
"/%s (invalid user range [% " PRIu32
"..%" PRIu32
"]) -> %" PRIu32
" (default)",
172 name.c_str(), res1, value.c_str(), min_allowed, max_allowed, res);
176 ERR_PRINT(
"env::getUint32Property %s: %" PRIu64
"/%s (invalid uint32_t range) -> %" PRIu32
" (default)",
177 name.c_str(), (uint64_t)res0, value.c_str(), res);
181 ERR_PRINT(
"env::getUint32Property %s: %s (invalid string) -> %" PRIu32
" (default)",
182 name.c_str(), value.c_str(), res);
190 const std::string value = getProperty(name);
191 if( 0 == value.length() ) {
192 COND_PRINT(local_debug,
"env::getFractionProperty %s: null -> %s (default)", name.c_str(), default_value.to_string().c_str());
193 return default_value;
197 ERR_PRINT(
"env::getFractionProperty %s: value %s not valid or in range[%s .. %s] -> %s (default)",
198 name.c_str(), value.c_str(), min_allowed.to_string().c_str(), max_allowed.to_string().c_str(), default_value.to_string().c_str());
204void environment::envSet(
const std::string& prefix_domain, std::string basepair)
noexcept {
206 if( basepair.length() > 0 ) {
207 size_t pos = 0, start = 0;
208 if( (pos = basepair.find(
'=', start)) != std::string::npos ) {
209 const size_t elem_len = pos-start;
210 std::string name = prefix_domain+
"."+basepair.substr(start, elem_len);
211 std::string value = basepair.substr(pos+1, std::string::npos);
214 if( name.length() > 0 ) {
215 if( value.length() > 0 ) {
216 COND_PRINT(local_debug,
"env::setProperty %s -> %s (explode)", name.c_str(), value.c_str());
217 ::setenv(name.c_str(), value.c_str(), 1 );
219 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default-1)", name.c_str());
220 ::setenv(name.c_str(),
"true", 1 );
224 const std::string name = prefix_domain+
"."+basepair;
225 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default-0)", name.c_str());
226 ::setenv(name.c_str(),
"true", 1 );
231void environment::envExplodeProperties(
const std::string& prefix_domain,
const std::string& list)
noexcept {
232 size_t pos = 0, start = 0;
233 while( (pos = list.find(
',', start)) != std::string::npos ) {
234 const size_t elem_len = pos-start;
235 envSet(prefix_domain, list.substr(start, elem_len));
238 const size_t elem_len = list.length()-start;
240 envSet(prefix_domain, list.substr(start, elem_len));
242 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default)", prefix_domain.c_str());
243 ::setenv(prefix_domain.c_str(),
"true", 1 );
246bool environment::getExplodingPropertiesImpl(
const std::string& root_prefix_domain,
const std::string & prefix_domain)
noexcept {
254 if( root_prefix_domain.length() > 0 && root_prefix_domain+
".debug" == prefix_domain ) {
257 envExplodeProperties(prefix_domain, value);
261environment::environment(
const std::string & root_prefix_domain_) noexcept
262: root_prefix_domain(root_prefix_domain_),
263 debug( getExplodingPropertiesImpl(root_prefix_domain_, root_prefix_domain_+
".debug") ),
264 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.
#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
@ verbose
Enable verbosity mode, potentially used by a path_visitor implementation like remove().
bool to_fraction_i64(fraction_i64 &result, const std::string &value, const fraction_i64 &min_allowed, const fraction_i64 &max_allowed) noexcept
Stores the fraction_i64 value of the given string value in format <num>/<denom>, which may contain wh...
fraction_timespec getMonotonicTime() noexcept
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
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...
CXX_ALWAYS_INLINE _Tp load() const noexcept