Loading configstore/utils/Android.bp +9 −1 Original line number Diff line number Diff line Loading @@ -14,14 +14,22 @@ // limitations under the License. // cc_library_headers { cc_library_shared { name: "android.hardware.configstore-utils", defaults: ["hidl_defaults"], srcs: [ "ConfigStoreUtils.cpp" ], export_include_dirs: ["include"], shared_libs: [ "android.hardware.configstore@1.0", "libbase", "libhidlbase" ], export_shared_lib_headers: [ "android.hardware.configstore@1.0", "libbase", "libhidlbase" ], } configstore/utils/ConfigStoreUtils.cpp 0 → 100644 +40 −0 Original line number Diff line number Diff line // // Copyright (C) 2017 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #define LOG_TAG "ConfigStore" #include <android-base/logging.h> #include <configstore/Utils.h> namespace android { namespace hardware { namespace details { bool wouldLogInfo() { return WOULD_LOG(INFO); } void logAlwaysInfo(const std::string& message) { LOG(INFO) << message; } void logAlwaysError(const std::string& message) { LOG(ERROR) << message; } } // namespace details } // namespace hardware } // namespace android configstore/utils/include/configstore/Utils.h +41 −6 Original line number Diff line number Diff line Loading @@ -17,20 +17,31 @@ #ifndef ANDROID_HARDWARE_CONFIGSTORE_UTILS_H #define ANDROID_HARDWARE_CONFIGSTORE_UTILS_H #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> #include <hidl/Status.h> #include <stdatomic.h> #include <sstream> namespace android { namespace hardware { namespace details { // Templated classes can use the below method // to avoid creating dependencies on liblog. bool wouldLogInfo(); void logAlwaysInfo(const std::string& message); void logAlwaysError(const std::string& message); } // namespace details namespace configstore { using namespace android::hardware::configstore::V1_0; // arguments V: type for the value (i.e., OptionalXXX) // I: interface class name // func: member function pointer using namespace V1_0; template<typename V, typename I, android::hardware::Return<void> (I::* func) (std::function<void(const V&)>)> decltype(V::value) get(const decltype(V::value) &defValue) { using namespace android::hardware::details; auto getHelper = []()->V { V ret; sp<I> configs = I::getService(); Loading @@ -39,15 +50,39 @@ decltype(V::value) get(const decltype(V::value) &defValue) { // fallback to the default value ret.specified = false; } else { (*configs.*func)([&ret](V v) { ret = v; }); auto status = (*configs.*func)([&ret](V v) { ret = v; }); if (!status.isOk()) { std::ostringstream oss; oss << "HIDL call failed for retrieving a config item from " "configstore : " << status.description().c_str(); logAlwaysError(oss.str()); ret.specified = false; } } return ret; }; static V cachedValue = getHelper(); if (wouldLogInfo()) { std::string iname = __PRETTY_FUNCTION__; // func name starts with "func = " in __PRETTY_FUNCTION__ auto pos = iname.find("func = "); if (pos != std::string::npos) { iname = iname.substr(pos + sizeof("func = ")); iname.pop_back(); // remove trailing ']' } else { iname += " (unknown)"; } std::ostringstream oss; oss << iname << " retrieved: " << (cachedValue.specified ? cachedValue.value : defValue) << (cachedValue.specified ? "" : " (default)"); logAlwaysInfo(oss.str()); } return cachedValue.specified ? cachedValue.value : defValue; } Loading Loading
configstore/utils/Android.bp +9 −1 Original line number Diff line number Diff line Loading @@ -14,14 +14,22 @@ // limitations under the License. // cc_library_headers { cc_library_shared { name: "android.hardware.configstore-utils", defaults: ["hidl_defaults"], srcs: [ "ConfigStoreUtils.cpp" ], export_include_dirs: ["include"], shared_libs: [ "android.hardware.configstore@1.0", "libbase", "libhidlbase" ], export_shared_lib_headers: [ "android.hardware.configstore@1.0", "libbase", "libhidlbase" ], }
configstore/utils/ConfigStoreUtils.cpp 0 → 100644 +40 −0 Original line number Diff line number Diff line // // Copyright (C) 2017 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #define LOG_TAG "ConfigStore" #include <android-base/logging.h> #include <configstore/Utils.h> namespace android { namespace hardware { namespace details { bool wouldLogInfo() { return WOULD_LOG(INFO); } void logAlwaysInfo(const std::string& message) { LOG(INFO) << message; } void logAlwaysError(const std::string& message) { LOG(ERROR) << message; } } // namespace details } // namespace hardware } // namespace android
configstore/utils/include/configstore/Utils.h +41 −6 Original line number Diff line number Diff line Loading @@ -17,20 +17,31 @@ #ifndef ANDROID_HARDWARE_CONFIGSTORE_UTILS_H #define ANDROID_HARDWARE_CONFIGSTORE_UTILS_H #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> #include <hidl/Status.h> #include <stdatomic.h> #include <sstream> namespace android { namespace hardware { namespace details { // Templated classes can use the below method // to avoid creating dependencies on liblog. bool wouldLogInfo(); void logAlwaysInfo(const std::string& message); void logAlwaysError(const std::string& message); } // namespace details namespace configstore { using namespace android::hardware::configstore::V1_0; // arguments V: type for the value (i.e., OptionalXXX) // I: interface class name // func: member function pointer using namespace V1_0; template<typename V, typename I, android::hardware::Return<void> (I::* func) (std::function<void(const V&)>)> decltype(V::value) get(const decltype(V::value) &defValue) { using namespace android::hardware::details; auto getHelper = []()->V { V ret; sp<I> configs = I::getService(); Loading @@ -39,15 +50,39 @@ decltype(V::value) get(const decltype(V::value) &defValue) { // fallback to the default value ret.specified = false; } else { (*configs.*func)([&ret](V v) { ret = v; }); auto status = (*configs.*func)([&ret](V v) { ret = v; }); if (!status.isOk()) { std::ostringstream oss; oss << "HIDL call failed for retrieving a config item from " "configstore : " << status.description().c_str(); logAlwaysError(oss.str()); ret.specified = false; } } return ret; }; static V cachedValue = getHelper(); if (wouldLogInfo()) { std::string iname = __PRETTY_FUNCTION__; // func name starts with "func = " in __PRETTY_FUNCTION__ auto pos = iname.find("func = "); if (pos != std::string::npos) { iname = iname.substr(pos + sizeof("func = ")); iname.pop_back(); // remove trailing ']' } else { iname += " (unknown)"; } std::ostringstream oss; oss << iname << " retrieved: " << (cachedValue.specified ? cachedValue.value : defValue) << (cachedValue.specified ? "" : " (default)"); logAlwaysInfo(oss.str()); } return cachedValue.specified ? cachedValue.value : defValue; } Loading