log_backend_native_posix.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2018 Nordic Semiconductor ASA
  3. * Copyright (c) 2018 Intel Corporation
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <stdio.h>
  8. #include <stddef.h>
  9. #include <logging/log_backend.h>
  10. #include <logging/log_backend_std.h>
  11. #include <logging/log_core.h>
  12. #include <logging/log_msg.h>
  13. #include <logging/log_output.h>
  14. #include <irq.h>
  15. #include <arch/posix/posix_trace.h>
  16. #define _STDOUT_BUF_SIZE 256
  17. static char stdout_buff[_STDOUT_BUF_SIZE];
  18. static int n_pend; /* Number of pending characters in buffer */
  19. static void preprint_char(int c)
  20. {
  21. int printnow = 0;
  22. if (c == '\r') {
  23. /* Discard carriage returns */
  24. return;
  25. }
  26. if (c != '\n') {
  27. stdout_buff[n_pend++] = c;
  28. stdout_buff[n_pend] = 0;
  29. } else {
  30. printnow = 1;
  31. }
  32. if (n_pend >= _STDOUT_BUF_SIZE - 1) {
  33. printnow = 1;
  34. }
  35. if (printnow) {
  36. posix_print_trace("%s\n", stdout_buff);
  37. n_pend = 0;
  38. stdout_buff[0] = 0;
  39. }
  40. }
  41. static uint8_t buf[_STDOUT_BUF_SIZE];
  42. static int char_out(uint8_t *data, size_t length, void *ctx)
  43. {
  44. for (size_t i = 0; i < length; i++) {
  45. preprint_char(data[i]);
  46. }
  47. return length;
  48. }
  49. LOG_OUTPUT_DEFINE(log_output_posix, char_out, buf, sizeof(buf));
  50. static void put(const struct log_backend *const backend,
  51. struct log_msg *msg)
  52. {
  53. log_msg_get(msg);
  54. uint32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
  55. if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
  56. if (posix_trace_over_tty(0)) {
  57. flags |= LOG_OUTPUT_FLAG_COLORS;
  58. }
  59. }
  60. if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
  61. flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
  62. }
  63. log_output_msg_process(&log_output_posix, msg, flags);
  64. log_msg_put(msg);
  65. }
  66. static void panic(struct log_backend const *const backend)
  67. {
  68. log_output_flush(&log_output_posix);
  69. }
  70. static void dropped(const struct log_backend *const backend, uint32_t cnt)
  71. {
  72. ARG_UNUSED(backend);
  73. log_output_dropped_process(&log_output_posix, cnt);
  74. }
  75. static void sync_string(const struct log_backend *const backend,
  76. struct log_msg_ids src_level, uint32_t timestamp,
  77. const char *fmt, va_list ap)
  78. {
  79. uint32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
  80. uint32_t key;
  81. if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
  82. flags |= LOG_OUTPUT_FLAG_COLORS;
  83. }
  84. if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
  85. flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
  86. }
  87. key = irq_lock();
  88. log_output_string(&log_output_posix, src_level,
  89. timestamp, fmt, ap, flags);
  90. irq_unlock(key);
  91. }
  92. static void sync_hexdump(const struct log_backend *const backend,
  93. struct log_msg_ids src_level, uint32_t timestamp,
  94. const char *metadata, const uint8_t *data, uint32_t length)
  95. {
  96. uint32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
  97. uint32_t key;
  98. if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
  99. flags |= LOG_OUTPUT_FLAG_COLORS;
  100. }
  101. if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
  102. flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
  103. }
  104. key = irq_lock();
  105. log_output_hexdump(&log_output_posix, src_level, timestamp,
  106. metadata, data, length, flags);
  107. irq_unlock(key);
  108. }
  109. static void process(const struct log_backend *const backend,
  110. union log_msg2_generic *msg)
  111. {
  112. uint32_t flags = log_backend_std_get_flags();
  113. log_output_msg2_process(&log_output_posix, &msg->log, flags);
  114. }
  115. const struct log_backend_api log_backend_native_posix_api = {
  116. .process = IS_ENABLED(CONFIG_LOG2) ? process : NULL,
  117. .put = IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) ? put : NULL,
  118. .put_sync_string = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
  119. sync_string : NULL,
  120. .put_sync_hexdump = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
  121. sync_hexdump : NULL,
  122. .panic = panic,
  123. .dropped = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : dropped,
  124. };
  125. LOG_BACKEND_DEFINE(log_backend_native_posix,
  126. log_backend_native_posix_api,
  127. true);