log.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /** Print log record
  39. *
  40. * @param level
  41. * @param tag
  42. * @param format
  43. * @param ...
  44. */
  45. void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...)
  46. _ATTRIBUTE((__format__(__printf__, 3, 4)));
  47. /** Print log record
  48. *
  49. * @param level
  50. * @param format
  51. * @param ...
  52. */
  53. void furi_log_print_raw_format(FuriLogLevel level, const char* format, ...)
  54. _ATTRIBUTE((__format__(__printf__, 2, 3)));
  55. /** Set log level
  56. *
  57. * @param[in] level The level
  58. */
  59. void furi_log_set_level(FuriLogLevel level);
  60. /** Get log level
  61. *
  62. * @return The furi log level.
  63. */
  64. FuriLogLevel furi_log_get_level();
  65. /** Set log output callback
  66. *
  67. * @param[in] puts The puts callback
  68. */
  69. void furi_log_set_puts(FuriLogPuts puts);
  70. /** Set timestamp callback
  71. *
  72. * @param[in] timestamp The timestamp callback
  73. */
  74. void furi_log_set_timestamp(FuriLogTimestamp timestamp);
  75. /** Log methods
  76. *
  77. * @param tag The application tag
  78. * @param format The format
  79. * @param ... VA Args
  80. */
  81. #define FURI_LOG_E(tag, format, ...) \
  82. furi_log_print_format(FuriLogLevelError, tag, format, ##__VA_ARGS__)
  83. #define FURI_LOG_W(tag, format, ...) \
  84. furi_log_print_format(FuriLogLevelWarn, tag, format, ##__VA_ARGS__)
  85. #define FURI_LOG_I(tag, format, ...) \
  86. furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  87. #define FURI_LOG_D(tag, format, ...) \
  88. furi_log_print_format(FuriLogLevelDebug, tag, format, ##__VA_ARGS__)
  89. #define FURI_LOG_T(tag, format, ...) \
  90. furi_log_print_format(FuriLogLevelTrace, tag, format, ##__VA_ARGS__)
  91. /** Log methods
  92. *
  93. * @param format The raw format
  94. * @param ... VA Args
  95. */
  96. #define FURI_LOG_RAW_E(format, ...) \
  97. furi_log_print_raw_format(FuriLogLevelError, format, ##__VA_ARGS__)
  98. #define FURI_LOG_RAW_W(format, ...) \
  99. furi_log_print_raw_format(FuriLogLevelWarn, format, ##__VA_ARGS__)
  100. #define FURI_LOG_RAW_I(format, ...) \
  101. furi_log_print_raw_format(FuriLogLevelInfo, format, ##__VA_ARGS__)
  102. #define FURI_LOG_RAW_D(format, ...) \
  103. furi_log_print_raw_format(FuriLogLevelDebug, format, ##__VA_ARGS__)
  104. #define FURI_LOG_RAW_T(format, ...) \
  105. furi_log_print_raw_format(FuriLogLevelTrace, format, ##__VA_ARGS__)
  106. #ifdef __cplusplus
  107. }
  108. #endif