| 1 | /* |
| 2 | * Copyright (C) 2020 James Turner |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "TestDataLogger.hxx" |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <cmath> |
| 24 | #include <limits> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <simgear/io/iostreams/sgstream.hxx> |
| 28 | |
| 29 | #include "Main/globals.hxx" |
| 30 | |
| 31 | namespace FGTestApi { |
| 32 | |
| 33 | using DoubleVec = std::vector<double>; |
| 34 | |
| 35 | static std::unique_ptr<DataLogger> static_instance; |
| 36 | |
| 37 | class DataLogger::DataLoggerPrivate |
| 38 | { |
| 39 | public: |
| 40 | sg_ofstream _stream; |
| 41 | |
| 42 | struct SampleInfo { |
| 43 | int column; |
| 44 | std::string name; |
| 45 | // range / units info, later |
| 46 | SGPropertyNode_ptr property; |
| 47 | }; |
| 48 | |
| 49 | double _currentTimeBase; |
| 50 | std::vector<SampleInfo> _samples; |
| 51 | DoubleVec _openRow; |
| 52 | bool _didHeader = false; |
| 53 | |
| 54 | void writeCurrentRow() |
| 55 | { |
| 56 | if (!_didHeader) { |
| 57 | writeHeader(); |
| 58 | _didHeader = true; |
| 59 | } |
| 60 | |
| 61 | // capture property values into the open row data |
| 62 | std::for_each(_samples.begin(), _samples.end(), [this](const SampleInfo& info) { |
| 63 | if (info.property) { |
| 64 | _openRow[info.column] = info.property->getDoubleValue(); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | // write time base |
| 69 | _stream << globals->get_sim_time_sec() << ","; |
| 70 | |
| 71 | for (const auto v : _openRow) { |
| 72 | if (std::isnan(v)) { |
| 73 | _stream << ","; // skip this data point |
| 74 | } else { |
| 75 | _stream << v << ","; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | _stream << "\n"; |
| 80 | |
| 81 | std::fill(_openRow.begin(), _openRow.end(), std::numeric_limits<double>::quiet_NaN()); |
| 82 | } |
| 83 | |
| 84 | void writeHeader() |
| 85 | { |
| 86 | _stream << "sim-time, "; |
| 87 | std::for_each(_samples.begin(), _samples.end(), [this](const SampleInfo& info) { |
| 88 | _stream << info.name << ", "; |
| 89 | }); |
| 90 | |
| 91 | _stream << "\n"; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | DataLogger::DataLogger() |
| 96 | { |
| 97 | d.reset(new DataLoggerPrivate); |
| 98 | } |
| 99 | |
| 100 | DataLogger::~DataLogger() |
| 101 | { |
| 102 | d->_stream.close(); |
| 103 | } |
| 104 | |
| 105 | bool DataLogger::isActive() |
| 106 | { |
| 107 | return static_instance != nullptr; |
| 108 | } |
| 109 | |
| 110 | DataLogger* DataLogger::instance() |
| 111 | { |
| 112 | if (!static_instance) { |
| 113 | static_instance.reset(new DataLogger); |
| 114 | } |
| 115 | |
| 116 | return static_instance.get(); |
| 117 | } |
| 118 | |
| 119 | void DataLogger::initTest(const std::string& testName) |
| 120 | { |
| 121 | d->_stream = sg_ofstream(testName + "_trace.csv"); |
| 122 | } |
| 123 | |
| 124 | void DataLogger::tearDown() |
| 125 | { |
| 126 | if (static_instance) { |
| 127 | static_instance.reset(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void DataLogger::writeRecord() |
| 132 | { |
| 133 | d->writeCurrentRow(); |
| 134 | } |
| 135 | |
| 136 | void DataLogger::recordProperty(const std::string& name, SGPropertyNode_ptr prop) |
| 137 | { |
| 138 | int index = static_cast<int>(d->_samples.size()); |
| 139 | DataLoggerPrivate::SampleInfo info{index, name, prop}; |
| 140 | d->_samples.push_back(info); |
| 141 | |
| 142 | if (d->_openRow.size() <= index) { |
| 143 | d->_openRow.resize(index + 1, std::numeric_limits<double>::quiet_NaN()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void DataLogger::recordSamplePoint(const std::string& name, double value) |
| 148 | { |
| 149 | auto it = std::find_if(d->_samples.begin(), d->_samples.end(), [&name](const DataLoggerPrivate::SampleInfo& sample) { |
| 150 | return name == sample.name; |
| 151 | }); |
| 152 | |
| 153 | int index = 0; |
| 154 | if (it == d->_samples.end()) { |
| 155 | index = static_cast<int>(d->_samples.size()); |
| 156 | DataLoggerPrivate::SampleInfo info{index, name}; |
| 157 | d->_samples.push_back(info); |
| 158 | } else { |
| 159 | index = it->column; |
| 160 | } |
| 161 | |
| 162 | // grow _openRow as required |
| 163 | if (d->_openRow.size() <= index) { |
| 164 | d->_openRow.resize(index + 1, std::numeric_limits<double>::quiet_NaN()); |
| 165 | } |
| 166 | |
| 167 | d->_openRow[index] = value; |
| 168 | } |
| 169 | |
| 170 | } // namespace FGTestApi |