-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTeensySDRotationalModuleLogger.h
396 lines (332 loc) · 9.08 KB
/
TeensySDRotationalModuleLogger.h
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#ifndef SD_FILE_LOGGER_H_
#define SD_FILE_LOGGER_H_
#include "Arduino.h"
#include "ArduinoLogger.h"
#include "SdFat.h"
#include "internal/circular_buffer.hpp"
#include <EEPROM.h>
#include <kinetis.h>
/** Rotational SD File Buffer with per-Module Log Levels
*
* Logs to a file on the SD card using a rotation strategy.
* This class also provides per-module log levels, allowing you to
* specify different level limits or different sections of code.
*
* This class uses the SdFat Arduino Library.
*
* NOTE that module APIs are not routed to the global instance manager,
* so you cannot use that class or the macros with this strategy.
* You can implement your own that forwards the appropriate APIs, however.
*
* @code
* using PlatformLogger =
* PlatformLogger_t<TeensySDRotationalLogger>;
* @endcode
*
* @tparam TModuleCount The maximum number of modules you want to support
* with this logging strategy.
*
* @ingroup LoggingSubsystem
*/
template<size_t TModuleCount = 1>
class TeensySDRotationalModuleLogger final : public LoggerBase
{
private:
static constexpr size_t BUFFER_SIZE = 512;
static constexpr size_t FILENAME_SIZE = 32;
static constexpr unsigned EEPROM_LOG_STORAGE_ADDR = 4095;
public:
/// Default constructor
TeensySDRotationalModuleLogger() : LoggerBase() {}
/// Default destructor
~TeensySDRotationalModuleLogger() noexcept = default;
size_t size() const noexcept final
{
return file_.size();
}
size_t capacity() const noexcept final
{
// size in blocks * bytes per block (512 Bytes = 2^9)
return fs_ ? fs_->card()->sectorCount() << 9 : 0;
}
void log_customprefix() noexcept final
{
print("[%d ms] ", millis());
}
void begin(SdFs& sd_inst)
{
fs_ = &sd_inst;
set_filename();
if(!file_.open(filename_, O_WRITE | O_CREAT))
{
errorHalt("Failed to open file");
}
// Clear current file contents
file_.truncate(0);
log_reset_reason();
// Manually flush, since the file is open
flush();
file_.close();
}
// Resets the log file counter back to 1
void resetFileCounter()
{
EEPROM.write(EEPROM_LOG_STORAGE_ADDR, 1);
}
/** Get the maximum log level (filtering) for the specified module
*
* @param module_id The ID for the corresponding module
* @returns the current log level maximum.
*/
log_level_e level(unsigned module_id) const noexcept
{
return module_levels_[module_id];
}
/** Set the maximum log level (filtering) for the specified module
*
* @param module_id The ID for the corresponding module
* @param l The maximum log level. Levels greater than `l` will not be added to the log buffer.
* @returns the current log level maximum.
*/
log_level_e level(unsigned module_id, log_level_e l) noexcept
{
if(l <= LOG_LEVEL_LIMIT())
{
module_levels_[module_id] = l;
}
return module_levels_[module_id];
}
/// Set the log level for ALL modules
/// We need to forward this version to the base class version
/// to prevent us from calling level(module_id) when we try to set
/// the global log level
log_level_e level(log_level_e l) noexcept
{
return LoggerBase::level(l);
}
/// Get the log level for ALL modules
/// We need to forward this version to the base class version
log_level_e level() const noexcept
{
return LoggerBase::level();
}
/// The following overrides should be used to log with module IDs
template<typename... Args>
void critical(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::critical)
{
log(log_level_e::critical, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void critical_interrupt(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::critical)
{
log_interrupt(log_level_e::critical, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void error(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::error)
{
log(log_level_e::error, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void error_interrupt(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::error)
{
log_interrupt(log_level_e::error, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void warning(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::warning)
{
log(log_level_e::warning, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void warning_interrupt(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::warning)
{
log_interrupt(log_level_e::warning, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void info(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::info)
{
log(log_level_e::info, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void info_interrupt(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::info)
{
log_interrupt(log_level_e::info, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void debug(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::debug)
{
log(log_level_e::debug, fmt, std::forward<const Args>(args)...);
}
}
template<typename... Args>
void debug_interrupt(unsigned module_id, const char* fmt, const Args&... args)
{
if(module_levels_[module_id] >= log_level_e::debug)
{
log_interrupt(log_level_e::debug, fmt, std::forward<const Args>(args)...);
}
}
protected:
void log_putc(char c) noexcept final
{
log_buffer_.put(c);
}
size_t internal_size() const noexcept override
{
return log_buffer_.size();
}
size_t internal_capacity() const noexcept override
{
return log_buffer_.capacity();
}
void flush_() noexcept final
{
writeBufferToSDFile();
}
void clear_() noexcept final
{
log_buffer_.reset();
}
private:
void errorHalt(const char* msg)
{
printf("Error: %s\n", msg);
if(fs_->sdErrorCode())
{
if(fs_->sdErrorCode() == SD_CARD_ERROR_ACMD41)
{
printf("Try power cycling the SD card.\n");
}
printSdErrorSymbol(&Serial, fs_->sdErrorCode());
printf(", ErrorData: 0x%x\n", fs_->sdErrorData());
}
while(true)
{
}
}
void writeBufferToSDFile()
{
if(!file_.open(filename_, O_WRITE | O_APPEND))
{
errorHalt("Failed to open file");
}
int bytes_written = 0;
// We need to get the front, the rear, and potentially write the files in two steps
// to prevent ordering problems
size_t head = log_buffer_.head();
size_t tail = log_buffer_.tail();
const char* buffer = log_buffer_.storage();
if((head < tail) || ((tail > 0) && (log_buffer_.size() == log_buffer_.capacity())))
{
// we have a wraparound case
// We will write from buffer[tail] to buffer[size] in one go
// Then we'll reset head to 0 so that we can write 0 to tail next
bytes_written = file_.write(&buffer[tail], log_buffer_.capacity() - tail);
bytes_written += file_.write(buffer, head);
}
else
{
// Write from tail position and send the specified number of bytes
bytes_written = file_.write(&buffer[tail], log_buffer_.size());
}
if(static_cast<size_t>(bytes_written) != log_buffer_.size())
{
errorHalt("Failed to write to log file");
}
log_buffer_.reset();
file_.close();
}
/// Checks the kinetis SoC's reset reason registers and logs them
/// This should only be called during begin().
void log_reset_reason()
{
auto srs0 = RCM_SRS0;
auto srs1 = RCM_SRS1;
// Clear the values
RCM_SRS0 = 0;
RCM_SRS1 = 0;
if(srs0 & RCM_SRS0_LVD)
{
LoggerBase::info("Low-voltage Detect Reset\n");
}
if(srs0 & RCM_SRS0_LOL)
{
LoggerBase::info("Loss of Lock in PLL Reset\n");
}
if(srs0 & RCM_SRS0_LOC)
{
LoggerBase::info("Loss of External Clock Reset\n");
}
if(srs0 & RCM_SRS0_WDOG)
{
LoggerBase::info("Watchdog Reset\n");
}
if(srs0 & RCM_SRS0_PIN)
{
LoggerBase::info("External Pin Reset\n");
}
if(srs0 & RCM_SRS0_POR)
{
LoggerBase::info("Power-on Reset\n");
}
if(srs1 & RCM_SRS1_SACKERR)
{
LoggerBase::info("Stop Mode Acknowledge Error Reset\n");
}
if(srs1 & RCM_SRS1_MDM_AP)
{
LoggerBase::info("MDM-AP Reset\n");
}
if(srs1 & RCM_SRS1_SW)
{
LoggerBase::info("Software Reset\n");
}
if(srs1 & RCM_SRS1_LOCKUP)
{
LoggerBase::info("Core Lockup Event Reset\n");
}
}
void set_filename()
{
uint8_t value = EEPROM.read(EEPROM_LOG_STORAGE_ADDR);
// 0xFF indicates a byte that's been reset, or value 255. Either way, reset to 0.
if(value == 0xFF)
{
value = 1;
}
snprintf(filename_, FILENAME_SIZE, "log_%d.txt", value);
EEPROM.write(EEPROM_LOG_STORAGE_ADDR, value + 1);
}
private:
SdFs* fs_;
char filename_[FILENAME_SIZE];
mutable FsFile file_;
log_level_e module_levels_[TModuleCount] = {log_level_e(LOG_LEVEL)};
CircularBuffer<char, BUFFER_SIZE> log_buffer_;
};
#endif // SD_FILE_LOGGER_H_