bh_log.h 5.7 KB

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