feat: add logging infra w/ backend support

Add functions to support logging. This enables a user specifying their
own logging format for all functions. TF-A's logging format is supported
out of the box by specifying the `DEBUG_BACKEND_HEADER` as
`log_backed_tf.h`.

Implement digest helpers that avoid heap/libc as some end projects don't
have access to this.

Change-Id: I55410ab963ea4d0c1956ba41f94b67eced8621d5
Signed-off-by: Harrison Mutai <harrison.mutai@arm.com>
diff --git a/include/private/debug.h b/include/private/debug.h
new file mode 100644
index 0000000..da10529
--- /dev/null
+++ b/include/private/debug.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2025, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef DEBUG_H
+#define DEBUG_H
+
+#include DEBUG_BACKEND_HEADER
+
+/*
+ * The log output macros print output to the console. These macros produce
+ * compiled log output only if the EVLOG_LOG_LEVEL defined in the makefile (or the
+ * make command line) is greater or equal than the level required for that
+ * type of log output.
+ *
+ * The format expected is the same as for printf(). For example:
+ * INFO("Info %s.\n", "message")    -> INFO:    Info message.
+ * WARN("Warning %s.\n", "message") -> WARNING: Warning message.
+ */
+
+#define EVLOG_LEVEL_NONE 0
+#define EVLOG_LEVEL_ERROR 10
+#define EVLOG_LEVEL_NOTICE 20
+#define EVLOG_LEVEL_WARNING 30
+#define EVLOG_LEVEL_INFO 40
+#define EVLOG_LEVEL_VERBOSE 50
+
+/*
+ * If the log output is too low then this macro is used in place of evlog_log()
+ * below. The intent is to get the compiler to evaluate the function call for
+ * type checking and format specifier correctness but let it optimize it out.
+ */
+#define no_evlog_log(fmt, ...)                         \
+	do {                                           \
+		if (false) {                           \
+			evlog_log(fmt, ##__VA_ARGS__); \
+		}                                      \
+	} while (false)
+
+#if EVLOG_LOG_LEVEL >= EVLOG_LEVEL_ERROR
+#define ERROR(...) evlog_log(EVLOG_MARKER_ERROR __VA_ARGS__)
+#else
+#define ERROR(...) no_evlog_log(EVLOG_MARKER_ERROR __VA_ARGS__)
+#endif
+
+#if EVLOG_LOG_LEVEL >= EVLOG_LEVEL_NOTICE
+#define NOTICE(...) evlog_log(EVLOG_MARKER_NOTICE __VA_ARGS__)
+#else
+#define NOTICE(...) no_evlog_log(EVLOG_MARKER_NOTICE __VA_ARGS__)
+#endif
+
+#if EVLOG_LOG_LEVEL >= EVLOG_LEVEL_WARNING
+#define WARN(...) evlog_log(EVLOG_MARKER_WARNING __VA_ARGS__)
+#else
+#define WARN(...) no_evlog_log(EVLOG_MARKER_WARNING __VA_ARGS__)
+#endif
+
+#if EVLOG_LOG_LEVEL >= EVLOG_LEVEL_INFO
+#define INFO(...) evlog_log(EVLOG_MARKER_INFO __VA_ARGS__)
+#else
+#define INFO(...) no_evlog_log(EVLOG_MARKER_INFO __VA_ARGS__)
+#endif
+
+#if EVLOG_LOG_LEVEL >= EVLOG_LEVEL_VERBOSE
+#define VERBOSE(...) evlog_log(EVLOG_MARKER_VERBOSE __VA_ARGS__)
+#else
+#define VERBOSE(...) no_evlog_log(EVLOG_MARKER_VERBOSE __VA_ARGS__)
+#endif
+
+#endif /* DEBUG_H */
diff --git a/include/private/digest.h b/include/private/digest.h
new file mode 100644
index 0000000..23567b8
--- /dev/null
+++ b/include/private/digest.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2025, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef DIGEST_H
+#define DIGEST_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "event_log.h"
+
+/**
+ * @brief Lookup algorithm metadata by numeric identifier.
+ *
+ * Translates a TPM/TCG-style algorithm ID into a description structure
+ * containing name, digest length, and other properties.
+ *
+ * @param[in] algo_id  Numeric algorithm identifier (e.g. TPM2_ALG_SHA256).
+ *
+ * @return Pointer to a constant algorithm descriptor, or NULL if not found.
+ */
+const struct event_log_algorithm *
+event_log_algorithm_lookup(uint16_t algorithm_id);
+
+/**
+ * @brief Append a C-string to a growing buffer with running position.
+ *
+ * Appends as many characters from @p s as fit, ensuring NUL-termination if any
+ * bytes were copied. Advances @p *pos by the full length of @p s regardless of
+ * truncation, enabling “would-have-written” accounting at the call site.
+ *
+ * @param[in,out] dst  Destination buffer.
+ * @param[in]     cap  Total capacity of @p dst (bytes, incl. space for NUL).
+ * @param[in,out] pos  In: current logical length; Out: increased by strlen(@p s).
+ * @param[in]     s    NUL-terminated string to append.
+ *
+ * @return strlen(@p s).
+ *
+ * @note Intended usage pattern:
+ * @code
+ * size_t pos = 0; buf[0] = '\0';
+ * append_str(buf, cap, &pos, "prefix: ");
+ * append_str(buf, cap, &pos, value);
+ * bool truncated = (pos >= cap);
+ * @endcode
+ */
+size_t event_log_append_str(char *dst, size_t cap, size_t *pos, const char *s);
+
+/**
+ * @brief Convert bytes to lower-hex with spaces (e.g., "aa ff …").
+ *
+ * Writes at most @p dst_len bytes including the terminating NUL (if @p dst_len > 0).
+ * Always returns the number of characters that would have been written
+ * (excluding the NUL), so you can detect truncation by checking
+ * `return_value >= dst_len`.
+ *
+ * @param[out] dst      Destination buffer for ASCII hex; may be NULL only if @p dst_len == 0.
+ * @param[in]  dst_len  Capacity of @p dst in bytes (including room for NUL).
+ * @param[in]  nbytes   Number of input bytes to render.
+ * @param[in]  digest   Pointer to @p nbytes of data; not modified.
+ *
+ * @return Characters that would have been written (excluding the terminating NUL).
+ *
+ * Example:
+ * @code
+ * // For 3 bytes, required size is 3*3 - 1 + 1 (for NUL) = 9
+ * char buf[9];
+ * size_t need = write_hex_spaced(buf, sizeof buf, 3, in);
+ * bool truncated = (need >= sizeof buf);
+ * @endcode
+ */
+size_t event_log_write_hex_spaced(char *dst, size_t dst_len, size_t nbytes,
+				  const uint8_t *digest);
+
+#endif /* DIGEST_H */