From f8b3bc1e47595e3b7b931544cdc9a9ad113e7d3e Mon Sep 17 00:00:00 2001 From: dario Date: Tue, 21 Nov 2023 21:34:07 +0100 Subject: [PATCH 1/3] Added formatted printing --- include/sta/debug/debug.hpp | 17 +++++++++++++---- include/sta/debug/printing/printable.hpp | 6 ++++++ src/debug/printing/printable.cpp | 15 ++++++++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/sta/debug/debug.hpp b/include/sta/debug/debug.hpp index 0396ea1..7a09609 100644 --- a/include/sta/debug/debug.hpp +++ b/include/sta/debug/debug.hpp @@ -19,7 +19,7 @@ namespace sta * * @ingroup sta_core_debug */ -# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) +# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) /** * @brief Debug print message followed by new-line to UART. @@ -28,12 +28,21 @@ namespace sta * * @ingroup sta_core_debug */ -# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__) +# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__) + +/** + * @brief Formatted debug printing with new-line. + * + * @param fmt See @ref sta::PrintableUART::printf + * @param ... See @ref sta::PrintableUART::printf + */ +# define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__) #else // !STA_DEBUGGING_ENABLED -# define STA_DEBUG_PRINT(...) ((void)0) -# define STA_DEBUG_PRINTLN(...) ((void)0) +# define STA_DEBUG_PRINT(...) ((void)0) +# define STA_DEBUG_PRINTLN(...) ((void)0) +# define STA_DEBUG_PRINTF(fmt, ...) ((void)0) #endif // STA_DEBUGGING_ENABLED diff --git a/include/sta/debug/printing/printable.hpp b/include/sta/debug/printing/printable.hpp index 77117a3..e1030fd 100644 --- a/include/sta/debug/printing/printable.hpp +++ b/include/sta/debug/printing/printable.hpp @@ -26,6 +26,12 @@ namespace sta class Printable { public: + /** + * @brief fmt The string defining the desired format. + * @breif ... The parameters for the formatted string. + */ + void printf(const char * fmt, ...); + /** * @brief Print single character. * diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index df6dcba..0c3bebd 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -3,12 +3,25 @@ #include #include #include +#include +#include #include #include namespace sta { + void Printable::printf(const char * fmt, ...) + { + va_list args; + va_start (args, fmt); + char str[2*strlen(fmt)]; + int n = vsnprintf(str, 2*strlen(fmt), fmt, args); + STA_ASSERT(n > 0); + + println(str); + } + void Printable::print(char c) { print(&c, 1); @@ -182,4 +195,4 @@ namespace sta print(buffer, digits); } -} // namespace sta \ No newline at end of file +} // namespace sta From a4ca79bdade0b929faa9206d11f65cb740672383 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 22 Nov 2023 19:16:32 +0100 Subject: [PATCH 2/3] Fixed some mistakes in debug printf --- src/debug/printing/printable.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index 0c3bebd..5498c5b 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -15,8 +15,13 @@ namespace sta { va_list args; va_start (args, fmt); - char str[2*strlen(fmt)]; - int n = vsnprintf(str, 2*strlen(fmt), fmt, args); + + char temp[1]; + int n = vsnprintf(temp, 1, fmt, args); + + va_start (args, fmt); + char str[n]; + vsnprintf(str, n, fmt, args); STA_ASSERT(n > 0); println(str); From bc6283404a2aa4a12a63fe0a002628ba742e9a1b Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 29 Nov 2023 20:18:24 +0100 Subject: [PATCH 3/3] Increased the buffer size for formatting to correct size --- src/debug/printing/printable.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index 5498c5b..a3206bb 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -20,8 +20,8 @@ namespace sta int n = vsnprintf(temp, 1, fmt, args); va_start (args, fmt); - char str[n]; - vsnprintf(str, n, fmt, args); + char str[n+1]; + vsnprintf(str, n+1, fmt, args); STA_ASSERT(n > 0); println(str);