LIB: Move TEST_LOG to use tfm_log

Move the existing TEST_LOG macro to use the new tfm_log API and remove
tfm_log_raw.c. This further unifies the logging APIs within the various
TF-M components.

For the case of the NS testsuite, we always set the USE_STDIO flag in
CMake which causes all NS test logging to use printf. This is
consistent with the NS image as a whole (which uses printf for its
logging).

Change-Id: If391d80ccb254964a1bf5fc13d98e2093bb2d9c6
Signed-off-by: Jackson Cooper-Driver <jackson.cooper-driver@arm.com>
diff --git a/erpc/tfm_reg_tests/CMakeLists.txt b/erpc/tfm_reg_tests/CMakeLists.txt
index bb7688d..888c248 100644
--- a/erpc/tfm_reg_tests/CMakeLists.txt
+++ b/erpc/tfm_reg_tests/CMakeLists.txt
@@ -151,10 +151,6 @@
 # Config check in case additional test configs passed in via command line.
 include(${TFM_REG_TEST_ROOT}/config/check_config.cmake)
 
-# Dummy tfm_ns_log for the tfm_test_framework_ns library
-add_library(tfm_ns_log INTERFACE)
-
-set(USE_STDIO ON)
 add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../lib/ext/qcbor ${CMAKE_BINARY_DIR}/lib/ext/qcbor)
 add_subdirectory(${TFM_REG_TEST_ROOT}/ns_regression      ${CMAKE_BINARY_DIR}/ns_regression)
 
