cross-platform logging
This commit is contained in:
parent
6550fa4583
commit
d8b22912e6
72
components/tactility-core/src/log.c
Normal file
72
components/tactility-core/src/log.c
Normal file
@ -0,0 +1,72 @@
|
||||
#ifndef ESP_PLATFORM
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include <time.h>
|
||||
|
||||
static char tt_loglevel_to_prefix(LogLevel level) {
|
||||
switch (level) {
|
||||
case LOG_LEVEL_ERROR:
|
||||
return 'E';
|
||||
case LOG_LEVEL_WARNING:
|
||||
return 'W';
|
||||
case LOG_LEVEL_INFO:
|
||||
return 'I';
|
||||
case LOG_LEVEL_DEBUG:
|
||||
return 'D';
|
||||
case LOG_LEVEL_TRACE:
|
||||
return 'T';
|
||||
default:
|
||||
return '?';
|
||||
}
|
||||
}
|
||||
|
||||
static const char* tt_loglevel_to_colour(LogLevel level) {
|
||||
switch (level) {
|
||||
case LOG_LEVEL_ERROR:
|
||||
return "\033[1;31m";
|
||||
case LOG_LEVEL_WARNING:
|
||||
return "\033[33m";
|
||||
case LOG_LEVEL_INFO:
|
||||
return "\033[32m";
|
||||
case LOG_LEVEL_DEBUG:
|
||||
return "\033[1;37m";
|
||||
case LOG_LEVEL_TRACE:
|
||||
return "\033[37m";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t tt_log_timestamp(void) {
|
||||
if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
|
||||
return clock() / CLOCKS_PER_SEC * 1000;
|
||||
}
|
||||
static uint32_t base = 0;
|
||||
if (base == 0 && xPortGetCoreID() == 0) {
|
||||
base = clock() / CLOCKS_PER_SEC * 1000;
|
||||
}
|
||||
TickType_t tick_count = xPortInIsrContext() ? xTaskGetTickCountFromISR() : xTaskGetTickCount();
|
||||
return base + tick_count * (1000 / configTICK_RATE_HZ);
|
||||
}
|
||||
|
||||
void tt_log(LogLevel level, const char* tag, const char* format, ...) {
|
||||
printf(
|
||||
"%s%c (%lu) %s: ",
|
||||
tt_loglevel_to_colour(level),
|
||||
tt_loglevel_to_prefix(level),
|
||||
tt_log_timestamp(),
|
||||
tag
|
||||
);
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args);
|
||||
va_end(args);
|
||||
|
||||
printf("\033[0m\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -1,11 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "esp_log.h"
|
||||
#else
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#define TT_LOG_E(tag, format, ...) \
|
||||
ESP_LOGE(tag, format, ##__VA_ARGS__)
|
||||
#define TT_LOG_W(tag, format, ...) \
|
||||
@ -15,7 +22,32 @@ extern "C" {
|
||||
#define TT_LOG_D(tag, format, ...) \
|
||||
ESP_LOGD(tag, format, ##__VA_ARGS__)
|
||||
#define TT_LOG_T(tag, format, ...) \
|
||||
ESP_LOGT(tag, format, ##__VA_ARGS__)
|
||||
ESP_LOGV(tag, format, ##__VA_ARGS__)
|
||||
|
||||
#else
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_DEBUG,
|
||||
LOG_LEVEL_TRACE
|
||||
} LogLevel;
|
||||
|
||||
void tt_log(LogLevel level, const char* tag, const char* format, ...);
|
||||
|
||||
#define TT_LOG_E(tag, format, ...) \
|
||||
tt_log(LOG_LEVEL_ERROR, tag, format, ##__VA_ARGS__)
|
||||
#define TT_LOG_W(tag, format, ...) \
|
||||
tt_log(LOG_LEVEL_WARNING, tag, format, ##__VA_ARGS__)
|
||||
#define TT_LOG_I(tag, format, ...) \
|
||||
tt_log(LOG_LEVEL_INFO, tag, format, ##__VA_ARGS__)
|
||||
#define TT_LOG_D(tag, format, ...) \
|
||||
tt_log(LOG_LEVEL_DEBUG, tag, format, ##__VA_ARGS__)
|
||||
#define TT_LOG_T(tag, format, ...) \
|
||||
tt_log(LOG_LEVEL_TRACE, tag, format, ##__VA_ARGS__)
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user