diff --git a/client/android/property.cpp b/client/android/property.cpp new file mode 100644 index 00000000..5b249d12 --- /dev/null +++ b/client/android/property.cpp @@ -0,0 +1,38 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2023 Guillaume Meunier + * Copyright (C) 2023 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "property.h" +#include "utils/wrap_lambda.h" + +#include + +std::optional get_property(const char * property) +{ + auto info = __system_property_find(property); + if (not info) + return std::nullopt; + + std::string result; + wrap_lambda cb = [&result](const char * name, const char * value, uint32_t serial) { + result = value; + }; + + __system_property_read_callback(info, cb.userdata_first(), cb); + return result; +} diff --git a/client/android/property.h b/client/android/property.h new file mode 100644 index 00000000..f9b6020b --- /dev/null +++ b/client/android/property.h @@ -0,0 +1,25 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2023 Guillaume Meunier + * Copyright (C) 2023 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include + +std::optional get_property(const char * property); diff --git a/client/hmd_traits.cpp b/client/hmd_traits.cpp index 40ea3fa2..4134ccce 100644 --- a/client/hmd_traits.cpp +++ b/client/hmd_traits.cpp @@ -19,8 +19,8 @@ #include "hmd_traits.h" +#include "utils/env.h" #include "utils/overloaded.h" -#include "utils/strings.h" #include "xr/to_string.h" #include @@ -41,114 +41,11 @@ #include #ifdef __ANDROID__ -#include "utils/wrap_lambda.h" - -#include - -static std::optional get_property(const char * property) -{ - auto info = __system_property_find(property); - if (not info) - return std::nullopt; - - std::string result; - wrap_lambda cb = [&result](const char * name, const char * value, uint32_t serial) { - result = value; - }; - - __system_property_read_callback(info, cb.userdata_first(), cb); - return result; -} +#include "android/property.h" +#include "utils/env.h" +#include "utils/strings.h" #endif -template -std::optional env(std::string_view name); - -template <> -std::optional env(std::string_view name) -{ -#ifdef __ANDROID__ - auto android_sysprop_name = std::format("debug.wivrn.{}", name); - return get_property(android_sysprop_name.c_str()); -#else - std::string env_name = "WIVRN_"; - env_name.reserve(env_name.size() + name.size()); - for (const auto & c: name) - env_name += std::toupper(c); - const char * value = std::getenv(env_name.c_str()); - if (value) - return value; - return std::nullopt; -#endif -} - -template <> -std::optional env(std::string_view name) -{ - const auto value = env(name); - if (not value) - return std::nullopt; - try - { - const unsigned long parsed = std::stoul(*value); - if (parsed > std::numeric_limits::max()) - return std::nullopt; - return static_cast(parsed); - } - catch (std::runtime_error & e) - { - spdlog::warn("failed to parse {} (value: {}: {}", name, *value, e.what()); - return std::nullopt; - } -} - -template <> -std::optional env(std::string_view name) -{ - const auto value = env(name); - if (not value) - return std::nullopt; - if (strcmp(value->c_str(), "1") == 0 || strcasecmp(value->c_str(), "true") == 0 || strcasecmp(value->c_str(), "yes") == 0 || strcasecmp(value->c_str(), "on") == 0) - return true; - if (strcmp(value->c_str(), "0") == 0 || strcasecmp(value->c_str(), "false") == 0 || strcasecmp(value->c_str(), "no") == 0 || strcasecmp(value->c_str(), "off") == 0) - return false; - return std::nullopt; -} - -template <> -std::optional> env(std::string_view name) -{ - const auto values = env(name); - if (not values) - return std::nullopt; - - std::unordered_map map; - for (auto value: utils::split(*values, ",")) - { - if (auto pos = value.find('='); pos != std::string::npos) - map.insert(std::make_pair(value.substr(0, pos), value.substr(pos + 1))); - } - - return map; -} - -template <> -std::optional> env(std::string_view name) -{ - const auto values = env(name); - if (not values) - return std::nullopt; - -#if defined(__cpp_lib_containers_ranges) && __cpp_lib_containers_ranges >= 202202L - return std::unordered_set{std::from_range, utils::split(*values, ",")}; -#else - std::optional> res{std::in_place}; - for (auto & val: utils::split(*values)) - res->insert(std::move(val)); - return res; -#endif -} - void hmd_traits::init() { #ifndef NDEBUG diff --git a/client/main.cpp b/client/main.cpp index 3e5d79ef..08abf413 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -20,10 +20,13 @@ #include "application.h" #include "scenes/lobby.h" #include "spdlog/spdlog.h" +#include "utils/env.h" #include "version.h" +#include #include #include +#include #include #include @@ -41,6 +44,28 @@ void real_main(android_app * native_app) void real_main(int argc, char ** argv) #endif { + auto log_level = env("loglevel"); + if (log_level) + { + std::transform(log_level->begin(), log_level->end(), log_level->begin(), [](char c) { return tolower(c); }); + if (log_level == "trace") + spdlog::set_level(spdlog::level::trace); + else if (log_level == "debug") + spdlog::set_level(spdlog::level::debug); + else if (log_level == "info") + spdlog::set_level(spdlog::level::info); + else if (log_level == "warning") + spdlog::set_level(spdlog::level::warn); + else if (log_level == "error") + spdlog::set_level(spdlog::level::err); + else if (log_level == "critical") + spdlog::set_level(spdlog::level::critical); + else if (log_level == "off") + spdlog::set_level(spdlog::level::off); + else + spdlog::warn("Invalid value for log level: {}", *log_level); + } + if (wivrn::is_tag) spdlog::info("Starting WiVRn version {}", wivrn::display_version()); else @@ -107,27 +132,6 @@ int main(int argc, char * argv[]) { spdlog::set_default_logger(spdlog::stdout_color_mt("WiVRn")); - char * loglevel = getenv("WIVRN_LOGLEVEL"); - if (loglevel) - { - if (!strcasecmp(loglevel, "trace")) - spdlog::set_level(spdlog::level::trace); - else if (!strcasecmp(loglevel, "debug")) - spdlog::set_level(spdlog::level::debug); - else if (!strcasecmp(loglevel, "info")) - spdlog::set_level(spdlog::level::info); - else if (!strcasecmp(loglevel, "warning")) - spdlog::set_level(spdlog::level::warn); - else if (!strcasecmp(loglevel, "error")) - spdlog::set_level(spdlog::level::err); - else if (!strcasecmp(loglevel, "critical")) - spdlog::set_level(spdlog::level::critical); - else if (!strcasecmp(loglevel, "off")) - spdlog::set_level(spdlog::level::off); - else - spdlog::warn("Invalid value for WIVRN_LOGLEVEL environment variable"); - } - real_main(argc, argv); } #endif diff --git a/client/utils/env.h b/client/utils/env.h new file mode 100644 index 00000000..07b89c5d --- /dev/null +++ b/client/utils/env.h @@ -0,0 +1,123 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2023 Guillaume Meunier + * Copyright (C) 2023 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "utils/strings.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __ANDROID__ +#include "android/property.h" +#endif + +template +std::optional env(std::string_view name); + +template <> +inline std::optional env(std::string_view name) +{ +#ifdef __ANDROID__ + auto android_sysprop_name = std::format("debug.wivrn.{}", name); + return get_property(android_sysprop_name.c_str()); +#else + std::string env_name = "WIVRN_"; + env_name.reserve(env_name.size() + name.size()); + for (const auto & c: name) + env_name += std::toupper(c); + const char * value = std::getenv(env_name.c_str()); + if (value) + return value; + return std::nullopt; +#endif +} + +template <> +inline std::optional env(std::string_view name) +{ + const auto value = env(name); + if (not value) + return std::nullopt; + try + { + const unsigned long parsed = std::stoul(*value); + if (parsed > std::numeric_limits::max()) + return std::nullopt; + return static_cast(parsed); + } + catch (std::runtime_error & e) + { + spdlog::warn("failed to parse {} (value: {}: {}", name, *value, e.what()); + return std::nullopt; + } +} + +template <> +inline std::optional env(std::string_view name) +{ + const auto value = env(name); + if (not value) + return std::nullopt; + if (strcmp(value->c_str(), "1") == 0 || strcasecmp(value->c_str(), "true") == 0 || strcasecmp(value->c_str(), "yes") == 0 || strcasecmp(value->c_str(), "on") == 0) + return true; + if (strcmp(value->c_str(), "0") == 0 || strcasecmp(value->c_str(), "false") == 0 || strcasecmp(value->c_str(), "no") == 0 || strcasecmp(value->c_str(), "off") == 0) + return false; + return std::nullopt; +} + +template <> +inline std::optional> env(std::string_view name) +{ + const auto values = env(name); + if (not values) + return std::nullopt; + + std::unordered_map map; + for (auto value: utils::split(*values, ",")) + { + if (auto pos = value.find('='); pos != std::string::npos) + map.insert(std::make_pair(value.substr(0, pos), value.substr(pos + 1))); + } + + return map; +} + +template <> +inline std::optional> env(std::string_view name) +{ + const auto values = env(name); + if (not values) + return std::nullopt; + +#if defined(__cpp_lib_containers_ranges) && __cpp_lib_containers_ranges >= 202202L + return std::unordered_set{std::from_range, utils::split(*values, ",")}; +#else + std::optional> res{std::in_place}; + for (auto & val: utils::split(*values)) + res->insert(std::move(val)); + return res; +#endif +} \ No newline at end of file