bh_log.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 = 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. }