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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * log.c
  3. */
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include "pico/stdlib.h"
  7. #include "hardware/watchdog.h"
  8. #include "ff.h"
  9. #include "config.h"
  10. #include "usb.h"
  11. #include "usb_cdc.h"
  12. #include "log.h"
  13. static char log_buff[4096];
  14. static size_t head = 0, tail = 0;
  15. static bool full = false;
  16. static bool got_input = false;
  17. static void add_to_log(const char *buff, int len) {
  18. for (int i = 0; i < len; i++) {
  19. log_buff[head] = buff[i];
  20. if (full && (++tail == sizeof(log_buff))) {
  21. tail = 0;
  22. }
  23. if (++(head) == sizeof(log_buff)) {
  24. head = 0;
  25. }
  26. full = (head == tail);
  27. }
  28. }
  29. void log_dump_to_usb(void) {
  30. if (head == tail) {
  31. return;
  32. }
  33. char buff[32];
  34. int l = snprintf(buff, sizeof(buff), "\r\n\r\nbuffered log output:\r\n");
  35. if ((l > 0) && (l <= (int)sizeof(buff))) {
  36. usb_cdc_write(buff, l);
  37. }
  38. if (head > tail) {
  39. usb_cdc_write(log_buff + tail, head - tail);
  40. } else {
  41. usb_cdc_write(log_buff + tail, sizeof(log_buff) - tail);
  42. usb_cdc_write(log_buff, head);
  43. }
  44. l = snprintf(buff, sizeof(buff), "\r\n\r\nlive log:\r\n");
  45. if ((l > 0) && (l <= (int)sizeof(buff))) {
  46. usb_cdc_write(buff, l);
  47. }
  48. }
  49. void log_dump_to_disk(void) {
  50. FIL file;
  51. FRESULT res = f_open(&file, "log.txt", FA_CREATE_ALWAYS | FA_WRITE);
  52. if (res != FR_OK) {
  53. debug("error: f_open returned %d", res);
  54. return;
  55. }
  56. UINT bw;
  57. if (head > tail) {
  58. res = f_write(&file, log_buff + tail, head - tail, &bw);
  59. if ((res != FR_OK) || (bw != head - tail)) {
  60. debug("error: f_write (A) returned %d", res);
  61. }
  62. } else if (head < tail) {
  63. res = f_write(&file, log_buff + tail, sizeof(log_buff) - tail, &bw);
  64. if ((res != FR_OK) || (bw != sizeof(log_buff) - tail)) {
  65. debug("error: f_write (B) returned %d", res);
  66. } else {
  67. res = f_write(&file, log_buff, head, &bw);
  68. if ((res != FR_OK) || (bw != head)) {
  69. debug("error: f_write (C) returned %d", res);
  70. }
  71. }
  72. }
  73. res = f_close(&file);
  74. if (res != FR_OK) {
  75. debug("error: f_close returned %d", res);
  76. }
  77. }
  78. static int format_debug_log(char *buff, size_t len, const char *format, va_list args) {
  79. int l = vsnprintf(buff, len, format, args);
  80. if (l < 0) {
  81. // encoding error
  82. l = snprintf(buff, len, "%s: encoding error\r\n", __func__);
  83. } else if (l >= (ssize_t)len) {
  84. // not enough space for string
  85. l = snprintf(buff, len, "%s: message too long (%d)\r\n", __func__, l);
  86. }
  87. return l;
  88. }
  89. void debug_log(bool log, const char* format, ...) {
  90. static char line_buff[512];
  91. va_list args;
  92. va_start(args, format);
  93. int l = format_debug_log(line_buff, sizeof(line_buff), format, args);
  94. va_end(args);
  95. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  96. usb_cdc_write(line_buff, l);
  97. if (log) {
  98. add_to_log(line_buff, l);
  99. }
  100. }
  101. }
  102. void debug_handle_input(char *buff, uint32_t len) {
  103. (void)buff;
  104. if (len > 0) {
  105. got_input = true;
  106. }
  107. }
  108. void debug_wait_input(const char *format, ...) {
  109. static char line_buff[512];
  110. va_list args;
  111. va_start(args, format);
  112. int l = format_debug_log(line_buff, sizeof(line_buff), format, args);
  113. va_end(args);
  114. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  115. usb_cdc_write(line_buff, l);
  116. }
  117. got_input = false;
  118. usb_cdc_set_reroute(true);
  119. while (!got_input) {
  120. watchdog_update();
  121. usb_run();
  122. }
  123. usb_cdc_set_reroute(false);
  124. }