-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.cpp
122 lines (107 loc) · 3.26 KB
/
debug.cpp
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
#include "debug.h"
// Initialize static member variables
LogLevel Debug::currentLogLevel = LOG_LEVEL_INFO;
//bool Debug::fileLoggingEnabled = false;
//nsigned long Debug::logFileRetentionPeriod = 0;
//unsigned long Debug::lastLogFileTime = 0;
void Debug::begin(LogLevel level, long baudRate) {
currentLogLevel = level;
Serial.begin(baudRate);
while (!Serial) {
// Wait for the serial port to connect
}
// Initialize SPIFFS
//if (!SPIFFS.begin(true)) {
// Serial.println("An error has occurred while mounting SPIFFS");
//}
}
void Debug::setLogLevel(LogLevel level) {
currentLogLevel = level;
}
//void Debug::enableFileLogging(bool enable) {
// fileLoggingEnabled = enable;
//}
//
//void Debug::setLogFileRetentionPeriod(unsigned long period) {
// logFileRetentionPeriod = period;
//}
void Debug::log(LogLevel level, const char* format, va_list args) {
if (level <= currentLogLevel) {
char logBuffer[256];
vsnprintf(logBuffer, sizeof(logBuffer), format, args);
// Print log level prefix
switch (level) {
case LOG_LEVEL_ERROR:
Serial.print("[ERROR] ");
break;
case LOG_LEVEL_WARN:
Serial.print("[WARN] ");
break;
case LOG_LEVEL_INFO:
Serial.print("[INFO] ");
break;
case LOG_LEVEL_DEBUG:
Serial.print("[DEBUG] ");
break;
case LOG_LEVEL_VERBOSE:
Serial.print("[VERBOSE] ");
break;
default:
break;
}
// Print the log message to serial
Serial.println(logBuffer);
// Log to file if enabled
//if (fileLoggingEnabled) {
// logToFile(logBuffer);
//}
}
}
/*void Debug::logToFile(const char* message) {
// Append the log message to the file
File logFile = SPIFFS.open("/log.txt", FILE_APPEND);
if (!logFile) {
Serial.println("Failed to open log file for writing");
return;
}
logFile.println(message);
logFile.close();
// Check if we need to rotate the log file
unsigned long currentTime = millis();
if (logFileRetentionPeriod > 0 && (currentTime - lastLogFileTime) > logFileRetentionPeriod) {
// Rename the log file with a timestamp
String oldLogFileName = "/log_" + String(lastLogFileTime) + ".txt";
SPIFFS.rename("/log.txt", oldLogFileName.c_str());
lastLogFileTime = currentTime;
}
}*/
void Debug::error(const char* format, ...) {
va_list args;
va_start(args, format);
log(LOG_LEVEL_ERROR, format, args);
va_end(args);
}
void Debug::warn(const char* format, ...) {
va_list args;
va_start(args, format);
log(LOG_LEVEL_WARN, format, args);
va_end(args);
}
void Debug::info(const char* format, ...) {
va_list args;
va_start(args, format);
log(LOG_LEVEL_INFO, format, args);
va_end(args);
}
void Debug::debug(const char* format, ...) {
va_list args;
va_start(args, format);
log(LOG_LEVEL_DEBUG, format, args);
va_end(args);
}
void Debug::verbose(const char* format, ...) {
va_list args;
va_start(args, format);
log(LOG_LEVEL_VERBOSE, format, args);
va_end(args);
}