From f9f03406a346a54f6888238a4532d1ef5d5db955 Mon Sep 17 00:00:00 2001 From: Xia Chao Date: Tue, 24 Nov 2026 21:08:53 +0800 Subject: [PATCH] Compare initial parameters across flight logs --- README.md | 11 +++ src/main.cpp | 125 ++++++++++++++++++++++++++++++++-- tests/CMakeLists.txt | 6 ++ tests/check_diff_params.cmake | 26 +++++++ 4 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 tests/check_diff_params.cmake diff --git a/README.md b/README.md index 6802e1a..a0e5d50 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,17 @@ nonzero exit status. The CLI keeps samples in memory while it builds the report. reader can process chunks incrementally for large logs. Logs with appended data segments may yield partial results. +## Compare initial parameters + +```sh +./install/bin/flight-log-inspector diff-params before.ulg after.ulg +``` + +Use `diff-params` to compare the configuration recorded at the start of each flight. It lists +added and removed parameters, value changes, and type changes in parameter-name order. It +compares values as stored and prints floating-point values with round-trip precision. A +successful comparison exits with status 0 whether or not there are differences. + ## C++ library Point `CMAKE_PREFIX_PATH` to the installation directory, then link the library in your project. diff --git a/src/main.cpp b/src/main.cpp index d573e4e..916c730 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,8 +8,11 @@ #include #include #include +#include +#include #include #include +#include #include #include #include @@ -20,22 +23,23 @@ namespace { -int inspectLog(const char* path) +using StorageConfig = flight_log::DataContainer::StorageConfig; + +std::shared_ptr readLog(const char* path, StorageConfig storage) { std::ifstream file(path, std::ios::binary); if (!file) { std::fprintf(stderr, "Cannot open log '%s'\n", path); - return 1; + return nullptr; } std::array buffer{}; - const auto storage = flight_log::DataContainer::StorageConfig::FullLog; const auto data_container = std::make_shared(storage); flight_log::Reader reader{data_container}; do { file.read(reinterpret_cast(buffer.data()), buffer.size()); if (file.bad() || (file.fail() && !file.eof())) { std::fprintf(stderr, "Cannot read log '%s'\n", path); - return 1; + return nullptr; } reader.readChunk(buffer.data(), static_cast(file.gcount())); } while (file && !data_container->hadFatalError()); @@ -44,6 +48,104 @@ int inspectLog(const char* path) std::fprintf(stderr, "%s: %s\n", path, parsing_error.c_str()); } if (data_container->hadFatalError()) { + return nullptr; + } + return data_container; +} + +template +void writeParameterScalar(std::ostream& output, T value) +{ + if constexpr (std::is_floating_point_v) { + output << std::setprecision(std::numeric_limits::max_digits10) << value; + } else if constexpr (std::is_integral_v) { + output << flight_log::staticCastEnsureUnsignedChar(value); + } else { + output << value; + } +} + +std::string describeParameter(const flight_log::Parameter& parameter) +{ + const auto& field = parameter.field(); + std::ostringstream description; + description << field.type().name; + if (field.arrayLength() >= 0) { + description << '[' << field.arrayLength() << ']'; + } + description << ' '; + std::visit( + [&description](const auto& value) { + using T = std::decay_t; + if constexpr (flight_log::is_vector::value) { + description << '['; + for (std::size_t i = 0; i < value.size(); ++i) { + if (i != 0) { + description << ", "; + } + writeParameterScalar(description, value[i]); + } + description << ']'; + } else { + writeParameterScalar(description, value); + } + }, + parameter.value().asNativeTypeVariant()); + return description.str(); +} + +int compareParameters(const char* before_path, const char* after_path) +{ + const auto before_log = readLog(before_path, StorageConfig::Header); + if (!before_log) { + return 1; + } + const auto after_log = readLog(after_path, StorageConfig::Header); + if (!after_log) { + return 1; + } + + const auto& before = before_log->initialParameters(); + const auto& after = after_log->initialParameters(); + auto old_param = before.begin(); + auto new_param = after.begin(); + bool changed = false; + while (old_param != before.end() || new_param != after.end()) { + if (new_param == after.end() || + (old_param != before.end() && old_param->first < new_param->first)) { + printf("removed %s: %s\n", old_param->first.c_str(), + describeParameter(old_param->second).c_str()); + ++old_param; + changed = true; + } else if (old_param == before.end() || new_param->first < old_param->first) { + printf("added %s: %s\n", new_param->first.c_str(), + describeParameter(new_param->second).c_str()); + ++new_param; + changed = true; + } else { + const auto& old_value = old_param->second; + const auto& new_value = new_param->second; + const bool same_type = old_value.field() == new_value.field(); + if (!same_type || old_value.valueRaw() != new_value.valueRaw()) { + printf("%s %s: %s -> %s\n", same_type ? "changed" : "type-changed", + old_param->first.c_str(), describeParameter(old_value).c_str(), + describeParameter(new_value).c_str()); + changed = true; + } + ++old_param; + ++new_param; + } + } + if (!changed) { + std::puts("No initial parameter differences."); + } + return 0; +} + +int inspectLog(const char* path) +{ + const auto data_container = readLog(path, StorageConfig::FullLog); + if (!data_container) { return 1; } @@ -130,11 +232,24 @@ int inspectLog(const char* path) int main(int argc, char** argv) { - constexpr auto usage = "Usage: flight-log-inspector \n"; + constexpr auto usage = "Usage: flight-log-inspector \n" + " flight-log-inspector diff-params \n"; if (argc == 2 && std::string_view(argv[1]) == "--help") { std::fputs(usage, stdout); return 0; } + if (argc > 2 && std::string_view(argv[1]) == "diff-params") { + if (argc != 4) { + std::fputs(usage, stderr); + return 2; + } + try { + return compareParameters(argv[2], argv[3]); + } catch (const std::exception& error) { + std::fprintf(stderr, "diff-params: %s\n", error.what()); + return 1; + } + } if (argc != 2) { std::fputs(usage, stderr); return 2; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1d021d0..a322661 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,3 +25,9 @@ if(FLIGHT_LOG_ENABLE_UNIT_TESTS) target_link_libraries(flight_log_unit_tests PRIVATE doctest::doctest flight_log::flight_log) add_test(NAME unit-tests COMMAND flight_log_unit_tests) endif() + +add_test(NAME cli-diff-params COMMAND ${CMAKE_COMMAND} + -DCLI=$ + -DLOG_DIR=${CMAKE_CURRENT_SOURCE_DIR}/log_files + -P ${CMAKE_CURRENT_SOURCE_DIR}/check_diff_params.cmake +) diff --git a/tests/check_diff_params.cmake b/tests/check_diff_params.cmake new file mode 100644 index 0000000..689d744 --- /dev/null +++ b/tests/check_diff_params.cmake @@ -0,0 +1,26 @@ +set(before "${LOG_DIR}/sample.ulg") +set(after "${LOG_DIR}/sample_log_small.ulg") +execute_process(COMMAND "${CLI}" diff-params "${before}" "${after}" + RESULT_VARIABLE status OUTPUT_VARIABLE output ERROR_VARIABLE error +) +if(NOT status STREQUAL "0") + message(FATAL_ERROR "diff-params exited ${status}\n${error}") +endif() + +foreach(expected IN ITEMS + "added ASPD_BETA_GATE: int32_t 1" + "removed ATT_ACC_COMP: int32_t 1" + "type-changed COM_DISARM_LAND: int32_t 0 -> float 2" + "changed BAT_N_CELLS: int32_t 3 -> int32_t 6") + string(FIND "\n${output}" "\n${expected}\n" position) + if(position EQUAL -1) + message(FATAL_ERROR "Missing parameter difference '${expected}'") + endif() +endforeach() + +execute_process(COMMAND "${CLI}" diff-params "${after}" "${after}" + RESULT_VARIABLE status OUTPUT_VARIABLE output ERROR_VARIABLE error +) +if(NOT status STREQUAL "0" OR NOT output STREQUAL "No initial parameter differences.\n") + message(FATAL_ERROR "Identical logs reported differences (exit ${status})\n${error}") +endif()