From 9604afc7a3860b7e27a663451ab9580dbbc93117 Mon Sep 17 00:00:00 2001 From: Kristoffer Eide Date: Mon, 18 Aug 2025 15:06:48 +0200 Subject: [PATCH 1/3] #776 Add optional configuration of timestamped filenames and floting point precision to log config xml file. --- src/cosim/observer/file_observer.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/cosim/observer/file_observer.cpp b/src/cosim/observer/file_observer.cpp index a3c19f91..42d8307d 100644 --- a/src/cosim/observer/file_observer.cpp +++ b/src/cosim/observer/file_observer.cpp @@ -567,7 +567,8 @@ file_observer_config file_observer_config::parse(const filesystem::path& configP boost::property_tree::xml_parser::no_comments | boost::property_tree::xml_parser::trim_whitespace); file_observer_config config; - for (const auto& simulator : ptree.get_child("simulators")) { + const auto simulators = ptree.get_child("simulators"); + for (const auto& simulator : simulators) { if (simulator.first == "simulator") { const auto modelName = get_attribute(simulator.second, "name"); const auto decimationFactor = get_optional_attribute(simulator.second, "decimationFactor"); @@ -581,6 +582,17 @@ file_observer_config file_observer_config::parse(const filesystem::path& configP config.log_simulator_variables(modelName, variableNames, decimationFactor); } } + if (const auto configuration = ptree.get_child("configuration")) { + const auto timestamps = get_optional_attribute(configuration, "timestampedFilenames"); + if (timestamps) { + config.set_timestamped_filenames(*timestamps); + } + const auto precision = get_optional_attribute(configuration, "floatingPointPrecision"); + if (precision) { + config.fixed_precision(*precision); + } + + } return config; } From f544b06872081ec0102f80108becf2f5957121cf Mon Sep 17 00:00:00 2001 From: Kristoffer Eide Date: Wed, 20 Aug 2025 10:48:11 +0200 Subject: [PATCH 2/3] #776 Use get_child_optional. Update corresponding test to verify absence of timestamps. --- include/cosim/observer/file_observer.hpp | 1 + src/cosim/observer/file_observer.cpp | 6 +++--- tests/data/LogConfig.xml | 2 +- tests/file_observer_logging_from_config_test.cpp | 3 +++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/cosim/observer/file_observer.hpp b/include/cosim/observer/file_observer.hpp index 6a2ea5b3..3fe6ba7c 100644 --- a/include/cosim/observer/file_observer.hpp +++ b/include/cosim/observer/file_observer.hpp @@ -15,6 +15,7 @@ #include #include +#include #include diff --git a/src/cosim/observer/file_observer.cpp b/src/cosim/observer/file_observer.cpp index 42d8307d..704af78e 100644 --- a/src/cosim/observer/file_observer.cpp +++ b/src/cosim/observer/file_observer.cpp @@ -582,12 +582,12 @@ file_observer_config file_observer_config::parse(const filesystem::path& configP config.log_simulator_variables(modelName, variableNames, decimationFactor); } } - if (const auto configuration = ptree.get_child("configuration")) { - const auto timestamps = get_optional_attribute(configuration, "timestampedFilenames"); + if (const auto configuration = ptree.get_child_optional("configuration")) { + const auto timestamps = get_optional_attribute(*configuration, "timestampedFilenames"); if (timestamps) { config.set_timestamped_filenames(*timestamps); } - const auto precision = get_optional_attribute(configuration, "floatingPointPrecision"); + const auto precision = get_optional_attribute(*configuration, "floatingPointPrecision"); if (precision) { config.fixed_precision(*precision); } diff --git a/tests/data/LogConfig.xml b/tests/data/LogConfig.xml index e5bcd976..9621355e 100644 --- a/tests/data/LogConfig.xml +++ b/tests/data/LogConfig.xml @@ -1,5 +1,5 @@ - + diff --git a/tests/file_observer_logging_from_config_test.cpp b/tests/file_observer_logging_from_config_test.cpp index f8378557..4e0d8d45 100644 --- a/tests/file_observer_logging_from_config_test.cpp +++ b/tests/file_observer_logging_from_config_test.cpp @@ -64,6 +64,9 @@ int main() auto simResult = execution.simulate_until(endTime); REQUIRE(simResult); + REQUIRE(cosim::filesystem::exists(cosim::filesystem::path(csvPath / "slave.csv"))); + REQUIRE(cosim::filesystem::exists(cosim::filesystem::path(csvPath / "slave2.csv"))); + } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; return 1; From d4737b39ff5a0e33f77a55650233d90c22e158e0 Mon Sep 17 00:00:00 2001 From: Kristoffer Eide Date: Wed, 20 Aug 2025 10:51:34 +0200 Subject: [PATCH 3/3] #776 Revert unnecessary change. Compress code. --- src/cosim/observer/file_observer.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/cosim/observer/file_observer.cpp b/src/cosim/observer/file_observer.cpp index 704af78e..73d912df 100644 --- a/src/cosim/observer/file_observer.cpp +++ b/src/cosim/observer/file_observer.cpp @@ -567,8 +567,7 @@ file_observer_config file_observer_config::parse(const filesystem::path& configP boost::property_tree::xml_parser::no_comments | boost::property_tree::xml_parser::trim_whitespace); file_observer_config config; - const auto simulators = ptree.get_child("simulators"); - for (const auto& simulator : simulators) { + for (const auto& simulator : ptree.get_child("simulators")) { if (simulator.first == "simulator") { const auto modelName = get_attribute(simulator.second, "name"); const auto decimationFactor = get_optional_attribute(simulator.second, "decimationFactor"); @@ -583,15 +582,12 @@ file_observer_config file_observer_config::parse(const filesystem::path& configP } } if (const auto configuration = ptree.get_child_optional("configuration")) { - const auto timestamps = get_optional_attribute(*configuration, "timestampedFilenames"); - if (timestamps) { + if (const auto timestamps = get_optional_attribute(*configuration, "timestampedFilenames")) { config.set_timestamped_filenames(*timestamps); } - const auto precision = get_optional_attribute(*configuration, "floatingPointPrecision"); - if (precision) { + if (const auto precision = get_optional_attribute(*configuration, "floatingPointPrecision")) { config.fixed_precision(*precision); } - } return config;