Skip to content

Commit

Permalink
diagnostic-logs-delegate: Add default implementation for EndLogCollec…
Browse files Browse the repository at this point in the history
…tion

- The one-argument EndLogCollection method is retained for backward compatibility.
- The new two-argument overloaded EndLogCollection method will serve as the primary
  method to be implemented in delegates.
  • Loading branch information
esp committed Dec 19, 2024
1 parent 38bf32b commit 5154436
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ CHIP_ERROR LogProvider::GetLogForIntent(IntentEnum intent, MutableByteSpan & out
err = CollectLog(sessionHandle, outBuffer, unusedOutIsEndOfLog);
VerifyOrReturnError(CHIP_NO_ERROR == err, err, outBuffer.reduce_size(0));

err = EndLogCollection(sessionHandle);
err = EndLogCollection(sessionHandle, err);
VerifyOrReturnError(CHIP_NO_ERROR == err, err, outBuffer.reduce_size(0));

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -276,21 +276,6 @@ CHIP_ERROR LogProvider::StartLogCollection(IntentEnum intent, LogSessionHandle &
return CHIP_NO_ERROR;
}

CHIP_ERROR LogProvider::EndLogCollection(LogSessionHandle sessionHandle)
{
VerifyOrReturnValue(sessionHandle != kInvalidLogSessionHandle, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnValue(mSessionContextMap.count(sessionHandle), CHIP_ERROR_INVALID_ARGUMENT);

LogContext * context = mSessionContextMap[sessionHandle];
VerifyOrReturnError(context, CHIP_ERROR_INCORRECT_STATE);

CleanupLogContextForIntent(context);
Platform::MemoryFree(context);
mSessionContextMap.erase(sessionHandle);

return CHIP_NO_ERROR;
}

CHIP_ERROR LogProvider::EndLogCollection(LogSessionHandle sessionHandle, CHIP_ERROR error)
{
if (error != CHIP_NO_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class LogProvider : public DiagnosticLogsProviderDelegate
/////////// DiagnosticLogsProviderDelegate Interface /////////
CHIP_ERROR StartLogCollection(IntentEnum intent, LogSessionHandle & outHandle, Optional<uint64_t> & outTimeStamp,
Optional<uint64_t> & outTimeSinceBoot) override;
CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle) override;
CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle, CHIP_ERROR error) override;
CHIP_ERROR CollectLog(LogSessionHandle sessionHandle, MutableByteSpan & outBuffer, bool & outIsEndOfLog) override;
size_t GetSizeForIntent(IntentEnum intent) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ void BDXDiagnosticLogsProvider::OnAckReceived()
// If the buffer has empty space, end the log collection session.
if (isEndOfLog)
{
mDelegate->EndLogCollection(mLogSessionHandle);
err = mDelegate->EndLogCollection(mLogSessionHandle, err);
if (err == CHIP_ERROR_NOT_IMPLEMENTED)
{
ChipLogError(DeviceLayer, "EndLogCollection not implemented in the delegate. Falling back to default behavior.");
}
mLogSessionHandle = kInvalidLogSessionHandle;
}

Expand Down Expand Up @@ -279,7 +283,11 @@ void BDXDiagnosticLogsProvider::Reset(CHIP_ERROR error)

if (mDelegate != nullptr)
{
mDelegate->EndLogCollection(mLogSessionHandle, error);
CHIP_ERROR err = mDelegate->EndLogCollection(mLogSessionHandle, error);
if (err == CHIP_ERROR_NOT_IMPLEMENTED)
{
ChipLogError(DeviceLayer, "EndLogCollection not implemented in the delegate. Falling back to default behavior.");
}
mDelegate = nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ class DiagnosticLogsProviderDelegate
* This must be called if StartLogCollection happens successfully and a valid sessionHandle has been
* returned from StartLogCollection.
*
* @param[in] sessionHandle The unique handle for this log session returned from a call to StartLogCollection.
* @note New implementations should override the two-argument version instead, as it is the primary
* method invoked during log collection.
*
* @param[in] sessionHandle The unique handle for this log session returned from a call to StartLogCollection.
* @return CHIP_ERROR_NOT_IMPLEMENTED by default unless overridden.
*/
virtual CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle) = 0;
virtual CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle) { return CHIP_ERROR_NOT_IMPLEMENTED; }

/**
* Called to end log collection for the log session identified by sessionHandle.
Expand All @@ -74,9 +77,7 @@ class DiagnosticLogsProviderDelegate
* @param[in] sessionHandle The unique handle for this log session returned from a call to StartLogCollection.
* @param[in] error A CHIP_ERROR value indicating the reason for ending the log collection.
* It is permissible to pass CHIP_NO_ERROR to indicate normal termination.
* @note Derived classes can override this method to handle additional termination logic specific to their
* implementation. If not overridden, this default implementation calls EndLogCollection with only
* sessionHandle.
* @return CHIP_ERROR_NOT_IMPLEMENTED by default unless overridden.
*
*/
virtual CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle, CHIP_ERROR error)
Expand Down

0 comments on commit 5154436

Please sign in to comment.