wasm_log.h 3.5 KB

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