diff --git a/scripts/tools/check_includes_config.py b/scripts/tools/check_includes_config.py index 2e79c6f8f9cfa9..a7da891ea59c12 100644 --- a/scripts/tools/check_includes_config.py +++ b/scripts/tools/check_includes_config.py @@ -119,6 +119,7 @@ # Only uses for zero-cost types. 'src/system/SystemClock.h': {'chrono'}, 'src/platform/mbed/MbedEventTimeout.h': {'chrono'}, + 'src/lib/core/StringBuilderAdapters.h': {'chrono'}, 'src/app/app-platform/ContentApp.h': {'list', 'string'}, 'src/app/app-platform/ContentAppPlatform.cpp': {'string'}, diff --git a/src/lib/core/StringBuilderAdapters.cpp b/src/lib/core/StringBuilderAdapters.cpp index be07ae92cf7923..7ed0774f10a2f2 100644 --- a/src/lib/core/StringBuilderAdapters.cpp +++ b/src/lib/core/StringBuilderAdapters.cpp @@ -28,6 +28,22 @@ StatusWithSize ToString(const CHIP_ERROR & err, pw::span buffe return pw::string::Format(buffer, "CHIP_ERROR:<%" CHIP_ERROR_FORMAT ">", err.Format()); } +template <> +StatusWithSize ToString>(const std::chrono::duration & time, + pw::span buffer) +{ + // Cast to llu because some platforms use lu and others use llu. + return pw::string::Format(buffer, "%llums", static_cast(time.count())); +} + +template <> +StatusWithSize ToString>(const std::chrono::duration & time, + pw::span buffer) +{ + // Cast to llu because some platforms use lu and others use llu. + return pw::string::Format(buffer, "%lluus", static_cast(time.count())); +} + } // namespace pw #if CHIP_CONFIG_TEST_GOOGLETEST @@ -42,5 +58,16 @@ void PrintTo(const CHIP_ERROR & err, std::ostream * os) } *os << "CHIP_ERROR:<" << err.Format() << ">"; } + +void PrintTo(const std::chrono::duration & time, std::ostream * os) +{ + *os << time.count() << "ms"; +} + +void PrintTo(const std::chrono::duration & time, std::ostream * os) +{ + *os << time.count() << "us"; +} + } // namespace chip #endif // CHIP_CONFIG_TEST_GOOGLETEST diff --git a/src/lib/core/StringBuilderAdapters.h b/src/lib/core/StringBuilderAdapters.h index ad3bfb71e2299a..b49fa229b46586 100644 --- a/src/lib/core/StringBuilderAdapters.h +++ b/src/lib/core/StringBuilderAdapters.h @@ -41,6 +41,8 @@ /// Expected: .... == CHIP_ERROR(0, "src/setup_payload/tests/TestAdditionalDataPayload.cpp", 234) /// Actual: CHIP_ERROR: == CHIP_NO_ERROR +#include + #include #include @@ -51,6 +53,14 @@ namespace pw { template <> StatusWithSize ToString(const CHIP_ERROR & err, pw::span buffer); +// Adapters for chip::System::Clock::Microseconds64 and Milliseconds64 +template <> +StatusWithSize ToString>(const std::chrono::duration & time, + pw::span buffer); +template <> +StatusWithSize ToString>(const std::chrono::duration & time, + pw::span buffer); + } // namespace pw #if CHIP_CONFIG_TEST_GOOGLETEST @@ -69,5 +79,10 @@ namespace chip { /// /// This enhances the readability and diagnostic information in GoogleTest test logs. void PrintTo(const CHIP_ERROR & err, std::ostream * os); + +// Adapters for chip::System::Clock::Microseconds64 and Milliseconds64 +void PrintTo(const std::chrono::duration & time, std::ostream * os); +void PrintTo(const std::chrono::duration & time, std::ostream * os); + } // namespace chip #endif // CHIP_CONFIG_TEST_GOOGLETEST