ZuluSCSI_log.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
  3. * Copyright (c) 2023 joshua stein <jcs@jcs.org>
  4. *
  5. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  6. *
  7. * https://www.gnu.org/licenses/gpl-3.0.html
  8. * ----
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version. 
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. * GNU General Public License for more details. 
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  21. **/
  22. // Helpers for log messages.
  23. #pragma once
  24. #include <stdint.h>
  25. #include <stddef.h>
  26. // Get total number of bytes that have been written to log
  27. uint32_t log_get_buffer_len();
  28. // Get log as a string.
  29. // If startpos is given, continues log reading from previous position and updates the position.
  30. // If available is given, number of bytes available is written there.
  31. const char *log_get_buffer(uint32_t *startpos, uint32_t *available = nullptr);
  32. // Whether to enable debug messages
  33. extern "C" bool g_log_debug;
  34. // Firmware version string
  35. extern const char *g_log_firmwareversion;
  36. // Log string
  37. void log_raw(const char *str);
  38. // Log byte as hex
  39. void log_raw(uint8_t value);
  40. // Log integer as hex
  41. void log_raw(uint32_t value);
  42. // Log integer as hex
  43. void log_raw(uint64_t value);
  44. // Log integer as decimal
  45. void log_raw(int value);
  46. // Log array of bytes
  47. struct bytearray {
  48. bytearray(const uint8_t *data, size_t len): data(data), len(len) {}
  49. const uint8_t *data;
  50. size_t len;
  51. };
  52. void log_raw(bytearray array);
  53. inline void log_raw()
  54. {
  55. // End of template recursion
  56. }
  57. extern "C" unsigned long millis();
  58. // Variadic template for printing multiple items
  59. template<typename T, typename T2, typename... Rest>
  60. inline void log_raw(T first, T2 second, Rest... rest)
  61. {
  62. log_raw(first);
  63. log_raw(second);
  64. log_raw(rest...);
  65. }
  66. // Format a complete log message
  67. template<typename... Params>
  68. inline void logmsg(Params... params)
  69. {
  70. log_raw("[", (int)millis(), "ms] ");
  71. log_raw(params...);
  72. log_raw("\r\n");
  73. }
  74. // Format a complete debug message
  75. template<typename... Params>
  76. inline void dbgmsg(Params... params)
  77. {
  78. if (g_log_debug)
  79. {
  80. log_raw("[", (int)millis(), "ms] DBG ");
  81. log_raw(params...);
  82. log_raw("\r\n");
  83. }
  84. }
  85. #ifdef NETWORK_DEBUG_LOGGING
  86. #ifdef __cplusplus
  87. extern "C" {
  88. #endif
  89. // Log long hex string
  90. void logmsg_buf(const unsigned char *buf, unsigned long size);
  91. // Log long hex string
  92. void dbgmsg_buf(const unsigned char *buf, unsigned long size);
  93. // Log formatted string
  94. void logmsg_f(const char *format, ...);
  95. // Log formatted string
  96. void dbgmsg_f(const char *format, ...);
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif