Jelajahi Sumber

Flush log files immediately after boot and low-battery events

Log lines written just before a brownout (imminent on low-battery
shutdown) or right after a log file opens (boot/config lines) could
otherwise sit unwritten for up to IVT_SYNC seconds and be lost.
k4be 1 Minggu lalu
induk
melakukan
52ac969b21
2 mengubah file dengan 15 tambahan dan 1 penghapusan
  1. 9 1
      soft/main.c
  2. 6 0
      soft/main.h

+ 9 - 1
soft/main.c

@@ -98,6 +98,10 @@ unsigned int get_stack_free_bytes(void) {
 	return count;
 }
 
+void request_immediate_sync(void) {
+	FLAGS |= F_SYNC;
+}
+
 void io_mark(unsigned long int start_ms) {
 	unsigned int dt = (unsigned int)(get_uptime_ms() - start_ms);
 	if (dt > io_max_ms)
@@ -229,8 +233,10 @@ ISR(ADC_vect)
 		}
 	} else {
 		if (bat_volt < VI_LVL) {
-			if (++lvt >= 3)
+			if (++lvt >= 3) {
 				FLAGS |= F_LVD;
+				FLAGS |= F_SYNC; /* flush now, as early as possible before an imminent brownout */
+			}
 		} else {
 			lvt = 0;
 		}
@@ -428,6 +434,7 @@ int main (void)
 	settings_load();
 	reset_counters();
 	log_boot_record();
+	request_immediate_sync(); /* flush the boot/config lines as soon as a log file opens, not after up to IVT_SYNC seconds */
 
 	menu_push(default_menu);
 
@@ -450,6 +457,7 @@ int main (void)
 
 		display_event(DISPLAY_EVENT_INITIALIZED);
 		log_loop_status(System.status);
+		request_immediate_sync(); /* flush the just-logged loop/error status promptly */
 		utc = 0;
 		localtime = 0;
 		prev_status = System.status;

+ 6 - 0
soft/main.h

@@ -286,6 +286,12 @@ unsigned int get_dist_avg_speed_x100(void);
 void io_mark(unsigned long int start_ms);
 unsigned int io_get_and_reset_max(void);
 
+/* Request that the next main-loop pass sync all open log files immediately,
+ * instead of waiting for the periodic IVT_SYNC timer - used around events
+ * (boot, low battery, fix lost, errors, shutdown) whose log lines must not be
+ * lost to a brownout before the next periodic sync. */
+void request_immediate_sync(void);
+
 /* Stack headroom: bytes of the stack-painted region between the end of
  * .bss/.noinit and RAMEND that are still untouched. Falls only, monotonically,
  * as the deepest call/interrupt nesting grows over the session. */