41 bool environment::local_debug =
false;
43 static const std::string
s_true(
"true");
44 static const std::string
s_false(
"false");
47 const char * value = getenv(name.c_str());
48 if(
nullptr != value ) {
49 COND_PRINT(local_debug,
"env::getProperty0 '%s': '%s'", name.c_str(), value);
50 return std::string( value );
52 if( std::string::npos != name.find(
'.', 0) ) {
54 std::string alt_name(name);
55 std::replace( alt_name.begin(), alt_name.end(),
'.',
'_');
56 value = getenv(alt_name.c_str());
57 if(
nullptr != value ) {
58 COND_PRINT(local_debug,
"env::getProperty0 '%s' -> '%s': '%s'", name.c_str(), alt_name.c_str(), value);
59 return std::string( value );
61 COND_PRINT(local_debug,
"env::getProperty0 '%s' -> '%s': NOT FOUND", name.c_str(), alt_name.c_str());
63 COND_PRINT(local_debug,
"env::getProperty0 '%s': NOT FOUND", name.c_str());
70 const std::string value = getProperty(name);
71 if( 0 == value.length() ) {
72 COND_PRINT(local_debug,
"env::getProperty1 %s: null -> %s (default)", name.c_str(), default_value.c_str());
75 COND_PRINT(local_debug,
"env::getProperty1 %s (default %s): %s", name.c_str(), default_value.c_str(), value.c_str());
81 const std::string value = getProperty(name);
82 if( 0 == value.length() ) {
83 COND_PRINT(local_debug,
"env::getBooleanProperty %s: null -> %d (default)", name.c_str(), default_value);
86 const bool res =
"true" == value;
87 COND_PRINT(local_debug,
"env::getBooleanProperty %s (default %d): %d/%s", name.c_str(), default_value, res, value.c_str());
95 const int32_t min_allowed,
const int32_t max_allowed) noexcept
97 const std::string value = getProperty(name);
98 if( 0 == value.length() ) {
99 COND_PRINT(local_debug,
"env::getInt32Property %s: null -> %" PRId32
" (default)", name.c_str(), default_value);
100 return default_value;
102 int32_t res = default_value;
104 const long int res0 = strtol(value.c_str(), &endptr, 10);
105 if( *endptr ==
'\0' ) {
107 if( INT32_MIN <= res0 && res0 <= INT32_MAX ) {
109 const int32_t res1 = (int32_t)res0;
110 if( min_allowed <= res1 && res1 <= max_allowed ) {
113 COND_PRINT(local_debug,
"env::getInt32Property %s (default %" PRId32
"): %" PRId32
"/%s",
114 name.c_str(), default_value, res, value.c_str());
117 ERR_PRINT(
"env::getInt32Property %s: %" PRId32
"/%s (invalid user range [% " PRId32
"..%" PRId32
"]) -> %" PRId32
" (default)",
118 name.c_str(), res1, value.c_str(), min_allowed, max_allowed, res);
122 ERR_PRINT(
"env::getInt32Property %s: %" PRIu64
"/%s (invalid int32_t range) -> %" PRId32
" (default)",
123 name.c_str(), (uint64_t)res0, value.c_str(), res);
127 ERR_PRINT(
"env::getInt32Property %s: %s (invalid string) -> %" PRId32
" (default)",
128 name.c_str(), value.c_str(), res);
135 const uint32_t min_allowed,
const uint32_t max_allowed) noexcept
137 const std::string value = getProperty(name);
138 if( 0 == value.length() ) {
139 COND_PRINT(local_debug,
"env::getUint32Property %s: null -> %" PRIu32
" (default)", name.c_str(), default_value);
140 return default_value;
142 uint32_t res = default_value;
144 unsigned long int res0 = strtoul(value.c_str(), &endptr, 10);
145 if( *endptr ==
'\0' ) {
147 if( res0 <= UINT32_MAX ) {
149 const uint32_t res1 = (uint32_t)res0;
150 if( min_allowed <= res1 && res1 <= max_allowed ) {
153 COND_PRINT(local_debug,
"env::getUint32Property %s (default %" PRIu32
"): %" PRIu32
"/%s",
154 name.c_str(), default_value, res, value.c_str());
157 ERR_PRINT(
"env::getUint32Property %s: %" PRIu32
"/%s (invalid user range [% " PRIu32
"..%" PRIu32
"]) -> %" PRIu32
" (default)",
158 name.c_str(), res1, value.c_str(), min_allowed, max_allowed, res);
162 ERR_PRINT(
"env::getUint32Property %s: %" PRIu64
"/%s (invalid uint32_t range) -> %" PRIu32
" (default)",
163 name.c_str(), (uint64_t)res0, value.c_str(), res);
167 ERR_PRINT(
"env::getUint32Property %s: %s (invalid string) -> %" PRIu32
" (default)",
168 name.c_str(), value.c_str(), res);
174 void environment::envSet(std::string prefix_domain, std::string basepair) noexcept {
176 if( basepair.length() > 0 ) {
177 size_t pos = 0, start = 0;
178 if( (pos = basepair.find(
'=', start)) != std::string::npos ) {
179 const size_t elem_len = pos-start;
180 std::string name = prefix_domain+
"."+basepair.substr(start, elem_len);
181 std::string value = basepair.substr(pos+1, std::string::npos);
184 if( name.length() > 0 ) {
185 if( value.length() > 0 ) {
186 COND_PRINT(local_debug,
"env::setProperty %s -> %s (explode)", name.c_str(), value.c_str());
187 setenv(name.c_str(), value.c_str(), 1 );
189 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default-1)", name.c_str());
190 setenv(name.c_str(),
"true", 1 );
194 const std::string name = prefix_domain+
"."+basepair;
195 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default-0)", name.c_str());
196 setenv(name.c_str(),
"true", 1 );
201 void environment::envExplodeProperties(std::string prefix_domain, std::string list) noexcept {
202 size_t pos = 0, start = 0;
203 while( (pos = list.find(
',', start)) != std::string::npos ) {
204 const size_t elem_len = pos-start;
205 envSet(prefix_domain, list.substr(start, elem_len));
208 const size_t elem_len = list.length()-start;
210 envSet(prefix_domain, list.substr(start, elem_len));
212 COND_PRINT(local_debug,
"env::setProperty %s -> true (explode default)", prefix_domain.c_str());
213 setenv(prefix_domain.c_str(),
"true", 1 );
216 bool environment::getExplodingPropertiesImpl(
const std::string & root_prefix_domain,
const std::string & prefix_domain) noexcept {
224 if( root_prefix_domain.length() > 0 && root_prefix_domain+
".debug" == prefix_domain ) {
227 envExplodeProperties(prefix_domain, value);
231 environment::environment(
const std::string & root_prefix_domain_) noexcept
232 : root_prefix_domain(root_prefix_domain_),
233 debug( getExplodingPropertiesImpl(root_prefix_domain_, root_prefix_domain_+
".debug") ),
234 debug_jni( getBooleanProperty(root_prefix_domain_+
".debug.jni",
false) ),
235 verbose( getExplodingPropertiesImpl(root_prefix_domain_, root_prefix_domain_+
".verbose") ||
environment::debug )