xref: /rk3399_ARM-atf/common/tf_log.c (revision bd97f83a62382633468034db1e81381c5990b62a)
17f56e9a3SSoby Mathew /*
2*bd97f83aSJohn 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 /*
18*bd97f83aSJohn 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 
377f56e9a3SSoby Mathew 	if (log_level > max_log_level)
387f56e9a3SSoby Mathew 		return;
397f56e9a3SSoby Mathew 
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 
527f56e9a3SSoby Mathew /*
537f56e9a3SSoby Mathew  * The helper function to set the log level dynamically by platform. The
547f56e9a3SSoby Mathew  * maximum log level is determined by `LOG_LEVEL` build flag at compile time
552adb7867SJunhan Zhou  * and this helper can set a lower (or equal) log level than the one at compile.
567f56e9a3SSoby Mathew  */
577f56e9a3SSoby Mathew void tf_log_set_max_level(unsigned int log_level)
587f56e9a3SSoby Mathew {
597f56e9a3SSoby Mathew 	assert(log_level <= LOG_LEVEL_VERBOSE);
605a22e461SAntonio Nino Diaz 	assert((log_level % 10U) == 0U);
617f56e9a3SSoby Mathew 
627f56e9a3SSoby Mathew 	/* Cap log_level to the compile time maximum. */
632adb7867SJunhan Zhou 	if (log_level <= (unsigned int)LOG_LEVEL)
647f56e9a3SSoby Mathew 		max_log_level = log_level;
657f56e9a3SSoby Mathew }
66