No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

log.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * log.c
  3. *
  4. * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include "pico/stdlib.h"
  21. #include "hardware/watchdog.h"
  22. #include "ff.h"
  23. #include "config.h"
  24. #include "usb.h"
  25. #include "usb_cdc.h"
  26. #include "log.h"
  27. static char log_buff[4096];
  28. static size_t head = 0, tail = 0;
  29. static bool full = false;
  30. static bool got_input = false;
  31. static void add_to_log(const char *buff, int len) {
  32. for (int i = 0; i < len; i++) {
  33. log_buff[head] = buff[i];
  34. if (full && (++tail == sizeof(log_buff))) {
  35. tail = 0;
  36. }
  37. if (++(head) == sizeof(log_buff)) {
  38. head = 0;
  39. }
  40. full = (head == tail);
  41. }
  42. }
  43. void log_dump_to_usb(void) {
  44. if (head == tail) {
  45. return;
  46. }
  47. char buff[32];
  48. int l = snprintf(buff, sizeof(buff), "\r\n\r\nbuffered log output:\r\n");
  49. if ((l > 0) && (l <= (int)sizeof(buff))) {
  50. usb_cdc_write(buff, l);
  51. }
  52. if (head > tail) {
  53. usb_cdc_write(log_buff + tail, head - tail);
  54. } else {
  55. usb_cdc_write(log_buff + tail, sizeof(log_buff) - tail);
  56. usb_cdc_write(log_buff, head);
  57. }
  58. l = snprintf(buff, sizeof(buff), "\r\n\r\nlive log:\r\n");
  59. if ((l > 0) && (l <= (int)sizeof(buff))) {
  60. usb_cdc_write(buff, l);
  61. }
  62. }
  63. void log_dump_to_disk(void) {
  64. FIL file;
  65. FRESULT res = f_open(&file, "log.txt", FA_CREATE_ALWAYS | FA_WRITE);
  66. if (res != FR_OK) {
  67. debug("error: f_open returned %d", res);
  68. return;
  69. }
  70. UINT bw;
  71. if (head > tail) {
  72. res = f_write(&file, log_buff + tail, head - tail, &bw);
  73. if ((res != FR_OK) || (bw != head - tail)) {
  74. debug("error: f_write (A) returned %d", res);
  75. }
  76. } else if (head < tail) {
  77. res = f_write(&file, log_buff + tail, sizeof(log_buff) - tail, &bw);
  78. if ((res != FR_OK) || (bw != sizeof(log_buff) - tail)) {
  79. debug("error: f_write (B) returned %d", res);
  80. } else {
  81. res = f_write(&file, log_buff, head, &bw);
  82. if ((res != FR_OK) || (bw != head)) {
  83. debug("error: f_write (C) returned %d", res);
  84. }
  85. }
  86. }
  87. res = f_close(&file);
  88. if (res != FR_OK) {
  89. debug("error: f_close returned %d", res);
  90. }
  91. }
  92. static int format_debug_log(char *buff, size_t len, const char *format, va_list args) {
  93. int l = vsnprintf(buff, len, format, args);
  94. if (l < 0) {
  95. // encoding error
  96. l = snprintf(buff, len, "%s: encoding error\r\n", __func__);
  97. } else if (l >= (ssize_t)len) {
  98. // not enough space for string
  99. l = snprintf(buff, len, "%s: message too long (%d)\r\n", __func__, l);
  100. }
  101. return l;
  102. }
  103. void debug_log(bool log, const char* format, ...) {
  104. static char line_buff[512];
  105. va_list args;
  106. va_start(args, format);
  107. int l = format_debug_log(line_buff, sizeof(line_buff), format, args);
  108. va_end(args);
  109. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  110. usb_cdc_write(line_buff, l);
  111. if (log) {
  112. add_to_log(line_buff, l);
  113. }
  114. }
  115. }
  116. void debug_handle_input(char *buff, uint32_t len) {
  117. (void)buff;
  118. if (len > 0) {
  119. got_input = true;
  120. }
  121. }
  122. void debug_wait_input(const char *format, ...) {
  123. static char line_buff[512];
  124. va_list args;
  125. va_start(args, format);
  126. int l = format_debug_log(line_buff, sizeof(line_buff), format, args);
  127. va_end(args);
  128. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  129. usb_cdc_write(line_buff, l);
  130. }
  131. got_input = false;
  132. usb_cdc_set_reroute(true);
  133. while (!got_input) {
  134. watchdog_update();
  135. usb_run();
  136. }
  137. usb_cdc_set_reroute(false);
  138. }