Skip to content

Commit bd58ccf

Browse files
Configure the Log through YAML (#433)
* Add configuration of logs via YAML Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Correct Log Configuration creation from YAML Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Add Router YAML parse test Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Update documentation and release notes Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Apply suggested changes Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Update documentation Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Apply suggested changes Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Add missing DLL dependency Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Correct uncrustify Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Minor change Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> --------- Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com>
1 parent 9128706 commit bd58ccf

14 files changed

Lines changed: 316 additions & 73 deletions

File tree

ddsrouter_core/include/ddsrouter_core/configuration/SpecsConfiguration.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <set>
1919

2020
#include <cpp_utils/Formatter.hpp>
21+
#include <cpp_utils/logging/LogConfiguration.hpp>
2122

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

7172
//! The type of the entities whose discovery triggers the discovery callbacks.
7273
ddspipe::core::DiscoveryTrigger discovery_trigger = ddspipe::core::DiscoveryTrigger::READER;
74+
75+
//! Configuration of the CustomStdLogConsumer.
76+
utils::LogConfiguration log_configuration;
7377
};
7478

7579
} /* namespace core */
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <cpp_utils/time/time_utils.hpp>
18+
19+
#include <ddspipe_core/configuration/CommandlineArgs.hpp>
20+
#include <ddsrouter_yaml/library/library_dll.h>
21+
22+
namespace eprosima {
23+
namespace ddsrouter {
24+
namespace yaml {
25+
26+
/*
27+
* Struct to parse the executable arguments
28+
*/
29+
struct DDSROUTER_YAML_DllAPI CommandlineArgsRouter : public ddspipe::core::CommandlineArgs
30+
{
31+
32+
/////////////////////////
33+
// CONSTRUCTORS
34+
/////////////////////////
35+
36+
CommandlineArgsRouter();
37+
38+
/////////////////////////
39+
// METHODS
40+
/////////////////////////
41+
42+
/**
43+
* @brief \c is_valid method.
44+
*/
45+
bool is_valid(
46+
utils::Formatter& error_msg) const noexcept override;
47+
48+
/////////////////////////
49+
// VARIABLES
50+
/////////////////////////
51+
52+
// Maximum timeout
53+
utils::Duration_ms timeout{0};
54+
55+
};
56+
57+
} /* namespace yaml */
58+
} /* namespace ddsrouter */
59+
} /* namespace eprosima */

ddsrouter_yaml/include/ddsrouter_yaml/YamlReaderConfiguration.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <ddsrouter_core/configuration/DdsRouterConfiguration.hpp>
2121

22+
#include <ddsrouter_yaml/CommandlineArgsRouter.hpp>
2223
#include <ddsrouter_yaml/library/library_dll.h>
2324

2425
namespace eprosima {
@@ -35,10 +36,12 @@ class DDSROUTER_YAML_DllAPI YamlReaderConfiguration
3536
public:
3637

3738
static ddsrouter::core::DdsRouterConfiguration load_ddsrouter_configuration(
38-
const Yaml& yml);
39+
const Yaml& yml,
40+
const CommandlineArgsRouter* args = nullptr);
3941

4042
static ddsrouter::core::DdsRouterConfiguration load_ddsrouter_configuration_from_file(
41-
const std::string& file_path);
43+
const std::string& file_path,
44+
const CommandlineArgsRouter* args = nullptr);
4245

4346
protected:
4447

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <ddsrouter_yaml/CommandlineArgsRouter.hpp>
16+
17+
namespace eprosima {
18+
namespace ddsrouter {
19+
namespace yaml {
20+
21+
CommandlineArgsRouter::CommandlineArgsRouter()
22+
{
23+
log_filter[utils::VerbosityKind::Info].set_value("DDSROUTER", utils::FuzzyLevelValues::fuzzy_level_default);
24+
log_filter[utils::VerbosityKind::Warning].set_value("DDSROUTER|DDSPIPE",
25+
utils::FuzzyLevelValues::fuzzy_level_default);
26+
log_filter[utils::VerbosityKind::Error].set_value("", utils::FuzzyLevelValues::fuzzy_level_default);
27+
}
28+
29+
bool CommandlineArgsRouter::is_valid(
30+
utils::Formatter& error_msg) const noexcept
31+
{
32+
return ddspipe::core::CommandlineArgs::is_valid(error_msg);
33+
}
34+
35+
} /* namespace yaml */
36+
} /* namespace ddsrouter */
37+
} /* namespace eprosima */

