log_minimal.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2019 Intel Corporation.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/printk.h>
  7. #include <ctype.h>
  8. #include <logging/log.h>
  9. #include <sys/printk.h>
  10. #define HEXDUMP_BYTES_IN_LINE 8U
  11. void z_log_minimal_printk(const char *fmt, ...)
  12. {
  13. va_list ap;
  14. va_start(ap, fmt);
  15. vprintk(fmt, ap);
  16. va_end(ap);
  17. }
  18. void z_log_minimal_vprintk(const char *fmt, va_list ap)
  19. {
  20. vprintk(fmt, ap);
  21. }
  22. static void minimal_hexdump_line_print(const char *data, size_t length)
  23. {
  24. for (size_t i = 0U; i < HEXDUMP_BYTES_IN_LINE; i++) {
  25. if (i < length) {
  26. printk("%02x ", (unsigned char)data[i] & 0xFFu);
  27. } else {
  28. printk(" ");
  29. }
  30. }
  31. printk("|");
  32. for (size_t i = 0U; i < HEXDUMP_BYTES_IN_LINE; i++) {
  33. if (i < length) {
  34. unsigned char c = data[i];
  35. printk("%c", isprint((int)c) != 0 ? c : '.');
  36. } else {
  37. printk(" ");
  38. }
  39. }
  40. printk("\n");
  41. }
  42. void z_log_minimal_hexdump_print(int level, const void *data, size_t size)
  43. {
  44. const char *data_buffer = (const char *)data;
  45. while (size > 0U) {
  46. printk("%c: ", z_log_minimal_level_to_char(level));
  47. minimal_hexdump_line_print(data_buffer, size);
  48. if (size < HEXDUMP_BYTES_IN_LINE) {
  49. break;
  50. }
  51. size -= HEXDUMP_BYTES_IN_LINE;
  52. data_buffer += HEXDUMP_BYTES_IN_LINE;
  53. }
  54. }