bh_log.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 output.
  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. #ifndef BH_LOG
  17. void
  18. bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...)
  19. {
  20. va_list ap;
  21. korp_tid self;
  22. char buf[32] = { 0 };
  23. uint64 usec;
  24. uint32 t, h, m, s, mills;
  25. if ((uint32)log_level > log_verbose_level)
  26. return;
  27. self = os_self_thread();
  28. usec = os_time_get_boot_us();
  29. t = (uint32)(usec / 1000000) % (24 * 60 * 60);
  30. h = t / (60 * 60);
  31. t = t % (60 * 60);
  32. m = t / 60;
  33. s = t % 60;
  34. mills = (uint32)(usec % 1000);
  35. snprintf(buf, sizeof(buf),
  36. "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%03" PRIu32, h, m, s,
  37. mills);
  38. #ifndef BH_VPRINTF
  39. os_printf("[%s - %" PRIXPTR "]: ", buf, (uintptr_t)self);
  40. #endif
  41. if (file)
  42. os_printf("%s, line %d, ", file, line);
  43. va_start(ap, fmt);
  44. os_vprintf(fmt, ap);
  45. va_end(ap);
  46. os_printf("\n");
  47. }
  48. #endif
  49. static uint32 last_time_ms = 0;
  50. static uint32 total_time_ms = 0;
  51. void
  52. bh_print_time(const char *prompt)
  53. {
  54. uint32 curr_time_ms;
  55. if (log_verbose_level < 3)
  56. return;
  57. curr_time_ms = (uint32)bh_get_tick_ms();
  58. if (last_time_ms == 0)
  59. last_time_ms = curr_time_ms;
  60. total_time_ms += curr_time_ms - last_time_ms;
  61. os_printf("%-48s time of last stage: %" PRIu32 " ms, total time: %" PRIu32
  62. " ms\n",
  63. prompt, curr_time_ms - last_time_ms, total_time_ms);
  64. last_time_ms = curr_time_ms;
  65. }
  66. void
  67. bh_print_proc_mem(const char *prompt)
  68. {
  69. char buf[1024] = { 0 };
  70. if (log_verbose_level < BH_LOG_LEVEL_DEBUG)
  71. return;
  72. if (os_dumps_proc_mem_info(buf, sizeof(buf)) != 0)
  73. return;
  74. os_printf("%s\n", prompt);
  75. os_printf("===== memory usage =====\n");
  76. os_printf("%s", buf);
  77. os_printf("==========\n");
  78. return;
  79. }
  80. void
  81. bh_log_proc_mem(const char *function, uint32 line)
  82. {
  83. char prompt[128] = { 0 };
  84. snprintf(prompt, sizeof(prompt), "[MEM] %s(...) L%" PRIu32, function, line);
  85. bh_print_proc_mem(prompt);
  86. }