ddsrouter_yaml/src/cpp/YamlReaderConfiguration.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ namespace yaml {
2727

2828
core::DdsRouterConfiguration
2929
YamlReaderConfiguration::load_ddsrouter_configuration(
30-
const Yaml& yml)
30+
const Yaml& yml,
31+
const CommandlineArgsRouter* args /*= nullptr*/)
3132
{
3233
try
3334
{
@@ -73,6 +74,13 @@ YamlReaderConfiguration::load_ddsrouter_configuration(
7374
core::DdsRouterConfiguration router_configuration =
7475
ddspipe::yaml::YamlReader::get<core::DdsRouterConfiguration>(yml, version);
7576

77+
78+
if (args != nullptr)
79+
{
80+
router_configuration.ddspipe_configuration.log_configuration.set(args->log_verbosity);
81+
router_configuration.ddspipe_configuration.log_configuration.set(args->log_filter);
82+
}
83+
7684
return router_configuration;
7785
}
7886
catch (const std::exception& e)
@@ -84,7 +92,8 @@ YamlReaderConfiguration::load_ddsrouter_configuration(
8492

8593
core::DdsRouterConfiguration
8694
YamlReaderConfiguration::load_ddsrouter_configuration_from_file(
87-
const std::string& file_path)
95+
const std::string& file_path,
96+
const CommandlineArgsRouter* args /*= nullptr*/)
8897
{
8998
Yaml yml;
9099

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

110-
return YamlReaderConfiguration::load_ddsrouter_configuration(yml);
119+
return YamlReaderConfiguration::load_ddsrouter_configuration(yml, args);
111120
}
112121

113122
ddspipe::yaml::YamlReaderVersion YamlReaderConfiguration::default_yaml_version()

ddsrouter_yaml/src/cpp/YamlReader_configuration.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ void YamlReader::fill(
7777
utils::Formatter() << "The discovery-trigger " << discovery_trigger << " is not valid.");
7878
}
7979
}
80+
81+
/////
82+
// Get optional Log Configuration
83+
if (YamlReader::is_tag_present(yml, LOG_CONFIGURATION_TAG))
84+
{
85+
object.log_configuration = YamlReader::get<utils::LogConfiguration>(yml, LOG_CONFIGURATION_TAG, version);
86+
}
8087
}
8188

