17f56e9a3SSoby Mathew /* 2870ce3ddSAntonio Nino Diaz * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. 37f56e9a3SSoby Mathew * 47f56e9a3SSoby Mathew * SPDX-License-Identifier: BSD-3-Clause 57f56e9a3SSoby Mathew */ 67f56e9a3SSoby Mathew 77f56e9a3SSoby Mathew #include <assert.h> 87f56e9a3SSoby Mathew #include <debug.h> 97f56e9a3SSoby Mathew #include <platform.h> 107f56e9a3SSoby Mathew 117f56e9a3SSoby Mathew /* Set the default maximum log level to the `LOG_LEVEL` build flag */ 127f56e9a3SSoby Mathew static unsigned int max_log_level = LOG_LEVEL; 137f56e9a3SSoby Mathew 147f56e9a3SSoby Mathew /* 157f56e9a3SSoby Mathew * The common log function which is invoked by ARM Trusted Firmware code. 167f56e9a3SSoby Mathew * This function should not be directly invoked and is meant to be 177f56e9a3SSoby Mathew * only used by the log macros defined in debug.h. The function 187f56e9a3SSoby Mathew * expects the first character in the format string to be one of the 197f56e9a3SSoby Mathew * LOG_MARKER_* macros defined in debug.h. 207f56e9a3SSoby Mathew */ 217f56e9a3SSoby Mathew void tf_log(const char *fmt, ...) 227f56e9a3SSoby Mathew { 237f56e9a3SSoby Mathew unsigned int log_level; 247f56e9a3SSoby Mathew va_list args; 257f56e9a3SSoby Mathew const char *prefix_str; 267f56e9a3SSoby Mathew 277f56e9a3SSoby Mathew /* We expect the LOG_MARKER_* macro as the first character */ 287f56e9a3SSoby Mathew log_level = fmt[0]; 297f56e9a3SSoby Mathew 307f56e9a3SSoby Mathew /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */ 31*5a22e461SAntonio Nino Diaz assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE)); 32*5a22e461SAntonio Nino Diaz assert((log_level % 10U) == 0U); 337f56e9a3SSoby Mathew 347f56e9a3SSoby Mathew if (log_level > max_log_level) 357f56e9a3SSoby Mathew return; 367f56e9a3SSoby Mathew 377f56e9a3SSoby Mathew prefix_str = plat_log_get_prefix(log_level); 387f56e9a3SSoby Mathew 39*5a22e461SAntonio Nino Diaz while (*prefix_str != '\0') { 40*5a22e461SAntonio Nino Diaz (void)putchar(*prefix_str); 41*5a22e461SAntonio Nino Diaz prefix_str++; 42*5a22e461SAntonio Nino Diaz } 437f56e9a3SSoby Mathew 447f56e9a3SSoby Mathew va_start(args, fmt); 45*5a22e461SAntonio Nino Diaz (void)vprintf(fmt + 1, args); 467f56e9a3SSoby Mathew va_end(args); 477f56e9a3SSoby Mathew } 487f56e9a3SSoby Mathew 497f56e9a3SSoby Mathew /* 507f56e9a3SSoby Mathew * The helper function to set the log level dynamically by platform. The 517f56e9a3SSoby Mathew * maximum log level is determined by `LOG_LEVEL` build flag at compile time 527f56e9a3SSoby Mathew * and this helper can set a lower log level than the one at compile. 537f56e9a3SSoby Mathew */ 547f56e9a3SSoby Mathew void tf_log_set_max_level(unsigned int log_level) 557f56e9a3SSoby Mathew { 567f56e9a3SSoby Mathew assert(log_level <= LOG_LEVEL_VERBOSE); 57*5a22e461SAntonio Nino Diaz assert((log_level % 10U) == 0U); 587f56e9a3SSoby Mathew 597f56e9a3SSoby Mathew /* Cap log_level to the compile time maximum. */ 60*5a22e461SAntonio Nino Diaz if (log_level < (unsigned int)LOG_LEVEL) 617f56e9a3SSoby Mathew max_log_level = log_level; 627f56e9a3SSoby Mathew } 63