Skip to content

Commit

Permalink
Merge pull request #385 from dic-iit/feature/set_verbosity
Browse files Browse the repository at this point in the history
Allow changing the log verbosity
  • Loading branch information
GiulioRomualdi authored Aug 3, 2021
2 parents eaa82b8 + 2a9f546 commit 5084fb7
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_subdirectory(RobotInterface)
add_subdirectory(Math)
add_subdirectory(FloatingBaseEstimators)
add_subdirectory(IK)
add_subdirectory(TextLogging)

include(ConfigureFileWithCMakeIf)

Expand Down
17 changes: 17 additions & 0 deletions bindings/python/TextLogging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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 <pybind11/pybind11.h>

namespace BipedalLocomotion::bindings::TextLogging
{
void CreateModule(pybind11::module& module);
} // namespace BipedalLocomotion::bindings::TextLogging

#endif // BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_MODULE_H
Original file line number Diff line number Diff line change
@@ -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 <pybind11/pybind11.h>

namespace BipedalLocomotion::bindings::TextLogging
{
void CreateTextLogging(pybind11::module& module);
} // namespace BipedalLocomotion::bindings::TextLogging

#endif // BIPEDAL_LOCOMOTION_BINDINGS_TEXT_LOGGING_H
20 changes: 20 additions & 0 deletions bindings/python/TextLogging/src/Module.cpp
Original file line number Diff line number Diff line change
@@ -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 <pybind11/pybind11.h>

#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
29 changes: 29 additions & 0 deletions bindings/python/TextLogging/src/TextLogging.cpp
Original file line number Diff line number Diff line change
@@ -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_<Verbosity>(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
5 changes: 5 additions & 0 deletions bindings/python/bipedal_locomotion_framework.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pybind11/pybind11.h>

#include <BipedalLocomotion/bindings/Math/Module.h>
#include <BipedalLocomotion/bindings/ParametersHandler/Module.h>
#include <BipedalLocomotion/bindings/TextLogging/Module.h>

@cmakeif FRAMEWORK_COMPILE_YarpImplementation
#include <BipedalLocomotion/bindings/ParametersHandler/YarpModule.h>
Expand Down Expand Up @@ -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);

Expand Down
18 changes: 18 additions & 0 deletions src/TextLogging/include/BipedalLocomotion/TextLogging/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
21 changes: 21 additions & 0 deletions src/TextLogging/src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,25 @@ TextLogging::Logger* const log()
return logger.get();
}

void TextLogging::setVerbosity(const Verbosity verbosity)
{
const std::unordered_map<TextLogging::Verbosity, spdlog::level::level_enum> 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

0 comments on commit 5084fb7

Please sign in to comment.