8289
template <>
@@ -264,6 +271,7 @@ void YamlReader::fill(
264271
*/
265272
object.ddspipe_configuration.remove_unused_entities = object.advanced_options.remove_unused_entities;
266273
object.ddspipe_configuration.discovery_trigger = object.advanced_options.discovery_trigger;
274+
object.ddspipe_configuration.log_configuration = object.advanced_options.log_configuration;
267275

268276
/////
269277
// Get optional xml configuration

ddsrouter_yaml/test/unittest/configuration/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ set(TEST_NAME YamlGetConfigurationDdsRouterTest)
6565

6666
set(TEST_SOURCES
6767
YamlGetConfigurationDdsRouterTest.cpp
68+
${PROJECT_SOURCE_DIR}/src/cpp/CommandlineArgsRouter.cpp
6869
${PROJECT_SOURCE_DIR}/src/cpp/YamlReaderConfiguration.cpp
6970
${PROJECT_SOURCE_DIR}/src/cpp/YamlReader_configuration.cpp
7071
)
7172

7273
set(TEST_LIST
7374
get_ddsrouter_configuration_trivial
7475
get_ddsrouter_configuration_ros_case
76+
get_ddsrouter_configuration_yaml_vs_commandline
7577
)
7678

7779
set(TEST_EXTRA_LIBRARIES

ddsrouter_yaml/test/unittest/configuration/YamlGetConfigurationDdsRouterTest.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727

2828
#include <ddsrouter_core/configuration/DdsRouterConfiguration.hpp>
2929

30+
#include <ddsrouter_yaml/CommandlineArgsRouter.hpp>
3031
#include <ddsrouter_yaml/YamlReaderConfiguration.hpp>
3132

3233
using namespace eprosima;
34+
using namespace eprosima::ddsrouter::yaml;
3335

3436
/**
3537
* Test load a whole DDS Router Configuration from yaml node.
@@ -142,6 +144,59 @@ TEST(YamlGetConfigurationDdsRouterTest, get_ddsrouter_configuration_ros_case)
142144
}
143145
}
144146

147+
/**
148+
* Check DdsRouterConfiguration structure creation.
149+
*
150+
* CASES:
151+
* Check if chooses correctly log configuration when parsing from terminal and from YAML.
152+
* In this case, it checks that:
153+
* - The error filter is the one configured through the YAML
154+
* - The warning filter is the one configured through the Command-Line
155+
* - The info filter is the default (DDSROUTER)
156+
*/
157+
TEST(YamlGetConfigurationDdsRouterTest, get_ddsrouter_configuration_yaml_vs_commandline)
158+
{
159+
CommandlineArgsRouter commandline_args;
160+
161+
// Setting CommandLine arguments as if configured from CommandLine
162+
commandline_args.log_filter[eprosima::utils::VerbosityKind::Warning].set_value("DDSROUTER|DDSPIPE|DEBUG");
163+
164+
const char* yml_str =
165+
R"(
166+
version: v4.0
167+
specs:
168+
logging:
169+
verbosity: info
170+
filter:
171+
error: "DEBUG"
172+
warning: "DDSROUTER"
173+
participants:
174+
- name: "P1"
175+
kind: "local"
176+
domain: 0
177+
)";
178+
179+
Yaml yml = YAML::Load(yml_str);
180+
181+
// Load configuration from YAML
182+
ddsrouter::core::DdsRouterConfiguration configuration =
183+
ddsrouter::yaml::YamlReaderConfiguration::load_ddsrouter_configuration(yml, &commandline_args);
184+
185+
utils::Formatter error_msg;
186+
187+
ASSERT_TRUE(configuration.ddspipe_configuration.log_configuration.is_valid(error_msg));
188+
ASSERT_EQ(configuration.ddspipe_configuration.log_configuration.verbosity.get_value(), utils::VerbosityKind::Info);
189+
ASSERT_EQ(
190+
configuration.ddspipe_configuration.log_configuration.filter[utils::VerbosityKind::Error].get_value(),
191+
"DEBUG");
192+
ASSERT_EQ(
193+
configuration.ddspipe_configuration.log_configuration.filter[utils::VerbosityKind::Warning].get_value(),
194+
"DDSROUTER|DDSPIPE|DEBUG");
195+
ASSERT_EQ(
196+
configuration.ddspipe_configuration.log_configuration.filter[utils::VerbosityKind::Info].get_value(),
197+
"DDSROUTER");
198+
}
199+
145200
int main(
146201
int argc,
147202
char** argv)
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
.. add orphan tag when new info added to this file
22
3-
:orphan:
3+
.. :orphan:
44
55
###################
66
Forthcoming Version
77
###################
8+
9+
This release will include the following **Configuration features**:
10+
11+
* New configuration option ``logging`` to configure the :ref:`Logs <router_specs_logging>`.

docs/rst/notes/notes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
.. _release_notes:
44

5-
.. .. include:: forthcoming_version.rst
5+
.. include:: forthcoming_version.rst
66

77
##############
88
Version v2.1.0

0 commit comments

Comments
 (0)