xref: /rk3399_ARM-atf/common/tf_log.c (revision fc7a7208e0ccc0722bb29fcbb0cb7a3d74ff0953)
17f56e9a3SSoby Mathew /*
2bd97f83aSJohn Tsichritzis  * Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.
37f56e9a3SSoby Mathew  *
47f56e9a3SSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
57f56e9a3SSoby Mathew  */
67f56e9a3SSoby Mathew 
709d40e0eSAntonio Nino Diaz #include <stdarg.h>
87f56e9a3SSoby Mathew #include <assert.h>
909d40e0eSAntonio Nino Diaz #include <stdio.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <common/debug.h>
1209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
137f56e9a3SSoby Mathew 
147f56e9a3SSoby Mathew /* Set the default maximum log level to the `LOG_LEVEL` build flag */
157f56e9a3SSoby Mathew static unsigned int max_log_level = LOG_LEVEL;
167f56e9a3SSoby Mathew 
177f56e9a3SSoby Mathew /*
18bd97f83aSJohn Tsichritzis  * The common log function which is invoked by TF-A code.
197f56e9a3SSoby Mathew  * This function should not be directly invoked and is meant to be
207f56e9a3SSoby Mathew  * only used by the log macros defined in debug.h. The function
217f56e9a3SSoby Mathew  * expects the first character in the format string to be one of the
227f56e9a3SSoby Mathew  * LOG_MARKER_* macros defined in debug.h.
237f56e9a3SSoby Mathew  */
247f56e9a3SSoby Mathew void tf_log(const char *fmt, ...)
257f56e9a3SSoby Mathew {
267f56e9a3SSoby Mathew 	unsigned int log_level;
277f56e9a3SSoby Mathew 	va_list args;
287f56e9a3SSoby Mathew 	const char *prefix_str;
297f56e9a3SSoby Mathew 
307f56e9a3SSoby Mathew 	/* We expect the LOG_MARKER_* macro as the first character */
317f56e9a3SSoby Mathew 	log_level = fmt[0];
327f56e9a3SSoby Mathew 
337f56e9a3SSoby Mathew 	/* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
345a22e461SAntonio Nino Diaz 	assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
355a22e461SAntonio Nino Diaz 	assert((log_level % 10U) == 0U);
367f56e9a3SSoby Mathew 
370eeda638SMaheedhar Bollapalli 	if (log_level > max_log_level) {
387f56e9a3SSoby Mathew 		return;
390eeda638SMaheedhar Bollapalli 	}
407f56e9a3SSoby Mathew 	prefix_str = plat_log_get_prefix(log_level);
417f56e9a3SSoby Mathew 
425a22e461SAntonio Nino Diaz 	while (*prefix_str != '\0') {
435a22e461SAntonio Nino Diaz 		(void)putchar(*prefix_str);
445a22e461SAntonio Nino Diaz 		prefix_str++;
455a22e461SAntonio Nino Diaz 	}
467f56e9a3SSoby Mathew 
477f56e9a3SSoby Mathew 	va_start(args, fmt);
485a22e461SAntonio Nino Diaz 	(void)vprintf(fmt + 1, args);
497f56e9a3SSoby Mathew 	va_end(args);
507f56e9a3SSoby Mathew }
517f56e9a3SSoby Mathew 
52fd1360a3SPali Rohár void tf_log_newline(const char log_fmt[2])
53fd1360a3SPali Rohár {
54fd1360a3SPali Rohár 	unsigned int log_level = log_fmt[0];
55fd1360a3SPali Rohár 
56fd1360a3SPali Rohár 	/* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
57fd1360a3SPali Rohár 	assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
58fd1360a3SPali Rohár 	assert((log_level % 10U) == 0U);
59fd1360a3SPali Rohár 
600eeda638SMaheedhar Bollapalli 	if (log_level > max_log_level) {
61fd1360a3SPali Rohár 		return;
620eeda638SMaheedhar Bollapalli 	}
63fd1360a3SPali Rohár 
64*fc7a7208SMaheedhar Bollapalli 	(void)putchar((int32_t)'\n');
65fd1360a3SPali Rohár }
66fd1360a3SPali Rohár 
677f56e9a3SSoby Mathew /*
687f56e9a3SSoby Mathew  * The helper function to set the log level dynamically by platform. The
697f56e9a3SSoby Mathew  * maximum log level is determined by `LOG_LEVEL` build flag at compile time
702adb7867SJunhan Zhou  * and this helper can set a lower (or equal) log level than the one at compile.
717f56e9a3SSoby Mathew  */
727f56e9a3SSoby Mathew void tf_log_set_max_level(unsigned int log_level)
737f56e9a3SSoby Mathew {
747f56e9a3SSoby Mathew 	assert(log_level <= LOG_LEVEL_VERBOSE);
755a22e461SAntonio Nino Diaz 	assert((log_level % 10U) == 0U);
767f56e9a3SSoby Mathew 
777f56e9a3SSoby Mathew 	/* Cap log_level to the compile time maximum. */
782adb7867SJunhan Zhou 	if (log_level <= (unsigned int)LOG_LEVEL)
797f56e9a3SSoby Mathew 		max_log_level = log_level;
807f56e9a3SSoby Mathew }
81