-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
60 lines (49 loc) · 1.41 KB
/
Copy pathlogger.cpp
File metadata and controls
60 lines (49 loc) · 1.41 KB
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
/*
* Implémentation du Logger - Optimisé pour ESP32
*/
#include "logger.h"
char Logger::logs[MAX_LOG_LINES][MAX_LOG_LENGTH];
uint8_t Logger::head = 0;
uint8_t Logger::count = 0;
void Logger::init() {
memset(logs, 0, sizeof(logs));
log("System initialized");
}
void Logger::log(const char* message) {
Serial.println(message);
unsigned long m = millis();
uint8_t h = (m / 3600000) % 24;
uint8_t mn = (m / 60000) % 60;
uint8_t s = (m / 1000) % 60;
snprintf(logs[head], MAX_LOG_LENGTH, "[%02u:%02u:%02u] %s", h, mn, s, message);
head = (head + 1) % MAX_LOG_LINES;
if (count < MAX_LOG_LINES) count++;
}
void Logger::log(const String& message) {
log(message.c_str());
}
void Logger::printf(const char* format, ...) {
char buf[MAX_LOG_LENGTH - 12]; // Reserve space for timestamp
va_list arg;
va_start(arg, format);
vsnprintf(buf, sizeof(buf), format, arg);
va_end(arg);
log(buf);
}
String Logger::getLogsHTML() {
String output;
output.reserve(count * 80); // Pre-allocate
// Renvoie les logs inversés (plus récents en haut)
for (int i = 0; i < count; i++) {
int idx = (head - 1 - i + MAX_LOG_LINES) % MAX_LOG_LINES;
output += F("<div class='log-line'>");
output += logs[idx];
output += F("</div>");
}
return output;
}
void Logger::clear() {
count = 0;
head = 0;
log("Logs cleared");
}