diff --git a/README.md b/README.md index 6802e1a..33755a5 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,21 @@ The CLI, library, and default CLI tests build without external dependencies. Set In the topic listing, `sensor_combined (0) - 1298` means instance 0 has 1,298 samples. Parsing warnings go to standard error. A fatal parse error or an unreadable file produces a -nonzero exit status. The CLI keeps samples in memory while it builds the report. The C++ +nonzero exit status. The full report keeps samples in memory while it scans the log. The C++ reader can process chunks incrementally for large logs. Logs with appended data segments may yield partial results. +## Summary for large logs + +```sh +./install/bin/flight-log-inspector --summary-only tests/log_files/sample.ulg +``` + +This mode scans the full log and prints each topic instance's sample count and the dropout totals. +It keeps one sample counter per observed subscription and two counters for dropouts. Memory use +also includes message formats, topic definitions, header metadata, parameter defaults, and +parsing diagnostics. + ## 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..a2962cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -20,7 +21,54 @@ namespace { -int inspectLog(const char* path) +class SummaryData : public flight_log::DataContainer { + public: + SummaryData() : DataContainer(StorageConfig::Header) {} + + void data(const flight_log::Data& sample) override + { + DataContainer::data(sample); + ++_sample_counts[sample.msgId()]; + } + + void dropout(const flight_log::Dropout& item) override + { + ++_dropout_count; + _total_dropout_ms += item.durationMs(); + } + + uint64_t sampleCount(uint16_t message_id) const + { + const auto count = _sample_counts.find(message_id); + return count != _sample_counts.end() ? count->second : 0; + } + + uint64_t dropoutCount() const { return _dropout_count; } + uint64_t totalDropoutMs() const { return _total_dropout_ms; } + + private: + std::unordered_map _sample_counts; + uint64_t _dropout_count{0}; + uint64_t _total_dropout_ms{0}; +}; + +void printDropouts(uint64_t count, uint64_t duration_ms) +{ + printf("Dropouts: %" PRIu64 ", total duration: %" PRIu64 " ms\n", count, duration_ms); +} + +void printTopicCounts(flight_log::DataContainer& data, const SummaryData* summary = nullptr) +{ + printf("\nName (multi id) - number of data points\n"); + for (const auto& item : data.subscriptionsByNameAndMultiId()) { + const auto& subscription = *item.second; + const uint64_t count = summary ? summary->sampleCount(subscription.getAddLoggedMessage().msgId()) + : subscription.size(); + printf(" %s (%i) - %" PRIu64 "\n", item.first.name.c_str(), item.first.multi_id, count); + } +} + +int inspectLog(const char* path, bool summary_only) { std::ifstream file(path, std::ios::binary); if (!file) { @@ -28,8 +76,10 @@ int inspectLog(const char* path) return 1; } std::array buffer{}; + const auto summary = summary_only ? std::make_shared() : nullptr; const auto storage = flight_log::DataContainer::StorageConfig::FullLog; - const auto data_container = std::make_shared(storage); + const std::shared_ptr data_container = + summary ? summary : std::make_shared(storage); flight_log::Reader reader{data_container}; do { file.read(reinterpret_cast(buffer.data()), buffer.size()); @@ -47,12 +97,18 @@ int inspectLog(const char* path) return 1; } + if (summary) { + printDropouts(summary->dropoutCount(), summary->totalDropoutMs()); + printTopicCounts(*summary, summary.get()); + return 0; + } + // Dropouts const auto& dropouts = data_container->dropouts(); const auto total_dropouts_ms = std::accumulate( dropouts.begin(), dropouts.end(), uint64_t{0}, [](uint64_t sum, const flight_log::Dropout& curr) { return sum + curr.durationMs(); }); - printf("Dropouts: %zu, total duration: %" PRIu64 " ms\n", dropouts.size(), total_dropouts_ms); + printDropouts(dropouts.size(), total_dropouts_ms); auto print_value = [](const std::string& name, const flight_log::Value& value) { std::visit( @@ -85,13 +141,7 @@ int inspectLog(const char* path) } printf("\n"); - // Messages - printf("\n"); - printf("Name (multi id) - number of data points\n"); - for (const auto& item : data_container->subscriptionsByNameAndMultiId()) { - printf(" %s (%i) - %zu\n", item.first.name.c_str(), item.first.multi_id, - item.second->size()); - } + printTopicCounts(*data_container); printf("Formats:\n"); for (const auto& msg_format : data_container->messageFormats()) { @@ -130,19 +180,21 @@ 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 [--summary-only] \n"; if (argc == 2 && std::string_view(argv[1]) == "--help") { std::fputs(usage, stdout); return 0; } - if (argc != 2) { + const bool summary_only = argc > 1 && std::string_view(argv[1]) == "--summary-only"; + if (argc != (summary_only ? 3 : 2)) { std::fputs(usage, stderr); return 2; } + const auto path = argv[summary_only ? 2 : 1]; try { - return inspectLog(argv[1]); + return inspectLog(path, summary_only); } catch (const std::exception& error) { - std::fprintf(stderr, "%s: %s\n", argv[1], error.what()); + std::fprintf(stderr, "%s: %s\n", path, error.what()); return 1; } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1d021d0..156fb14 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,6 +13,11 @@ add_test(NAME cli-summary COMMAND ${CMAKE_COMMAND} -DEXPECTED_STATUS=0 "-DEXPECTED_OUTPUT=sensor_combined (0) - 1298" -P ${cli_tests} ) +add_test(NAME cli-summary-only COMMAND ${CMAKE_COMMAND} + -DCLI=$ + -DLOG_DIR=${CMAKE_CURRENT_SOURCE_DIR}/log_files + -P ${CMAKE_CURRENT_SOURCE_DIR}/check_summary_only.cmake +) add_test(NAME cli-empty-input COMMAND ${CMAKE_COMMAND} -DCLI=$ -DARGUMENT=${empty_log} -DEXPECTED_STATUS=1 "-DEXPECTED_ERROR=Not enough data to read file magic" diff --git a/tests/check_summary_only.cmake b/tests/check_summary_only.cmake new file mode 100644 index 0000000..4c3ea59 --- /dev/null +++ b/tests/check_summary_only.cmake @@ -0,0 +1,22 @@ +foreach(log IN ITEMS sample.ulg sample_log_small.ulg sample_logging_tagged_and_default_params.ulg) + execute_process(COMMAND "${CLI}" "${LOG_DIR}/${log}" + RESULT_VARIABLE full_status OUTPUT_VARIABLE full ERROR_VARIABLE full_error + ) + execute_process(COMMAND "${CLI}" --summary-only "${LOG_DIR}/${log}" + RESULT_VARIABLE brief_status OUTPUT_VARIABLE brief ERROR_VARIABLE brief_error + ) + if(NOT full_status STREQUAL "0" OR NOT brief_status STREQUAL "0") + message(FATAL_ERROR "${log}: full exit ${full_status}, summary exit ${brief_status}\n${full_error}${brief_error}") + endif() + string(REGEX MATCH "^Dropouts:[^\n]*" dropouts "${full}") + string(FIND "${full}" "\nName (multi id) - number of data points\n" start) + string(FIND "${full}" "\nFormats:\n" end) + if(dropouts STREQUAL "" OR start LESS 0 OR end LESS_EQUAL start) + message(FATAL_ERROR "${log}: full report has no dropout or topic section") + endif() + math(EXPR length "${end} - ${start}") + string(SUBSTRING "${full}" ${start} ${length} topics) + if(NOT brief STREQUAL "${dropouts}\n${topics}\n" OR NOT brief_error STREQUAL full_error) + message(FATAL_ERROR "${log}: summary report or diagnostics differ from the full report") + endif() +endforeach()