forked from hidaba/PylontechMonitoring
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy_log.cpp
More file actions
136 lines (107 loc) · 3.35 KB
/
Copy pathpy_log.cpp
File metadata and controls
136 lines (107 loc) · 3.35 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
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
#include "py_log.h"
#include <Arduino.h>
#include <Preferences.h>
#include "esp_log.h"
#include "config.h"
extern AppConfig config;
//static const int MAX_PLOG = 200;
// ---------------------------------------------------------
// Timestamp: YYYY.MM.DD hh:mm:ss,ms
// ---------------------------------------------------------
String getTimestampWithMs() {
time_t now;
time(&now);
struct tm timeinfo;
localtime_r(&now, &timeinfo);
unsigned long ms = millis() % 1000;
char buf[40];
snprintf(
buf, sizeof(buf),
"%04d.%02d.%02d %02d:%02d:%02d,%03lu",
timeinfo.tm_year + 1900,
timeinfo.tm_mon + 1,
timeinfo.tm_mday,
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec,
ms
);
return String(buf);
}
// ---------------------------------------------------------
// Web-Log Buffer
// ---------------------------------------------------------
static String webLog;
void WebLog(const String& msg) {
webLog += msg + "\n";
if (webLog.length() > 8000) {
webLog.remove(0, 2000);
}
}
String WebLogGet() {
return webLog;
}
void WebLogClear() {
webLog = "";
}
// ---------------------------------------------------------
// Main-Logfunction
// ---------------------------------------------------------
void Log(LogLevel lvl, const String& msg) {
// --- Log-Level-Filter ---
if ((lvl == LOG_INFO && !config.logInfo) ||
(lvl == LOG_WARN && !config.logWarn) ||
(lvl == LOG_ERROR && !config.logError) ||
(lvl == LOG_DEBUG && !config.logDebug)) {
return;
}
// --- Zeitstempel ---
String ts = getTimestampWithMs() + " ";
// --- Prefix ---
String prefix;
switch (lvl) {
case LOG_INFO: prefix = "[INFO] "; break;
case LOG_WARN: prefix = "[WARN] "; break;
case LOG_ERROR: prefix = "[ERROR] "; break;
case LOG_DEBUG: prefix = "[DEBUG] "; break;
}
String line = ts + prefix + msg;
// --- ESP_LOG ---
switch (lvl) {
case LOG_INFO: ESP_LOGI("Pylontech", "%s", line.c_str()); break;
case LOG_WARN: ESP_LOGW("Pylontech", "%s", line.c_str()); break;
case LOG_ERROR: ESP_LOGE("Pylontech", "%s", line.c_str()); break;
case LOG_DEBUG: ESP_LOGD("Pylontech", "%s", line.c_str()); break;
}
// --- Web-Log ---
WebLog(line);
// --- Serial ---
Serial.println(line);
// Optional persistent log
//PersistentLog(line);
}
static Preferences plogPrefs;
//void PersistentLog(const String& msg) {
// if (!persistentLoggingEnabled) return;
//
// plogPrefs.begin("plog", false);
//
// int count = plogPrefs.getInt("count", 0);
// String key = "e" + String(count % MAX_PLOG);
// plogPrefs.putString(key.c_str(), msg);
// plogPrefs.putInt("count", count + 1);
//
// plogPrefs.end();
//}
//String PersistentLogDump() {
// plogPrefs.begin("plog", true);
// int count = plogPrefs.getInt("count", 0);
// int start = max(0, count - MAX_PLOG);
// String out = "";
// for (int i = start; i < count; i++) {
// String key = "e" + String(i % MAX_PLOG);
// out += plogPrefs.getString(key.c_str(), "") + "\n";
// }
// plogPrefs.end();
// return out;
//}