fiatlux/firmware/log.cpp
2023-02-16 11:38:59 +01:00

67 lines
No EOL
1.6 KiB
C++

//
// Created by jedi on 18.11.21.
//
#include "log.h"
#include <espressif/esp_common.h>
constexpr unsigned syslog_buffer_size = 1024;
char syslog_buf[syslog_buffer_size + 1];
volatile unsigned head = 0;
volatile unsigned streams = 0;
const char hex_lookup[] = "0123456789ABCDEF";
char str_i32[] = "00000000";
extern "C" void syslog_i32(const uint32_t val) {
str_i32[7] = hex_lookup[val & 0xF];
str_i32[6] = hex_lookup[(val >> 4) & 0xF];
str_i32[5] = hex_lookup[(val >> 8) & 0xF];
str_i32[4] = hex_lookup[(val >> 12) & 0xF];
str_i32[3] = hex_lookup[(val >> 16) & 0xF];
str_i32[2] = hex_lookup[(val >> 20) & 0xF];
str_i32[1] = hex_lookup[(val >> 24) & 0xF];
str_i32[0] = hex_lookup[(val >> 28) & 0xF];
//
syslog(str_i32);
}
extern "C" void syslog(const char *msg) {
//printf("syslog> %s", msg);
while (char c = *msg++) {
syslog_buf[head++ % syslog_buffer_size] = c;
}
syslog_buf[head] = 0;
}
unsigned syslog_current_tail() {
if(head < syslog_buffer_size)
return 0;
return head + 1 - syslog_buffer_size;
}
unsigned syslog_data_after(unsigned local_tail) {
if(local_tail > head)
return 0;
return (head % syslog_buffer_size) - (local_tail % syslog_buffer_size);
}
extern "C" int syslog_copy_out(char *out, unsigned len, unsigned local_tail) {
unsigned cnt = 0;
while (cnt < syslog_data_after(local_tail) && cnt < len) {
out[cnt] = syslog_buf[local_tail % syslog_buffer_size + cnt];
cnt++;
}
return cnt;
}
extern "C" void syslog_attach() {
streams++;
}
extern "C" void syslog_detach() {
streams--;
}