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

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