Skip to content

Commit

Permalink
Better debug logs on response (project-chip#19595)
Browse files Browse the repository at this point in the history
* 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 15639cf.

# 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 <[email protected]>
  • Loading branch information
2 people authored and isiu-apple committed Sep 16, 2022
1 parent 5a543a2 commit b323591
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 47 deletions.
48 changes: 1 addition & 47 deletions src/lib/dnssd/minimal_mdns/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/
#include <lib/dnssd/minimal_mdns/Logging.h>
#include <lib/support/StringBuilder.h>
#include <lib/dnssd/minimal_mdns/core/QNameString.h>
#include <lib/support/logging/CHIPLogging.h>

namespace mdns {
Expand Down Expand Up @@ -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<kMaxQNameLength> mBuffer;
};

} // namespace

void LogSendingQuery(const mdns::Minimal::Query & query)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/dnssd/minimal_mdns/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ static_library("core") {
"DnsHeader.h",
"QName.cpp",
"QName.h",
"QNameString.cpp",
"QNameString.h",
"RecordWriter.cpp",
"RecordWriter.h",
]
Expand Down
56 changes: 56 additions & 0 deletions src/lib/dnssd/minimal_mdns/core/QNameString.cpp
Original file line number Diff line number Diff line change
@@ -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 <lib/dnssd/minimal_mdns/core/QNameString.h>

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
43 changes: 43 additions & 0 deletions src/lib/dnssd/minimal_mdns/core/QNameString.h
Original file line number Diff line number Diff line change
@@ -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 <lib/dnssd/minimal_mdns/core/QName.h>
#include <lib/support/StringBuilder.h>

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<kMaxQNameLength> mBuffer;
};

} // namespace Minimal
} // namespace mdns
2 changes: 2 additions & 0 deletions src/lib/dnssd/minimal_mdns/responders/QueryResponder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "QueryResponder.h"

#include <lib/dnssd/minimal_mdns/core/QNameString.h>
#include <lib/dnssd/minimal_mdns/records/Ptr.h>

#include <lib/support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -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++)
{
Expand Down

0 comments on commit b323591

Please sign in to comment.