fiatlux/firmware/log.cpp

67 lines
1.6 KiB
C++
Raw Normal View History

//
// Created by jedi on 18.11.21.
//
#include "log.h"
#include <espressif/esp_common.h>
constexpr unsigned syslog_buffer_size = 1024;
2022-09-22 19:48:36 +00:00
char syslog_buf[syslog_buffer_size + 1];
volatile unsigned head = 0;
volatile unsigned streams = 0;
2022-09-24 05:48:47 +00:00
const char hex_lookup[] = "0123456789ABCDEF";
char str_i32[] = "00000000";
extern "C" void syslog_i32(const uint32_t val) {
2023-01-19 06:07:24 +00:00
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];
2022-09-24 05:48:47 +00:00
//
syslog(str_i32);
}
extern "C" void syslog(const char *msg) {
2023-02-12 05:53:41 +00:00
printf("%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);
}
2022-09-13 03:29:26 +00:00
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--;
}