log.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @file log.h
  3. * Furi Logging system
  4. */
  5. #pragma once
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <stdarg.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. typedef enum {
  13. FuriLogLevelDefault = 0,
  14. FuriLogLevelNone = 1,
  15. FuriLogLevelError = 2,
  16. FuriLogLevelWarn = 3,
  17. FuriLogLevelInfo = 4,
  18. FuriLogLevelDebug = 5,
  19. FuriLogLevelTrace = 6,
  20. } FuriLogLevel;
  21. #define FURI_LOG_CLR(clr) "\033[0;" clr "m"
  22. #define FURI_LOG_CLR_RESET "\033[0m"
  23. #define FURI_LOG_CLR_BLACK "30"
  24. #define FURI_LOG_CLR_RED "31"
  25. #define FURI_LOG_CLR_GREEN "32"
  26. #define FURI_LOG_CLR_BROWN "33"
  27. #define FURI_LOG_CLR_BLUE "34"
  28. #define FURI_LOG_CLR_PURPLE "35"
  29. #define FURI_LOG_CLR_E FURI_LOG_CLR(FURI_LOG_CLR_RED)
  30. #define FURI_LOG_CLR_W FURI_LOG_CLR(FURI_LOG_CLR_BROWN)
  31. #define FURI_LOG_CLR_I FURI_LOG_CLR(FURI_LOG_CLR_GREEN)
  32. #define FURI_LOG_CLR_D FURI_LOG_CLR(FURI_LOG_CLR_BLUE)
  33. #define FURI_LOG_CLR_T FURI_LOG_CLR(FURI_LOG_CLR_PURPLE)
  34. typedef void (*FuriLogPuts)(const char* data);
  35. typedef uint32_t (*FuriLogTimestamp)(void);
  36. /** Initialize logging */
  37. void furi_log_init();
  38. /** Log record
  39. *
  40. * @param[in] level The level
  41. * @param[in] format The format
  42. * @param[in] <unnamed> VA args
  43. */
  44. void furi_log_print(FuriLogLevel level, const char* format, ...);
  45. /** Set log level
  46. *
  47. * @param[in] level The level
  48. */
  49. void furi_log_set_level(FuriLogLevel level);
  50. /** Get log level
  51. *
  52. * @return The furi log level.
  53. */
  54. FuriLogLevel furi_log_get_level();
  55. /** Set log output callback
  56. *
  57. * @param[in] puts The puts callback
  58. */
  59. void furi_log_set_puts(FuriLogPuts puts);
  60. /** Set timestamp callback
  61. *
  62. * @param[in] timestamp The timestamp callback
  63. */
  64. void furi_log_set_timestamp(FuriLogTimestamp timestamp);
  65. #define FURI_LOG_FORMAT(log_letter, tag, format) \
  66. FURI_LOG_CLR_##log_letter "[" #log_letter "][" tag "]: " FURI_LOG_CLR_RESET format "\r\n"
  67. #define FURI_LOG_SHOW(tag, format, log_level, log_letter, ...) \
  68. furi_log_print(log_level, FURI_LOG_FORMAT(log_letter, tag, format), ##__VA_ARGS__)
  69. /** Log methods
  70. *
  71. * @param tag The application tag
  72. * @param format The format
  73. * @param ... VA Args
  74. */
  75. #define FURI_LOG_E(tag, format, ...) \
  76. FURI_LOG_SHOW(tag, format, FuriLogLevelError, E, ##__VA_ARGS__)
  77. #define FURI_LOG_W(tag, format, ...) FURI_LOG_SHOW(tag, format, FuriLogLevelWarn, W, ##__VA_ARGS__)
  78. #define FURI_LOG_I(tag, format, ...) FURI_LOG_SHOW(tag, format, FuriLogLevelInfo, I, ##__VA_ARGS__)
  79. #define FURI_LOG_D(tag, format, ...) \
  80. FURI_LOG_SHOW(tag, format, FuriLogLevelDebug, D, ##__VA_ARGS__)
  81. #define FURI_LOG_T(tag, format, ...) \
  82. FURI_LOG_SHOW(tag, format, FuriLogLevelTrace, T, ##__VA_ARGS__)
  83. #ifdef __cplusplus
  84. }
  85. #endif