Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, 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 | |
| 7 | #include <assert.h> |
| 8 | #include <debug.h> |
| 9 | #include <platform.h> |
| 10 | |
| 11 | /* Set the default maximum log level to the `LOG_LEVEL` build flag */ |
| 12 | static unsigned int max_log_level = LOG_LEVEL; |
| 13 | |
| 14 | /* |
| 15 | * The common log function which is invoked by ARM Trusted Firmware code. |
| 16 | * This function should not be directly invoked and is meant to be |
| 17 | * only used by the log macros defined in debug.h. The function |
| 18 | * expects the first character in the format string to be one of the |
| 19 | * LOG_MARKER_* macros defined in debug.h. |
| 20 | */ |
| 21 | void tf_log(const char *fmt, ...) |
| 22 | { |
| 23 | unsigned int log_level; |
| 24 | va_list args; |
| 25 | const char *prefix_str; |
| 26 | |
| 27 | /* We expect the LOG_MARKER_* macro as the first character */ |
| 28 | log_level = fmt[0]; |
| 29 | |
| 30 | /* 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] | 31 | assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE)); |
| 32 | assert((log_level % 10U) == 0U); |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 33 | |
| 34 | if (log_level > max_log_level) |
| 35 | return; |
| 36 | |
| 37 | prefix_str = plat_log_get_prefix(log_level); |
| 38 | |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 39 | while (*prefix_str != '\0') { |
| 40 | (void)putchar(*prefix_str); |
| 41 | prefix_str++; |
| 42 | } |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 43 | |
| 44 | va_start(args, fmt); |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 45 | (void)vprintf(fmt + 1, args); |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 46 | va_end(args); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * The helper function to set the log level dynamically by platform. The |
| 51 | * maximum log level is determined by `LOG_LEVEL` build flag at compile time |
| 52 | * and this helper can set a lower log level than the one at compile. |
| 53 | */ |
| 54 | void tf_log_set_max_level(unsigned int log_level) |
| 55 | { |
| 56 | assert(log_level <= LOG_LEVEL_VERBOSE); |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 57 | assert((log_level % 10U) == 0U); |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 58 | |
| 59 | /* Cap log_level to the compile time maximum. */ |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 60 | if (log_level < (unsigned int)LOG_LEVEL) |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 61 | max_log_level = log_level; |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 62 | } |