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

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