47 const int TASK_SDCARD_LOG_CLOSE = BIT1; ///< Close the logfile. |
47 const int TASK_SDCARD_LOG_CLOSE = BIT1; ///< Close the logfile. |
48 |
48 |
49 extern time_t now; |
49 extern time_t now; |
50 extern struct tm timeinfo; |
50 extern struct tm timeinfo; |
51 extern uint32_t TimeBrewing; |
51 extern uint32_t TimeBrewing; |
|
52 extern bool System_TimeOk; |
|
53 |
|
54 |
|
55 |
|
56 void log_msg(const char *tag, const char *format, ...) |
|
57 { |
|
58 char *outstr, logfn[64], stamp[32]; |
|
59 va_list va_ptr; |
|
60 FILE *fp; |
|
61 |
|
62 outstr = calloc(1024, sizeof(char)); |
|
63 va_start(va_ptr, format); |
|
64 vsnprintf(outstr, 1023, format, va_ptr); |
|
65 ESP_LOGI(tag, "%s", outstr); |
|
66 |
|
67 if (System_TimeOk && sdcard_state->card_present == true) { |
|
68 snprintf(logfn, 64, "/sdcard/log/sys%04d%02d%02d.log", timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday); |
|
69 snprintf(stamp, 31, "%02d-%02d %02d:%02d:%02d", timeinfo.tm_mday, timeinfo.tm_mon+1, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); |
|
70 fp = fopen(logfn, "a+"); |
|
71 if (fp) { |
|
72 fprintf(fp, "%s %s: %s\n", stamp, tag, outstr); |
|
73 fclose(fp); |
|
74 } |
|
75 } |
|
76 |
|
77 va_end(va_ptr); |
|
78 free(outstr); |
|
79 } |
|
80 |
52 |
81 |
53 |
82 |
54 void log_begin(time_t t) |
83 void log_begin(time_t t) |
55 { |
84 { |
56 struct tm timeinfo; |
85 struct tm timeinfo; |
58 /* |
87 /* |
59 * If there is an open logfile from an crashed brew, open that one. |
88 * If there is an open logfile from an crashed brew, open that one. |
60 */ |
89 */ |
61 if (! strlen(sdcard_state->logfile) && strlen(runtime.Logfile)) { |
90 if (! strlen(sdcard_state->logfile) && strlen(runtime.Logfile)) { |
62 sprintf(sdcard_state->logfile, "%s", runtime.Logfile); |
91 sprintf(sdcard_state->logfile, "%s", runtime.Logfile); |
63 ESP_LOGI(TAG, "Resumed %s", sdcard_state->logfile); |
92 log_msg(TAG, "Resumed %s", sdcard_state->logfile); |
64 } else { |
93 } else { |
65 localtime_r(&t, &timeinfo); |
94 localtime_r(&t, &timeinfo); |
66 if (timeinfo.tm_year > (2016 - 1900)) { |
95 if (timeinfo.tm_year > (2016 - 1900)) { |
67 // Valid time, construct the filename. |
96 // Valid time, construct the filename. |
68 strftime(sdcard_state->logfile, sizeof(sdcard_state->logfile), "br%y%m%d%H%M", &timeinfo); |
97 strftime(sdcard_state->logfile, sizeof(sdcard_state->logfile), "br%y%m%d%H%M", &timeinfo); |
69 } else { |
98 } else { |
70 sprintf(sdcard_state->logfile, "brewlog"); |
99 sprintf(sdcard_state->logfile, "brewlog"); |
71 } |
100 } |
72 ESP_LOGI(TAG, "Create %s", sdcard_state->logfile); |
101 log_msg(TAG, "Create %s", sdcard_state->logfile); |
73 sprintf(runtime.Logfile, "%s", sdcard_state->logfile); |
102 sprintf(runtime.Logfile, "%s", sdcard_state->logfile); |
74 } |
103 } |
75 } |
104 } |
76 |
105 |
77 |
106 |
480 } |
509 } |
481 |
510 |
482 if (uxBits & TASK_SDCARD_LOG_CLOSE) { |
511 if (uxBits & TASK_SDCARD_LOG_CLOSE) { |
483 // Close the logfile. |
512 // Close the logfile. |
484 if (strlen(sdcard_state->logfile) && (sdcard_state->card_present == true)) { |
513 if (strlen(sdcard_state->logfile) && (sdcard_state->card_present == true)) { |
485 ESP_LOGI(TAG, "Closing logfile"); |
514 log_msg(TAG, "Closing logfile"); |
486 char destname[64]; |
515 char destname[64]; |
487 sprintf(destname, "/sdcard/w/log"); |
516 sprintf(destname, "/sdcard/w/log"); |
488 int rc = mkdir(destname, 0755); |
517 int rc = mkdir(destname, 0755); |
489 if (rc && (errno != EEXIST)) { |
518 if (rc && (errno != EEXIST)) { |
490 ESP_LOGE(TAG, "Cannot create %s error %d", destname, errno); |
519 ESP_LOGE(TAG, "Cannot create %s error %d", destname, errno); |
515 fprintf(f, "}\n"); |
544 fprintf(f, "}\n"); |
516 fclose(f); |
545 fclose(f); |
517 } |
546 } |
518 sprintf(destname, "/sdcard/w/log/%s.json", sdcard_state->logfile); |
547 sprintf(destname, "/sdcard/w/log/%s.json", sdcard_state->logfile); |
519 if (FileCopy(filename, destname) == 0) { |
548 if (FileCopy(filename, destname) == 0) { |
520 ESP_LOGI(TAG, "JSON file copied to %s", destname); |
549 log_msg(TAG, "JSON file copied to %s", destname); |
521 unlink(filename); |
550 unlink(filename); |
522 } |
551 } |
523 } |
552 } |
524 } |
553 } |
525 sdcard_state->logfile[0] = '\0'; // Clear logfile name |
554 sdcard_state->logfile[0] = '\0'; // Clear logfile name |