bh_log.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_log.h"
  6. /**
  7. * The verbose level of the log system. Only those verbose logs whose
  8. * levels are less than or equal to this value are outputed.
  9. */
  10. static uint32 log_verbose_level = BH_LOG_LEVEL_WARNING;
  11. void
  12. bh_log_set_verbose_level(uint32 level)
  13. {
  14. log_verbose_level = level;
  15. }
  16. void
  17. bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...)
  18. {
  19. va_list ap;
  20. korp_tid self;
  21. char buf[32] = { 0 };
  22. uint64 usec;
  23. uint32 t, h, m, s, mills;
  24. if (log_level > log_verbose_level)
  25. return;
  26. self = os_self_thread();
  27. usec = os_time_get_boot_microsecond();
  28. t = (uint32)(usec / 1000000) % (24 * 60 * 60);
  29. h = t / (60 * 60);
  30. t = t % (60 * 60);
  31. m = t / 60;
  32. s = t % 60;
  33. mills = (uint32)(usec % 1000);
  34. snprintf(buf, sizeof(buf), "%02u:%02u:%02u:%03u", h, m, s, mills);
  35. os_printf("[%s - %X]: ", buf, (uint32)self);
  36. if (file)
  37. os_printf("%s, line %d, ", file, line);
  38. va_start(ap, fmt);
  39. os_vprintf(fmt, ap);
  40. va_end(ap);
  41. os_printf("\n");
  42. }
  43. static uint32 last_time_ms = 0;
  44. static uint32 total_time_ms = 0;
  45. void
  46. bh_print_time(const char *prompt)
  47. {
  48. uint32 curr_time_ms;
  49. if (log_verbose_level < 3)
  50. return;
  51. curr_time_ms = (uint32)bh_get_tick_ms();
  52. if (last_time_ms == 0)
  53. last_time_ms = curr_time_ms;
  54. total_time_ms += curr_time_ms - last_time_ms;
  55. os_printf("%-48s time of last stage: %u ms, total time: %u ms\n",
  56. prompt, curr_time_ms - last_time_ms, total_time_ms);
  57. last_time_ms = curr_time_ms;
  58. }