-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ino
66 lines (54 loc) · 1.36 KB
/
logger.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
void logToSPIFFS( const time_t now )
{
deleteOldLogFiles();
struct tm timeinfo;
char fileName[17];
char content[60];
localtime_r( &now, &timeinfo );
strftime( fileName , sizeof( fileName ), "/%F.log", &timeinfo );
snprintf( content, sizeof( content ), "%i,%3.2f", now - DST_SEC - TZ_SEC, currentTemp );
if ( !writelnFile( SPIFFS, fileName, content ) )
{
Serial.println( F( "Something wrong writing to file" ) );
}
}
bool writelnFile( fs::FS &fs, const char * path, const char * message )
{
File file = fs.open( path, "a+" );
if ( !file )
{
Serial.println( F( "file could not be opened" ) );
return false;
}
if ( !file.println( message ) )
{
Serial.println( F( "file could not be written to" ) );
file.close();
return false;
}
file.close();
return true;
}
void deleteOldLogFiles()
{
std::list<String> logFiles;
Dir dir = SPIFFS.openDir( F( "/" ) );
while ( dir.next() )
{
if ( dir.fileName().endsWith( F( ".log" ) ) )
{
logFiles.push_back( dir.fileName() );
}
}
if ( logFiles.size() > SAVED_LOGFILES )
{
logFiles.sort();
}
while ( logFiles.size() > SAVED_LOGFILES )
{
std::list<String>::iterator oldestFile = logFiles.begin();
String filename = *oldestFile;
SPIFFS.remove( filename.c_str() );
logFiles.erase( oldestFile );
}
}