[Concept,03/24] log: Provide a macro to log a hex string

Message ID 20251031065439.3251464-4-sjg@u-boot.org
State New
Headers
Series luks: Provide basic support for unlocking a LUKS1 partition |

Commit Message

Simon Glass Oct. 31, 2025, 6:53 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

Add a new log_debug_hex() macro which can log a a buffer as a hex
string, e.g. for showing a hash value.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 include/log.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
  

Patch

diff --git a/include/log.h b/include/log.h
index 4f6d6a2c2cf..e1a12c26133 100644
--- a/include/log.h
+++ b/include/log.h
@@ -188,6 +188,22 @@  int _log_buffer(enum log_category_t cat, enum log_level_t level,
 #define log_io(_fmt...)		log(LOG_CATEGORY, LOGL_DEBUG_IO, ##_fmt)
 #define log_cont(_fmt...)	log(LOGC_CONT, LOGL_CONT, ##_fmt)
 
+/**
+ * log_debug_hex() - Print hex bytes for debugging
+ *
+ * @_label: Label to print before the hex bytes
+ * @_data: Pointer to the data to print
+ * @_len: Number of bytes to print
+ */
+#define log_debug_hex(_label, _data, _len) do { \
+	const u8 *__data = (const u8 *)(_data); \
+	int __len = (_len); \
+	log_debug("%s ", _label); \
+	for (int __i = 0; __i < __len; __i++) \
+		log_cont("%02x", __data[__i]); \
+	log_cont("\n"); \
+} while (0)
+
 #ifdef LOG_DEBUG
 #define _LOG_DEBUG	LOGL_FORCE_DEBUG
 #ifndef DEBUG