esp_log.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <ctype.h>
  14. #include "esp_log.h"
  15. #include "hosted_os_adapter.h"
  16. #define BYTES_PER_LINE 16
  17. uint32_t esp_log_early_timestamp(void)
  18. {
  19. return 0;
  20. }
  21. void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level)
  22. {
  23. if (buff_len == 0) {
  24. return;
  25. }
  26. char temp_buffer[BYTES_PER_LINE + 3]; //for not-byte-accessible memory
  27. const char *ptr_line;
  28. //format: field[length]
  29. // ADDR[10]+" "+DATA_HEX[8*3]+" "+DATA_HEX[8*3]+" |"+DATA_CHAR[8]+"|"
  30. char hd_buffer[2 + sizeof(void *) * 2 + 3 + BYTES_PER_LINE * 3 + 1 + 3 + BYTES_PER_LINE + 1 + 1];
  31. char *ptr_hd;
  32. int bytes_cur_line;
  33. do {
  34. if (buff_len > BYTES_PER_LINE) {
  35. bytes_cur_line = BYTES_PER_LINE;
  36. } else {
  37. bytes_cur_line = buff_len;
  38. }
  39. //use memcpy to get around alignment issue
  40. memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4);
  41. ptr_line = temp_buffer;
  42. ptr_hd = hd_buffer;
  43. ptr_hd += sprintf(ptr_hd, "%p ", buffer);
  44. for (int i = 0; i < BYTES_PER_LINE; i ++) {
  45. if ((i & 7) == 0) {
  46. ptr_hd += sprintf(ptr_hd, " ");
  47. }
  48. if (i < bytes_cur_line) {
  49. ptr_hd += sprintf(ptr_hd, " %02x", (unsigned char) ptr_line[i]);
  50. } else {
  51. ptr_hd += sprintf(ptr_hd, " ");
  52. }
  53. }
  54. ptr_hd += sprintf(ptr_hd, " |");
  55. for (int i = 0; i < bytes_cur_line; i ++) {
  56. if (isprint((int)ptr_line[i])) {
  57. ptr_hd += sprintf(ptr_hd, "%c", ptr_line[i]);
  58. } else {
  59. ptr_hd += sprintf(ptr_hd, ".");
  60. }
  61. }
  62. ptr_hd += sprintf(ptr_hd, "|");
  63. ESP_LOG_LEVEL(log_level, tag, "%s", hd_buffer);
  64. buffer = (uint8_t *)buffer + bytes_cur_line;
  65. buff_len -= bytes_cur_line;
  66. } while (buff_len);
  67. }