11void logToSPIFFS ( const time_t now )
22{
3+ deleteOldLogFiles ();
4+
35 struct tm timeinfo;
46 char fileName[17 ];
57 char content[60 ];
68
79 localtime_r ( &now, &timeinfo );
810 strftime ( fileName , sizeof ( fileName ), " /%F.log" , &timeinfo );
911
10- Serial.printf ( " Current logfile: %s\n " , fileName );
11-
1212 snprintf ( content, sizeof ( content ), " %i,%3.2f" , now - DST_SEC - TZ_SEC, currentTemp );
1313
1414 if ( !writelnFile ( SPIFFS, fileName, content ) )
1515 {
1616 Serial.println ( F ( " Something wrong writing to file" ) );
1717 }
18-
19- Serial.println ();
20- Serial.println ( F ( " Files on spiffs:" ) );
21- Dir dir = SPIFFS.openDir ( " /" );
22- while ( dir.next () ) {
23- Serial.print ( dir.fileName () );
24- Serial.print ( F ( " size: " ) );
25- File f = dir.openFile ( " r" );
26- Serial.println ( f.size () );
27- }
28- Serial.println ();
2918}
3019
3120bool writelnFile ( fs::FS &fs, const char * path, const char * message )
@@ -45,3 +34,35 @@ bool writelnFile( fs::FS &fs, const char * path, const char * message )
4534 file.close ();
4635 return true ;
4736}
37+
38+ void deleteOldLogFiles ()
39+ {
40+ std::list<String> logFiles;
41+
42+ Dir dir = SPIFFS.openDir ( F ( " /" ) );
43+ while ( dir.next () )
44+ {
45+ if ( dir.fileName ().endsWith ( F ( " .log" ) ) )
46+ {
47+ logFiles.push_back ( dir.fileName () );
48+ }
49+ }
50+
51+ if ( logFiles.size () > SAVED_LOGFILES )
52+ {
53+ logFiles.sort ();
54+ }
55+
56+ while ( logFiles.size () > SAVED_LOGFILES )
57+ {
58+ std::list<String>::iterator thisFile;
59+
60+ thisFile = logFiles.begin ();
61+
62+ String filename = *thisFile;
63+
64+ SPIFFS.remove ( filename.c_str () );
65+ logFiles.erase ( thisFile );
66+ }
67+ }
68+
0 commit comments