Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/cosim/observer/file_observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,16 @@ file_observer_config file_observer_config::parse(const filesystem::path& configP
boost::property_tree::read_xml(configPath.string(), ptree,
boost::property_tree::xml_parser::no_comments | boost::property_tree::xml_parser::trim_whitespace);

const auto& simulators = ptree.get_child("simulators");

file_observer_config config;
for (const auto& simulator : ptree.get_child("simulators")) {
if (const auto timestamps = get_optional_attribute<bool>(simulators, "timestampedFilenames")) {
config.set_timestamped_filenames(*timestamps);
}
if (const auto precision = get_optional_attribute<int>(simulators, "floatingPointPrecision")) {
config.fixed_precision(*precision);
}
for (const auto& simulator : simulators) {
if (simulator.first == "simulator") {
const auto modelName = get_attribute<std::string>(simulator.second, "name");
const auto decimationFactor = get_optional_attribute<size_t>(simulator.second, "decimationFactor");
Expand All @@ -581,15 +589,6 @@ 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_optional("configuration")) {
if (const auto timestamps = get_optional_attribute<bool>(*configuration, "timestampedFilenames")) {
config.set_timestamped_filenames(*timestamps);
}
if (const auto precision = get_optional_attribute<int>(*configuration, "floatingPointPrecision")) {
config.fixed_precision(*precision);
}
}

return config;
}

Expand Down
3 changes: 1 addition & 2 deletions tests/data/LogConfig.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration timestampedFilenames="false" floatingPointPrecision="1"/>
<simulators xmlns="http://opensimulationplatform.com/LogConfig">
<simulators xmlns="http://opensimulationplatform.com/LogConfig" timestampedFilenames="false" floatingPointPrecision="1">

<simulator name="slave" decimationFactor="20">
<variable name="realOut"/>
Expand Down
9 changes: 9 additions & 0 deletions tests/file_observer_logging_from_config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <cosim/observer/file_observer.hpp>

#include <exception>
#include <fstream>
#include <memory>
#include <sstream>
#include <stdexcept>


Expand Down Expand Up @@ -63,10 +65,17 @@ int main()
// Run the simulation
auto simResult = execution.simulate_until(endTime);
REQUIRE(simResult);
csv_observer->stop_recording();

REQUIRE(cosim::filesystem::exists(cosim::filesystem::path(csvPath / "slave.csv")));
REQUIRE(cosim::filesystem::exists(cosim::filesystem::path(csvPath / "slave2.csv")));

std::ifstream slaveLog(csvPath / "slave.csv");
REQUIRE(slaveLog);
std::stringstream slaveLogContents;
slaveLogContents << slaveLog.rdbuf();
REQUIRE(slaveLogContents.str().find(",1.2,1,hello log") != std::string::npos);

} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
Expand Down
Loading