log.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "log.h"
  2. #include "check.h"
  3. #include "mutex.h"
  4. #include <furi_hal.h>
  5. #define FURI_LOG_LEVEL_DEFAULT FuriLogLevelInfo
  6. typedef struct {
  7. FuriLogLevel log_level;
  8. FuriLogPuts puts;
  9. FuriLogTimestamp timestamp;
  10. FuriMutex* mutex;
  11. } FuriLogParams;
  12. static FuriLogParams furi_log;
  13. void furi_log_init() {
  14. // Set default logging parameters
  15. furi_log.log_level = FURI_LOG_LEVEL_DEFAULT;
  16. furi_log.puts = furi_hal_console_puts;
  17. furi_log.timestamp = furi_get_tick;
  18. furi_log.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  19. }
  20. void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...) {
  21. if(level <= furi_log.log_level &&
  22. furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk) {
  23. FuriString* string;
  24. string = furi_string_alloc();
  25. const char* color = _FURI_LOG_CLR_RESET;
  26. const char* log_letter = " ";
  27. switch(level) {
  28. case FuriLogLevelError:
  29. color = _FURI_LOG_CLR_E;
  30. log_letter = "E";
  31. break;
  32. case FuriLogLevelWarn:
  33. color = _FURI_LOG_CLR_W;
  34. log_letter = "W";
  35. break;
  36. case FuriLogLevelInfo:
  37. color = _FURI_LOG_CLR_I;
  38. log_letter = "I";
  39. break;
  40. case FuriLogLevelDebug:
  41. color = _FURI_LOG_CLR_D;
  42. log_letter = "D";
  43. break;
  44. case FuriLogLevelTrace:
  45. color = _FURI_LOG_CLR_T;
  46. log_letter = "T";
  47. break;
  48. default:
  49. break;
  50. }
  51. // Timestamp
  52. furi_string_printf(
  53. string,
  54. "%lu %s[%s][%s] " _FURI_LOG_CLR_RESET,
  55. furi_log.timestamp(),
  56. color,
  57. log_letter,
  58. tag);
  59. furi_log.puts(furi_string_get_cstr(string));
  60. furi_string_reset(string);
  61. va_list args;
  62. va_start(args, format);
  63. furi_string_vprintf(string, format, args);
  64. va_end(args);
  65. furi_log.puts(furi_string_get_cstr(string));
  66. furi_string_free(string);
  67. furi_log.puts("\r\n");
  68. furi_mutex_release(furi_log.mutex);
  69. }
  70. }
  71. void furi_log_print_raw_format(FuriLogLevel level, const char* format, ...) {
  72. if(level <= furi_log.log_level &&
  73. furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk) {
  74. FuriString* string;
  75. string = furi_string_alloc();
  76. va_list args;
  77. va_start(args, format);
  78. furi_string_vprintf(string, format, args);
  79. va_end(args);
  80. furi_log.puts(furi_string_get_cstr(string));
  81. furi_string_free(string);
  82. furi_mutex_release(furi_log.mutex);
  83. }
  84. }
  85. void furi_log_set_level(FuriLogLevel level) {
  86. if(level == FuriLogLevelDefault) {
  87. level = FURI_LOG_LEVEL_DEFAULT;
  88. }
  89. furi_log.log_level = level;
  90. }
  91. FuriLogLevel furi_log_get_level(void) {
  92. return furi_log.log_level;
  93. }
  94. void furi_log_set_puts(FuriLogPuts puts) {
  95. furi_assert(puts);
  96. furi_log.puts = puts;
  97. }
  98. void furi_log_set_timestamp(FuriLogTimestamp timestamp) {
  99. furi_assert(timestamp);
  100. furi_log.timestamp = timestamp;
  101. }