Hello folks,
I am working now on an issue I do not find even an explanation for.
The following function writes seamlessly some logs to the SD card before core 0 panics with "Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled."
Code: Select all
static int mday;
static int inUse = 0;
vprintf_like_t my_IRAM_ATTR vprintf( const char *format, ... ){
va_list args;
FILE * logfile = NULL;
time_t now;
while( inUse){
vTaskDelay(100 / portTICK_PERIOD_MS);
}
inUse++;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
if(timeinfo.tm_mday != mday){
mday = timeinfo.tm_mday;
}
char filename[strlen(MOUNT_POINT)+40];
memset(filename,0,sizeof(filename));
strcat(filename,MOUNT_POINT);
sprintf( &filename[strlen(MOUNT_POINT)], "/[%04i-%02i-%02i].log", (timeinfo.tm_year+1900), (timeinfo.tm_mon+1), timeinfo.tm_mday);
// open file if not open
if(!logfile)
logfile = fopen(filename, "a");
if (!logfile){
inUse--;
return NULL;
}
va_start (args, format);
vfprintf (logfile, format, args);
va_end (args);
fclose(logfile);
logfile = NULL;
inUse--;
return NULL;
}
I looked on the web and people seem to use esp_log_set_vprintf() with no problem, and I can use it without problem as long as i do not try to access any of the variables in the variable list.
e.g. this useless function works fine just printing the format string to SD card
Code: Select all
vprintf_like_t IRAM_ATTR maxill_vprintf( const char *format, ... ){
va_list args;
FILE * logfile = NULL;
time_t now;
char * frmt = strdup(format);
while( inUse){
vTaskDelay(100 / portTICK_PERIOD_MS);
}
inUse++;
// ****** create and open file name
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
if(timeinfo.tm_mday != mday){
mday = timeinfo.tm_mday;
}
char filename[strlen(MOUNT_POINT)+40];
memset(filename,0,sizeof(filename));
strcat(filename,MOUNT_POINT);
sprintf( &filename[strlen(MOUNT_POINT)], "/[%04i-%02i-%02i].log", (timeinfo.tm_year+1900), (timeinfo.tm_mon+1), timeinfo.tm_mday);
// open file if not open
if(!logfile)
logfile = fopen(filename, "a");
if (!logfile){
inUse--;
return NULL;
}
// how many format tokens are in format
int ccount = char_in_str( format, '%');
char *token;
va_start (args, format);
// vfprintf (logfile, format, args);
size_t bufsize = snprintf((char *)0, 0, "%s", format);
char * buffer = malloc(bufsize+1);
bufsize = snprintf( buffer, bufsize, "%s", format);
va_end (args);
fwrite( buffer, sizeof(char), bufsize, logfile);
fclose(logfile);
free(buffer);
logfile = NULL;
inUse--;
free(frmt);
return NULL;
}
Best wishes
Ralph