-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog_buffer.cpp
45 lines (36 loc) · 952 Bytes
/
log_buffer.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
#include <syslog.h>
#include <stdio.h>
#include "log_buffer.h"
using namespace std;
string cLogBuffer_var::getStr() {
char var_buffer[20];
switch(type) {
case _int:
snprintf(var_buffer, sizeof(var_buffer), "%li", var_int);
return(var_buffer);
case _str:
return(var_str);
}
return("");
}
string cLogBuffer_item::getStr() {
string rslt = str;
for(unsigned i = 0; i < sizeof(vars) / sizeof(vars[0]); i++) {
size_t pos = rslt.find('%');
if(pos != string::npos) {
rslt = rslt.substr(0, pos) + vars[i].getStr() + rslt.substr(pos + 1);
}
}
return(rslt);
}
void cLogBuffer::apply() {
lock();
for(unsigned i = 0; i < min(count, (unsigned)(sizeof(items) / sizeof(items[0]))); i++) {
syslog(items[i].type, "%s", items[i].getStr().c_str());
}
if(count > (unsigned)(sizeof(items) / sizeof(items[0]))) {
syslog(LOG_NOTICE, "hidden %u logs", (unsigned)(count - sizeof(items) / sizeof(items[0])));
}
count = 0;
unlock();
}