Skip to content
Open
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
125 changes: 120 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
#include <cstdio>
#include <exception>
#include <fstream>
#include <iomanip>
#include <limits>
#include <memory>
#include <numeric>
#include <sstream>
#include <string>
#include <string_view>
#include <type_traits>
Expand All @@ -20,22 +23,23 @@

namespace {

int inspectLog(const char* path)
using StorageConfig = flight_log::DataContainer::StorageConfig;

std::shared_ptr<flight_log::DataContainer> 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<uint8_t, 4096> buffer{};
const auto storage = flight_log::DataContainer::StorageConfig::FullLog;
const auto data_container = std::make_shared<flight_log::DataContainer>(storage);
flight_log::Reader reader{data_container};
do {
file.read(reinterpret_cast<char*>(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<int>(file.gcount()));
} while (file && !data_container->hadFatalError());
Expand All @@ -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 <typename T>
void writeParameterScalar(std::ostream& output, T value)
{
if constexpr (std::is_floating_point_v<T>) {
output << std::setprecision(std::numeric_limits<T>::max_digits10) << value;
} else if constexpr (std::is_integral_v<T>) {
output << flight_log::staticCastEnsureUnsignedChar<decltype(+value)>(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<decltype(value)>;
if constexpr (flight_log::is_vector<T>::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;
}

Expand Down Expand Up @@ -130,11 +232,24 @@ int inspectLog(const char* path)

int main(int argc, char** argv)
{
constexpr auto usage = "Usage: flight-log-inspector <file.ulg>\n";
constexpr auto usage = "Usage: flight-log-inspector <file.ulg>\n"
" flight-log-inspector diff-params <before.ulg> <after.ulg>\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;
Expand Down
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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=$<TARGET_FILE:flight-log-inspector>
-DLOG_DIR=${CMAKE_CURRENT_SOURCE_DIR}/log_files
-P ${CMAKE_CURRENT_SOURCE_DIR}/check_diff_params.cmake
)
26 changes: 26 additions & 0 deletions tests/check_diff_params.cmake
Original file line number Diff line number Diff line change
@@ -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()
Loading