Geen omschrijving
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <stdio.h>
  19. #include <string.h>
  20. #include "hardware/watchdog.h"
  21. #include "main.h"
  22. #include "usb_cdc.h"
  23. #include "ring.h"
  24. #include "log.h"
  25. static uint8_t log_buff[4096] = {0};
  26. static struct ring_buffer log_rb = RB_INIT(log_buff, sizeof(log_buff), 1);
  27. static uint8_t line_buff[256] = {0};
  28. static volatile bool got_input = false;
  29. static void add_to_log(const void *buff, size_t len) {
  30. rb_add(&log_rb, buff, len);
  31. }
  32. struct ring_buffer *log_get(void) {
  33. return &log_rb;
  34. }
  35. static void log_dump_to_x(void (*write)(const void *, size_t)) {
  36. if (rb_len(&log_rb) == 0) {
  37. return;
  38. }
  39. int l = snprintf((char *)line_buff, sizeof(line_buff), "\r\n\r\nbuffered log output:\r\n");
  40. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  41. write(line_buff, l);
  42. }
  43. rb_dump(&log_rb, write, 0);
  44. l = snprintf((char *)line_buff, sizeof(line_buff), "\r\n\r\nlive log:\r\n");
  45. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  46. write(line_buff, l);
  47. }
  48. }
  49. void log_dump_to_usb(void) {
  50. log_dump_to_x(usb_cdc_write);
  51. }
  52. void debug_log_va(bool do_log, const char *format, va_list args) {
  53. int l = vsnprintf((char *)line_buff, sizeof(line_buff), format, args);
  54. if (l < 0) {
  55. // encoding error
  56. l = snprintf((char *)line_buff, sizeof(line_buff), "%s: encoding error\r\n", __func__);
  57. } else if (l >= (ssize_t)sizeof(line_buff)) {
  58. // not enough space for string
  59. l = snprintf((char *)line_buff, sizeof(line_buff), "%s: message too long (%d)\r\n", __func__, l);
  60. }
  61. if ((l > 0) && (l <= (int)sizeof(line_buff))) {
  62. usb_cdc_write(line_buff, l);
  63. if (do_log) {
  64. add_to_log(line_buff, l);
  65. }
  66. }
  67. }
  68. void debug_log(bool do_log, const char* format, ...) {
  69. va_list args;
  70. va_start(args, format);
  71. debug_log_va(do_log, format, args);
  72. va_end(args);
  73. }
  74. void debug_handle_input(const void *buff, size_t len) {
  75. (void)buff;
  76. if (len > 0) {
  77. got_input = true;
  78. }
  79. }
  80. void debug_wait_input(const char *format, ...) {
  81. va_list args;
  82. va_start(args, format);
  83. debug_log_va(false, format, args);
  84. va_end(args);
  85. got_input = false;
  86. usb_cdc_set_reroute(true);
  87. while (!got_input) {
  88. main_loop_hw();
  89. }
  90. usb_cdc_set_reroute(false);
  91. }