diff --git a/CHANGELOG.md b/CHANGELOG.md index 92e184a3ff..233e1fedb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project are documented in this file. - Implement FeasibleContactWrenchTask for TSID component (https://github.com/dic-iit/bipedal-locomotion-framework/pull/369). - Implement python bindings for QPInverseKinematics class (https://github.com/dic-iit/bipedal-locomotion-framework/pull/303) - Implement `ControlTask` in for System component (https://github.com/dic-iit/bipedal-locomotion-framework/pull/373). +- Allow changing the log verbosity (https://github.com/dic-iit/bipedal-locomotion-framework/pull/385) ### Changed - Add common Python files to gitignore (https://github.com/dic-iit/bipedal-locomotion-framework/pull/338) diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index c4bac2e0a7..8c66b3b850 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -10,6 +10,7 @@ add_subdirectory(RobotInterface) add_subdirectory(Math) add_subdirectory(FloatingBaseEstimators) add_subdirectory(IK) +add_subdirectory(TextLogging) include(ConfigureFileWithCMakeIf) diff --git a/bindings/python/TextLogging/CMakeLists.txt b/bindings/python/TextLogging/CMakeLists.txt new file mode 100644 index 0000000000..fde5525ab0 --- /dev/null +++ b/bindings/python/TextLogging/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright (C) 2021 Istituto Italiano di Tecnologia (IIT). All rights reserved. +# This software may be modified and distributed under the terms of the +# GNU Lesser General Public License v2.1 or any later version. + +set(H_PREFIX include/BipedalLocomotion/bindings/TextLogging) + +add_bipedal_locomotion_python_module( + NAME TextLogging + SOURCES + src/Module.cpp + src/TextLogging.cpp + HEADERS + ${H_PREFIX}/Module.h + ${H_PREFIX}/TextLogging.h + LINK_LIBRARIES + BipedalLocomotion::TextLogging + ) diff --git a/bindings/python/TextLogging/include/BipedalLocomotion/bindings/TextLogging/Module.h b/bindings/python/TextLogging/include/BipedalLocomotion/bindings/TextLogging/Module.h new file mode 100644 index 0000000000..e0b69bd77c --- /dev/null +++ b/bindings/python/TextLogging/include/BipedalLocomotion/bindings/TextLogging/Module.h @@ -0,0 +1,18 @@ +/** + * @file Module.h + * @authors Diego Ferigo + * @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and + * distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. + */ + +#ifndef BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_MODULE_H +#define BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_MODULE_H + +#include + +namespace BipedalLocomotion::bindings::TextLogging +{ +void CreateModule(pybind11::module& module); +} // namespace BipedalLocomotion::bindings::TextLogging + +#endif // BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_MODULE_H diff --git a/bindings/python/TextLogging/include/BipedalLocomotion/bindings/TextLogging/TextLogging.h b/bindings/python/TextLogging/include/BipedalLocomotion/bindings/TextLogging/TextLogging.h new file mode 100644 index 0000000000..c4ac6de74e --- /dev/null +++ b/bindings/python/TextLogging/include/BipedalLocomotion/bindings/TextLogging/TextLogging.h @@ -0,0 +1,18 @@ +/** + * @file TextLogging.h + * @authors Diego Ferigo + * @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and + * distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. + */ + +#ifndef BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_H +#define BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_H + +#include + +namespace BipedalLocomotion::bindings::TextLogging +{ +void CreateTextLogging(pybind11::module& module); +} // namespace BipedalLocomotion::bindings::TextLogging + +#endif // BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_H diff --git a/bindings/python/TextLogging/src/Module.cpp b/bindings/python/TextLogging/src/Module.cpp new file mode 100644 index 0000000000..347bf7d20b --- /dev/null +++ b/bindings/python/TextLogging/src/Module.cpp @@ -0,0 +1,20 @@ +/** + * @file Module.cpp + * @authors Diego Ferigo + * @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and + * distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. + */ + +#include + +#include "BipedalLocomotion/bindings/TextLogging/Module.h" +#include "BipedalLocomotion/bindings/TextLogging/TextLogging.h" + +namespace BipedalLocomotion::bindings::TextLogging +{ +void CreateModule(pybind11::module& module) +{ + module.doc() = "Text logging module"; + CreateTextLogging(module); +} +} // namespace BipedalLocomotion::bindings::TextLogging diff --git a/bindings/python/TextLogging/src/TextLogging.cpp b/bindings/python/TextLogging/src/TextLogging.cpp new file mode 100644 index 0000000000..35dbeac0bd --- /dev/null +++ b/bindings/python/TextLogging/src/TextLogging.cpp @@ -0,0 +1,29 @@ +/** + * @file TextLogging.cpp + * @authors Diego Ferigo + * @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and + * distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. + */ + +#include "BipedalLocomotion/bindings/TextLogging/TextLogging.h" +#include "BipedalLocomotion/TextLogging/Logger.h" + +namespace BipedalLocomotion::bindings::TextLogging +{ +void CreateTextLogging(pybind11::module& module) +{ + namespace py = ::pybind11; + using namespace BipedalLocomotion::TextLogging; + + py::enum_(module, "Verbosity", py::arithmetic()) + .value("Trace", Verbosity::Trace) + .value("Debug", Verbosity::Debug) + .value("Info", Verbosity::Info) + .value("Warn", Verbosity::Warn) + .value("Err", Verbosity::Err) + .value("Critical", Verbosity::Critical) + .value("Off", Verbosity::Off); + + module.def("set_verbosity", &setVerbosity, py::arg("verbosity")); +} +} // namespace BipedalLocomotion::bindings::TextLogging diff --git a/bindings/python/bipedal_locomotion_framework.cpp.in b/bindings/python/bipedal_locomotion_framework.cpp.in index ee85fdf787..e1d8133bf8 100644 --- a/bindings/python/bipedal_locomotion_framework.cpp.in +++ b/bindings/python/bipedal_locomotion_framework.cpp.in @@ -5,10 +5,12 @@ * distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. */ +// clang-format off #include #include #include +#include @cmakeif FRAMEWORK_COMPILE_YarpImplementation #include @@ -51,6 +53,9 @@ PYBIND11_MODULE(bindings, m) m.doc() = "BipedalLocomotionFramework bindings"; + py::module textLoggingModule = m.def_submodule("logging"); + bindings::TextLogging::CreateModule(textLoggingModule); + py::module parametersHandlerModule = m.def_submodule("parameters_handler"); bindings::ParametersHandler::CreateModule(parametersHandlerModule); diff --git a/src/TextLogging/include/BipedalLocomotion/TextLogging/Logger.h b/src/TextLogging/include/BipedalLocomotion/TextLogging/Logger.h index 64e88ceefc..fe0ba4bb8d 100644 --- a/src/TextLogging/include/BipedalLocomotion/TextLogging/Logger.h +++ b/src/TextLogging/include/BipedalLocomotion/TextLogging/Logger.h @@ -20,6 +20,24 @@ namespace TextLogging using Logger = spdlog::logger; +enum class Verbosity +{ + Trace, + Debug, + Info, + Warn, + Err, + Critical, + Off, +}; + +/** + * Set the logger verbosity. + * + * @param verbosity The desired verbosity level. + */ +void setVerbosity(const TextLogging::Verbosity verbosity); + } // namespace TextLogging /** diff --git a/src/TextLogging/src/Logger.cpp b/src/TextLogging/src/Logger.cpp index d3e062608a..b14135b32b 100644 --- a/src/TextLogging/src/Logger.cpp +++ b/src/TextLogging/src/Logger.cpp @@ -47,4 +47,25 @@ TextLogging::Logger* const log() return logger.get(); } +void TextLogging::setVerbosity(const Verbosity verbosity) +{ + const std::unordered_map map{ + {TextLogging::Verbosity::Trace, spdlog::level::level_enum::trace}, + {TextLogging::Verbosity::Debug, spdlog::level::level_enum::debug}, + {TextLogging::Verbosity::Info, spdlog::level::level_enum::info}, + {TextLogging::Verbosity::Warn, spdlog::level::level_enum::warn}, + {TextLogging::Verbosity::Err, spdlog::level::level_enum::err}, + {TextLogging::Verbosity::Critical, spdlog::level::level_enum::critical}, + {TextLogging::Verbosity::Off, spdlog::level::level_enum::off}, + }; + + if (map.find(verbosity) == map.end()) + { + log()->error("Failed to change verbosity to level {}", verbosity); + return; + } + + log()->set_level(map.at(verbosity)); +} + } // namespace BipedalLocomotion