Skip to content

Commit

Permalink
[logging] Use CHIP_ERROR_FORMAT in more places (#19983)
Browse files Browse the repository at this point in the history
We have two alternative ways of logging an error code:
- using CHIP_ERROR_FORMAT which resolves either to %s or %u
- using %s + ErrorStr()

Both methods are supposed to have the same effect, but the
former generates slightly smaller code.

Signed-off-by: Damian Krolik <[email protected]>
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Oct 10, 2023
1 parent f1ba8da commit f802dee
Show file tree
Hide file tree
Showing 38 changed files with 90 additions and 95 deletions.
1 change: 0 additions & 1 deletion src/app/DeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <lib/core/CHIPEncoding.h>
#include <lib/dnssd/Resolver.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/logging/CHIPLogging.h>

using namespace chip::Callback;
Expand Down
3 changes: 1 addition & 2 deletions src/app/EventManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <lib/core/CHIPEventLoggingConfig.h>
#include <lib/core/CHIPTLVUtilities.hpp>
#include <lib/support/CodeUtils.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/logging/CHIPLogging.h>

using namespace chip::TLV;
Expand Down Expand Up @@ -468,7 +467,7 @@ CHIP_ERROR EventManagement::LogEventPrivate(EventLoggingDelegate * apDelegate, c
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(EventLogging, "Log event with error %s", ErrorStr(err));
ChipLogError(EventLogging, "Log event with error %" CHIP_ERROR_FORMAT, err.Format());
writer = checkpoint;
}
else if (opts.mPriority >= CHIP_CONFIG_EVENT_GLOBAL_PRIORITY)
Expand Down
1 change: 0 additions & 1 deletion src/app/MessageDef/StatusIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include <app/AppBuildConfig.h>
#include <lib/core/CHIPCore.h>
#include <lib/support/ErrorStr.h>

using namespace chip;
using namespace chip::TLV;
Expand Down
1 change: 0 additions & 1 deletion src/app/OperationalDeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <lib/core/CHIPEncoding.h>
#include <lib/dnssd/Resolver.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/logging/CHIPLogging.h>
#include <system/SystemLayer.h>

Expand Down
2 changes: 1 addition & 1 deletion src/app/WriteClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ CHIP_ERROR WriteClient::SendWriteRequest(const SessionHandle & session, System::
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(DataManagement, "Write client failed to SendWriteRequest: %s", ErrorStr(err));
ChipLogError(DataManagement, "Write client failed to SendWriteRequest: %" CHIP_ERROR_FORMAT, err.Format());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/CommissioningWindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void CommissioningWindowManager::OnSessionEstablishmentError(CHIP_ERROR err)
void CommissioningWindowManager::HandleFailedAttempt(CHIP_ERROR err)
{
mFailedCommissioningAttempts++;
ChipLogError(AppServer, "Commissioning failed (attempt %d): %s", mFailedCommissioningAttempts, ErrorStr(err));
ChipLogError(AppServer, "Commissioning failed (attempt %d): %" CHIP_ERROR_FORMAT, mFailedCommissioningAttempts, err.Format());
#if CONFIG_NETWORK_LAYER_BLE
mServer->GetBleLayerObject()->CloseAllBleConnections();
#endif
Expand Down
26 changes: 13 additions & 13 deletions src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,35 @@ bool DnssdServer::OnExpiration(System::Clock::Timestamp expirationMs)
CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(chip::DeviceLayer::UDPEndPointManager());
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to initialize advertiser: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to initialize advertiser: %" CHIP_ERROR_FORMAT, err.Format());
}

// reset advertising
err = Dnssd::ServiceAdvertiser::Instance().RemoveServices();
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to remove advertised services: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to remove advertised services: %" CHIP_ERROR_FORMAT, err.Format());
}

// restart operational (if needed)
err = AdvertiseOperational();
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise operational node: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to advertise operational node: %" CHIP_ERROR_FORMAT, err.Format());
}

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
err = AdvertiseCommissioner();
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise commissioner: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to advertise commissioner: %" CHIP_ERROR_FORMAT, err.Format());
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

