tf_log.c 1.69 KB
Newer Older
Soby Mathew's avatar
Soby Mathew committed
1
/*
2
 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Soby Mathew's avatar
Soby Mathew committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <debug.h>
#include <platform.h>

/* Set the default maximum log level to the `LOG_LEVEL` build flag */
static unsigned int max_log_level = LOG_LEVEL;

/*
 * The common log function which is invoked by ARM Trusted Firmware code.
 * This function should not be directly invoked and is meant to be
 * only used by the log macros defined in debug.h. The function
 * expects the first character in the format string to be one of the
 * LOG_MARKER_* macros defined in debug.h.
 */
void tf_log(const char *fmt, ...)
{
	unsigned int log_level;
	va_list args;
	const char *prefix_str;

	/* We expect the LOG_MARKER_* macro as the first character */
	log_level = fmt[0];

	/* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
31
32
	assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
	assert((log_level % 10U) == 0U);
Soby Mathew's avatar
Soby Mathew committed
33
34
35
36
37
38

	if (log_level > max_log_level)
		return;

	prefix_str = plat_log_get_prefix(log_level);

39
40
41
42
	while (*prefix_str != '\0') {
		(void)putchar(*prefix_str);
		prefix_str++;
	}
Soby Mathew's avatar
Soby Mathew committed
43
44

	va_start(args, fmt);
45
	(void)vprintf(fmt + 1, args);
Soby Mathew's avatar
Soby Mathew committed
46
47
48
49
50
51
	va_end(args);
}

/*
 * The helper function to set the log level dynamically by platform. The
 * maximum log level is determined by `LOG_LEVEL` build flag at compile time
52
 * and this helper can set a lower (or equal) log level than the one at compile.
Soby Mathew's avatar
Soby Mathew committed
53
54
55
56
 */
void tf_log_set_max_level(unsigned int log_level)
{
	assert(log_level <= LOG_LEVEL_VERBOSE);
57
	assert((log_level % 10U) == 0U);
Soby Mathew's avatar
Soby Mathew committed
58
59

	/* Cap log_level to the compile time maximum. */
60
	if (log_level <= (unsigned int)LOG_LEVEL)
Soby Mathew's avatar
Soby Mathew committed
61
62
		max_log_level = log_level;
}