Skip to content

Commit

Permalink
Configure the Log through YAML (#433)
Browse files Browse the repository at this point in the history
* Add configuration of logs via YAML

Signed-off-by: Lucia Echevarria <[email protected]>

* Correct Log Configuration creation from YAML

Signed-off-by: Lucia Echevarria <[email protected]>

* Add Router YAML parse test

Signed-off-by: Lucia Echevarria <[email protected]>

* Update documentation and release notes

Signed-off-by: Lucia Echevarria <[email protected]>

* Apply suggested changes

Signed-off-by: Lucia Echevarria <[email protected]>

* Update documentation

Signed-off-by: Lucia Echevarria <[email protected]>

* Apply suggested changes

Signed-off-by: Lucia Echevarria <[email protected]>

* Add missing DLL dependency

Signed-off-by: Lucia Echevarria <[email protected]>

* Correct uncrustify

Signed-off-by: Lucia Echevarria <[email protected]>

* Minor change

Signed-off-by: Lucia Echevarria <[email protected]>

---------

Signed-off-by: Lucia Echevarria <[email protected]>
  • Loading branch information
LuciaEchevarria99 authored Mar 6, 2024
1 parent 9128706 commit bd58ccf
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <set>

#include <cpp_utils/Formatter.hpp>
#include <cpp_utils/logging/LogConfiguration.hpp>

#include <ddspipe_core/configuration/DdsPipeConfiguration.hpp>
#include <ddspipe_core/configuration/IConfiguration.hpp>
Expand Down Expand Up @@ -70,6 +71,9 @@ struct SpecsConfiguration : public ddspipe::core::IConfiguration

//! The type of the entities whose discovery triggers the discovery callbacks.
ddspipe::core::DiscoveryTrigger discovery_trigger = ddspipe::core::DiscoveryTrigger::READER;

//! Configuration of the CustomStdLogConsumer.
utils::LogConfiguration log_configuration;
};

} /* namespace core */
Expand Down
59 changes: 59 additions & 0 deletions ddsrouter_yaml/include/ddsrouter_yaml/CommandlineArgsRouter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

#pragma once

#include <cpp_utils/time/time_utils.hpp>

#include <ddspipe_core/configuration/CommandlineArgs.hpp>
#include <ddsrouter_yaml/library/library_dll.h>

namespace eprosima {
namespace ddsrouter {
namespace yaml {

/*
* Struct to parse the executable arguments
*/
struct DDSROUTER_YAML_DllAPI CommandlineArgsRouter : public ddspipe::core::CommandlineArgs
{

/////////////////////////
// CONSTRUCTORS
/////////////////////////

CommandlineArgsRouter();

/////////////////////////
// METHODS
/////////////////////////

/**
* @brief \c is_valid method.
*/
bool is_valid(
utils::Formatter& error_msg) const noexcept override;

/////////////////////////
// VARIABLES
/////////////////////////

// Maximum timeout
utils::Duration_ms timeout{0};

};

} /* namespace yaml */
} /* namespace ddsrouter */
} /* namespace eprosima */
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <ddsrouter_core/configuration/DdsRouterConfiguration.hpp>

#include <ddsrouter_yaml/CommandlineArgsRouter.hpp>
#include <ddsrouter_yaml/library/library_dll.h>

namespace eprosima {
Expand All @@ -35,10 +36,12 @@ class DDSROUTER_YAML_DllAPI YamlReaderConfiguration
public:

static ddsrouter::core::DdsRouterConfiguration load_ddsrouter_configuration(
const Yaml& yml);
const Yaml& yml,
const CommandlineArgsRouter* args = nullptr);

static ddsrouter::core::DdsRouterConfiguration load_ddsrouter_configuration_from_file(
const std::string& file_path);
const std::string& file_path,
const CommandlineArgsRouter* args = nullptr);

protected:

Expand Down
37 changes: 37 additions & 0 deletions ddsrouter_yaml/src/cpp/CommandlineArgsRouter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

#include <ddsrouter_yaml/CommandlineArgsRouter.hpp>

