wasm_log.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /**
  6. * @brief This log system supports wrapping multiple outputs into one
  7. * log message. This is useful for outputting variable-length logs
  8. * without additional memory overhead (the buffer for concatenating
  9. * the message), e.g. exception stack trace, which cannot be printed
  10. * by a single log calling without the help of an additional buffer.
  11. * Avoiding additional memory buffer is useful for resource-constraint
  12. * systems. It can minimize the impact of log system on applications
  13. * and logs can be printed even when no enough memory is available.
  14. * Functions with prefix "_" are private functions. Only macros that
  15. * are not start with "_" are exposed and can be used.
  16. */
  17. #ifndef _WASM_LOG_H
  18. #define _WASM_LOG_H
  19. #include "bh_platform.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /**
  24. * The following functions are the primitive operations of this log system.
  25. * A normal usage of the log system is to call wasm_log_begin and then call
  26. * wasm_log_printf or wasm_log_vprintf one or multiple times and then call
  27. * wasm_log_end to wrap (mark) the previous outputs into one log message.
  28. * The wasm_log and macros LOG_ERROR etc. can be used to output log messages
  29. * by one log calling.
  30. */
  31. int _wasm_log_init (void);
  32. void _wasm_log_set_verbose_level (int level);
  33. bool _wasm_log_begin (int level);
  34. void _wasm_log_printf (const char *fmt, ...);
  35. void _wasm_log_vprintf (const char *fmt, va_list ap);
  36. void _wasm_log_end (void);
  37. void _wasm_log (int level, const char *file, int line,
  38. const char *fmt, ...);
  39. #if WASM_ENABLE_LOG != 0
  40. # define wasm_log_init() _wasm_log_init ()
  41. # define wasm_log_set_verbose_level(l) _wasm_log_set_verbose_level (l)
  42. # define wasm_log_begin(l) _wasm_log_begin (l)
  43. # define wasm_log_printf(...) _wasm_log_printf (__VA_ARGS__)
  44. # define wasm_log_vprintf(...) _wasm_log_vprintf (__VA_ARGS__)
  45. # define wasm_log_end() _wasm_log_end ()
  46. # define wasm_log(...) _wasm_log (__VA_ARGS__)
  47. #else /* WASM_ENABLE_LOG != 0 */
  48. # define wasm_log_init() 0
  49. # define wasm_log_set_verbose_level(l) (void)0
  50. # define wasm_log_begin() false
  51. # define wasm_log_printf(...) (void)0
  52. # define wasm_log_vprintf(...) (void)0
  53. # define wasm_log_end() (void)0
  54. # define wasm_log(...) (void)0
  55. #endif /* WASM_ENABLE_LOG != 0 */
  56. #define LOG_ERROR(...) wasm_log (0, NULL, 0, __VA_ARGS__)
  57. #define LOG_WARNING(...) wasm_log (1, NULL, 0, __VA_ARGS__)
  58. #define LOG_VERBOSE(...) wasm_log (2, NULL, 0, __VA_ARGS__)
  59. #if defined(WASM_DEBUG)
  60. # define LOG_DEBUG(...) _wasm_log (1, __FILE__, __LINE__, __VA_ARGS__)
  61. #else /* defined(WASM_DEBUG) */
  62. # define LOG_DEBUG(...) (void)0
  63. #endif /* defined(WASM_DEBUG) */
  64. #define LOG_PROFILE_HEAP_GC(heap, size) \
  65. LOG_VERBOSE("PROF.HEAP.GC: HEAP=%08X SIZE=%d", heap, size)
  66. #ifdef __cplusplus
  67. }
  68. #endif
  69. #endif /* _WASM_LOG_H */