err = Dnssd::ServiceAdvertiser::Instance().FinalizeServiceUpdate();
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to finalize service update: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to finalize service update: %" CHIP_ERROR_FORMAT, err.Format());
}

return true;
Expand All @@ -183,7 +183,7 @@ void DnssdServer::OnDiscoveryExpiration(System::Layer * aSystemLayer, void * aAp
CHIP_ERROR err = AdvertiseCommissionableNode(chip::Dnssd::CommissioningMode::kDisabled);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise extended commissionable node: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to advertise extended commissionable node: %" CHIP_ERROR_FORMAT, err.Format());
}
// set timeout
ScheduleExtendedDiscoveryExpiration();
Expand Down Expand Up @@ -433,27 +433,27 @@ void DnssdServer::StartServer(Dnssd::CommissioningMode mode)
CHIP_ERROR err = Dnssd::ServiceAdvertiser::Instance().Init(chip::DeviceLayer::UDPEndPointManager());
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to initialize advertiser: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to initialize advertiser: %" CHIP_ERROR_FORMAT, err.Format());
}

err = Dnssd::ServiceAdvertiser::Instance().RemoveServices();
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to remove advertised services: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to remove advertised services: %" CHIP_ERROR_FORMAT, err.Format());
}

err = AdvertiseOperational();
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise operational node: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to advertise operational node: %" CHIP_ERROR_FORMAT, err.Format());
}

if (mode != chip::Dnssd::CommissioningMode::kDisabled)
{
err = AdvertiseCommissionableNode(mode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise commissionable node: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to advertise commissionable node: %" CHIP_ERROR_FORMAT, err.Format());
}

// If any fabrics exist, the commissioning window must have been opened by the administrator
Expand All @@ -469,7 +469,7 @@ void DnssdServer::StartServer(Dnssd::CommissioningMode mode)
err = AdvertiseCommissionableNode(mode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise extended commissionable node: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to advertise extended commissionable node: %" CHIP_ERROR_FORMAT, err.Format());
}
// set timeout
ScheduleExtendedDiscoveryExpiration();
Expand All @@ -480,14 +480,14 @@ void DnssdServer::StartServer(Dnssd::CommissioningMode mode)
err = AdvertiseCommissioner();
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to advertise commissioner: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to advertise commissioner: %" CHIP_ERROR_FORMAT, err.Format());
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

err = Dnssd::ServiceAdvertiser::Instance().FinalizeServiceUpdate();
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to finalize service update: %s", chip::ErrorStr(err));
ChipLogError(Discovery, "Failed to finalize service update: %" CHIP_ERROR_FORMAT, err.Format());
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/app/server/OnboardingCodesUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void PrintOnboardingCodes(chip::RendezvousInformationFlags aRendezvousFlags)
CHIP_ERROR err = GetPayloadContents(payload, aRendezvousFlags);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "GetPayloadContents() failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "GetPayloadContents() failed: %" CHIP_ERROR_FORMAT, err.Format());
}

PrintOnboardingCodes(payload);
Expand Down Expand Up @@ -105,7 +105,7 @@ CHIP_ERROR GetPayloadContents(chip::PayloadContents & aPayload, chip::Rendezvous
err = GetCommissionableDataProvider()->GetSetupPasscode(aPayload.setUpPINCode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "GetCommissionableDataProvider()->GetSetupPasscode() failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "GetCommissionableDataProvider()->GetSetupPasscode() failed: %" CHIP_ERROR_FORMAT, err.Format());
#if defined(CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE) && CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE
ChipLogProgress(AppServer, "*** Using default EXAMPLE passcode %u ***",
static_cast<unsigned>(CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE));
Expand All @@ -118,21 +118,22 @@ CHIP_ERROR GetPayloadContents(chip::PayloadContents & aPayload, chip::Rendezvous
err = GetCommissionableDataProvider()->GetSetupDiscriminator(aPayload.discriminator);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "GetCommissionableDataProvider()->GetSetupDiscriminator() failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "GetCommissionableDataProvider()->GetSetupDiscriminator() failed: %" CHIP_ERROR_FORMAT,
err.Format());
return err;
}

err = chip::DeviceLayer::GetDeviceInstanceInfoProvider()->GetVendorId(aPayload.vendorID);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "GetDeviceInstanceInfoProvider()->GetVendorId() failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "GetDeviceInstanceInfoProvider()->GetVendorId() failed: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}

