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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#![no_std]
use core::fmt::{self, Write};
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
static DEFAULT_LOGGER: EtsPrintfLogger = EtsPrintfLogger;
pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(&DEFAULT_LOGGER).map(|()| log::set_max_level(LevelFilter::Info))
}
struct EtsPrintfLogger;
impl log::Log for EtsPrintfLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Info
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
const SIZE: usize = 256;
const SUFFIX: usize = 2;
const ELLIPSES: usize = 3;
let mut buffer = [0u8; SIZE];
let mut writer = Wrapper::new(&mut buffer[..SIZE - SUFFIX]);
let res = write!(writer, "{}", record.args());
let offset = writer.offset();
if res.is_err() && offset >= ELLIPSES {
let ellipses: &[u8; ELLIPSES] = b"...";
buffer[offset - ELLIPSES..offset].copy_from_slice(ellipses);
}
let suffix: &[u8; SUFFIX] = b"\n\0";
buffer[offset..offset + SUFFIX].copy_from_slice(suffix);
unsafe {
esp_idf_sys::ets_printf(buffer.as_ptr() as *const _);
}
}
}
fn flush(&self) {}
}
struct Wrapper<'a> {
buf: &'a mut [u8],
offset: usize,
}
impl<'a> Wrapper<'a> {
fn new(buf: &'a mut [u8]) -> Self {
Wrapper { buf, offset: 0 }
}
fn offset(&self) -> usize {
self.offset
}
}
impl<'a> fmt::Write for Wrapper<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let remainder = &mut self.buf[self.offset..];
let bytes = s.as_bytes();
if remainder.len() < bytes.len() {
return Err(fmt::Error);
}
let remainder = &mut remainder[..bytes.len()];
remainder.copy_from_slice(bytes);
self.offset += bytes.len();
Ok(())
}
}