ZuluSCSI_log.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #include "ZuluSCSI_log.h"
  22. #include "ZuluSCSI_config.h"
  23. #include "ZuluSCSI_platform.h"
  24. const char *g_log_firmwareversion = ZULU_FW_VERSION " " __DATE__ " " __TIME__;
  25. bool g_log_debug = true;
  26. // This memory buffer can be read by debugger and is also saved to zululog.txt
  27. #define LOGBUFMASK (LOGBUFSIZE - 1)
  28. // The log buffer is in special uninitialized RAM section so that it is not reset
  29. // when soft rebooting or jumping from bootloader.
  30. uint32_t g_log_magic;
  31. char g_logbuffer[LOGBUFSIZE + 1];
  32. uint32_t g_logpos;
  33. void log_raw(const char *str)
  34. {
  35. // Keep log from reboot / bootloader if magic matches expected value
  36. if (g_log_magic != 0xAA55AA55)
  37. {
  38. g_log_magic = 0xAA55AA55;
  39. g_logpos = 0;
  40. }
  41. const char *p = str;
  42. while (*p)
  43. {
  44. g_logbuffer[g_logpos & LOGBUFMASK] = *p++;
  45. g_logpos++;
  46. }
  47. // Keep buffer null-terminated
  48. g_logbuffer[g_logpos & LOGBUFMASK] = '\0';
  49. platform_log(str);
  50. }
  51. // Log byte as hex
  52. void log_raw(uint8_t value)
  53. {
  54. const char *nibble = "0123456789ABCDEF";
  55. char hexbuf[5] = {
  56. '0', 'x',
  57. nibble[(value >> 4) & 0xF], nibble[(value >> 0) & 0xF],
  58. 0
  59. };
  60. log_raw(hexbuf);
  61. }
  62. // Log integer as hex
  63. void log_raw(uint32_t value)
  64. {
  65. const char *nibble = "0123456789ABCDEF";
  66. char hexbuf[11] = {
  67. '0', 'x',
  68. nibble[(value >> 28) & 0xF], nibble[(value >> 24) & 0xF],
  69. nibble[(value >> 20) & 0xF], nibble[(value >> 16) & 0xF],
  70. nibble[(value >> 12) & 0xF], nibble[(value >> 8) & 0xF],
  71. nibble[(value >> 4) & 0xF], nibble[(value >> 0) & 0xF],
  72. 0
  73. };
  74. log_raw(hexbuf);
  75. }
  76. // Log integer as hex
  77. void log_raw(uint64_t value)
  78. {
  79. const char *nibble = "0123456789ABCDEF";
  80. char hexbuf[19] = {
  81. '0', 'x',
  82. nibble[(value >> 60) & 0xF], nibble[(value >> 56) & 0xF],
  83. nibble[(value >> 52) & 0xF], nibble[(value >> 48) & 0xF],
  84. nibble[(value >> 44) & 0xF], nibble[(value >> 40) & 0xF],
  85. nibble[(value >> 36) & 0xF], nibble[(value >> 32) & 0xF],
  86. nibble[(value >> 28) & 0xF], nibble[(value >> 24) & 0xF],
  87. nibble[(value >> 20) & 0xF], nibble[(value >> 16) & 0xF],
  88. nibble[(value >> 12) & 0xF], nibble[(value >> 8) & 0xF],
  89. nibble[(value >> 4) & 0xF], nibble[(value >> 0) & 0xF],
  90. 0
  91. };
  92. log_raw(hexbuf);
  93. }
  94. // Log integer as decimal
  95. void log_raw(int value)
  96. {
  97. char decbuf[16] = {0};
  98. char *p = &decbuf[14];
  99. int remainder = (value < 0) ? -value : value;
  100. do
  101. {
  102. *--p = '0' + (remainder % 10);
  103. remainder /= 10;
  104. } while (remainder > 0);
  105. if (value < 0)
  106. {
  107. *--p = '-';
  108. }
  109. log_raw(p);
  110. }
  111. void log_raw(bytearray array)
  112. {
  113. for (size_t i = 0; i < array.len; i++)
  114. {
  115. log_raw(array.data[i]);
  116. log_raw(" ");
  117. if (i > 32)
  118. {
  119. log_raw("... (total ", (int)array.len, ")");
  120. break;
  121. }
  122. }
  123. }
  124. uint32_t log_get_buffer_len()
  125. {
  126. return g_logpos;
  127. }
  128. const char *log_get_buffer(uint32_t *startpos, uint32_t *available)
  129. {
  130. uint32_t default_pos = 0;
  131. if (startpos == NULL)
  132. {
  133. startpos = &default_pos;
  134. }
  135. // Check oldest data available in buffer
  136. uint32_t lag = (g_logpos - *startpos);
  137. if (lag >= LOGBUFSIZE)
  138. {
  139. // If we lose data, skip 512 bytes forward to give us time to transmit
  140. // pending data before new log messages arrive. Also skip to next line
  141. // break to keep formatting consistent.
  142. uint32_t oldest = g_logpos - LOGBUFSIZE + 512;
  143. while (oldest < g_logpos)
  144. {
  145. char c = g_logbuffer[oldest & LOGBUFMASK];
  146. if (c == '\r' || c == '\n') break;
  147. oldest++;
  148. }
  149. if (oldest > g_logpos)
  150. {
  151. oldest = g_logpos;
  152. }
  153. *startpos = oldest;
  154. }
  155. const char *result = &g_logbuffer[*startpos & LOGBUFMASK];
  156. // Calculate number of bytes available
  157. uint32_t len;
  158. if ((g_logpos & LOGBUFMASK) >= (*startpos & LOGBUFMASK))
  159. {
  160. // Can read directly to g_logpos
  161. len = g_logpos - *startpos;
  162. }
  163. else
  164. {
  165. // Buffer wraps, read to end of buffer now and start from beginning on next call.
  166. len = LOGBUFSIZE - (*startpos & LOGBUFMASK);
  167. }
  168. if (available) { *available = len; }
  169. *startpos += len;
  170. return result;
  171. }