-
Notifications
You must be signed in to change notification settings - Fork 0
/
j_file.ino
280 lines (233 loc) · 7.06 KB
/
j_file.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// List the content of the directory.
// parameters:
// directory ... the directory to list
// returns nothing
void listDirectory(String directory) {
Serial.println("all files in " + directory + " :");
Dir dir = SPIFFS.openDir(directory);
while (dir.next()) {
Serial.print(dir.fileName());
Serial.print(" size: ");
File f = dir.openFile("r");
Serial.println(f.size());
}
}
// Get the name for the log file backup.
// parameters:
// logfileName ... base name of the log files
// num ... number of the log file
// returns the name of the log file
String getLogfileBackupName(String logfileName, int num) {
return logfileName + "." + String(num);
}
// Rotate the log files.
// parameters:
// logfileName ... base name for log files
// maxBackup ... maximum backups
// returns nothing
void rotateLogfiles(String logfileName, int maxBackup) {
Serial.println("rotating log files: " + logfileName);
// remove last file
String lastBackupName = getLogfileBackupName(logfileName, maxBackup);
if (SPIFFS.exists(lastBackupName)) {
SPIFFS.remove(lastBackupName);
}
// rotate all backup files
for (int i = maxBackup - 1; i >= 0; i--) {
String currentBackupName = getLogfileBackupName(logfileName, i);
if (SPIFFS.exists(currentBackupName)) {
String nextBackupName = getLogfileBackupName(logfileName, i + 1);
SPIFFS.rename(currentBackupName, nextBackupName);
}
}
// rotate current logfile to first backup file
if (SPIFFS.exists(logfileName)) {
String firstBackupName = getLogfileBackupName(logfileName, 0);
SPIFFS.rename(logfileName, firstBackupName);
}
// ready to log in log file
global_log_enabled = true;
// dump the current log buffer to the log file
dumpLogBuffer();
}
// Prints all log files.
// parameters:
// logfileName ... base name for log files
// maxBackup ... maximum backups
// returns nothing
void printLogfiles(String logfileName, int maxBackup) {
Serial.println("printing log files: " + logfileName);
// print current logfile
if (SPIFFS.exists(logfileName)) {
printFileToSerial(logfileName);
}
// print last good logfile
if (SPIFFS.exists(logfileName + LOGFILE_LASTGOOD_EXTENSION)) {
printFileToSerial(logfileName + LOGFILE_LASTGOOD_EXTENSION);
}
// print last bad logfile
if (SPIFFS.exists(logfileName + LOGFILE_LASTBAD_EXTENSION)) {
printFileToSerial(logfileName + LOGFILE_LASTBAD_EXTENSION);
}
// print all backup files
for (int i = 0; i <= maxBackup; i++) {
String currentBackupName = getLogfileBackupName(logfileName, i);
if (SPIFFS.exists(currentBackupName)) {
printFileToSerial(currentBackupName);
}
}
}
// Deletes all log files.
// parameters:
// directory ... the directory to delete the logfiles in
// logfileName ... base name for log files
// returns nothing
void deleteLogfiles(String directory, String logfileName) {
Serial.println(
"deleting in " + directory + " the log files: " + logfileName);
Dir dir = SPIFFS.openDir(directory);
while (dir.next()) {
String filename = dir.fileName();
if (filename.indexOf(logfileName) > -1) {
Serial.println(" deleting: " + filename);
SPIFFS.remove(filename);
}
}
}
// Writes message with logLevel to serial and log file if global_log_enabled is true.
// If global_log_enabled is false, the content is written to an internal log buffer.
// Use dumpLogBuffer() to write the content to the log file.
// parameters:
// logLevel ... log level
// message ... message to log
// returns nothing
void doLog(String logLevel, String message) {
String logLine = getDateTime() + " " + String(millis()) + " " + logLevel
+ ": " + message;
Serial.println(logLine);
if (global_log_enabled) {
writeLog(global_logfileName, logLine);
} else {
if (global_logBuffer == "") {
global_logBuffer += logLine;
} else {
global_logBuffer += "\n" + logLine;
}
}
}
// Logs the message with LOGLEVEL_INFO.
// parameters:
// message ... the message to log
// returns nothing
void doLogInfo(String message) {
doLog(LOGLEVEL_INFO, message);
}
// Logs the message with LOGLEVEL_WARN.
// parameters:
// message ... the message to log
// returns nothing
void doLogWarn(String message) {
doLog(LOGLEVEL_WARN, message);
}
// Logs the message with LOGLEVEL_ERROR.
// parameters:
// message ... the message to log
// returns nothing
void doLogError(String message) {
doLog(LOGLEVEL_ERROR, message);
}
// Logs the message with LOGLEVEL_TIME.
// parameters:
// message ... the message to log
// returns nothing
void doLogTime(String message) {
doLog(LOGLEVEL_TIME, message);
}
// Dumps the content of the log buffer to the log file.
// parameters: none
// returns nothing
void dumpLogBuffer() {
writeLog(global_logfileName, global_logBuffer);
}
// Write to log file.
// parameters:
// logfileName ... base name of the log file
// logmessage ... message to log
// returns nothing
void writeLog(String logfileName, String logmessage) {
//open file for appending new blank line to EOF.
File f = SPIFFS.open(logfileName, "a");
f.print(logmessage);
f.print('\n');
f.flush();
delay(1);
f.close();
}
// Print the contents of the file to the serial.
// parameters:
// fileName ... name of the log file
// returns nothing
void printFileToSerial(String fileName) {
Serial.println("------------------------------------------------------");
File f = SPIFFS.open(fileName, "r");
if (!f) {
Serial.println("file \"" + fileName + "\" open failed");
Serial.println(
"------------------------------------------------------");
return;
}
Serial.println("Content of file \"" + fileName + "\":");
while (f.available()) {
//Lets read line by line from the file
String line = f.readStringUntil('\n');
Serial.println(line);
}
f.close();
Serial.println("------------------------------------------------------");
}
// Copies the source file to the destination file. Overwrites the destination file if it already!
// parameters:
// sourceFilename ... name of the source file
// destFilename ... name of the destination file
// returns nothing
void copyFile(String sourceFilename, String destFilename) {
if (!SPIFFS.exists(sourceFilename)) {
Serial.println("could not find \"" + sourceFilename + "\"");
return;
}
File fSrc = SPIFFS.open(sourceFilename, "r");
if (!fSrc) {
Serial.println("error opening \"" + sourceFilename + "\" for reading");
return;
}
File fDst = SPIFFS.open(destFilename, "w");
if (!fDst) {
Serial.println("error opening \"" + destFilename + "\" for writing");
return;
}
while (fSrc.available()) {
fDst.print(fSrc.readStringUntil('\n') + "\n");
}
fDst.close();
fSrc.close();
}
// Renames the file. Overwrites the destination file if it already exists!
// parameters:
// sourceFilename ... name of the source file
// destFilename ... name of the destination file
// returns nothing
void renameFile(String sourceFilename, String destFilename) {
if (SPIFFS.exists(destFilename)) {
SPIFFS.remove(destFilename);
}
SPIFFS.rename(sourceFilename, destFilename);
}
// Deletes the file if it exists.
// parameters:
// filename ... name of the file
// returns nothing
void deleteFile(String filename) {
if (SPIFFS.exists(filename)) {
SPIFFS.remove(filename);
}
}