wasm_log.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_log.h"
  6. #include "wasm_platform_log.h"
  7. #include "wasm_thread.h"
  8. /**
  9. * The verbose level of the log system. Only those verbose logs whose
  10. * levels are less than or equal to this value are outputed.
  11. */
  12. static int log_verbose_level;
  13. /**
  14. * The lock for protecting the global output stream of logs.
  15. */
  16. static korp_mutex log_stream_lock;
  17. int
  18. _wasm_log_init ()
  19. {
  20. log_verbose_level = 1;
  21. return ws_mutex_init (&log_stream_lock, false);
  22. }
  23. void
  24. _wasm_log_set_verbose_level (int level)
  25. {
  26. log_verbose_level = level;
  27. }
  28. bool
  29. _wasm_log_begin (int level)
  30. {
  31. korp_tid self;
  32. if (level > log_verbose_level) {
  33. return false;
  34. }
  35. /* Try to own the log stream and start the log output. */
  36. ws_mutex_lock (&log_stream_lock);
  37. self = ws_self_thread ();
  38. wasm_printf ("[%X]: ", (int)self);
  39. return true;
  40. }
  41. void
  42. _wasm_log_vprintf (const char *fmt, va_list ap)
  43. {
  44. wasm_vprintf (fmt, ap);
  45. }
  46. void
  47. _wasm_log_printf (const char *fmt, ...)
  48. {
  49. va_list ap;
  50. va_start (ap, fmt);
  51. _wasm_log_vprintf (fmt, ap);
  52. va_end (ap);
  53. }
  54. void
  55. _wasm_log_end ()
  56. {
  57. ws_mutex_unlock (&log_stream_lock);
  58. }
  59. void
  60. _wasm_log (int level, const char *file, int line,
  61. const char *fmt, ...)
  62. {
  63. if (_wasm_log_begin (level)) {
  64. va_list ap;
  65. if (file)
  66. _wasm_log_printf ("%s:%d ", file, line);
  67. va_start (ap, fmt);
  68. _wasm_log_vprintf (fmt, ap);
  69. va_end (ap);
  70. _wasm_log_end ();
  71. }
  72. }