bh_log.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.h
  7. * @date Tue Nov 8 18:19:10 2011
  8. *
  9. * @brief This log system supports wrapping multiple outputs into one
  10. * log message. This is useful for outputting variable-length logs
  11. * without additional memory overhead (the buffer for concatenating
  12. * the message), e.g. exception stack trace, which cannot be printed
  13. * by a single log calling without the help of an additional buffer.
  14. * Avoiding additional memory buffer is useful for resource-constraint
  15. * systems. It can minimize the impact of log system on applications
  16. * and logs can be printed even when no enough memory is available.
  17. * Functions with prefix "_" are private functions. Only macros that
  18. * are not start with "_" are exposed and can be used.
  19. */
  20. #ifndef _BH_LOG_H
  21. #define _BH_LOG_H
  22. #include <stdarg.h>
  23. #include "bh_types.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * The following functions are the primitive operations of this log
  29. * system. A normal usage of the log system is to call bh_log_printf
  30. * or bh_log_vprintf one or multiple times and then call bh_log_commit
  31. * to wrap (mark) the previous outputs into one log message. The
  32. * bh_log and macros LOG_ERROR etc. can be used to output log messages
  33. * that can be wrapped into one log calling.
  34. */
  35. int _bh_log_init(void);
  36. void _bh_log_set_verbose_level(int level);
  37. void _bh_log_printf(const char *fmt, ...);
  38. void _bh_log_vprintf(const char *fmt, va_list ap);
  39. void _bh_log_commit(void);
  40. #if WASM_ENABLE_LOG != 0
  41. # define bh_log_init() _bh_log_init ()
  42. # define bh_log_set_verbose_level(l) _bh_log_set_verbose_level (l)
  43. # define bh_log_printf(...) _bh_log_printf (__VA_ARGS__)
  44. # define bh_log_vprintf(...) _bh_log_vprintf (__VA_ARGS__)
  45. # define bh_log_commit() _bh_log_commit ()
  46. #else /* WASM_ENABLE_LOG != 0 */
  47. # define bh_log_init() 0
  48. # define bh_log_set_verbose_level(l) (void)0
  49. # define bh_log_printf(...) (void)0
  50. # define bh_log_vprintf(...) (void)0
  51. # define bh_log_commit() (void)0
  52. #endif /* WASM_ENABLE_LOG != 0 */
  53. void _bh_log(const char *tag, const char *file, int line, const char *fmt, ...);
  54. /* Always print fatal message */
  55. # define LOG_FATAL(...) _bh_log ("V0.", NULL, 0, __VA_ARGS__)
  56. #if WASM_ENABLE_LOG != 0
  57. # define LOG_ERROR(...) _bh_log ("V1.", NULL, 0, __VA_ARGS__)
  58. # define LOG_WARNING(...) _bh_log ("V2.", NULL, 0, __VA_ARGS__)
  59. # define LOG_INFO_RELEASE(...) _bh_log ("V3.", NULL, 0, __VA_ARGS__)
  60. # define LOG_INFO_APP_DEV(...) _bh_log ("V4.", NULL, 0, __VA_ARGS__)
  61. # define LOG_VERBOSE(...) _bh_log ("V5.", NULL, 0, __VA_ARGS__)
  62. # if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  63. # define LOG_PROFILE(...) _bh_log ("V3.", NULL, 0, __VA_ARGS__)
  64. # else
  65. # define LOG_PROFILE(...) (void)0
  66. #endif
  67. # if BEIHAI_ENABLE_QUEUE_PROFILING != 0
  68. # define LOG_QUEUE_PROFILE(...) _bh_log ("V3.", NULL, 0, __VA_ARGS__)
  69. # else
  70. # define LOG_QUEUE_PROFILE(...) (void)0
  71. #endif
  72. # if BEIHAI_ENABLE_GC_STAT_PROFILING != 0
  73. # define LOG_GC_STAT_PROFILE(...) _bh_log ("V3.", NULL, 0, __VA_ARGS__)
  74. # else
  75. # define LOG_GC_STAT_PROFILE(...) (void)0
  76. #endif
  77. #else /* WASM_ENABLE_LOG != 0 */
  78. # define LOG_ERROR(...) (void)0
  79. # define LOG_WARNING(...) (void)0
  80. # define LOG_INFO_APP_DEV(...) (void)0
  81. # define LOG_INFO_RELEASE(...) (void)0
  82. # define LOG_VERBOSE(...) (void)0
  83. # define LOG_PROFILE(...) (void)0
  84. # define LOG_QUEUE_PROFILE(...) (void)0
  85. # define LOG_GC_STAT_PROFILE(...) (void)0
  86. #endif /* WASM_ENABLE_LOG != 0 */
  87. #define LOG_PROFILE_INSTANCE_HEAP_CREATED(heap) \
  88. LOG_PROFILE ("PROF.INSTANCE.HEAP_CREATED: HEAP=%08X", heap)
  89. #define LOG_PROFILE_INSTANCE_THREAD_STARTED(heap) \
  90. LOG_PROFILE ("PROF.INSTANCE.THREAD_STARTED: HEAP=%08X", heap)
  91. #define LOG_PROFILE_HEAP_ALLOC(heap, size) \
  92. LOG_PROFILE ("PROF.HEAP.ALLOC: HEAP=%08X SIZE=%d", heap, size)
  93. #define LOG_PROFILE_HEAP_FREE(heap, size) \
  94. LOG_PROFILE ("PROF.HEAP.FREE: HEAP=%08X SIZE=%d", heap, size)
  95. #define LOG_PROFILE_HEAP_NEW(heap, size) \
  96. LOG_PROFILE ("PROF.HEAP.NEW: HEAP=%08X SIZE=%d", heap, size)
  97. #define LOG_PROFILE_HEAP_GC(heap, size) \
  98. LOG_PROFILE ("PROF.HEAP.GC: HEAP=%08X SIZE=%d", heap, size)
  99. #define LOG_PROFILE_STACK_PUSH(used) \
  100. LOG_PROFILE ("PROF.STACK.PUSH: USED=%d", used)
  101. #define LOG_PROFILE_STACK_POP() \
  102. LOG_PROFILE ("PROF.STACK.POP")
  103. /* Please add your component ahead of LOG_COM_MAX */
  104. enum {
  105. LOG_COM_APP_MANAGER = 0,
  106. LOG_COM_GC,
  107. LOG_COM_HMC,
  108. LOG_COM_UTILS,
  109. LOG_COM_VERIFIER_JEFF,
  110. LOG_COM_VMCORE_JEFF,
  111. LOG_COM_MAX
  112. };
  113. #if defined(BH_DEBUG)
  114. void log_parse_coms(const char *coms);
  115. int bh_log_dcom_is_enabled(int component);
  116. #define LOG_DEBUG(component, ...) do { \
  117. if (bh_log_dcom_is_enabled (component)) \
  118. _bh_log ("V6: ", __FILE__, __LINE__, __VA_ARGS__); \
  119. } while (0)
  120. #else /* defined(BH_DEBUG) */
  121. #define LOG_DEBUG(component, ...) (void)0
  122. #endif /* defined(BH_DEBUG) */
  123. #ifdef __cplusplus
  124. }
  125. #endif
  126. #endif /* _BH_LOG_H */