-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_log.cpp
More file actions
176 lines (149 loc) · 4.31 KB
/
access_log.cpp
File metadata and controls
176 lines (149 loc) · 4.31 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
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
#include "access_log.h"
#include <time.h>
#include <vector>
AccessLog::AccessLog() : mutex(NULL) {
mutex = xSemaphoreCreateMutex();
}
AccessLog::~AccessLog() {
if (mutex != NULL) {
vSemaphoreDelete(mutex);
}
}
bool AccessLog::begin() {
if (!initializeFile()) {
Serial.println("Failed to initialize access log file");
return false;
}
return true;
}
bool AccessLog::takeMutex() {
if (mutex == NULL) {
return false;
}
return xSemaphoreTake(mutex, pdMS_TO_TICKS(1000)) == pdTRUE;
}
void AccessLog::giveMutex() {
if (mutex != NULL) {
xSemaphoreGive(mutex);
}
}
bool AccessLog::initializeFile() {
if (!takeMutex()) {
return false;
}
bool success = true;
if (!LittleFS.exists(LOG_PATH)) {
File file = LittleFS.open(LOG_PATH, FILE_WRITE);
if (!file) {
Serial.println("Failed to create access log file");
success = false;
} else {
file.print(""); // Create empty file
file.close();
}
}
giveMutex();
return success;
}
String AccessLog::getTimestamp() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return "Time not set";
}
char timeStr[32];
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &timeinfo);
return String(timeStr);
}
bool AccessLog::writeToLog(const String& entry) {
if (!takeMutex()) {
return false;
}
bool success = false;
File file = LittleFS.open(LOG_PATH, FILE_APPEND);
if (file) {
file.println(entry);
file.close();
success = true;
}
giveMutex();
return success;
}
bool AccessLog::addCardAccess(unsigned long cardNumber, bool accessGranted) {
String timestamp = getTimestamp();
String status = accessGranted ? "GRANTED" : "DENIED";
String entry = timestamp + " - Card " + String(cardNumber) + " - Access " + status;
return writeToLog(entry);
}
bool AccessLog::addMessage(const String& message) {
String timestamp = getTimestamp();
String entry = timestamp + " - " + message;
return writeToLog(entry);
}
String AccessLog::getLogContents() {
if (!takeMutex()) {
return "Error: Could not access log file";
}
String contents;
File file = LittleFS.open(LOG_PATH, FILE_READ);
if (file) {
contents = file.readString();
file.close();
}
giveMutex();
return contents;
}
bool AccessLog::pruneLog(int maxLines) {
// Don't take mutex if we're already holding it
bool mutexTaken = false;
if (xSemaphoreGetMutexHolder(mutex) != xTaskGetCurrentTaskHandle()) {
if (!takeMutex()) {
return false;
}
mutexTaken = true;
}
bool success = false;
// Read all lines from the file
File file = LittleFS.open(LOG_PATH, FILE_READ);
if (file) {
std::vector<String> lines;
String line;
// Read all lines into memory
while (file.available()) {
line = file.readStringUntil('\n');
if (line.length() > 0) {
lines.push_back(line);
}
}
file.close();
// If we have more lines than the maximum, keep only the most recent ones
if (lines.size() > maxLines) {
// Keep only the last maxLines entries
std::vector<String> recentLines;
int startIndex = lines.size() - maxLines;
for (int i = startIndex; i < lines.size(); i++) {
recentLines.push_back(lines[i]);
}
// Write the pruned content back to the file
file = LittleFS.open(LOG_PATH, FILE_WRITE);
if (file) {
for (size_t i = 0; i < recentLines.size(); i++) {
file.println(recentLines[i]);
}
file.close();
success = true;
Serial.print("Pruned access log from ");
Serial.print(lines.size());
Serial.print(" to ");
Serial.print(recentLines.size());
Serial.println(" lines");
}
} else {
// No pruning needed
success = true;
}
}
if (mutexTaken) {
giveMutex();
}
return success;
}