namespace eprosima {
namespace ddsrouter {
namespace yaml {

CommandlineArgsRouter::CommandlineArgsRouter()
{
log_filter[utils::VerbosityKind::Info].set_value("DDSROUTER", utils::FuzzyLevelValues::fuzzy_level_default);
log_filter[utils::VerbosityKind::Warning].set_value("DDSROUTER|DDSPIPE",
utils::FuzzyLevelValues::fuzzy_level_default);
log_filter[utils::VerbosityKind::Error].set_value("", utils::FuzzyLevelValues::fuzzy_level_default);
}

bool CommandlineArgsRouter::is_valid(
utils::Formatter& error_msg) const noexcept
{
return ddspipe::core::CommandlineArgs::is_valid(error_msg);
}

} /* namespace yaml */
} /* namespace ddsrouter */
} /* namespace eprosima */
15 changes: 12 additions & 3 deletions ddsrouter_yaml/src/cpp/YamlReaderConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace yaml {

core::DdsRouterConfiguration
YamlReaderConfiguration::load_ddsrouter_configuration(
const Yaml& yml)
const Yaml& yml,
const CommandlineArgsRouter* args /*= nullptr*/)
{
try
{
Expand Down Expand Up @@ -73,6 +74,13 @@ YamlReaderConfiguration::load_ddsrouter_configuration(
core::DdsRouterConfiguration router_configuration =
ddspipe::yaml::YamlReader::get<core::DdsRouterConfiguration>(yml, version);


if (args != nullptr)
{
router_configuration.ddspipe_configuration.log_configuration.set(args->log_verbosity);
router_configuration.ddspipe_configuration.log_configuration.set(args->log_filter);
}

return router_configuration;
}
catch (const std::exception& e)
Expand All @@ -84,7 +92,8 @@ YamlReaderConfiguration::load_ddsrouter_configuration(

core::DdsRouterConfiguration
YamlReaderConfiguration::load_ddsrouter_configuration_from_file(
const std::string& file_path)
const std::string& file_path,
const CommandlineArgsRouter* args /*= nullptr*/)
{
Yaml yml;

Expand All @@ -107,7 +116,7 @@ YamlReaderConfiguration::load_ddsrouter_configuration_from_file(
"> :\n " << "yaml node is null.");
}

return YamlReaderConfiguration::load_ddsrouter_configuration(yml);
return YamlReaderConfiguration::load_ddsrouter_configuration(yml, args);
}

ddspipe::yaml::YamlReaderVersion YamlReaderConfiguration::default_yaml_version()
Expand Down
8 changes: 8 additions & 0 deletions ddsrouter_yaml/src/cpp/YamlReader_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ void YamlReader::fill(
utils::Formatter() << "The discovery-trigger " << discovery_trigger << " is not valid.");
}
}

/////
// Get optional Log Configuration
if (YamlReader::is_tag_present(yml, LOG_CONFIGURATION_TAG))
{
object.log_configuration = YamlReader::get<utils::LogConfiguration>(yml, LOG_CONFIGURATION_TAG, version);
}
}

