Skip to content

Commit 2dc88f2

Browse files
Keep only 30 log files.
1 parent 63cb53b commit 2dc88f2

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

ESP8266-temp-server.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
#include <FS.h>
66
#include <time.h> // time() ctime()
77
#include <sys/time.h> // struct timeval
8+
#include <list>
89

910
#include "index_htm.h"
1011
#include "logs_htm.h"
1112
#include "wifi_credentials.h" // change your WiFi settings in this file
1213

1314
#define ONE_WIRE_BUS D1
1415
#define ONBOARD_LED D4
16+
#define SAVED_LOGFILES 30
1517

1618
#define TZ 1 // (utc+) TZ in hours
1719
#define DST_MN 60 // use 60mn for summer time in some countries

logger.ino

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
void 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

3120
bool 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

Comments
 (0)