[FL-1147] System logging (#434)
* furi_log: introduce log system * applications: rework with furi logging * furi_log: reset color after tag printed * core: add log info Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
52
core/furi/log.c
Normal file
52
core/furi/log.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "log.h"
|
||||
#include <stm32wbxx_hal.h>
|
||||
#include "check.h"
|
||||
|
||||
typedef struct {
|
||||
FuriLogLevel log_level;
|
||||
FuriLogPrint print;
|
||||
FuriLogVPrint vprint;
|
||||
FuriLogTimestamp timetamp;
|
||||
} FuriLogParams;
|
||||
|
||||
static FuriLogParams furi_log;
|
||||
|
||||
void furi_log_init() {
|
||||
// Set default logging parameters
|
||||
furi_log.log_level = FURI_LOG_LEVEL;
|
||||
furi_log.print = printf;
|
||||
furi_log.vprint = vprintf;
|
||||
furi_log.timetamp = HAL_GetTick;
|
||||
}
|
||||
|
||||
void furi_log_print(FuriLogLevel level, const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
if(level <= furi_log.log_level) {
|
||||
furi_log.print("%lu ", furi_log.timetamp());
|
||||
furi_log.vprint(format, args);
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void furi_log_set_level(FuriLogLevel level) {
|
||||
furi_log.log_level = level;
|
||||
}
|
||||
|
||||
FuriLogLevel furi_log_get_level(void) {
|
||||
return furi_log.log_level;
|
||||
}
|
||||
|
||||
void furi_log_set_print(FuriLogPrint print, FuriLogVPrint vprint) {
|
||||
furi_assert(print);
|
||||
furi_assert(vprint);
|
||||
|
||||
furi_log.print = print;
|
||||
furi_log.vprint = vprint;
|
||||
}
|
||||
|
||||
void furi_log_set_timestamp(FuriLogTimestamp timestamp) {
|
||||
furi_assert(timestamp);
|
||||
|
||||
furi_log.timetamp = timestamp;
|
||||
}
|
66
core/furi/log.h
Normal file
66
core/furi/log.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define FURI_LOG_LEVEL_DEFAULT 3
|
||||
|
||||
#ifndef FURI_LOG_LEVEL
|
||||
#define FURI_LOG_LEVEL FURI_LOG_LEVEL_DEFAULT
|
||||
#endif
|
||||
|
||||
#define FURI_LOG_CLR(clr) "\033[0;" clr "m"
|
||||
#define FURI_LOG_CLR_RESET "\033[0m"
|
||||
|
||||
#define FURI_LOG_CLR_BLACK "30"
|
||||
#define FURI_LOG_CLR_RED "31"
|
||||
#define FURI_LOG_CLR_GREEN "32"
|
||||
#define FURI_LOG_CLR_BROWN "33"
|
||||
#define FURI_LOG_CLR_BLUE "34"
|
||||
#define FURI_LOG_CLR_PURPLE "35"
|
||||
|
||||
#define FURI_LOG_CLR_E FURI_LOG_CLR(FURI_LOG_CLR_RED)
|
||||
#define FURI_LOG_CLR_W FURI_LOG_CLR(FURI_LOG_CLR_BROWN)
|
||||
#define FURI_LOG_CLR_I FURI_LOG_CLR(FURI_LOG_CLR_GREEN)
|
||||
#define FURI_LOG_CLR_D FURI_LOG_CLR(FURI_LOG_CLR_BLUE)
|
||||
#define FURI_LOG_CLR_V
|
||||
|
||||
typedef int (*FuriLogPrint)(const char*, ...);
|
||||
typedef int (*FuriLogVPrint)(const char*, va_list);
|
||||
typedef uint32_t (*FuriLogTimestamp)(void);
|
||||
|
||||
typedef enum {
|
||||
FURI_LOG_NONE = 0,
|
||||
FURI_LOG_ERROR = 1,
|
||||
FURI_LOG_WARN = 2,
|
||||
FURI_LOG_INFO = 3,
|
||||
FURI_LOG_DEBUG = 4,
|
||||
FURI_LOG_VERBOSE = 5,
|
||||
} FuriLogLevel;
|
||||
|
||||
void furi_log_init();
|
||||
void furi_log_print(FuriLogLevel level, const char* format, ...);
|
||||
void furi_log_set_level(FuriLogLevel level);
|
||||
FuriLogLevel furi_log_get_level();
|
||||
void furi_log_set_print(FuriLogPrint print, FuriLogVPrint vprint);
|
||||
void furi_log_set_timestamp(FuriLogTimestamp timestamp);
|
||||
|
||||
#define FURI_LOG_FORMAT(log_letter, tag, format) \
|
||||
FURI_LOG_CLR_##log_letter "[" #log_letter "][" tag "]: " FURI_LOG_CLR_RESET format "\r\n"
|
||||
#define FURI_LOG_SHOW(tag, format, log_level, log_letter, ...) \
|
||||
furi_log_print(log_level, FURI_LOG_FORMAT(log_letter, tag, format), ##__VA_ARGS__)
|
||||
|
||||
#define FURI_LOG_E(tag, format, ...) FURI_LOG_SHOW(tag, format, FURI_LOG_ERROR, E, ##__VA_ARGS__)
|
||||
#define FURI_LOG_W(tag, format, ...) FURI_LOG_SHOW(tag, format, FURI_LOG_WARN, W, ##__VA_ARGS__)
|
||||
#define FURI_LOG_I(tag, format, ...) FURI_LOG_SHOW(tag, format, FURI_LOG_INFO, I, ##__VA_ARGS__)
|
||||
#define FURI_LOG_D(tag, format, ...) FURI_LOG_SHOW(tag, format, FURI_LOG_DEBUG, D, ##__VA_ARGS__)
|
||||
#define FURI_LOG_V(tag, format, ...) FURI_LOG_SHOW(tag, format, FURI_LOG_VERBOSE, V, ##__VA_ARGS__)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user