aboutsummaryrefslogtreecommitdiffstats
path: root/src/direct_bt/DBTEnv.cpp
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-08-24 05:33:27 +0200
committerSven Gothel <[email protected]>2020-08-24 05:33:27 +0200
commitc1e543aacebd4c769abf4d6415c63d1a7ee6809d (patch)
tree7f7dc38fe61ca83406a2fda3956933e59cf32a64 /src/direct_bt/DBTEnv.cpp
parent790e705aa16f47effd1db6e58a84123b9e357c9f (diff)
BluetoothFactory/DBTEnv: Pass JVM properties to environment, access DEBUG, VERBOSE via lazy DBTEnv from C++ (
DEBUG := environment 'direct_bt_debug' or JVM property 'direct_bt.debug' VERBOSE := environment 'direct_bt_verbose' or JVM property 'direct_bt.verbose' This changes allows passing JVM properties as C++ environment variables, to be accessed via DBTEnv. JVM property names are renamed from 'foo.bar' to 'jvm_foo_bar' and can be queried via 'DBTEnv::getProperty("foo_bar")' as it will also attempt the 'jvm_' prefix if the plain name wasn't resolved. The singleton DBTEnv instance can be retrieved via DBTEnv::get(), which allows lazy initialization of DEBUG, VERBOSE from environment variables. This is required, as the JVM loads the native libraries first, initializes all native static variables and only then can pass the properties to the native environment via POSIX 'setenv(..)'. Hence users should never use static initialization from native code in such cases, otherwise they can't benefit from the unified JVM properties.
Diffstat (limited to 'src/direct_bt/DBTEnv.cpp')
-rw-r--r--src/direct_bt/DBTEnv.cpp49
1 files changed, 43 insertions, 6 deletions
diff --git a/src/direct_bt/DBTEnv.cpp b/src/direct_bt/DBTEnv.cpp
index 7ea33e35..b76eeff5 100644
--- a/src/direct_bt/DBTEnv.cpp
+++ b/src/direct_bt/DBTEnv.cpp
@@ -30,13 +30,50 @@
#include <vector>
#include <cstdio>
-#include "DBTEnv.hpp"
+#include "direct_bt/DBTEnv.hpp"
+#include "direct_bt/dbt_debug.hpp"
-extern "C" {
- #include <inttypes.h>
- #include <unistd.h>
+using namespace direct_bt;
+
+const uint64_t DBTEnv::startupTimeMilliseconds = direct_bt::getCurrentMilliseconds();
+
+const char * DBTEnv::getProperty(const char *name) {
+ const char * value = getenv(name);
+ if( nullptr != value ) {
+ return value;
+ } else {
+ char name2[strlen(name)+4+1];
+ strcpy(name2, "jvm_");
+ strcpy(name2+4, name);
+ return getenv(name2);
+ }
}
-using namespace direct_bt;
+std::string DBTEnv::getProperty(const char *name, const std::string & default_value) {
+ const char * value = getProperty(name);
+ if( nullptr != value ) {
+ PLAIN_PRINT("DBTEnv::getProperty %s (default %s) -> %s", name, default_value.c_str(), value);
+ return value;
+ } else {
+ PLAIN_PRINT("DBTEnv::getProperty %s -> null -> %s (default)", name, default_value.c_str());
+ return default_value;
+ }
+}
+
+bool DBTEnv::getBooleanProperty(const char *name, const bool default_value) {
+ const char * value = getProperty(name);
+ if( nullptr != value ) {
+ bool res = 0==strcmp("true", value);
+ PLAIN_PRINT("DBTEnv::getBooleanProperty %s (default %d) -> %d/%s", name, default_value, res, value);
+ return res;
+ } else {
+ PLAIN_PRINT("DBTEnv::getBooleanProperty %s -> null -> %d (default)", name, default_value);
+ return default_value;
+ }
+}
-const uint64_t DBTEnv::startupTimeMilliseconds = getCurrentMilliseconds();
+DBTEnv::DBTEnv()
+: DEBUG( DBTEnv::getBooleanProperty("direct_bt_debug", false) ),
+ VERBOSE( DBTEnv::DEBUG || DBTEnv::getBooleanProperty("direct_bt_verbose", false) )
+{
+}