diff --git a/lib/log/tfm_log_raw.c b/lib/log/tfm_log_raw.c
deleted file mode 100644
index 61f75ca..0000000
--- a/lib/log/tfm_log_raw.c
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#include <stdarg.h>
-#include <stddef.h>
-#include <stdint.h>
-#include "uart_stdout.h"
-
-#define PRINT_BUFF_SIZE 32
-#define NUM_BUFF_SIZE 12
-
-struct formatted_buffer_t {
-    size_t pos;
-    char buf[PRINT_BUFF_SIZE];
-};
-
-const char hex_digits_lo[] = {'0', '1', '2', '3', '4', '5', '6', '7',
-                              '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
-const char hex_digits_up[] = {'0', '1', '2', '3', '4', '5', '6', '7',
-                              '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
-
-static void _tfm_flush_formatted_buffer(struct formatted_buffer_t *pb,
-                                        uint8_t data)
-{
-    pb->buf[pb->pos++] = data;
-    if (pb->pos >= PRINT_BUFF_SIZE) {
-        pb->pos = 0;
-        /* uart flush and print here. */
-        stdio_output_string(pb->buf, PRINT_BUFF_SIZE);
-    }
-}
-
-static int _tfm_string_output(struct formatted_buffer_t *pb,
-                              const char *str)
-{
-    int count = 0;
-
-    while (*str) {
-        _tfm_flush_formatted_buffer(pb, *str++);
-        count++;
-    }
-
-    return count;
-}
-
-static int _tfm_dec_num_output(struct formatted_buffer_t *pb,
-                               int32_t num, uint8_t sign)
-{
-    int count = 0;
-    uint8_t num_buff[NUM_BUFF_SIZE] = {0};
-    uint32_t number = (uint32_t)num;
-    uint32_t k = 0;
-
-    if (sign == 'd' && num < 0) {
-        _tfm_flush_formatted_buffer(pb, '-');
-        count++;
-        number = -num;
-    }
-
-    do {
-        num_buff[k++] = '0' + number % 10;
-        number /= 10;
-    } while (number);
-
-    while (k) {
-        _tfm_flush_formatted_buffer(pb, num_buff[--k]);
-        count++;
-    }
-
-    return count;
-}
-
-static int _tfm_hex_num_output(struct formatted_buffer_t *pb, uint32_t num,
-                               const char *hex_digits)
-{
-    int count = 0;
-    uint8_t num_buff[NUM_BUFF_SIZE] = {0};
-    uint32_t k = 0;
-
-    do {
-        num_buff[k++] = hex_digits[num & 0x0f];
-        num >>= 4;
-    } while (num);
-
-    while (k) {
-        _tfm_flush_formatted_buffer(pb, num_buff[--k]);
-        count++;
-    }
-
-    return count;
-}
-
-static int _tfm_log_vprintf(const char *fmt, va_list ap)
-{
-    int count = 0;
-    struct formatted_buffer_t outputbuf;
-
-    outputbuf.pos = 0;
-
-    while (*fmt) {
-        if (*fmt == '%') {
-            switch (*(++fmt)) {
-            case 'd':
-            case 'i':
-                count += _tfm_dec_num_output(&outputbuf,
-                                             va_arg(ap, int32_t), 'd');
-                break;
-            case 'u':
-                count += _tfm_dec_num_output(&outputbuf,
-                                             va_arg(ap, int32_t), 'u');
-                break;
-            case 'x':
-                count += _tfm_hex_num_output(&outputbuf, va_arg(ap, uint32_t),
-                                             hex_digits_lo);
-                break;
-            case 'X':
-                count += _tfm_hex_num_output(&outputbuf, va_arg(ap, uint32_t),
-                                             hex_digits_up);
-                break;
-            case 'p':
-                count += _tfm_string_output(&outputbuf, "0x");
-                count += _tfm_hex_num_output(&outputbuf, va_arg(ap, uint32_t),
-                                             hex_digits_lo);
-                break;
-            case 's':
-                count += _tfm_string_output(&outputbuf, va_arg(ap, char*));
-                break;
-            case 'c':
-                _tfm_flush_formatted_buffer(&outputbuf,
-                                            (uint8_t)va_arg(ap, int32_t));
-                count++;
-                break;
-            case '%':
-                _tfm_flush_formatted_buffer(&outputbuf, '%');
-                count++;
-                break;
-            default:
-                count += _tfm_string_output(&outputbuf, "[Unsupported Tag]");
-                continue;
-            }
-            fmt++;
-        } else {
-            _tfm_flush_formatted_buffer(&outputbuf, *fmt++);
-            count++;
-        }
-    }
-
-    /* End of printf, flush buf */
-    if (outputbuf.pos) {
-        count += stdio_output_string(outputbuf.buf, outputbuf.pos);
-    }
-
-    return count;
-}
-
-int tfm_log_printf(const char *fmt, ...)
-{
-    int count = 0;
-    va_list ap;
-
-    va_start(ap, fmt);
-    count = _tfm_log_vprintf(fmt, ap);
-    va_end(ap);
-
-    return count;
-}
diff --git a/lib/log/tfm_log_raw.h b/lib/log/tfm_log_raw.h
deleted file mode 100644
index 7a5a942..0000000
--- a/lib/log/tfm_log_raw.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2019, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __TFM_LOG_RAW_H__
-#define __TFM_LOG_RAW_H__
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \brief Prints log messages
- *
- * \param[in]   fmt     Formatted string
- * \param[in]   ...     Viriable length argument
- *
- * \return              Number of chars printed
- *
- * \note                This function has the similar input argument format as
- *                      the 'printf' function. But it supports only some basic
- *                      formats like 'sdicpuxX' and '%'. It will output
- *                      "[Unsupported Tag]" when none of the above formats match
- *
- * \details             The following output formats are supported.
- *                      %s - string
- *                      %d - decimal signed integer (same for %i)
- *                      %u - decimal unsigned integer
- *                      %x - hex in lowercase
- *                      %X - hex in uppercase
- *                      %p - hex address of a pointer in lowercase
- *                      %c - character
- *                      %% - the '%' symbol
- */
-int tfm_log_printf(const char *fmt, ...);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __TFM_LOG_RAW_H__ */
diff --git a/tests_reg/test/bl1/bl1_1/CMakeLists.txt b/tests_reg/test/bl1/bl1_1/CMakeLists.txt
index ce0a33d..1d08110 100644
--- a/tests_reg/test/bl1/bl1_1/CMakeLists.txt
+++ b/tests_reg/test/bl1/bl1_1/CMakeLists.txt
@@ -11,28 +11,9 @@
 
 add_library(bl1_1_tests STATIC)
 
-set(LOG_SOURCE_ROOT ${TFM_TESTS_ROOT_DIR}/lib/log)
-
-add_library(bl1_1_log INTERFACE)
-
-target_sources(bl1_1_log
-    INTERFACE
-        ${LOG_SOURCE_ROOT}/tfm_log_raw.c
-)
-
-target_include_directories(bl1_1_log
-    INTERFACE
-        ${LOG_SOURCE_ROOT}
-)
-
-target_link_libraries(bl1_1_log
-    INTERFACE
-        platform_bl1_1_interface
-)
-
 target_link_libraries(tfm_test_framework_common
     INTERFACE
-        bl1_1_log
+        platform_bl1_1_interface
 )
 
 add_subdirectory(suites/crypto)
diff --git a/tests_reg/test/bl2/mcuboot/CMakeLists.txt b/tests_reg/test/bl2/mcuboot/CMakeLists.txt
index 17c7ab6..48f2deb 100644
--- a/tests_reg/test/bl2/mcuboot/CMakeLists.txt
+++ b/tests_reg/test/bl2/mcuboot/CMakeLists.txt
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021-2022, Arm Limited. All rights reserved.
+# SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -10,31 +10,11 @@
 # It links platform_ns which is not built by SPE build.
 # So a dedicated library is duplicated here, and it links the platform_s instead of platform_ns.
 
-# TO-DO: find a better approach to reference the directory
-set(LOG_SOURCE_ROOT ${TFM_TESTS_ROOT_DIR}/lib/log)
-
-add_library(mcuboot_test_log INTERFACE)
 add_library(mcuboot_test_framework_common INTERFACE)
 
-target_sources(mcuboot_test_log
-    INTERFACE
-        ${LOG_SOURCE_ROOT}/tfm_log_raw.c
-)
-
-target_include_directories(mcuboot_test_log
-    INTERFACE
-        ${LOG_SOURCE_ROOT}
-)
-
-target_link_libraries(mcuboot_test_log
-    INTERFACE
-        platform_bl2
-)
-
 target_link_libraries(mcuboot_test_framework_common
     INTERFACE
         tfm_test_framework_common
-        mcuboot_test_log
 )
 
 add_subdirectory(suites/integration)
diff --git a/tests_reg/test/framework/test_log.h b/tests_reg/test/framework/test_log.h
index 4c14c60..303793b 100644
--- a/tests_reg/test/framework/test_log.h
+++ b/tests_reg/test/framework/test_log.h
@@ -13,7 +13,17 @@
 #elif defined USE_STDIO
 #include <stdio.h>
 #else
-#include "tfm_log_raw.h"
+/*
+ * Depending on how the tests are compiled, they may
+ * pick up the LOG_LEVEL definition from other components. These other values
+ * will have no effect on logging in the tests as we always call the tfm_log
+ * function. In the case that we do not have a definition, just set
+ * it to VERBOSE
+ */
+#ifndef LOG_LEVEL
+#define LOG_LEVEL LOG_LEVEL_VERBOSE
+#endif
+#include "tfm_log.h"
 #endif /* USE_SP_LOG */
 
 #ifdef __cplusplus
@@ -25,7 +35,7 @@
 #elif defined USE_STDIO
 #define TEST_LOG(...) printf(__VA_ARGS__)
 #else
-#define TEST_LOG(...) tfm_log_printf(__VA_ARGS__)
+#define TEST_LOG(...) tfm_log(LOG_MARKER_RAW __VA_ARGS__)
 #endif /* USE_SP_LOG */
 
 #ifdef __cplusplus
diff --git a/tests_reg/test/ns_regression/CMakeLists.txt b/tests_reg/test/ns_regression/CMakeLists.txt
index 543d746..4da9a37 100644
--- a/tests_reg/test/ns_regression/CMakeLists.txt
+++ b/tests_reg/test/ns_regression/CMakeLists.txt
@@ -41,14 +41,13 @@
     INTERFACE
         DOMAIN_NS=1
         $<$<BOOL:${CONFIG_TFM_ERPC_TEST_FRAMEWORK}>:CONFIG_TFM_ERPC_TEST_FRAMEWORK=1>
-        $<$<BOOL:${USE_STDIO}>:USE_STDIO>
+        USE_STDIO
 )
 
 target_link_libraries(tfm_test_framework_ns
     INTERFACE
         tfm_test_framework_common
         tfm_api_ns
-        tfm_ns_log
 )
 
 target_sources(tfm_ns_tests