--- a/main/task_sdcard.c Tue Jul 02 17:33:23 2019 +0200 +++ b/main/task_sdcard.c Fri Jul 05 16:17:04 2019 +0200 @@ -49,6 +49,35 @@ extern time_t now; extern struct tm timeinfo; extern uint32_t TimeBrewing; +extern bool System_TimeOk; + + + +void log_msg(const char *tag, const char *format, ...) +{ + char *outstr, logfn[64], stamp[32]; + va_list va_ptr; + FILE *fp; + + outstr = calloc(1024, sizeof(char)); + va_start(va_ptr, format); + vsnprintf(outstr, 1023, format, va_ptr); + ESP_LOGI(tag, "%s", outstr); + + if (System_TimeOk && sdcard_state->card_present == true) { + snprintf(logfn, 64, "/sdcard/w/log/sys%04d%02d%02d.log", timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday); + snprintf(stamp, 31, "%02d-%02d %02d:%02d:%02d", timeinfo.tm_mday, timeinfo.tm_mon+1, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); + fp = fopen(logfn, "a+"); + if (fp) { + fprintf(fp, "%s %s: %s\n", stamp, tag, outstr); + fclose(fp); + } + } + + va_end(va_ptr); + free(outstr); +} + void log_begin(time_t t) @@ -60,7 +89,7 @@ */ if (! strlen(sdcard_state->logfile) && strlen(runtime.Logfile)) { sprintf(sdcard_state->logfile, "%s", runtime.Logfile); - ESP_LOGI(TAG, "Resumed %s", sdcard_state->logfile); + log_msg(TAG, "Resumed %s", sdcard_state->logfile); } else { localtime_r(&t, &timeinfo); if (timeinfo.tm_year > (2016 - 1900)) { @@ -69,7 +98,7 @@ } else { sprintf(sdcard_state->logfile, "brewlog"); } - ESP_LOGI(TAG, "Create %s", sdcard_state->logfile); + log_msg(TAG, "Create %s", sdcard_state->logfile); sprintf(runtime.Logfile, "%s", sdcard_state->logfile); } } @@ -208,13 +237,13 @@ // connect SDMMC driver to FATFS pdrv = 0xFF; if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == 0xFF) { - ESP_LOGI(TAG, "the maximum count of volumes is already mounted"); + ESP_LOGE(TAG, "the maximum count of volumes is already mounted"); return ESP_ERR_NO_MEM; } s_base_path = strdup(base_path); if (!s_base_path) { - ESP_LOGI(TAG, "could not copy base_path"); + ESP_LOGE(TAG, "could not copy base_path"); return ESP_ERR_NO_MEM; } esp_err_t err = ESP_OK; @@ -226,7 +255,7 @@ err = (*host_config->init)(); if (err != ESP_OK) { - ESP_LOGI(TAG, "host init returned rc=0x%x", err); + ESP_LOGE(TAG, "host init returned rc=0x%x", err); goto fail; } @@ -237,7 +266,7 @@ err = sdmmc_host_init_slot(host_config->slot, (const sdmmc_slot_config_t*) slot_config); } if (err != ESP_OK) { - ESP_LOGI(TAG, "slot_config returned rc=0x%x", err); + ESP_LOGE(TAG, "slot_config returned rc=0x%x", err); goto fail; } return ESP_OK; @@ -344,13 +373,13 @@ f = fopen(ff, "r"); if (f == NULL) { - ESP_LOGE(TAG, "FileCopy cannot open %s for read, error %d", ff, errno); + ESP_LOGW(TAG, "FileCopy cannot open %s for read, error %d", ff, errno); return 1; } t = fopen(tf, "w+"); if (t == NULL) { - ESP_LOGE(TAG, "FileCopy cannot open %s for create/write, error %d", tf, errno); + ESP_LOGW(TAG, "FileCopy cannot open %s for create/write, error %d", tf, errno); fclose(f); return 1; } @@ -482,7 +511,7 @@ if (uxBits & TASK_SDCARD_LOG_CLOSE) { // Close the logfile. if (strlen(sdcard_state->logfile) && (sdcard_state->card_present == true)) { - ESP_LOGI(TAG, "Closing logfile"); + log_msg(TAG, "Closing logfile"); char destname[64]; sprintf(destname, "/sdcard/w/log"); int rc = mkdir(destname, 0755); @@ -517,7 +546,7 @@ } sprintf(destname, "/sdcard/w/log/%s.json", sdcard_state->logfile); if (FileCopy(filename, destname) == 0) { - ESP_LOGI(TAG, "JSON file copied to %s", destname); + log_msg(TAG, "JSON file copied to %s", destname); unlink(filename); } }