log_output_dict.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2018 Nordic Semiconductor ASA
  3. * Copyright (c) 2021 Intel Corporation
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <logging/log.h>
  8. #include <logging/log_ctrl.h>
  9. #include <logging/log_output.h>
  10. #include <logging/log_output_dict.h>
  11. #include <sys/__assert.h>
  12. #include <sys/util.h>
  13. static void buffer_write(log_output_func_t outf, uint8_t *buf, size_t len,
  14. void *ctx)
  15. {
  16. int processed;
  17. do {
  18. processed = outf(buf, len, ctx);
  19. len -= processed;
  20. buf += processed;
  21. } while (len != 0);
  22. }
  23. void log_dict_output_msg2_process(const struct log_output *output,
  24. struct log_msg2 *msg, uint32_t flags)
  25. {
  26. struct log_dict_output_normal_msg_hdr_t output_hdr;
  27. void *source = (void *)log_msg2_get_source(msg);
  28. /* Keep sync with header in struct log_msg2 */
  29. output_hdr.type = MSG_NORMAL;
  30. output_hdr.domain = msg->hdr.desc.domain;
  31. output_hdr.level = msg->hdr.desc.level;
  32. output_hdr.package_len = msg->hdr.desc.package_len;
  33. output_hdr.data_len = msg->hdr.desc.data_len;
  34. output_hdr.timestamp = msg->hdr.timestamp;
  35. output_hdr.source = (source != NULL) ?
  36. (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ?
  37. log_dynamic_source_id(source) :
  38. log_const_source_id(source)) :
  39. 0U;
  40. buffer_write(output->func, (uint8_t *)&output_hdr, sizeof(output_hdr),
  41. (void *)output);
  42. size_t len;
  43. uint8_t *data = log_msg2_get_package(msg, &len);
  44. if (len > 0U) {
  45. buffer_write(output->func, data, len, (void *)output);
  46. }
  47. data = log_msg2_get_data(msg, &len);
  48. if (len > 0U) {
  49. buffer_write(output->func, data, len, (void *)output);
  50. }
  51. log_output_flush(output);
  52. }
  53. void log_dict_output_dropped_process(const struct log_output *output, uint32_t cnt)
  54. {
  55. struct log_dict_output_dropped_msg_t msg;
  56. msg.type = MSG_DROPPED_MSG;
  57. msg.num_dropped_messages = MIN(cnt, 9999);
  58. buffer_write(output->func, (uint8_t *)&msg, sizeof(msg),
  59. (void *)output);
  60. }