-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogBufferedModule.cpp
More file actions
54 lines (46 loc) · 1.14 KB
/
LogBufferedModule.cpp
File metadata and controls
54 lines (46 loc) · 1.14 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
#include "LogBufferedModule.h"
void LogBufferedModule::write_message(const String& message) {
if (isConnected()) {
flushCache();
if (!send(message)) {
bufferMessage(message);
}
} else {
bufferMessage(message);
}
}
void LogBufferedModule::bufferMessage(const String& message) {
cache += message;
cache += "\n";
// Enforce the cap by discarding the oldest messages (lines) first.
while (cache.length() > maxCacheSize) {
int nl = cache.indexOf('\n');
if (nl < 0) {
// A single message longer than the cap: keep its tail.
cache = cache.substring(cache.length() - maxCacheSize);
break;
}
cache = cache.substring(nl + 1);
}
}
void LogBufferedModule::flushCache() {
if (cache.length() == 0) {
return;
}
// Replay cached messages oldest first. If a send fails, keep the unsent
// remainder cached so nothing is lost.
int start = 0;
while (start < (int)cache.length()) {
int nl = cache.indexOf('\n', start);
if (nl < 0) {
nl = cache.length();
}
String line = cache.substring(start, nl);
if (line.length() && !send(line)) {
cache = cache.substring(start);
return;
}
start = nl + 1;
}
cache = "";
}