From 23ea188e606b19c8a0338975658a56a88aef4819 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 23 Oct 2023 23:36:23 -0700 Subject: [PATCH] [glass] Add protobuf decode error log message (#5812) --- glass/src/libnt/native/cpp/NetworkTables.cpp | 16 ++++++++++------ .../cpp/protobuf/ProtobufMessageDatabase.cpp | 9 ++++++--- .../wpi/protobuf/ProtobufMessageDatabase.h | 3 ++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/glass/src/libnt/native/cpp/NetworkTables.cpp b/glass/src/libnt/native/cpp/NetworkTables.cpp index c367798bc6..f186746506 100644 --- a/glass/src/libnt/native/cpp/NetworkTables.cpp +++ b/glass/src/libnt/native/cpp/NetworkTables.cpp @@ -895,12 +895,16 @@ void NetworkTablesModel::Update() { entry->info.type_str == "proto:FileDescriptorProto") { // protobuf descriptor handling auto filename = wpi::drop_front(entry->info.name, 15); - m_protoDb.Add(filename, entry->value.GetRaw()); - // loop over all protobuf entries and update (conservatively) - for (auto&& entryPair : m_entries) { - auto ts = entryPair.second->info.type_str; - if (wpi::starts_with(ts, "proto:")) { - entryPair.second->UpdateFromValue(*this); + if (!m_protoDb.Add(filename, entry->value.GetRaw())) { + fmt::print("could not decode protobuf '{}' filename '{}'\n", + entry->info.name, filename); + } else { + // loop over all protobuf entries and update (conservatively) + for (auto&& entryPair : m_entries) { + auto& ts = entryPair.second->info.type_str; + if (wpi::starts_with(ts, "proto:")) { + entryPair.second->UpdateFromValue(*this); + } } } } diff --git a/wpiutil/src/main/native/cpp/protobuf/ProtobufMessageDatabase.cpp b/wpiutil/src/main/native/cpp/protobuf/ProtobufMessageDatabase.cpp index 35e17747ab..303bf9d30a 100644 --- a/wpiutil/src/main/native/cpp/protobuf/ProtobufMessageDatabase.cpp +++ b/wpiutil/src/main/native/cpp/protobuf/ProtobufMessageDatabase.cpp @@ -12,12 +12,15 @@ using google::protobuf::Arena; using google::protobuf::FileDescriptorProto; using google::protobuf::Message; -void ProtobufMessageDatabase::Add(std::string_view filename, +bool ProtobufMessageDatabase::Add(std::string_view filename, std::span data) { auto& file = m_files[filename]; if (file.complete) { file.complete = false; + m_factory.reset(); + m_msgs.clear(); + // rebuild the pool EXCEPT for this descriptor m_pool = std::make_unique(); @@ -31,7 +34,6 @@ void ProtobufMessageDatabase::Add(std::string_view filename, } // clear messages and reset factory; Find() will recreate as needed - m_msgs.clear(); m_factory = std::make_unique(); } @@ -49,10 +51,11 @@ void ProtobufMessageDatabase::Add(std::string_view filename, // parse data if (!file.proto->ParseFromArray(data.data(), data.size())) { - return; + return false; } Build(filename, file); + return true; } Message* ProtobufMessageDatabase::Find(std::string_view name) const { diff --git a/wpiutil/src/main/native/include/wpi/protobuf/ProtobufMessageDatabase.h b/wpiutil/src/main/native/include/wpi/protobuf/ProtobufMessageDatabase.h index f2c6882e26..f1ab105abe 100644 --- a/wpiutil/src/main/native/include/wpi/protobuf/ProtobufMessageDatabase.h +++ b/wpiutil/src/main/native/include/wpi/protobuf/ProtobufMessageDatabase.h @@ -33,8 +33,9 @@ class ProtobufMessageDatabase { * * @param filename filename * @param data FileDescriptorProto serialized data + * @return False if could not parse data */ - void Add(std::string_view filename, std::span data); + bool Add(std::string_view filename, std::span data); /** * Finds a message in the database by name.