From b323591a114924cb9428e105375f9c162673eb4e Mon Sep 17 00:00:00 2001 From: Marcos B <15697303+gmarcosb@users.noreply.github.com> Date: Wed, 20 Jul 2022 03:17:24 -0600 Subject: [PATCH] Better debug logs on response (#19595) * Output information about what's being broadcast * Expose/promote QNameString in Logging to allow other logging locations to get a formatted FullQName * Revert inadvertent build_overrides/pigweed_environment.gni This partially reverts commit 15639cf3c5ef3c21480e68b11bdbcd39012a7a11. # Conflicts: # build_overrides/pigweed_environment.gni * Update BUILD.gn deps for QNameString & move it to its own file to avoid circular deps in /responders * Remove unnecessary StringBuilder addition as QNameString is now in its own file * Restyled by whitespace * Restyled by clang-format Co-authored-by: Restyled.io --- src/lib/dnssd/minimal_mdns/Logging.cpp | 48 +--------------- src/lib/dnssd/minimal_mdns/core/BUILD.gn | 2 + .../dnssd/minimal_mdns/core/QNameString.cpp | 56 +++++++++++++++++++ src/lib/dnssd/minimal_mdns/core/QNameString.h | 43 ++++++++++++++ .../responders/QueryResponder.cpp | 2 + 5 files changed, 104 insertions(+), 47 deletions(-) create mode 100644 src/lib/dnssd/minimal_mdns/core/QNameString.cpp create mode 100644 src/lib/dnssd/minimal_mdns/core/QNameString.h diff --git a/src/lib/dnssd/minimal_mdns/Logging.cpp b/src/lib/dnssd/minimal_mdns/Logging.cpp index 355086c5b4b7a0..544fa2587f0232 100644 --- a/src/lib/dnssd/minimal_mdns/Logging.cpp +++ b/src/lib/dnssd/minimal_mdns/Logging.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ #include -#include +#include #include namespace mdns { @@ -53,52 +53,6 @@ const char * QueryTypeToString(mdns::Minimal::QType type) } #endif // CHIP_PROGRESS_LOGGING - -class QNameString -{ -public: - QNameString(const mdns::Minimal::FullQName & name) - { - for (unsigned i = 0; i < name.nameCount; i++) - { - if (i != 0) - { - mBuffer.Add("."); - } - mBuffer.Add(name.names[i]); - } - } - - QNameString(mdns::Minimal::SerializedQNameIterator name) - { - bool first = true; - while (name.Next()) - { - if (first) - { - first = false; - } - else - { - mBuffer.Add("."); - } - mBuffer.Add(name.Value()); - } - if (!name.IsValid()) - { - mBuffer.Add("(!INVALID!)"); - } - } - - const char * c_str() const { return mBuffer.c_str(); } - - bool Fit() const { return mBuffer.Fit(); } - -private: - static constexpr size_t kMaxQNameLength = 128; - chip::StringBuilder mBuffer; -}; - } // namespace void LogSendingQuery(const mdns::Minimal::Query & query) diff --git a/src/lib/dnssd/minimal_mdns/core/BUILD.gn b/src/lib/dnssd/minimal_mdns/core/BUILD.gn index 0fc83eaddfdd15..c0be6ef0680f9d 100644 --- a/src/lib/dnssd/minimal_mdns/core/BUILD.gn +++ b/src/lib/dnssd/minimal_mdns/core/BUILD.gn @@ -21,6 +21,8 @@ static_library("core") { "DnsHeader.h", "QName.cpp", "QName.h", + "QNameString.cpp", + "QNameString.h", "RecordWriter.cpp", "RecordWriter.h", ] diff --git a/src/lib/dnssd/minimal_mdns/core/QNameString.cpp b/src/lib/dnssd/minimal_mdns/core/QNameString.cpp new file mode 100644 index 00000000000000..c299520b75b547 --- /dev/null +++ b/src/lib/dnssd/minimal_mdns/core/QNameString.cpp @@ -0,0 +1,56 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +namespace mdns { +namespace Minimal { + +QNameString::QNameString(const mdns::Minimal::FullQName & name) +{ + for (unsigned i = 0; i < name.nameCount; i++) + { + if (i != 0) + { + mBuffer.Add("."); + } + mBuffer.Add(name.names[i]); + } +} + +QNameString::QNameString(mdns::Minimal::SerializedQNameIterator name) +{ + bool first = true; + while (name.Next()) + { + if (first) + { + first = false; + } + else + { + mBuffer.Add("."); + } + mBuffer.Add(name.Value()); + } + if (!name.IsValid()) + { + mBuffer.Add("(!INVALID!)"); + } +} + +} // namespace Minimal +} // namespace mdns diff --git a/src/lib/dnssd/minimal_mdns/core/QNameString.h b/src/lib/dnssd/minimal_mdns/core/QNameString.h new file mode 100644 index 00000000000000..6a548e8b474394 --- /dev/null +++ b/src/lib/dnssd/minimal_mdns/core/QNameString.h @@ -0,0 +1,43 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include +#include + +namespace mdns { +namespace Minimal { + +// Allows for a FullQName to be represented as a user-readable logging string +class QNameString +{ +public: + QNameString(const mdns::Minimal::FullQName & name); + + QNameString(mdns::Minimal::SerializedQNameIterator name); + + inline const char * c_str() const { return mBuffer.c_str(); } + + inline bool Fit() const { return mBuffer.Fit(); } + +private: + static constexpr size_t kMaxQNameLength = 128; + chip::StringBuilder mBuffer; +}; + +} // namespace Minimal +} // namespace mdns diff --git a/src/lib/dnssd/minimal_mdns/responders/QueryResponder.cpp b/src/lib/dnssd/minimal_mdns/responders/QueryResponder.cpp index 6873ed49bf2d7b..47731c7a42ddfd 100644 --- a/src/lib/dnssd/minimal_mdns/responders/QueryResponder.cpp +++ b/src/lib/dnssd/minimal_mdns/responders/QueryResponder.cpp @@ -17,6 +17,7 @@ #include "QueryResponder.h" +#include #include #include @@ -56,6 +57,7 @@ QueryResponderSettings QueryResponderBase::AddResponder(RecordResponder * respon { return QueryResponderSettings(); } + ChipLogDetail(Discovery, "Responding with %s", QNameString(responder->GetQName()).c_str()); for (size_t i = 0; i < mResponderInfoSize; i++) {