bh_log.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /**
  6. * @file bh_log.c
  7. * @date Tue Nov 8 18:20:06 2011
  8. *
  9. * @brief Implementation of Beihai's log system.
  10. */
  11. #include "bh_assert.h"
  12. #include "bh_time.h"
  13. #include "bh_thread.h"
  14. #include "bh_log.h"
  15. #include "bh_platform_log.h"
  16. /**
  17. * The verbose level of the log system. Only those verbose logs whose
  18. * levels are less than or equal to this value are outputed.
  19. */
  20. static int log_verbose_level;
  21. /**
  22. * The lock for protecting the global output stream of logs.
  23. */
  24. static korp_mutex log_stream_lock;
  25. /**
  26. * The current logging thread that owns the log_stream_lock.
  27. */
  28. static korp_tid cur_logging_thread = INVALID_THREAD_ID;
  29. /**
  30. * Whether the currently being printed log is ennabled.
  31. */
  32. static bool cur_log_enabled;
  33. int _bh_log_init()
  34. {
  35. log_verbose_level = 3;
  36. return vm_mutex_init(&log_stream_lock);
  37. }
  38. void _bh_log_set_verbose_level(int level)
  39. {
  40. log_verbose_level = level;
  41. }
  42. void _bh_log_printf(const char *fmt, ...)
  43. {
  44. va_list ap;
  45. va_start(ap, fmt);
  46. _bh_log_vprintf(fmt, ap);
  47. va_end(ap);
  48. }
  49. /**
  50. * Return true if the given tag is enabled by the configuration.
  51. *
  52. * @param tag the tag (or prefix) of the log message.
  53. *
  54. * @return true if the log should be enabled.
  55. */
  56. static bool is_log_enabled(const char *tag)
  57. {
  58. /* Print all non-verbose or verbose logs whose levels are less than
  59. or equal to the configured verbose level. */
  60. return tag[0] != 'V' || tag[1] - '0' <= log_verbose_level;
  61. }
  62. /**
  63. * Helper function for converting "..." to va_list.
  64. */
  65. static void bh_log_emit_helper(const char *fmt, ...)
  66. {
  67. va_list ap;
  68. va_start(ap, fmt);
  69. bh_log_emit(fmt, ap);
  70. va_end(ap);
  71. }
  72. extern size_t _bh_time_strftime(char *s, size_t max, const char *format,
  73. int64 time);
  74. void _bh_log_vprintf(const char *fmt, va_list ap)
  75. {
  76. korp_tid self = vm_self_thread();
  77. /* Try to own the log stream and start the log output. */
  78. if (self != cur_logging_thread) {
  79. vm_mutex_lock(&log_stream_lock);
  80. cur_logging_thread = self;
  81. if (fmt && (cur_log_enabled = is_log_enabled(fmt))) {
  82. char buf[32];
  83. bh_time_strftime(buf, 32, "%Y-%m-%d %H:%M:%S",
  84. (int64)bh_time_get_millisecond_from_1970());
  85. bh_log_emit_helper("\n[%s - %X]: ", buf, (int) self);
  86. /* Strip the "Vn." prefix. */
  87. if (fmt && strlen(fmt) >= 3 && fmt[0] == 'V' && fmt[1] && fmt[2])
  88. fmt += 3;
  89. }
  90. }
  91. if (cur_log_enabled && fmt)
  92. bh_log_emit(fmt, ap);
  93. }
  94. void _bh_log_commit()
  95. {
  96. if (vm_self_thread() != cur_logging_thread)
  97. /* Ignore the single commit without printing anything. */
  98. return;
  99. cur_logging_thread = INVALID_THREAD_ID;
  100. vm_mutex_unlock(&log_stream_lock);
  101. }
  102. void _bh_log(const char *tag, const char *file, int line, const char *fmt, ...)
  103. {
  104. va_list ap;
  105. if (tag)
  106. _bh_log_printf(tag);
  107. if (file)
  108. _bh_log_printf("%s:%d", file, line);
  109. va_start(ap, fmt);
  110. _bh_log_vprintf(fmt, ap);
  111. va_end(ap);
  112. _bh_log_commit();
  113. }
  114. #if defined(BH_DEBUG)
  115. BH_STATIC char com_switchs[LOG_COM_MAX]; /* 0: off; 1: on */
  116. BH_STATIC char *com_names[LOG_COM_MAX] = {"app_manager", "gc", "hmc", "utils",
  117. "verifier_jeff", "vmcore_jeff"};
  118. BH_STATIC int com_find_name(const char **com)
  119. {
  120. int i;
  121. const char *c, *name;
  122. for (i = 0; i < LOG_COM_MAX; i++) {
  123. c = *com;
  124. name = com_names[i];
  125. while (*name != '\0') {
  126. if (*c != *name)
  127. break;
  128. c++;
  129. name++;
  130. }
  131. if (*name == '\0') {
  132. *com = c;
  133. return i;
  134. }
  135. }
  136. return LOG_COM_MAX;
  137. }
  138. void log_parse_coms(const char *coms)
  139. {
  140. int i;
  141. for (i = LOG_COM_MAX; i--; )
  142. com_switchs[i] = 0;
  143. com_switchs[LOG_COM_UTILS] = 1; /* utils is a common part */
  144. if (coms == NULL)
  145. return;
  146. while (*coms != '\0') {
  147. i = com_find_name(&coms);
  148. if (i == LOG_COM_MAX)
  149. break;
  150. com_switchs[i] = 1;
  151. if (*coms == '\0')
  152. return;
  153. if (*coms != ';')
  154. break;
  155. /* *com == ';' */
  156. coms++;
  157. }
  158. /* Log the message without aborting. */
  159. LOG_DEBUG(LOG_COM_UTILS, "The component names for logging are not right: %s.", coms);
  160. }
  161. int bh_log_dcom_is_enabled(int component)
  162. {
  163. return com_switchs[component];
  164. }
  165. #endif /* defined(BH_DEBUG) */