BlueSCSI_log.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Helpers for log messages.
  2. #pragma once
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. // Get total number of bytes that have been written to log
  6. uint32_t log_get_buffer_len();
  7. // Get log as a string.
  8. // If startpos is given, continues log reading from previous position and updates the position.
  9. // If available is given, number of bytes available is written there.
  10. const char *log_get_buffer(uint32_t *startpos, uint32_t *available = nullptr);
  11. // Whether to enable debug messages
  12. extern bool g_log_debug;
  13. // Enables output test mode
  14. extern bool g_test_mode;
  15. // Firmware version string
  16. extern const char *g_log_firmwareversion;
  17. // Log string
  18. void log_raw(const char *str);
  19. // Log byte as hex
  20. void log_raw(uint8_t value);
  21. // Log integer as hex
  22. void log_raw(uint32_t value);
  23. // Log integer as hex
  24. void log_raw(uint64_t value);
  25. // Log integer as decimal
  26. void log_raw(int value);
  27. // Log double
  28. void log_raw(double value);
  29. // Log bool
  30. void log_raw(bool value);
  31. // Log array of bytes
  32. struct bytearray {
  33. bytearray(const uint8_t *data, size_t len): data(data), len(len) {}
  34. const uint8_t *data;
  35. size_t len;
  36. };
  37. void log_raw(bytearray array);
  38. inline void log_raw()
  39. {
  40. // End of template recursion
  41. }
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. void log_buf(const unsigned char *buf, unsigned long size);
  46. // Log formatted string
  47. void log_f(const char *format, ...);
  48. #ifdef __cplusplus
  49. }
  50. #endif
  51. extern "C" unsigned long millis();
  52. // Variadic template for printing multiple items
  53. template<typename T, typename T2, typename... Rest>
  54. inline void log_raw(T first, T2 second, Rest... rest)
  55. {
  56. log_raw(first);
  57. log_raw(second);
  58. log_raw(rest...);
  59. }
  60. // Format a complete log message
  61. template<typename... Params>
  62. inline void log(Params... params)
  63. {
  64. if (g_log_debug)
  65. {
  66. log_raw("[", (int)millis(), "ms] ");
  67. }
  68. log_raw(params...);
  69. log_raw("\n");
  70. }
  71. // Format a complete debug message
  72. template<typename... Params>
  73. inline void debuglog(Params... params)
  74. {
  75. if (g_log_debug)
  76. {
  77. log_raw("[", (int)millis(), "ms] DBG ");
  78. log_raw(params...);
  79. log_raw("\n");
  80. }
  81. }