template <>
Expand Down Expand Up @@ -264,6 +271,7 @@ void YamlReader::fill(
*/
object.ddspipe_configuration.remove_unused_entities = object.advanced_options.remove_unused_entities;
object.ddspipe_configuration.discovery_trigger = object.advanced_options.discovery_trigger;
object.ddspipe_configuration.log_configuration = object.advanced_options.log_configuration;

/////
// Get optional xml configuration
Expand Down
2 changes: 2 additions & 0 deletions ddsrouter_yaml/test/unittest/configuration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ set(TEST_NAME YamlGetConfigurationDdsRouterTest)

set(TEST_SOURCES
YamlGetConfigurationDdsRouterTest.cpp
${PROJECT_SOURCE_DIR}/src/cpp/CommandlineArgsRouter.cpp
${PROJECT_SOURCE_DIR}/src/cpp/YamlReaderConfiguration.cpp
${PROJECT_SOURCE_DIR}/src/cpp/YamlReader_configuration.cpp
)

set(TEST_LIST
get_ddsrouter_configuration_trivial
get_ddsrouter_configuration_ros_case
get_ddsrouter_configuration_yaml_vs_commandline
)

set(TEST_EXTRA_LIBRARIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@

#include <ddsrouter_core/configuration/DdsRouterConfiguration.hpp>

#include <ddsrouter_yaml/CommandlineArgsRouter.hpp>
#include <ddsrouter_yaml/YamlReaderConfiguration.hpp>

using namespace eprosima;
using namespace eprosima::ddsrouter::yaml;

/**
* Test load a whole DDS Router Configuration from yaml node.
Expand Down Expand Up @@ -142,6 +144,59 @@ TEST(YamlGetConfigurationDdsRouterTest, get_ddsrouter_configuration_ros_case)
}
}

/**
* Check DdsRouterConfiguration structure creation.
*
* CASES:
* Check if chooses correctly log configuration when parsing from terminal and from YAML.
* In this case, it checks that:
* - The error filter is the one configured through the YAML
* - The warning filter is the one configured through the Command-Line
* - The info filter is the default (DDSROUTER)
*/
TEST(YamlGetConfigurationDdsRouterTest, get_ddsrouter_configuration_yaml_vs_commandline)
{
CommandlineArgsRouter commandline_args;

// Setting CommandLine arguments as if configured from CommandLine
commandline_args.log_filter[eprosima::utils::VerbosityKind::Warning].set_value("DDSROUTER|DDSPIPE|DEBUG");

const char* yml_str =
R"(
version: v4.0
specs:
logging:
verbosity: info
filter:
error: "DEBUG"
warning: "DDSROUTER"
participants:
- name: "P1"
kind: "local"
domain: 0
)";

Yaml yml = YAML::Load(yml_str);

// Load configuration from YAML
ddsrouter::core::DdsRouterConfiguration configuration =
ddsrouter::yaml::YamlReaderConfiguration::load_ddsrouter_configuration(yml, &commandline_args);

utils::Formatter error_msg;

ASSERT_TRUE(configuration.ddspipe_configuration.log_configuration.is_valid(error_msg));
ASSERT_EQ(configuration.ddspipe_configuration.log_configuration.verbosity.get_value(), utils::VerbosityKind::Info);
ASSERT_EQ(
configuration.ddspipe_configuration.log_configuration.filter[utils::VerbosityKind::Error].get_value(),
"DEBUG");
ASSERT_EQ(
configuration.ddspipe_configuration.log_configuration.filter[utils::VerbosityKind::Warning].get_value(),
"DDSROUTER|DDSPIPE|DEBUG");
ASSERT_EQ(
configuration.ddspipe_configuration.log_configuration.filter[utils::VerbosityKind::Info].get_value(),
"DDSROUTER");
}

int main(
int argc,
char** argv)
Expand Down
6 changes: 5 additions & 1 deletion docs/rst/notes/forthcoming_version.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.. add orphan tag when new info added to this file
:orphan:
.. :orphan:
###################
Forthcoming Version
###################

This release will include the following **Configuration features**:

* New configuration option ``logging`` to configure the :ref:`Logs <router_specs_logging>`.
2 changes: 1 addition & 1 deletion docs/rst/notes/notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

.. _release_notes:

.. .. include:: forthcoming_version.rst
.. include:: forthcoming_version.rst

##############
Version v2.1.0
Expand Down
63 changes: 63 additions & 0 deletions docs/rst/user_manual/configuration.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.. include:: ../exports/alias.include
.. include:: ../exports/roles.include

.. _user_manual_configuration:

Expand Down Expand Up @@ -387,6 +388,62 @@ QoS

The :ref:`Topic QoS <user_manual_configuration_topic_qos>` configured in ``specs`` can be overwritten by the :ref:`Participant Topic QoS <user_manual_configuration_participant_topic_qos>` and the :ref:`Manual Topics <user_manual_configuration_manual_topics>`.

.. _router_specs_logging:

Logging
-------

``specs`` supports a ``logging`` **optional** tag to configure the |ddsrouter| logs.
Under the ``logging`` tag, users can configure the type of logs to display and filter the logs based on their content and category.
When configuring the verbosity to ``info``, all types of logs, including informational messages, warnings, and errors, will be displayed.
Conversely, setting it to ``warning`` will only show warnings and errors, while choosing ``error`` will exclusively display errors.
By default, the filter allows all errors to be displayed, while selectively permitting warning messages from ``DDSPIPE|DDSROUTER`` and informational messages from the ``DDSROUTER`` category.

.. code-block:: yaml
logging:
verbosity: info
filter:
error: "DDSPIPE|DDSROUTER"
warning: "DDSPIPE|DDSROUTER"
info: "DDSROUTER"
.. note::

Configuring the logs via the Command-Line is still active and takes precedence over YAML configuration when both methods are used simultaneously.

.. list-table::
:header-rows: 1

* - Logging
- Yaml tag
- Description
- Data type
- Default value
- Possible values

* - Verbosity
- ``verbosity``
- Show messages of equal |br|
or higher importance.
- *enum*
- ``warning``
- ``info`` / ``warning`` / ``error``

* - Filter
- ``filter``
- Regex to filter the category |br|
or message of the logs.
- *string*
- info : ``DDSROUTER`` |br|
warning : ``DDSPIPE|DDSROUTER`` |br|
error : ``""``
- Regex string

.. note::

For the logs to function properly, the ``-DLOG_INFO=ON`` compilation flag is required.

Participant Configuration
=========================

Expand Down Expand Up @@ -827,6 +884,12 @@ A complete example of all the configurations described on this page can be found
max-tx-rate: 0
max-rx-rate: 20
downsampling: 3
logging:
verbosity: info
filter:
error: "DDSPIPE|DDSROUTER"
warning: "DDSPIPE|DDSROUTER"
info: "DDSROUTER"
# XML configurations to load
xml:
Expand Down
Loading

0 comments on commit bd58ccf

Please sign in to comment.