Added syslog like logging on the sdcard for the manual and automatic processes.

Thu, 04 Jul 2019 21:46:12 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 04 Jul 2019 21:46:12 +0200
changeset 62
2e90ada37476
parent 61
c7b8a9931b59
child 63
1d2b4358db4b

Added syslog like logging on the sdcard for the manual and automatic processes.

main/automation.c file | annotate | diff | comparison | revisions
main/manual.c file | annotate | diff | comparison | revisions
main/task_sdcard.c file | annotate | diff | comparison | revisions
main/task_sdcard.h file | annotate | diff | comparison | revisions
main/task_wifi.c file | annotate | diff | comparison | revisions
--- a/main/automation.c	Thu Jul 04 15:57:43 2019 +0200
+++ b/main/automation.c	Thu Jul 04 21:46:12 2019 +0200
@@ -59,6 +59,21 @@
 static const char		*TAG = "automation";
 
 
+void change_sp(bool up)
+{
+    if (up)
+	stageTemp += 0.25;
+    else
+	stageTemp -= 0.25;
+    if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
+	driver_state->mlt_sp = stageTemp;
+	xSemaphoreGive(xSemaphoreDriver);
+    }
+    log_msg(TAG, "Changed sp to %.2f", stageTemp);
+}
+
+
+
 /*
  * Automation init function that only runs once when a
  * new screen is entered.
@@ -69,6 +84,7 @@
 
     switch (Main_Screen) {
 	case MAIN_AUTO_INIT1:
+		log_msg(TAG, "Automation startup");
 #ifdef CONFIG_TEMP_SENSORS_SIMULATOR
 		Fake_MLT = recipe.MashStep[0].Temperature - 10;
 		Fake_HLT = recipe.SpargeTemp - 15;
@@ -82,7 +98,7 @@
                     if (recipe.MashStep[i].Resttime)
                         LastMashStep = i;
                 }
-                ESP_LOGI(TAG, "Last mash step %d", LastMashStep);
+                log_msg(TAG, "Last mash step %d", LastMashStep);
 
                 // Check for a crashed session.
                 if (runtime.AutoModeStarted) {
@@ -111,7 +127,7 @@
 					    }
 					    xSemaphoreGive(xSemaphoreDriver);
 					}
-					ESP_LOGI(TAG, "Resume brew screen %d, time left %d", Main_Screen, TimeLeft);
+					log_msg(TAG, "Resume brew screen %d, time left %d", Main_Screen, TimeLeft);
 					log_begin((time_t)0);
 					update_json();
 					log_annotation(ANNOTATION_SYSTEM, (char *)"Resume");
@@ -232,7 +248,7 @@
                 Buttons_Add(  5,  30, 60, 40, (char *)"+sp",  0);
                 Buttons_Add(255,  30, 60, 40, (char *)"-sp",  1);
                 Buttons_Show();
-                ESP_LOGI(TAG, "Mash done, going to boil.");
+                log_msg(TAG, "Mash done, going to boil.");
 		Sub_Screen = 0;
                 break;
 
@@ -262,7 +278,7 @@
                 Buttons_Add(  3, 190, 60, 40, (char *)"+1m",  2);
                 Buttons_Add(257, 190, 60, 40, (char *)"-1m",  3);
                 Buttons_Show();
-                ESP_LOGI(TAG, "Boil temperature reached, boil %d minutes", (TimeLeft / 60) -1);
+                log_msg(TAG, "Boil temperature reached, boil %d minutes", (TimeLeft / 60) -1);
 		log_annotation(ANNOTATION_STAGE, (char *)"Koken");
 		Sub_Screen = 0;
                 break;
@@ -345,11 +361,11 @@
                 TFT_setFont(DEJAVU24_FONT, NULL);
                 if (Main_Screen == MAIN_AUTO_DONE) {
                     TFT_print((char *)"Brouwen is gereed.", CENTER, CENTER);
-                    ESP_LOGI(TAG, "Brew is done");
+                    log_msg(TAG, "Brew is done");
                     SoundPlay(SOUND_End);
                 } else {
                     TFT_print((char *)"Brouwen is afgebroken.", CENTER, CENTER);
-                    ESP_LOGI(TAG, "Brew is aborted");
+                    log_msg(TAG, "Brew is aborted");
                     SoundPlay(SOUND_Warn);
                 }
                 log_close();
@@ -476,6 +492,7 @@
 
 			case 1:         loop = false;
 					Main_Screen = MAIN_AUTO_INIT2;
+					log_msg(TAG, "Brew `%s' on `%s'", recipe.Name, equipment.Name); 
 					break;
 
 			default:        break;
@@ -533,6 +550,7 @@
 		} else {
 		    runtime.UseHLT = _UseHLT = false;
 		}
+		log_msg(TAG, "Use HLT %s", (_UseHLT)?"true":"false");
                 updateRuntime = true;
                 if (_UseHLT) {
                     /*
@@ -545,7 +563,7 @@
                         AvailableTime += recipe.MashStep[i].Resttime;
                     if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
                         driver_state->hlt_sp = recipe.SpargeTemp - ((AvailableTime / 2) + 2);
-                        ESP_LOGI(TAG, "HLT preheat set to %4.1f", driver_state->hlt_sp);
+                        log_msg(TAG, "HLT preheat set to %4.2f", driver_state->hlt_sp);
                         xSemaphoreGive(xSemaphoreDriver);
                     }
                 }
@@ -569,6 +587,7 @@
                     if (driver_state->hlt_pv >= driver_state->hlt_sp) {
                         Main_Screen = MAIN_AUTO_MASH_IN;
                         driver_state->hlt_sp = recipe.SpargeTemp;       // Set final setpoint
+			log_msg(TAG, "HLT preheat done");
                     }
                     xSemaphoreGive(xSemaphoreDriver);
                 }
@@ -616,9 +635,9 @@
                     MashState = Sub_Screen = MASH_WAITTEMP;
 		    snprintf(msg, 255, "{\"main\":\"%d\",\"sub\":\"%d\",\"timer\":\"\"}", Main_Screen, Sub_Screen);
                     ws_server_send_text_clients((char *)"/ws", msg, strlen(msg));
-                    ESP_LOGI(TAG, "Mash step %d type: %s time: %d temp: %4.1f  min: %4.1f max: %4.1f",
+                    log_msg(TAG, "Mash step %d type: %s time: %d sp: %4.2f  sv: %4.3f",
 				    Main_Screen - MAIN_AUTO_MASH_IN, mashTypes[recipe.MashStep[Main_Screen - MAIN_AUTO_MASH_IN].Type],
-                                    stageTime, stageTemp, MinMash, MaxMash);
+                                    stageTime, stageTemp, temp_MLT);
 
 		    if (Main_Screen > MAIN_AUTO_MASH_IN) {
 			// Do not annotate before the log is open.
@@ -658,7 +677,7 @@
 			    driver_state->mlt_mode = MLT_MODE_OFF;
 			    xSemaphoreGive(xSemaphoreDriver);
 			}
-			ESP_LOGI(TAG, "Mash infusion prompt");
+			log_msg(TAG, "Mash infusion prompt");
 		    } else {
 			Buttons_Add(  5,  30, 60, 40, (char *)"+sp",   0);
 			Buttons_Add(255,  30, 60, 40, (char *)"-sp",   1);
@@ -687,7 +706,7 @@
 			Resume = false;
 			runtime.StageTimeLeft = TimeLeft / 60;
 			updateRuntime = true;
-                        ESP_LOGI(TAG, "Mash step %d temperature reached, rest time %d", Main_Screen - MAIN_AUTO_MASH_IN, TimeLeft / 60);
+                        log_msg(TAG, "Mash step %d temperature reached, rest time %d", Main_Screen - MAIN_AUTO_MASH_IN, TimeLeft / 60);
                         Buttons_Clear();
                         Buttons_Add(  0, 120, 60, 40, (char *)"+1m",   0);
                         Buttons_Add(260, 120, 60, 40, (char *)"-1m",   1);
@@ -695,20 +714,12 @@
                     }
                     switch (Buttons_Scan()) {
                         case 0:         if (stageTemp < MaxMash) {
-                                            stageTemp += 0.25;
-                                            if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
-                                                driver_state->mlt_sp = stageTemp;
-                                                xSemaphoreGive(xSemaphoreDriver);
-                                            }
+					    change_sp(true);
                                         }
                                         break;
 
                         case 1:         if (stageTemp > MinMash) {
-                                            stageTemp -= 0.25;
-                                            if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
-                                                driver_state->mlt_sp = stageTemp;
-                                                xSemaphoreGive(xSemaphoreDriver);
-                                            }
+					    change_sp(false);
                                         }
                                         break;
 
@@ -731,12 +742,12 @@
                         if (pumpTime >= equipment.PumpCycle) {
                             if (! pumpRest) {
                                 pumpRest = true;
-                                ESP_LOGI(TAG, "Pump rest");
+                                log_msg(TAG, "Pump rest");
                             }
                         } else {
                             if (pumpRest) {
                                 pumpRest = false;
-                                ESP_LOGI(TAG, "Pump start");
+                                log_msg(TAG, "Pump start");
                             }
                         }
                     }
@@ -745,6 +756,7 @@
                             case 0:     TimeLeft += 60;
 					runtime.StageTimeLeft = TimeLeft / 60;
 					updateRuntime = true;
+					log_msg(TAG, "Changed resttime to %d", runtime.StageTimeLeft);
                                         break;
 
                             case 1:     if (TimeLeft < 60)
@@ -753,6 +765,7 @@
                                             TimeLeft -= 60;
 					runtime.StageTimeLeft = TimeLeft / 60;
 					updateRuntime = true;
+					log_msg(TAG, "Changed resttime to %d", runtime.StageTimeLeft);
                                         break;
 
                             default:    break;
@@ -780,7 +793,7 @@
 			    MashState = Sub_Screen = MASH_ADD;
 			    snprintf(msg, 63, "{\"main\":\"%d\",\"sub\":\"%d\",\"timer\":\"Mout storten?\"}", Main_Screen, Sub_Screen);
                 	    ws_server_send_text_clients((char *)"/ws", msg, strlen(msg));
-			    ESP_LOGI(TAG, "Mash add prompt");
+			    log_msg(TAG, "Mash add prompt");
 			    break;
                         }
                         if (((Main_Screen - MAIN_AUTO_MASH_IN) == LastMashStep) && config.AskIodine) {
@@ -804,7 +817,7 @@
 			    MashState = Sub_Screen = MASH_IODINE;
 			    snprintf(msg, 63, "{\"main\":\"%d\",\"sub\":\"%d\",\"timer\":\"Jodium test?\"}", Main_Screen, Sub_Screen);
                 	    ws_server_send_text_clients((char *)"/ws", msg, strlen(msg));
-			    ESP_LOGI(TAG, "Mash iodine test prompt");
+			    log_msg(TAG, "Mash iodine test prompt");
 			    break;
                         }
                         if ((Main_Screen == MAIN_AUTO_MASH_OUT) && config.AskRemove) {
@@ -826,7 +839,7 @@
 			    MashState = Sub_Screen = MASH_REMOVE;
 			    snprintf(msg, 63, "{\"main\":\"%d\",\"sub\":\"%d\",\"timer\":\"Mout verwijderen?\"}", Main_Screen, Sub_Screen);
                 	    ws_server_send_text_clients((char *)"/ws", msg, strlen(msg));
-			    ESP_LOGI(TAG, "Mash remove prompt");
+			    log_msg(TAG, "Mash remove prompt");
 			    break;
                         }
                         if (Main_Screen != MAIN_AUTO_ABORT)
@@ -923,16 +936,14 @@
 
                 MLT_info(71, 26, true);
                 switch (Buttons_Scan()) {
-                    case 0:     if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
-                                    driver_state->mlt_sp += 0.25;
-                                    xSemaphoreGive(xSemaphoreDriver);
-                                }
+                    case 0:     if (stageTemp < 105) {
+				    change_sp(true);
+				}
                                 break;
 
-                    case 1:     if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
-                                    driver_state->mlt_sp -= 0.25;
-                                    xSemaphoreGive(xSemaphoreDriver);
-                                }
+                    case 1:     if (stageTemp > 80) {
+				    change_sp(false);
+				}
                                 break;
 
                     default:    break;
@@ -946,7 +957,7 @@
 		    Resume = false;
                 if (NewMinute) {
                     if ((runtime.HopAddition < recipe.Additions) && (TimeLeft <= ((recipe.Addition[runtime.HopAddition].Time * 60) + 60))) {
-                        ESP_LOGI(TAG, "Hop addition %d at %d minutes", runtime.HopAddition + 1, recipe.Addition[runtime.HopAddition].Time);
+                        log_msg(TAG, "Hop addition %d at %d minutes", runtime.HopAddition + 1, recipe.Addition[runtime.HopAddition].Time);
                         TopMessage(recipe.Addition[runtime.HopAddition].Name);
 			log_annotation(ANNOTATION_EVENT, recipe.Addition[runtime.HopAddition].Name);
                         SoundPlay(SOUND_AddHop);
@@ -960,6 +971,7 @@
                 if (TimeLeft < 60) {
 		    if (Output) {
 			log_annotation(ANNOTATION_STAGE, (char *)"Vlamuit");
+			log_msg(TAG, "Boil flame off");
 		    }
                     // Flameout
                     Output = 0;
@@ -984,33 +996,35 @@
                 TimerShow(TimeLeft, 65, 190);
 
                 switch (Buttons_Scan()) {
-                    case 0:     if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
-                                    driver_state->mlt_sp += 0.25;
-                                    xSemaphoreGive(xSemaphoreDriver);
-                                }
+                    case 0:     if (stageTemp < 105) {
+				    change_sp(true);
+				}
                                 break;
 
-                    case 1:     if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
-                                    driver_state->mlt_sp -= 0.25;
-                                    xSemaphoreGive(xSemaphoreDriver);
-                                }
+                    case 1:     if (stageTemp > 80) {
+				    change_sp(false);
+				}
                                 break;
 
                     case 2:     TimeLeft += 60;
+				log_msg(TAG, "Increase boiltime to %d minutes", TimeLeft / 60);
                                 break;
 
                     case 3:     if (TimeLeft > 60)
                                     TimeLeft -= 60;
                                 else
                                     TimeLeft = 0;
+				log_msg(TAG, "Decrease boiltime to %d minutes", TimeLeft / 60);
                                 break;
 
                     case 4:     if (BoilPower < 100)
                                     BoilPower++;
+				log_msg(TAG, "Increase boilpower to %d%%", BoilPower);
                                 break;
 
                     case 5:     if (BoilPower > 0)
                                     BoilPower--;
+				log_msg(TAG, "Decrease boilpower to %d%%", BoilPower);
                                 break;
 
                     default:    break;
@@ -1018,7 +1032,7 @@
 
                 if (TimeLeft == 0) {
                     Main_Screen = MAIN_AUTO_WHIRLPOOL9;
-                    ESP_LOGI(TAG, "Boil is ready");
+                    log_msg(TAG, "Boil is ready");
                 }
                 break;
 
@@ -1058,7 +1072,7 @@
 			snprintf(msg, 63, "{\"main\":\"%d\",\"sub\":\"%d\",\"timer\":\"\"}", Main_Screen, Sub_Screen);
                         ws_server_send_text_clients((char *)"/ws", msg, strlen(msg));
 			CoolBeep = false;
-			ESP_LOGI(TAG, "Start cooling from %6.2f to %4.1f", ds18b20_state->mlt_temperature, stageTemp);
+			log_msg(TAG, "Start cooling from %6.2f to %4.1f", ds18b20_state->mlt_temperature, stageTemp);
 			if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
 			    driver_state->mlt_mode = MLT_MODE_OFF;
 			    driver_state->mlt_sp = stageTemp;
@@ -1121,6 +1135,7 @@
                                     }
                                     xSemaphoreGive(xSemaphoreDriver);
                                 }
+				log_msg(TAG, "Increase sp to %.2f", driver_state->mlt_sp);
                                 break;
 
                     	case 0: Buttons_Add( 60, 150, 90, 40, (char *)"Stoppen", 4);
@@ -1142,6 +1157,7 @@
                                     }
                                     xSemaphoreGive(xSemaphoreDriver);
                                 }
+				log_msg(TAG, "Decrease sp to %.2f", driver_state->mlt_sp);
                                 break;
 
                     	case 3: if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
@@ -1154,6 +1170,7 @@
                                         driver_state->pump_run = 0;
                                     }
 				    runtime.PumpCooling = driver_state->pump_run;
+				    log_msg(TAG, "Pump turned %s", (driver_state->pump_run)?"on":"off");
 				    updateRuntime = true;
                                     xSemaphoreGive(xSemaphoreDriver);
                                 }
@@ -1260,7 +1277,7 @@
 			runtime.StageTimeLeft = TimeWhirlPool;
 			updateRuntime = true;
 			MLT_info(71, 26, false);
-			ESP_LOGI(TAG, "Whirlpool %d minutes, sp %4.1f", TimeWhirlPool, driver_state->mlt_sp);
+			log_msg(TAG, "Whirlpool %d minutes, sp %4.1f", TimeWhirlPool, driver_state->mlt_sp);
 			Buttons_Add(255, 120, 60, 40, (char *)"+1m", 0);
 			Buttons_Add(  5, 120, 60, 40, (char *)"-1m", 1);
 			Buttons_Add(255, 200, 60, 40, (char *)"Pomp",  2);
--- a/main/manual.c	Thu Jul 04 15:57:43 2019 +0200
+++ b/main/manual.c	Thu Jul 04 21:46:12 2019 +0200
@@ -32,7 +32,7 @@
 {
     switch (Main_Screen) {
 	case MAIN_MANUAL_INIT:
-		ESP_LOGI(TAG, "Start manual mode");
+		log_msg(TAG, "Start manual mode");
 		if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
 		    driver_state->enable = true;
 		    xSemaphoreGive(xSemaphoreDriver);
@@ -121,7 +121,7 @@
 		Buttons_Clear();
 
 		if (! _ManualHLT && ! _ManualMLT) {
-		    ESP_LOGI(TAG, "End manual mode");
+		    log_msg(TAG, "End manual mode");
 		    Main_Screen = MAIN_MODE_FREE;
 		    break;
 		}
@@ -143,6 +143,7 @@
 			xSemaphoreGive(xSemaphoreDriver);
 		    }
 		}
+		log_msg(TAG, "HLT:%s MLT:%s", (_ManualHLT)?"true":"false", (_ManualMLT)?"true":"false");
 		Main_Screen = MAIN_MANUAL_MAIN;
 		break;
 
@@ -155,7 +156,7 @@
 		}
 		switch (Buttons_Scan()) {
 		    case 0:	Main_Screen = MAIN_MODE_FREE;
-				ESP_LOGI(TAG, "End manual mode");
+				log_msg(TAG, "End manual mode");
 				if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
 				    runtime.ManualHLT = driver_state->hlt_sp;
 				    runtime.ManualMLT = driver_state->mlt_sp;
@@ -173,8 +174,10 @@
 		    case 1:	if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
 				    if (_ManualMLT) {
 				    	driver_state->mlt_sp += 0.25;
+					log_msg(TAG, "MLT sp %.2f", driver_state->mlt_sp);
 				    } else {
 					driver_state->hlt_sp += 0.25;
+					log_msg(TAG, "HLT sp %.2f", driver_state->hlt_sp);
 				    }
 				    xSemaphoreGive(xSemaphoreDriver);
 				}
@@ -183,8 +186,10 @@
 		    case 2:	if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
 				    if (_ManualMLT) {
 					driver_state->mlt_sp -= 0.25;
+					log_msg(TAG, "MLT sp %.2f", driver_state->mlt_sp);
 				    } else {
 					driver_state->hlt_sp -= 0.25;
+					log_msg(TAG, "HLT sp %.2f", driver_state->hlt_sp);
 				    }
 				    xSemaphoreGive(xSemaphoreDriver);
 				}
@@ -198,16 +203,17 @@
 					    driver_state->pump_run = 1;
 					xSemaphoreGive(xSemaphoreDriver);
 				    }
+				    log_msg(TAG, "Pump turned %s", driver_state->pump_run?"on":"off");
 				} else {
 				    if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
 					if (driver_state->hlt_mode == HLT_MODE_BANG) {
 					    driver_state->hlt_mode = HLT_MODE_OFF;
 					    Buttons_Add(255, 200, 60, 40, (char *)"Aan"  , 3);
-					    ESP_LOGI(TAG, "HLT turned off");
+					    log_msg(TAG, "HLT turned off");
 					} else {
 					    driver_state->hlt_mode = HLT_MODE_BANG;
 					    Buttons_Add(255, 200, 60, 40, (char *)"Uit"  , 3);
-					    ESP_LOGI(TAG, "HLT turned on");
+					    log_msg(TAG, "HLT turned on sp %.2f", driver_state->hlt_sp);
 					}
 					xSemaphoreGive(xSemaphoreDriver);
 				    }
@@ -219,11 +225,11 @@
 				    if (driver_state->mlt_mode == MLT_MODE_PID) {
 					driver_state->mlt_mode = MLT_MODE_OFF;
 					Buttons_Add(255,  76, 60, 40, (char *)"Aan"  , 4);
-					ESP_LOGI(TAG, "MLT turned off");
+					log_msg(TAG, "MLT turned off");
 				    } else {
 					driver_state->mlt_mode = MLT_MODE_PID;
 					Buttons_Add(255,  76, 60, 40, (char *)"Uit"  , 4);
-					ESP_LOGI(TAG, "MLT turned on");
+					log_msg(TAG, "MLT turned on sp %.2f", driver_state->mlt_sp);
 				    }
 				    xSemaphoreGive(xSemaphoreDriver);
 				}
@@ -234,23 +240,25 @@
 				    driver_state->hlt_sp += 0.25;
 				    xSemaphoreGive(xSemaphoreDriver);
 				}
+				log_msg(TAG, "HLT sp %.2f", driver_state->hlt_sp);
 				break;
 
 		    case 6:	if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
 				    driver_state->hlt_sp -= 0.25;
 				    xSemaphoreGive(xSemaphoreDriver);
 				}
+				log_msg(TAG, "HLT sp %.2f", driver_state->hlt_sp);
 				break;
 
 		    case 7:	if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) {
 				    if (driver_state->hlt_mode == HLT_MODE_BANG) {
 					driver_state->hlt_mode = HLT_MODE_OFF;
 					Buttons_Add(255, 200, 60, 40, (char *)"Aan"  , 7);
-					ESP_LOGI(TAG, "HLT turned off");
+					log_msg(TAG, "HLT turned off");
 				    } else {
 					driver_state->hlt_mode = HLT_MODE_BANG;
 					Buttons_Add(255, 200, 60, 40, (char *)"Uit"  , 7);
-					ESP_LOGI(TAG, "HLT turned on");
+					log_msg(TAG, "HLT turned on %.2f", driver_state->hlt_sp);
 				    }
 				    xSemaphoreGive(xSemaphoreDriver);
 				}
--- a/main/task_sdcard.c	Thu Jul 04 15:57:43 2019 +0200
+++ b/main/task_sdcard.c	Thu Jul 04 21:46:12 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/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);
     }
 }
@@ -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);
 		    }
 		}
--- a/main/task_sdcard.h	Thu Jul 04 15:57:43 2019 +0200
+++ b/main/task_sdcard.h	Thu Jul 04 21:46:12 2019 +0200
@@ -50,6 +50,17 @@
     char	event[64];		///< Event message.
 } JSON_log;
 
+
+/**
+ * @brief Write application messages to a logfile on the sdcard. The
+ *        logfile is /sdcard/log/sysyyymmdd.log and is only used
+ *        when the time is set from NTP and a card is mounted.
+ *        Log messages are always copied to ESP_LOGI() too.
+ * @param tag is the log tag just as in ESP_LOGx().
+ * @param * is the formatted string.
+ */
+void log_msg(const char *tag, const char *, ...);
+
 /**
  * @brief Begin a new logfile. The logfile is created and written to on
  *        the spiffs filesystem.
--- a/main/task_wifi.c	Thu Jul 04 15:57:43 2019 +0200
+++ b/main/task_wifi.c	Thu Jul 04 21:46:12 2019 +0200
@@ -356,7 +356,7 @@
         localtime_r(&now, &timeinfo);
 	System_TimeOk = true;
         strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
-        ESP_LOGI(TAG, "NTP time is set: %s", strftime_buf);
+	log_msg(TAG, "NTP time is set: %s", strftime_buf);
     } else {
     	ESP_LOGI(TAG, "NTP unknown time sync event rc=%d", rc);
     }

mercurial