Skip to content

Commit 2f53c66

Browse files
committed
Change log system. Send always last n entrys with automatic purge
1 parent 08985d1 commit 2f53c66

File tree

1 file changed

+20
-57
lines changed

1 file changed

+20
-57
lines changed

main/main.c

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define CA_PK "VnZ0epkCQ5PnguMMIxZCIqFvrTpmMxOve3iCYK2hKX4="
2-
#define FW_VER 31
2+
#define FW_VER 32
33
#define PRODUCT "VIRKEY"
44
#define LOG_TAG "MAIN"
55

@@ -114,7 +114,7 @@ static CODE errors[] = {
114114
// --- End Errors
115115

116116
// Log stuff
117-
#define LOG_SIZE 1024
117+
#define LOG_SIZE 30
118118

119119
#define LOG_OP_BOOT 1
120120
#define LOG_OP_ERROR 2
@@ -334,15 +334,19 @@ static void log_add(session_t *s, int32_t op, int32_t par, int32_t res) {
334334
}
335335
log_elements--;
336336
}
337-
if (log_elements > 0) {
338-
int32_t last_log_idx = log_rear - 1;
339-
if (last_log_idx < 0) {
340-
last_log_idx = LOG_SIZE - 1;
341-
}
342-
if (log[last_log_idx].usr == usr && log[last_log_idx].op == op && log[last_log_idx].par == par && log[last_log_idx].res == res && (now - log[last_log_idx].ts) < 60) {
343-
log[last_log_idx].rep ++;
344-
ESP_LOGI("LOGGER","[%d] last entry repeated %d times", sh, log[last_log_idx].rep);
345-
return;
337+
338+
if (log_elements > 0) { // Check log repetitions
339+
int f = log_front;
340+
for(int n = 0; n < log_elements; n++) {
341+
if (log[f].usr == usr && log[f].op == op && log[f].par == par && log[f].res == res && (now - log[f].ts) < 60) {
342+
log[f].rep ++;
343+
ESP_LOGI("LOGGER","[%d] previous entry repeated %d times", sh, log[f].rep);
344+
return;
345+
}
346+
f++;
347+
if (f >= LOG_SIZE) {
348+
f = 0;
349+
}
346350
}
347351
}
348352

@@ -360,36 +364,13 @@ static void log_add(session_t *s, int32_t op, int32_t par, int32_t res) {
360364
log_rear = 0;
361365
}
362366
log_elements++;
363-
ESP_LOGI("LOGGER","[%d] boot:%d cnt:%d usr:%d ts:%d op:%d opd:\"%s\" par:%d res:%d uncnf:%d", sh, config.boot_cnt, log_cnt, usr, now, op, code2str(log_ops, op), par, res, log_elements);
367+
ESP_LOGI("LOGGER","[%d] boot:%d cnt:%d usr:%d ts:%d op:%d opd:\"%s\" par:%d res:%d", sh, config.boot_cnt, log_cnt, usr, now, op, code2str(log_ops, op), par, res);
364368
}
365369

366-
static int log_purge(uint32_t bc, uint32_t lc) {
367-
int purged = 0;
368-
369-
while (log_elements > 0) {
370-
if(log[log_front].bcnt > bc) {
371-
break;
372-
}
373-
if(log[log_front].bcnt == bc && log[log_front].cnt > lc ) {
374-
break;
375-
}
376-
log_front++;
377-
if (log_front >= LOG_SIZE) {
378-
log_front = 0;
379-
}
380-
log_elements--;
381-
purged++;
382-
}
383-
return purged;
384-
}
385-
386-
static void log_append_msgpack(cw_pack_context *pc, int max){
387-
if (max > log_elements){
388-
max = log_elements;
389-
}
390-
cw_pack_cstr(pc, "lg"); cw_pack_array_size(pc, max);
370+
static void log_append_msgpack(cw_pack_context *pc){
371+
cw_pack_cstr(pc, "lg"); cw_pack_array_size(pc, log_elements);
391372
int f = log_front;
392-
for(int n = 0; n < max; n++) {
373+
for(int n = 0; n < log_elements; n++) {
393374
cw_pack_array_size(pc, 8);
394375
cw_pack_unsigned(pc, log[f].bcnt);
395376
cw_pack_unsigned(pc, log[f].cnt);
@@ -872,7 +853,7 @@ static int append_egg(session_t *s, cw_pack_context *out) {
872853
cw_pack_cstr(&pc, "bo"); cw_pack_cstr(&pc, HW_BOARD);
873854
cw_pack_cstr(&pc, "pr"); cw_pack_cstr(&pc, PRODUCT);
874855
cw_pack_cstr(&pc, "ts"); cw_pack_unsigned(&pc, time(NULL));
875-
log_append_msgpack(&pc, 30);
856+
log_append_msgpack(&pc);
876857
if (ota.start) {
877858
cw_pack_cstr(&pc, "st"); cw_pack_boolean(&pc, ota.start);
878859
cw_pack_cstr(&pc, "ha"); cw_pack_bin(&pc, ota.sha256sum, sizeof(ota.sha256sum));
@@ -1033,24 +1014,6 @@ static int process_login_frame(session_t *s) {
10331014
}
10341015
ESP_LOGI("LOGIN", "[%d] User: %d", s->h, s->user);
10351016

1036-
r = cw_unpack_map_search(&upc, "l");
1037-
if (!r) {
1038-
cw_unpack_next(&upc);
1039-
if (upc.return_code == CWP_RC_OK && upc.item.type == CWP_ITEM_ARRAY && upc.item.as.array.size == 2) {
1040-
cw_unpack_next(&upc);
1041-
uint32_t bc = upc.item.as.u64;
1042-
cw_unpack_next(&upc);
1043-
uint32_t lc = upc.item.as.u64;
1044-
int purged = log_purge(bc, lc);
1045-
if (purged > 0) {
1046-
ESP_LOGI("LOGIN", "[%d] purged %d log entries", s->h, purged);
1047-
}
1048-
} else {
1049-
ESP_LOGE("LOGIN", "[%d] malformed \"l\" field", s->h);
1050-
}
1051-
}
1052-
cw_unpack_restore(&upc);
1053-
10541017
r = cw_unpack_map_get_bufptr(&upc, "uk", &buf, &buf_sz);
10551018
if (r){
10561019
ESP_LOGE("LOGIN", "[%d] \"uk\" %s", s->h, cw_unpack_map_strerr(r));

0 commit comments

Comments
 (0)