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.8KB

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