blob: f67897523155f3ea217ce289c65f94165b8248f2 [file] [log] [blame]
Soby Mathew7f56e9a2017-09-04 11:49:29 +01001/*
John Tsichritzisbd97f832019-07-05 14:22:12 +01002 * Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.
Soby Mathew7f56e9a2017-09-04 11:49:29 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00007#include <stdarg.h>
Soby Mathew7f56e9a2017-09-04 11:49:29 +01008#include <assert.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00009#include <stdio.h>
10
11#include <common/debug.h>
12#include <plat/common/platform.h>
Soby Mathew7f56e9a2017-09-04 11:49:29 +010013
14/* Set the default maximum log level to the `LOG_LEVEL` build flag */
Maheedhar Bollapallif3ecd832024-04-24 18:43:34 +053015static uint32_t max_log_level = LOG_LEVEL;
Soby Mathew7f56e9a2017-09-04 11:49:29 +010016
17/*
John Tsichritzisbd97f832019-07-05 14:22:12 +010018 * The common log function which is invoked by TF-A code.
Soby Mathew7f56e9a2017-09-04 11:49:29 +010019 * This function should not be directly invoked and is meant to be
20 * only used by the log macros defined in debug.h. The function
21 * expects the first character in the format string to be one of the
22 * LOG_MARKER_* macros defined in debug.h.
23 */
24void tf_log(const char *fmt, ...)
25{
Maheedhar Bollapallif3ecd832024-04-24 18:43:34 +053026 uint32_t log_level;
Soby Mathew7f56e9a2017-09-04 11:49:29 +010027 va_list args;
28 const char *prefix_str;
29
30 /* We expect the LOG_MARKER_* macro as the first character */
Maheedhar Bollapallif3ecd832024-04-24 18:43:34 +053031 log_level = (uint32_t)fmt[0];
Soby Mathew7f56e9a2017-09-04 11:49:29 +010032
33 /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
Antonio Nino Diaz5a22e462018-08-28 11:44:44 +010034 assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
35 assert((log_level % 10U) == 0U);
Soby Mathew7f56e9a2017-09-04 11:49:29 +010036
Maheedhar Bollapalli0eeda632024-04-25 10:46:18 +053037 if (log_level > max_log_level) {
Soby Mathew7f56e9a2017-09-04 11:49:29 +010038 return;
Maheedhar Bollapalli0eeda632024-04-25 10:46:18 +053039 }
Soby Mathew7f56e9a2017-09-04 11:49:29 +010040 prefix_str = plat_log_get_prefix(log_level);
41
Antonio Nino Diaz5a22e462018-08-28 11:44:44 +010042 while (*prefix_str != '\0') {
Maheedhar Bollapallif3ecd832024-04-24 18:43:34 +053043 (void)putchar((int)*prefix_str);
Antonio Nino Diaz5a22e462018-08-28 11:44:44 +010044 prefix_str++;
45 }
Soby Mathew7f56e9a2017-09-04 11:49:29 +010046
47 va_start(args, fmt);
Antonio Nino Diaz5a22e462018-08-28 11:44:44 +010048 (void)vprintf(fmt + 1, args);
Soby Mathew7f56e9a2017-09-04 11:49:29 +010049 va_end(args);
50}
51
Pali Rohárfd1360a2021-07-20 23:57:47 +020052void tf_log_newline(const char log_fmt[2])
53{
Maheedhar Bollapallif3ecd832024-04-24 18:43:34 +053054 uint32_t log_level = (uint32_t)log_fmt[0];
Pali Rohárfd1360a2021-07-20 23:57:47 +020055
56 /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
57 assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
58 assert((log_level % 10U) == 0U);
59
Maheedhar Bollapalli0eeda632024-04-25 10:46:18 +053060 if (log_level > max_log_level) {
Pali Rohárfd1360a2021-07-20 23:57:47 +020061 return;
Maheedhar Bollapalli0eeda632024-04-25 10:46:18 +053062 }
Pali Rohárfd1360a2021-07-20 23:57:47 +020063
Maheedhar Bollapallifc7a7202024-04-22 17:54:50 +053064 (void)putchar((int32_t)'\n');
Pali Rohárfd1360a2021-07-20 23:57:47 +020065}
66
Soby Mathew7f56e9a2017-09-04 11:49:29 +010067/*
68 * The helper function to set the log level dynamically by platform. The
69 * maximum log level is determined by `LOG_LEVEL` build flag at compile time
Junhan Zhou2adb7862018-09-10 16:36:06 -040070 * and this helper can set a lower (or equal) log level than the one at compile.
Soby Mathew7f56e9a2017-09-04 11:49:29 +010071 */
Maheedhar Bollapallif3ecd832024-04-24 18:43:34 +053072void tf_log_set_max_level(uint32_t log_level)
Soby Mathew7f56e9a2017-09-04 11:49:29 +010073{
74 assert(log_level <= LOG_LEVEL_VERBOSE);
Antonio Nino Diaz5a22e462018-08-28 11:44:44 +010075 assert((log_level % 10U) == 0U);
Soby Mathew7f56e9a2017-09-04 11:49:29 +010076
77 /* Cap log_level to the compile time maximum. */
Maheedhar Bollapallif3ecd832024-04-24 18:43:34 +053078 if (log_level <= (uint32_t)LOG_LEVEL)
Soby Mathew7f56e9a2017-09-04 11:49:29 +010079 max_log_level = log_level;
Soby Mathew7f56e9a2017-09-04 11:49:29 +010080}