unleashed-firmware/furi/core/log.c

210 lines
5.8 KiB
C
Raw Normal View History

#include "log.h"
#include "check.h"
#include "mutex.h"
#include <furi_hal.h>
#include <m-list.h>
LIST_DEF(FuriLogHandlersList, FuriLogHandler, M_POD_OPLIST)
#define FURI_LOG_LEVEL_DEFAULT FuriLogLevelInfo
typedef struct {
FuriLogLevel log_level;
FuriMutex* mutex;
FuriLogHandlersList_t tx_handlers;
} FuriLogParams;
static FuriLogParams furi_log = {0};
typedef struct {
const char* str;
FuriLogLevel level;
} FuriLogLevelDescription;
static const FuriLogLevelDescription FURI_LOG_LEVEL_DESCRIPTIONS[] = {
{"default", FuriLogLevelDefault},
{"none", FuriLogLevelNone},
{"error", FuriLogLevelError},
{"warn", FuriLogLevelWarn},
{"info", FuriLogLevelInfo},
{"debug", FuriLogLevelDebug},
{"trace", FuriLogLevelTrace},
};
void furi_log_init() {
// Set default logging parameters
furi_log.log_level = FURI_LOG_LEVEL_DEFAULT;
furi_log.mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
FuriLogHandlersList_init(furi_log.tx_handlers);
}
bool furi_log_add_handler(FuriLogHandler handler) {
furi_check(handler.callback);
bool ret = true;
furi_check(furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk);
FuriLogHandlersList_it_t it;
FuriLogHandlersList_it(it, furi_log.tx_handlers);
while(!FuriLogHandlersList_end_p(it)) {
if(memcmp(FuriLogHandlersList_ref(it), &handler, sizeof(FuriLogHandler)) == 0) {
ret = false;
} else {
FuriLogHandlersList_next(it);
}
}
if(ret) {
FuriLogHandlersList_push_back(furi_log.tx_handlers, handler);
}
furi_mutex_release(furi_log.mutex);
return ret;
}
bool furi_log_remove_handler(FuriLogHandler handler) {
bool ret = false;
furi_check(furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk);
FuriLogHandlersList_it_t it;
FuriLogHandlersList_it(it, furi_log.tx_handlers);
while(!FuriLogHandlersList_end_p(it)) {
if(memcmp(FuriLogHandlersList_ref(it), &handler, sizeof(FuriLogHandler)) == 0) {
FuriLogHandlersList_remove(furi_log.tx_handlers, it);
ret = true;
} else {
FuriLogHandlersList_next(it);
}
}
furi_mutex_release(furi_log.mutex);
return ret;
}
void furi_log_tx(const uint8_t* data, size_t size) {
if(!FURI_IS_ISR()) {
furi_check(furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk);
} else {
if(furi_mutex_get_owner(furi_log.mutex)) return;
}
FuriLogHandlersList_it_t it;
FuriLogHandlersList_it(it, furi_log.tx_handlers);
while(!FuriLogHandlersList_end_p(it)) {
FuriLogHandlersList_ref(it)->callback(data, size, FuriLogHandlersList_ref(it)->context);
FuriLogHandlersList_next(it);
}
if(!FURI_IS_ISR()) furi_mutex_release(furi_log.mutex);
}
void furi_log_puts(const char* data) {
furi_check(data);
furi_log_tx((const uint8_t*)data, strlen(data));
}
void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...) {
if(level <= furi_log.log_level &&
furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk) {
FuriString* string;
string = furi_string_alloc();
const char* color = _FURI_LOG_CLR_RESET;
const char* log_letter = " ";
switch(level) {
case FuriLogLevelError:
color = _FURI_LOG_CLR_E;
log_letter = "E";
break;
case FuriLogLevelWarn:
color = _FURI_LOG_CLR_W;
log_letter = "W";
break;
case FuriLogLevelInfo:
color = _FURI_LOG_CLR_I;
log_letter = "I";
break;
case FuriLogLevelDebug:
color = _FURI_LOG_CLR_D;
log_letter = "D";
break;
case FuriLogLevelTrace:
color = _FURI_LOG_CLR_T;
log_letter = "T";
break;
default:
break;
}
// Timestamp
furi_string_printf(
string, "%lu %s[%s][%s] " _FURI_LOG_CLR_RESET, furi_get_tick(), color, log_letter, tag);
furi_log_puts(furi_string_get_cstr(string));
furi_string_reset(string);
va_list args;
va_start(args, format);
furi_string_vprintf(string, format, args);
va_end(args);
furi_log_puts(furi_string_get_cstr(string));
furi_string_free(string);
furi_log_puts("\r\n");
furi_mutex_release(furi_log.mutex);
}
}
void furi_log_print_raw_format(FuriLogLevel level, const char* format, ...) {
if(level <= furi_log.log_level &&
furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk) {
FuriString* string;
string = furi_string_alloc();
va_list args;
va_start(args, format);
furi_string_vprintf(string, format, args);
va_end(args);
furi_log_puts(furi_string_get_cstr(string));
furi_string_free(string);
furi_mutex_release(furi_log.mutex);
}
}
void furi_log_set_level(FuriLogLevel level) {
if(level == FuriLogLevelDefault) {
level = FURI_LOG_LEVEL_DEFAULT;
}
furi_log.log_level = level;
}
FuriLogLevel furi_log_get_level(void) {
return furi_log.log_level;
}
bool furi_log_level_to_string(FuriLogLevel level, const char** str) {
for(size_t i = 0; i < COUNT_OF(FURI_LOG_LEVEL_DESCRIPTIONS); i++) {
if(level == FURI_LOG_LEVEL_DESCRIPTIONS[i].level) {
*str = FURI_LOG_LEVEL_DESCRIPTIONS[i].str;
return true;
}
}
return false;
}
bool furi_log_level_from_string(const char* str, FuriLogLevel* level) {
for(size_t i = 0; i < COUNT_OF(FURI_LOG_LEVEL_DESCRIPTIONS); i++) {
if(strcmp(str, FURI_LOG_LEVEL_DESCRIPTIONS[i].str) == 0) {
*level = FURI_LOG_LEVEL_DESCRIPTIONS[i].level;
return true;
}
}
return false;
[FL-3669] Expansion module protocol (#3250) * ApiSymbols: add furi_record_destroy * FuriHal: cleanup serial API, add logging configuration in RTC * FuriHal: hide private part in _i header. Toolbox: cleanup value index. SystemSettings: logging device and baudrate. * FuriHal: RTC logging method documentation * Synchronize API Symbols * Furi: mark HEAP_PRINT_DEBUG as broken * FuriHal: furi_hal_serial, add custom IRQ func * Fix PR review issues * Implement basic external module detection and echo * Update api symbols for f18 * Minimally working implementation (can create directory via rpc) * Make expansion protocol parser a header-only library * Rename a function * Improve thread syncronisation * Implement multi-packet transmissions * Improve test application * Clean up expansion worker code * Send heartbeat when host is ready * Update API symbols * Add draft documentation * Expansion worker: proper timeout and error handling * Expansion worker: correct TX, do not disable expansion callback * Expansion protocol: pc side test script * PC side expansion test: trying to change baudrate * Working comms between 2 flippers * Cleaner exit from expansion worker thread * Better checks * Add debug logs * Remove unneeded delays * Use USART as default expansion port * Refactor furi_hal_serial_control, fix crash * Improve furi_hal abstraction, wait for stable rx pin * Remove rogue include * Set proper exit reason on RPC error * Remove rogue comment * Remove RX stability check as potentially problematic * Improve expansion_test application * Remove rogue define * Give up on TODO * Implement expansion protocol checksum support * Update ExpansionModules.md * RPC: reverse input * Assets: sync protobuf * Fix typos * FuriHal: UART add reception DMA (#3220) * FuriHal: add DMA serial rx mode * usb_uart_bridge: switch to working with DMA * Sync api symbol versions * FuriHal: update serial docs and api * FuriHal: Selial added similar API for simple reception mode as with DMA * FuriHal: Update API target H18 * API: ver API H7 * FuriHal: Serial error processing * FuriHal: fix furi_hal_serial set baudrate * Sync api symbols * FuriHal: cleanup serial isr and various flag handling procedures * FuriHal: cleanup and simplify serial API * Debug: update UART Echo serial related flags * FuriHal: update serial API symbols naming * Make expansion_test compile * Remove unneeded file * Make PVS-studio happy * Optimise stack usage * Optimise heap usage, improve api signature * Fix typo * Clean up code * Update expansion_protocol.h * Fix unit tests * Add doxygen comments to expansion.h * Update/add doxygen comments * Update ExpansionModules.md * Github: new global code owner * FuriHal: naming in serial control * Expansion: check mutex acquire return result Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com> Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: SkorP <skorpionm@yandex.ru> Co-authored-by: SG <who.just.the.doctor@gmail.com> Co-authored-by: Skorpionm <85568270+Skorpionm@users.noreply.github.com>
2024-01-16 09:18:56 +00:00
}