I'm writing a logging system using the Teensy 3.6 and onboard SD card with the sdfat library and the RTC.
In my application I log the time that the device turns on, and then on the next line I immediately store a time indicating the time it turned off. Every 10 seconds I delete the last line and replace it with an updated time so that when the device turns off it has a stored log showing the duration accurate to ~10 seconds. The only way the device stops logging is if it is powered off.
In my code I'm opening the file and closing the file each time like this (well, this is the initial write, but it's all done with the same structure):
The idea here is that I don't want it to run at all if the logging isn't working (they need to bring it in for repair if logging is broken so I'd rather it just not work at all). But the log file is extremely valuable at the end, so I need to ensure that data is actually written to the SD card.
What happens if I fail to close a file before it is powered off? I have other threads running so I'd much rather avoid the time involved in opening and closing the file every time (might cause glitches in other behavior), but it is critical that I avoid corrupting the file or losing the data when the system is powered off without warning.
I'm worried that the file class may only store changes in RAM and only flush to the SD card when you run close. Does anyone know for sure what the promised behavior is if the system is powered off after a write but before close? Or during a write, since theoretically they could power it off precisely at the wrong time?
In my application I log the time that the device turns on, and then on the next line I immediately store a time indicating the time it turned off. Every 10 seconds I delete the last line and replace it with an updated time so that when the device turns off it has a stored log showing the duration accurate to ~10 seconds. The only way the device stops logging is if it is powered off.
In my code I'm opening the file and closing the file each time like this (well, this is the initial write, but it's all done with the same structure):
Code:
if (!file.open("TrialLog.txt", O_RDWR | O_CREAT | O_AT_END)) {
errorHalt("Open File Failed.");
}
file.seek(file.size());
file.println(getTime() + " - Started. T" + String(now()));
file.println(getTime() + " - Ended. T" + String(now()));
file.close();
The idea here is that I don't want it to run at all if the logging isn't working (they need to bring it in for repair if logging is broken so I'd rather it just not work at all). But the log file is extremely valuable at the end, so I need to ensure that data is actually written to the SD card.
What happens if I fail to close a file before it is powered off? I have other threads running so I'd much rather avoid the time involved in opening and closing the file every time (might cause glitches in other behavior), but it is critical that I avoid corrupting the file or losing the data when the system is powered off without warning.
I'm worried that the file class may only store changes in RAM and only flush to the SD card when you run close. Does anyone know for sure what the promised behavior is if the system is powered off after a write but before close? Or during a write, since theoretically they could power it off precisely at the wrong time?