tf_log.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. void tf_log_newline(const char log_fmt[2])
  42. {
  43. unsigned int log_level = log_fmt[0];
  44. /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */
  45. assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE));
  46. assert((log_level % 10U) == 0U);
  47. if (log_level > max_log_level)
  48. return;
  49. putchar('\n');
  50. }
  51. /*
  52. * The helper function to set the log level dynamically by platform. The
  53. * maximum log level is determined by `LOG_LEVEL` build flag at compile time
  54. * and this helper can set a lower (or equal) log level than the one at compile.
  55. */
  56. void tf_log_set_max_level(unsigned int log_level)
  57. {
  58. assert(log_level <= LOG_LEVEL_VERBOSE);
  59. assert((log_level % 10U) == 0U);
  60. /* Cap log_level to the compile time maximum. */
  61. if (log_level <= (unsigned int)LOG_LEVEL)
  62. max_log_level = log_level;
  63. }