Skip to content

Commit bfe6e5a

Browse files
committed
optimize log facility for multi threading
- Shorten filenames - Add log_printf with mutex locking to play nice with Serial and two cores
1 parent a5d52ac commit bfe6e5a

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

Diff for: cores/esp32/esp32-hal-log.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,38 @@ extern "C"
6565
#define ARDUHAL_LOG_RESET_COLOR
6666
#endif
6767

68+
const char * pathToFileName(const char * path);
69+
int log_printf(const char *fmt, ...);
70+
6871
#define ARDUHAL_SHORT_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter format ARDUHAL_LOG_RESET_COLOR "\r\n"
69-
#define ARDUHAL_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter "[" #letter "][%s():%d] " format ARDUHAL_LOG_RESET_COLOR "\r\n", __FUNCTION__, __LINE__
72+
#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__
7073

7174
#if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
72-
#define log_v(format, ...) ets_printf(ARDUHAL_SHORT_LOG_FORMAT(V, format), ##__VA_ARGS__)
75+
#define log_v(format, ...) log_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__)
7376
#else
7477
#define log_v(format, ...)
7578
#endif
7679

7780
#if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
78-
#define log_d(format, ...) ets_printf(ARDUHAL_SHORT_LOG_FORMAT(D, format), ##__VA_ARGS__)
81+
#define log_d(format, ...) log_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__)
7982
#else
8083
#define log_d(format, ...)
8184
#endif
8285

8386
#if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
84-
#define log_i(format, ...) ets_printf(ARDUHAL_SHORT_LOG_FORMAT(I, format), ##__VA_ARGS__)
87+
#define log_i(format, ...) log_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__)
8588
#else
8689
#define log_i(format, ...)
8790
#endif
8891

8992
#if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_WARN
90-
#define log_w(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
93+
#define log_w(format, ...) log_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
9194
#else
9295
#define log_w(format, ...)
9396
#endif
9497

9598
#if CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
96-
#define log_e(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
99+
#define log_e(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
97100
#else
98101
#define log_e(format, ...)
99102
#endif

Diff for: cores/esp32/esp32-hal-misc.c

+14
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,18 @@ void delayMicroseconds(uint32_t us)
4747
}
4848
}
4949

50+
//used by hal log
51+
const char * IRAM_ATTR pathToFileName(const char * path){
52+
size_t i = 0;
53+
size_t pos = 0;
54+
char * p = (char *)path;
55+
while(*p){
56+
i++;
57+
if(*p == '/' || *p == '\\'){
58+
pos = i;
59+
}
60+
p++;
61+
}
62+
return path+pos;
63+
}
5064

Diff for: cores/esp32/esp32-hal-uart.c

+34-1
Original file line numberDiff line numberDiff line change
@@ -358,5 +358,38 @@ int uartGetDebug()
358358
return s_uart_debug_nr;
359359
}
360360

361-
361+
int log_printf(const char *format, ...)
362+
{
363+
if(s_uart_debug_nr < 0){
364+
return 0;
365+
}
366+
char loc_buf[64];
367+
char * temp = loc_buf;
368+
int len;
369+
va_list arg;
370+
va_list copy;
371+
va_start(arg, format);
372+
va_copy(copy, arg);
373+
len = vsnprintf(NULL, 0, format, arg);
374+
va_end(copy);
375+
if(len >= sizeof(loc_buf)){
376+
temp = (char*)malloc(len+1);
377+
if(temp == NULL) {
378+
return 0;
379+
}
380+
}
381+
vsnprintf(temp, len+1, format, arg);
382+
if(_uart_bus_array[s_uart_debug_nr].lock){
383+
while (xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY) != pdPASS);
384+
ets_printf("%s", temp);
385+
xSemaphoreGive(_uart_bus_array[s_uart_debug_nr].lock);
386+
} else {
387+
ets_printf("%s", temp);
388+
}
389+
va_end(arg);
390+
if(len > 64){
391+
free(temp);
392+
}
393+
return len;
394+
}
362395

Diff for: cores/esp32/esp32-hal.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extern "C" {
2828
#include <stdbool.h>
2929
#include <stdio.h>
3030
#include <stdlib.h>
31+
#include <stdarg.h>
3132
#include <inttypes.h>
3233
#include <string.h>
3334
#include <math.h>

0 commit comments

Comments
 (0)