How to pass parameter to ESP_LOGI?

pinock
Posts: 12
Joined: Thu Oct 17, 2019 12:32 pm

How to pass parameter to ESP_LOGI?

Postby pinock » Thu Nov 28, 2019 9:05 pm

i want to wrapper for esp_log

void Log(esp_log_level_t level, const char* tag, const char* format, ...)
{
va_list va;
va_start(va, format);
printf(format, va);
ESP_LOGI(tag, format, va);
va_end(va);
}

Severity Code Description Project File Line Suppression State
Error [Clang IntelliSense] Error: expected ')' BakelorEDK C:\Users\xxxx\source\embed\Embedded\Core\\components\Core\Log.cpp 14

pinock
Posts: 12
Joined: Thu Oct 17, 2019 12:32 pm

Re: How to pass parameter to ESP_LOGI?

Postby pinock » Thu Nov 28, 2019 9:07 pm

btw. i don't want #define approach. there is no usable for my scenario.


pinock
Posts: 12
Joined: Thu Oct 17, 2019 12:32 pm

Re: How to pass parameter to ESP_LOGI?

Postby pinock » Fri Nov 29, 2019 8:30 am

thanks @wifive but i want to use exactly esp_logi(), esp_loge() etc. because i want colorizing and globally set log level.

pinock
Posts: 12
Joined: Thu Oct 17, 2019 12:32 pm

Re: How to pass parameter to ESP_LOGI?

Postby pinock » Fri Nov 29, 2019 9:34 am

Ok Solved
here is the solution.


int Log(LogType logType, const char* tag, const char* format, ...)
{
va_list vArgs;
va_start(vArgs, format);

char temp[VPRINTF_STACK_BUFFER_SIZE];
int len = vsnprintf(temp, sizeof(temp) - 1, format, vArgs);
temp[sizeof(temp) - 1] = 0;

int i;

for (i = len - 1; i >= 0; --i) {
if (temp != '\n' && temp != '\r' && temp != ' ') {
break;
}
temp = 0;
}

if (i > 0) {

switch (logType)
{
case LogType::None:
case LogType::Info:
ESP_LOGI(tag, "%s", temp);
break;
case LogType::Error:
ESP_LOGE(tag, "%s", temp);
break;
case LogType::Warning:
ESP_LOGW(tag, "%s", temp);
break;
case LogType::Debug:
ESP_LOGD(tag, "%s", temp);
break;
case LogType::Verbose:
ESP_LOGV(tag, "%s", temp);
break;
default:
break;
}

}

va_end(vArgs);

return len;
}

Who is online

Users browsing this forum: No registered users and 125 guests