diff --git a/cores/esp32/esp32-hal-log.h b/cores/esp32/esp32-hal-log.h index cedf1625..bd42b79b 100644 --- a/cores/esp32/esp32-hal-log.h +++ b/cores/esp32/esp32-hal-log.h @@ -65,35 +65,38 @@ extern "C" #define ARDUHAL_LOG_RESET_COLOR #endif +const char * pathToFileName(const char * path); +int log_printf(const char *fmt, ...); + #define ARDUHAL_SHORT_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter format ARDUHAL_LOG_RESET_COLOR "\r\n" -#define ARDUHAL_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter "[" #letter "][%s():%d] " format ARDUHAL_LOG_RESET_COLOR "\r\n", __FUNCTION__, __LINE__ +#define ARDUHAL_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter "[" #letter "][%s:%u] %s(): " format ARDUHAL_LOG_RESET_COLOR "\r\n", pathToFileName(__FILE__), __LINE__, __FUNCTION__ #if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE -#define log_v(format, ...) ets_printf(ARDUHAL_SHORT_LOG_FORMAT(V, format), ##__VA_ARGS__) +#define log_v(format, ...) log_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__) #else #define log_v(format, ...) #endif #if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG -#define log_d(format, ...) ets_printf(ARDUHAL_SHORT_LOG_FORMAT(D, format), ##__VA_ARGS__) +#define log_d(format, ...) log_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__) #else #define log_d(format, ...) #endif #if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_INFO -#define log_i(format, ...) ets_printf(ARDUHAL_SHORT_LOG_FORMAT(I, format), ##__VA_ARGS__) +#define log_i(format, ...) log_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__) #else #define log_i(format, ...) #endif #if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_WARN -#define log_w(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__) +#define log_w(format, ...) log_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__) #else #define log_w(format, ...) #endif #if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR -#define log_e(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__) +#define log_e(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__) #else #define log_e(format, ...) #endif diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c index 092a36a5..07e2e27f 100644 --- a/cores/esp32/esp32-hal-misc.c +++ b/cores/esp32/esp32-hal-misc.c @@ -47,4 +47,18 @@ void delayMicroseconds(uint32_t us) } } +//used by hal log +const char * IRAM_ATTR pathToFileName(const char * path){ + size_t i = 0; + size_t pos = 0; + char * p = (char *)path; + while(*p){ + i++; + if(*p == '/' || *p == '\\'){ + pos = i; + } + p++; + } + return path+pos; +} diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index f4a03c54..127a5881 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -358,5 +358,38 @@ int uartGetDebug() return s_uart_debug_nr; } - +int log_printf(const char *format, ...) +{ + if(s_uart_debug_nr < 0){ + return 0; + } + char loc_buf[64]; + char * temp = loc_buf; + int len; + va_list arg; + va_list copy; + va_start(arg, format); + va_copy(copy, arg); + len = vsnprintf(NULL, 0, format, arg); + va_end(copy); + if(len >= sizeof(loc_buf)){ + temp = (char*)malloc(len+1); + if(temp == NULL) { + return 0; + } + } + vsnprintf(temp, len+1, format, arg); + if(_uart_bus_array[s_uart_debug_nr].lock){ + while (xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY) != pdPASS); + ets_printf("%s", temp); + xSemaphoreGive(_uart_bus_array[s_uart_debug_nr].lock); + } else { + ets_printf("%s", temp); + } + va_end(arg); + if(len > 64){ + free(temp); + } + return len; +} diff --git a/cores/esp32/esp32-hal.h b/cores/esp32/esp32-hal.h index 8889cd57..331ee42c 100644 --- a/cores/esp32/esp32-hal.h +++ b/cores/esp32/esp32-hal.h @@ -28,6 +28,7 @@ extern "C" { #include #include #include +#include #include #include #include