bh_log.c 4.9 KB

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