Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 1 | /* |
John Tsichritzis | 63801cd | 2019-07-05 14:22:12 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved. |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 7 | #include <stdarg.h> |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 8 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | |
| 11 | #include <common/debug.h> |
| 12 | #include <plat/common/platform.h> |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 13 | |
| 14 | /* Set the default maximum log level to the `LOG_LEVEL` build flag */ |
| 15 | static unsigned int max_log_level = LOG_LEVEL; |
| 16 | |
| 17 | /* |
John Tsichritzis | 63801cd | 2019-07-05 14:22:12 +0100 | [diff] [blame] | 18 | * The common log function which is invoked by TF-A code. |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 19 | * 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 | */ |
| 24 | void tf_log(const char *fmt, ...) |
| 25 | { |
| 26 | unsigned int log_level; |
| 27 | va_list args; |
| 28 | const char *prefix_str; |
| 29 | |
| 30 | /* We expect the LOG_MARKER_* macro as the first character */ |
| 31 | log_level = fmt[0]; |
| 32 | |
| 33 | /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */ |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 34 | assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE)); |
| 35 | assert((log_level % 10U) == 0U); |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 36 | |
| 37 | if (log_level > max_log_level) |
| 38 | return; |
| 39 | |
| 40 | prefix_str = plat_log_get_prefix(log_level); |
| 41 | |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 42 | while (*prefix_str != '\0') { |
| 43 | (void)putchar(*prefix_str); |
| 44 | prefix_str++; |
| 45 | } |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 46 | |
| 47 | va_start(args, fmt); |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 48 | (void)vprintf(fmt + 1, args); |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 49 | va_end(args); |
| 50 | } |
| 51 | |
Pali Rohár | b6cfd03 | 2021-07-20 23:57:47 +0200 | [diff] [blame] | 52 | void tf_log_newline(const char log_fmt[2]) |
| 53 | { |
| 54 | unsigned int log_level = log_fmt[0]; |
| 55 | |
| 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 | |
| 60 | if (log_level > max_log_level) |
| 61 | return; |
| 62 | |
| 63 | putchar('\n'); |
| 64 | } |
| 65 | |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 66 | /* |
| 67 | * The helper function to set the log level dynamically by platform. The |
| 68 | * maximum log level is determined by `LOG_LEVEL` build flag at compile time |
Junhan Zhou | 5a1451e | 2018-09-10 16:36:06 -0400 | [diff] [blame] | 69 | * and this helper can set a lower (or equal) log level than the one at compile. |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 70 | */ |
| 71 | void tf_log_set_max_level(unsigned int log_level) |
| 72 | { |
| 73 | assert(log_level <= LOG_LEVEL_VERBOSE); |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 74 | assert((log_level % 10U) == 0U); |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 75 | |
| 76 | /* Cap log_level to the compile time maximum. */ |
Junhan Zhou | 5a1451e | 2018-09-10 16:36:06 -0400 | [diff] [blame] | 77 | if (log_level <= (unsigned int)LOG_LEVEL) |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 78 | max_log_level = log_level; |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 79 | } |