xref: /rk3399_ARM-atf/common/tf_log.c (revision fd1360a339e84ccd49f8a2d8a42e4c131a681b3c)
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 
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 
52*fd1360a3SPali Rohár void tf_log_newline(const char log_fmt[2])
53*fd1360a3SPali Rohár {
54*fd1360a3SPali Rohár 	unsigned int log_level = log_fmt[0];
55*fd1360a3SPali Rohár 
56*fd1360a3SPali Rohár 	/* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
57*fd1360a3SPali Rohár 	assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
58*fd1360a3SPali Rohár 	assert((log_level % 10U) == 0U);
59*fd1360a3SPali Rohár 
60*fd1360a3SPali Rohár 	if (log_level > max_log_level)
61*fd1360a3SPali Rohár 		return;
62*fd1360a3SPali Rohár 
63*fd1360a3SPali Rohár 	putchar('\n');
64*fd1360a3SPali Rohár }
65*fd1360a3SPali Rohár 
667f56e9a3SSoby Mathew /*
677f56e9a3SSoby Mathew  * The helper function to set the log level dynamically by platform. The
687f56e9a3SSoby Mathew  * maximum log level is determined by `LOG_LEVEL` build flag at compile time
692adb7867SJunhan Zhou  * and this helper can set a lower (or equal) log level than the one at compile.
707f56e9a3SSoby Mathew  */
717f56e9a3SSoby Mathew void tf_log_set_max_level(unsigned int log_level)
727f56e9a3SSoby Mathew {
737f56e9a3SSoby Mathew 	assert(log_level <= LOG_LEVEL_VERBOSE);
745a22e461SAntonio Nino Diaz 	assert((log_level % 10U) == 0U);
757f56e9a3SSoby Mathew 
767f56e9a3SSoby Mathew 	/* Cap log_level to the compile time maximum. */
772adb7867SJunhan Zhou 	if (log_level <= (unsigned int)LOG_LEVEL)
787f56e9a3SSoby Mathew 		max_log_level = log_level;
797f56e9a3SSoby Mathew }
80