err = chip::DeviceLayer::GetDeviceInstanceInfoProvider()->GetProductId(aPayload.productID);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "GetDeviceInstanceInfoProvider()->GetProductId() failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "GetDeviceInstanceInfoProvider()->GetProductId() failed: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}

Expand All @@ -146,7 +147,7 @@ CHIP_ERROR GetQRCode(chip::MutableCharSpan & aQRCode, chip::RendezvousInformatio
CHIP_ERROR err = GetPayloadContents(payload, aRendezvousFlags);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "GetPayloadContents() failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "GetPayloadContents() failed: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}

Expand All @@ -158,7 +159,7 @@ CHIP_ERROR GetQRCode(chip::MutableCharSpan & aQRCode, const chip::PayloadContent
CHIP_ERROR err = chip::QRCodeBasicSetupPayloadGenerator(payload).payloadBase38Representation(aQRCode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Generating QR Code failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "Generating QR Code failed: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}

Expand Down Expand Up @@ -186,7 +187,7 @@ CHIP_ERROR GetManualPairingCode(chip::MutableCharSpan & aManualPairingCode, chip
CHIP_ERROR err = GetPayloadContents(payload, aRendezvousFlags);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "GetPayloadContents() failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "GetPayloadContents() failed: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}
return GetManualPairingCode(aManualPairingCode, payload);
Expand All @@ -197,7 +198,7 @@ CHIP_ERROR GetManualPairingCode(chip::MutableCharSpan & aManualPairingCode, cons
CHIP_ERROR err = chip::ManualSetupPayloadGenerator(payload).payloadDecimalStringRepresentation(aManualPairingCode);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Generating Manual Pairing Code failed: %s", chip::ErrorStr(err));
ChipLogError(AppServer, "Generating Manual Pairing Code failed: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}

Expand Down
7 changes: 3 additions & 4 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <lib/dnssd/ServiceNaming.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/PersistedCounter.h>
#include <lib/support/TestGroupData.h>
#include <lib/support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -306,7 +305,7 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "ERROR setting up transport: %s", ErrorStr(err));
ChipLogError(AppServer, "ERROR setting up transport: %" CHIP_ERROR_FORMAT, err.Format());
}
else
{
Expand Down Expand Up @@ -436,7 +435,7 @@ CHIP_ERROR Server::SendUserDirectedCommissioningRequest(chip::Transport::PeerAdd
err = app::DnssdServer::Instance().GetCommissionableInstanceName(nameBuffer, sizeof(nameBuffer));
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Failed to get mdns instance name error: %s", ErrorStr(err));
ChipLogError(AppServer, "Failed to get mdns instance name error: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}
ChipLogDetail(AppServer, "instanceName=%s", nameBuffer);
Expand All @@ -455,7 +454,7 @@ CHIP_ERROR Server::SendUserDirectedCommissioningRequest(chip::Transport::PeerAdd
}
else
{
ChipLogError(AppServer, "Send UDC request failed, err: %s\n", chip::ErrorStr(err));
ChipLogError(AppServer, "Send UDC request failed, err: %" CHIP_ERROR_FORMAT, err.Format());
}
return err;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/java/AndroidAppServerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CHIP_ERROR ChipAndroidAppInit(void)
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "Failed to run ChipAndroidAppInit: %s ", ErrorStr(err));
ChipLogProgress(NotSpecified, "Failed to run ChipAndroidAppInit: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}
return err;
Expand Down
2 changes: 1 addition & 1 deletion src/app/util/ember-compatibility-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ CHIP_ERROR WriteSingleClusterData(const SubjectDescriptor & aSubjectDescriptor,
uint16_t dataLen = 0;
if ((preparationError = prepareWriteData(attributeMetadata, aReader, dataLen)) != CHIP_NO_ERROR)
{
ChipLogDetail(Zcl, "Failed to prepare data to write: %s", ErrorStr(preparationError));
ChipLogDetail(Zcl, "Failed to prepare data to write: %" CHIP_ERROR_FORMAT, preparationError.Format());
return apWriteHandler->AddStatus(aPath, Protocols::InteractionModel::Status::InvalidValue);
}

Expand Down
2 changes: 0 additions & 2 deletions src/ble/BleError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include <ble/BleError.h>
#include <ble/BleLayer.h>

#include <lib/support/ErrorStr.h>

namespace chip {
namespace Ble {

Expand Down
15 changes: 8 additions & 7 deletions src/include/platform/internal/GenericPlatformManagerImpl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,31 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack()
err = InitEntropy();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Entropy initialization failed: %s", ErrorStr(err));
ChipLogError(DeviceLayer, "Entropy initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
}
SuccessOrExit(err);

// Initialize the CHIP system layer.
err = SystemLayer().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "SystemLayer initialization failed: %s", ErrorStr(err));
ChipLogError(DeviceLayer, "SystemLayer initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
}
SuccessOrExit(err);

// Initialize the Configuration Manager.
err = ConfigurationMgr().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Configuration Manager initialization failed: %s", ErrorStr(err));
ChipLogError(DeviceLayer, "Configuration Manager initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
}
SuccessOrExit(err);

// Initialize the CHIP UDP layer.
err = UDPEndPointManager()->Init(SystemLayer());
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "UDP initialization failed: %s", ErrorStr(err));
ChipLogError(DeviceLayer, "UDP initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
}
SuccessOrExit(err);

Expand All @@ -96,7 +96,7 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack()
err = BLEMgr().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "BLEManager initialization failed: %s", ErrorStr(err));
ChipLogError(DeviceLayer, "BLEManager initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
}
SuccessOrExit(err);
#endif
Expand All @@ -105,14 +105,15 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack()
err = ConnectivityMgr().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Connectivity Manager initialization failed: %s", ErrorStr(err));
ChipLogError(DeviceLayer, "Connectivity Manager initialization failed: %" CHIP_ERROR_FORMAT, err.Format());
}
SuccessOrExit(err);

// Initialize the NFC Manager.
#if CHIP_DEVICE_CONFIG_ENABLE_NFC
err = NFCMgr().Init();
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "NFC Manager initialization failed: %s", ErrorStr(err)));
VerifyOrExit(err == CHIP_NO_ERROR,
ChipLogError(DeviceLayer, "NFC Manager initialization failed: %" CHIP_ERROR_FORMAT, err.Format()));
#endif

// TODO Initialize CHIP Event Logging.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunEventLoop(void)
err = static_cast<System::LayerImplFreeRTOS &>(DeviceLayer::SystemLayer()).HandlePlatformTimer();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Error handling CHIP timers: %s", ErrorStr(err));
ChipLogError(DeviceLayer, "Error handling CHIP timers: %" CHIP_ERROR_FORMAT, err.Format());
}

// When processing the event queue below, do not wait if the queue is empty. Instead
Expand Down
2 changes: 0 additions & 2 deletions src/inet/InetError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include <inet/Inet.h>
#include <inet/InetError.h>

#include <lib/support/ErrorStr.h>

extern void FormatError(char * buf, uint16_t bufSize, const char * subsys, int32_t err, const char * desc);

namespace chip {
Expand Down
1 change: 0 additions & 1 deletion src/lib/asn1/ASN1Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <stdlib.h>

#include <lib/asn1/ASN1.h>
#include <lib/support/ErrorStr.h>

namespace chip {
namespace ASN1 {
Expand Down
Loading

0 comments on commit f802dee

Please sign in to comment.