fix(log) reduce the stack usage in log function (#2649)
* fix(log): change fwrite to puts since not all platform support fwrite
This reverts commit 539388a66f.
* fix(log): don't call printf and custom_print_cb at the same time
* fix(log): remove 768B temp buffer if LV_LOG_PRINTF == 1
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
* feat(printf): support %pV format specifier
to support the recursive print:
https://www.kernel.org/doc/html/latest/core-api/printk-formats.html
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
* fix(log): save 256B temp buffer if LV_LOG_PRINTF == 0
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
@@ -72,9 +72,6 @@ void _lv_log_add(lv_log_level_t level, const char * file, int line, const char *
|
||||
if(level >= LV_LOG_LEVEL) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
char msg[256];
|
||||
lv_vsnprintf(msg, sizeof(msg), format, args);
|
||||
va_end(args);
|
||||
|
||||
/*Use only the file name not the path*/
|
||||
size_t p;
|
||||
@@ -85,23 +82,43 @@ void _lv_log_add(lv_log_level_t level, const char * file, int line, const char *
|
||||
}
|
||||
}
|
||||
|
||||
char buf[512];
|
||||
uint32_t t = lv_tick_get();
|
||||
static const char * lvl_prefix[] = {"Trace", "Info", "Warn", "Error", "User"};
|
||||
lv_snprintf(buf, sizeof(buf), "[%s]\t(%" LV_PRId32 ".%03" LV_PRId32 ", +%" LV_PRId32 ")\t %s: %s \t(in %s line #%d)\n",
|
||||
lvl_prefix[level], t / 1000, t % 1000, t - last_log_time, func, msg, &file[p], line);
|
||||
|
||||
#if LV_LOG_PRINTF
|
||||
printf("[%s]\t(%" LV_PRId32 ".%03" LV_PRId32 ", +%" LV_PRId32 ")\t %s: ",
|
||||
lvl_prefix[level], t / 1000, t % 1000, t - last_log_time, func);
|
||||
vprintf(format, args);
|
||||
printf(" \t(in %s line #%d)\n", &file[p], line);
|
||||
#else
|
||||
if (custom_print_cb) {
|
||||
char buf[512];
|
||||
#if LV_SPRINTF_CUSTOM
|
||||
char msg[256];
|
||||
lv_vsnprintf(msg, sizeof(msg), format, args);
|
||||
lv_snprintf(buf, sizeof(buf), "[%s]\t(%" LV_PRId32 ".%03" LV_PRId32 ", +%" LV_PRId32 ")\t %s: %s \t(in %s line #%d)\n",
|
||||
lvl_prefix[level], t / 1000, t % 1000, t - last_log_time, func, msg, &file[p], line);
|
||||
#else
|
||||
lv_vaformat_t vaf = {format, &args};
|
||||
lv_snprintf(buf, sizeof(buf), "[%s]\t(%" LV_PRId32 ".%03" LV_PRId32 ", +%" LV_PRId32 ")\t %s: %pV \t(in %s line #%d)\n",
|
||||
lvl_prefix[level], t / 1000, t % 1000, t - last_log_time, func, &vaf, &file[p], line);
|
||||
#endif
|
||||
custom_print_cb(buf);
|
||||
}
|
||||
#endif
|
||||
|
||||
last_log_time = t;
|
||||
lv_log(buf);
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
|
||||
void lv_log(const char * buf)
|
||||
{
|
||||
#if LV_LOG_PRINTF
|
||||
fwrite(buf, 1, strlen(buf), stdout);
|
||||
#endif
|
||||
puts(buf);
|
||||
#else
|
||||
if(custom_print_cb) custom_print_cb(buf);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
/**********************
|
||||
|
||||
@@ -699,6 +699,9 @@ static int _vsnprintf(out_fct_type out, char * buffer, const size_t maxlen, cons
|
||||
else
|
||||
#endif
|
||||
flags |= FLAGS_LONG;
|
||||
|
||||
if(*(format + 1) == 'V')
|
||||
format++;
|
||||
}
|
||||
else if(*format == 'o') {
|
||||
base = 8U;
|
||||
@@ -747,6 +750,14 @@ static int _vsnprintf(out_fct_type out, char * buffer, const size_t maxlen, cons
|
||||
width, flags);
|
||||
}
|
||||
}
|
||||
else if (*format == 'V') {
|
||||
lv_vaformat_t * vaf = va_arg(va, lv_vaformat_t *);
|
||||
va_list copy;
|
||||
|
||||
va_copy(copy, *vaf->va);
|
||||
idx += _vsnprintf(out, buffer + idx, maxlen - idx, vaf->fmt, copy);
|
||||
va_end(copy);
|
||||
}
|
||||
else {
|
||||
// unsigned
|
||||
if(flags & FLAGS_LONG_LONG) {
|
||||
|
||||
@@ -54,6 +54,11 @@ extern "C" {
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
const char * fmt;
|
||||
va_list * va;
|
||||
} lv_vaformat_t;
|
||||
|
||||
/**
|
||||
* Tiny snprintf/vsnprintf implementation
|
||||
* \param buffer A pointer to the buffer where to store the formatted string
|
||||
|
||||
Reference in New Issue
Block a user