bh_log.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #ifndef BH_PLATFORM_ANDROID
  50. /**
  51. * Return true if the given tag is enabled by the configuration.
  52. *
  53. * @param tag the tag (or prefix) of the log message.
  54. *
  55. * @return true if the log should be enabled.
  56. */
  57. static bool is_log_enabled(const char *tag)
  58. {
  59. /* Print all non-verbose or verbose logs whose levels are less than
  60. or equal to the configured verbose level. */
  61. return tag[0] != 'V' || tag[1] - '0' <= log_verbose_level;
  62. }
  63. /**
  64. * Helper function for converting "..." to va_list.
  65. */
  66. static void bh_log_emit_helper(const char *fmt, ...)
  67. {
  68. va_list ap;
  69. va_start(ap, fmt);
  70. bh_log_emit(fmt, ap);
  71. va_end(ap);
  72. }
  73. #endif
  74. extern size_t _bh_time_strftime(char *s, size_t max, const char *format,
  75. int64 time);
  76. void _bh_log_vprintf(const char *fmt, va_list ap)
  77. {
  78. #ifndef BH_PLATFORM_ANDROID
  79. korp_tid self = vm_self_thread();
  80. /* Try to own the log stream and start the log output. */
  81. if (self != cur_logging_thread) {
  82. vm_mutex_lock(&log_stream_lock);
  83. cur_logging_thread = self;
  84. if (fmt && (cur_log_enabled = is_log_enabled(fmt))) {
  85. char buf[32];
  86. bh_time_strftime(buf, 32, "%Y-%m-%d %H:%M:%S",
  87. (int64)bh_time_get_millisecond_from_1970());
  88. bh_log_emit_helper("\n[%s - %X]: ", buf, (int) self);
  89. /* Strip the "Vn." prefix. */
  90. if (fmt && strlen(fmt) >= 3 && fmt[0] == 'V' && fmt[1] && fmt[2])
  91. fmt += 3;
  92. }
  93. }
  94. #else
  95. // since we are using android log, do not worry about that
  96. cur_log_enabled = true;
  97. #endif//BH_PLATFORM_ANDROID
  98. if (cur_log_enabled && fmt)
  99. bh_log_emit(fmt, ap);
  100. }
  101. void _bh_log_commit()
  102. {
  103. if (vm_self_thread() != cur_logging_thread)
  104. /* Ignore the single commit without printing anything. */
  105. return;
  106. cur_logging_thread = INVALID_THREAD_ID;
  107. vm_mutex_unlock(&log_stream_lock);
  108. }
  109. void _bh_log(const char *tag, const char *file, int line, const char *fmt, ...)
  110. {
  111. va_list ap;
  112. #ifndef BH_PLATFORM_ANDROID
  113. if (tag)
  114. _bh_log_printf(tag);
  115. if (file)
  116. _bh_log_printf("%s:%d", file, line);
  117. #else
  118. (void)tag;
  119. (void)file;
  120. (void)line;
  121. #endif//BH_PLATFORM_ANDROID
  122. va_start(ap, fmt);
  123. _bh_log_vprintf(fmt, ap);
  124. va_end(ap);
  125. _bh_log_commit();
  126. }
  127. #if defined(BH_DEBUG)
  128. BH_STATIC char com_switchs[LOG_COM_MAX]; /* 0: off; 1: on */
  129. BH_STATIC char *com_names[LOG_COM_MAX] = {"app_manager", "gc", "hmc", "utils",
  130. "verifier_jeff", "vmcore_jeff"};
  131. BH_STATIC int com_find_name(const char **com)
  132. {
  133. int i;
  134. const char *c, *name;
  135. for (i = 0; i < LOG_COM_MAX; i++) {
  136. c = *com;
  137. name = com_names[i];
  138. while (*name != '\0') {
  139. if (*c != *name)
  140. break;
  141. c++;
  142. name++;
  143. }
  144. if (*name == '\0') {
  145. *com = c;
  146. return i;
  147. }
  148. }
  149. return LOG_COM_MAX;
  150. }
  151. void log_parse_coms(const char *coms)
  152. {
  153. int i;
  154. for (i = LOG_COM_MAX; i--; )
  155. com_switchs[i] = 0;
  156. com_switchs[LOG_COM_UTILS] = 1; /* utils is a common part */
  157. if (coms == NULL)
  158. return;
  159. while (*coms != '\0') {
  160. i = com_find_name(&coms);
  161. if (i == LOG_COM_MAX)
  162. break;
  163. com_switchs[i] = 1;
  164. if (*coms == '\0')
  165. return;
  166. if (*coms != ';')
  167. break;
  168. /* *com == ';' */
  169. coms++;
  170. }
  171. /* Log the message without aborting. */
  172. LOG_DEBUG(LOG_COM_UTILS, "The component names for logging are not right: %s.", coms);
  173. }
  174. int bh_log_dcom_is_enabled(int component)
  175. {
  176. return com_switchs[component];
  177. }
  178. #endif /* defined(BH_DEBUG) */