ZuluSCSI_log.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // Helpers for log messages.
  22. #pragma once
  23. #include <stdint.h>
  24. #include <stddef.h>
  25. // Get total number of bytes that have been written to log
  26. uint32_t log_get_buffer_len();
  27. // Get log as a string.
  28. // If startpos is given, continues log reading from previous position and updates the position.
  29. // If available is given, number of bytes available is written there.
  30. const char *log_get_buffer(uint32_t *startpos, uint32_t *available = nullptr);
  31. // Whether to enable debug messages
  32. extern bool g_log_debug;
  33. // Firmware version string
  34. extern const char *g_log_firmwareversion;
  35. // Log string
  36. void log_raw(const char *str);
  37. // Log byte as hex
  38. void log_raw(uint8_t value);
  39. // Log integer as hex
  40. void log_raw(uint32_t value);
  41. // Log integer as hex
  42. void log_raw(uint64_t value);
  43. // Log integer as decimal
  44. void log_raw(int value);
  45. // Log array of bytes
  46. struct bytearray {
  47. bytearray(const uint8_t *data, size_t len): data(data), len(len) {}
  48. const uint8_t *data;
  49. size_t len;
  50. };
  51. void log_raw(bytearray array);
  52. inline void log_raw()
  53. {
  54. // End of template recursion
  55. }
  56. extern "C" unsigned long millis();
  57. // Variadic template for printing multiple items
  58. template<typename T, typename T2, typename... Rest>
  59. inline void log_raw(T first, T2 second, Rest... rest)
  60. {
  61. log_raw(first);
  62. log_raw(second);
  63. log_raw(rest...);
  64. }
  65. // Format a complete log message
  66. template<typename... Params>
  67. inline void logmsg(Params... params)
  68. {
  69. log_raw("[", (int)millis(), "ms] ");
  70. log_raw(params...);
  71. log_raw("\r\n");
  72. }
  73. // Format a complete debug message
  74. template<typename... Params>
  75. inline void dbgmsg(Params... params)
  76. {
  77. if (g_log_debug)
  78. {
  79. log_raw("[", (int)millis(), "ms] DBG ");
  80. log_raw(params...);
  81. log_raw("\r\n");
  82. }
  83. }