Page 1 of 1

How to pass parameter to ESP_LOGI?

Posted: Thu Nov 28, 2019 9:05 pm
by pinock
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

Re: How to pass parameter to ESP_LOGI?

Posted: Thu Nov 28, 2019 9:07 pm
by pinock
btw. i don't want #define approach. there is no usable for my scenario.

Re: How to pass parameter to ESP_LOGI?

Posted: Fri Nov 29, 2019 12:45 am
by WiFive

Re: How to pass parameter to ESP_LOGI?

Posted: Fri Nov 29, 2019 8:30 am
by pinock
thanks @wifive but i want to use exactly esp_logi(), esp_loge() etc. because i want colorizing and globally set log level.

Re: How to pass parameter to ESP_LOGI?

Posted: Fri Nov 29, 2019 9:34 am
by pinock
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;
}