tf_log.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdarg.h>
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <common/debug.h>
  10. #include <plat/common/platform.h>
  11. /* Set the default maximum log level to the `LOG_LEVEL` build flag */
  12. static unsigned int max_log_level = LOG_LEVEL;
  13. /*
  14. * The common log function which is invoked by TF-A code.
  15. * This function should not be directly invoked and is meant to be
  16. * only used by the log macros defined in debug.h. The function
  17. * expects the first character in the format string to be one of the
  18. * LOG_MARKER_* macros defined in debug.h.
  19. */
  20. void tf_log(const char *fmt, ...)
  21. {
  22. unsigned int log_level;
  23. va_list args;
  24. const char *prefix_str;
  25. /* We expect the LOG_MARKER_* macro as the first character */
  26. log_level = fmt[0];
  27. /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
  28. assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
  29. assert((log_level % 10U) == 0U);
  30. if (log_level > max_log_level)
  31. return;
  32. prefix_str = plat_log_get_prefix(log_level);
  33. while (*prefix_str != '\0') {
  34. (void)putchar(*prefix_str);
  35. prefix_str++;
  36. }
  37. va_start(args, fmt);
  38. (void)vprintf(fmt + 1, args);
  39. va_end(args);
  40. }
  41. /*
  42. * The helper function to set the log level dynamically by platform. The
  43. * maximum log level is determined by `LOG_LEVEL` build flag at compile time
  44. * and this helper can set a lower (or equal) log level than the one at compile.
  45. */
  46. void tf_log_set_max_level(unsigned int log_level)
  47. {
  48. assert(log_level <= LOG_LEVEL_VERBOSE);
  49. assert((log_level % 10U) == 0U);
  50. /* Cap log_level to the compile time maximum. */
  51. if (log_level <= (unsigned int)LOG_LEVEL)
  52. max_log_level = log_level;
  53. }