bh_log.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 ((uint32)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),
  35. "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%03" PRIu32, h, m, s,
  36. mills);
  37. os_printf("[%s - %" PRIXPTR "]: ", buf, (uintptr_t)self);
  38. if (file)
  39. os_printf("%s, line %d, ", file, line);
  40. va_start(ap, fmt);
  41. os_vprintf(fmt, ap);
  42. va_end(ap);
  43. os_printf("\n");
  44. }
  45. static uint32 last_time_ms = 0;
  46. static uint32 total_time_ms = 0;
  47. void
  48. bh_print_time(const char *prompt)
  49. {
  50. uint32 curr_time_ms;
  51. if (log_verbose_level < 3)
  52. return;
  53. curr_time_ms = (uint32)bh_get_tick_ms();
  54. if (last_time_ms == 0)
  55. last_time_ms = curr_time_ms;
  56. total_time_ms += curr_time_ms - last_time_ms;
  57. os_printf("%-48s time of last stage: %" PRIu32 " ms, total time: %" PRIu32
  58. " ms\n",
  59. prompt, curr_time_ms - last_time_ms, total_time_ms);
  60. last_time_ms = curr_time_ms;
  61. }
  62. void
  63. bh_print_proc_mem(const char *prompt)
  64. {
  65. char buf[1024] = { 0 };
  66. if (log_verbose_level < BH_LOG_LEVEL_DEBUG)
  67. return;
  68. if (os_dumps_proc_mem_info(buf, sizeof(buf)) != 0)
  69. return;
  70. os_printf("%s\n", prompt);
  71. os_printf("===== memory usage =====\n");
  72. os_printf("%s", buf);
  73. os_printf("==========\n");
  74. return;
  75. }
  76. void
  77. bh_log_proc_mem(const char *function, uint32 line)
  78. {
  79. char prompt[128] = { 0 };
  80. snprintf(prompt, sizeof(prompt), "[MEM] %s(...) L%u", function, line);
  81. return bh_print_proc_mem(prompt);
  82. }