Log

Struct Log 

Source
pub struct Log { /* private fields */ }
Expand description

A ring-buffered log structure

Implementations§

Source§

impl Log

Source

pub fn new() -> Self

Source

pub fn init(&'static self) -> Result<(), SetLoggerError>

Source

pub fn start_once() -> &'static Self

Allocate a log in static memory, and set it up

Examples found in repository?
examples/std_embedded_nal_minicoapserver.rs (line 17)
11fn run<S>(stack: &mut S)
12where
13    S: UdpFullStack,
14{
15    let mut sock = stack.socket().expect("Can't create a socket");
16
17    let log = Some(coap_message_demos::log::Log::start_once());
18
19    let mut handler = coap_message_demos::full_application_tree(log);
20
21    stack.bind(&mut sock, 5683).expect("Can't bind to port");
22    info!("Server is ready.");
23
24    loop {
25        match embedded_nal_minimal_coapserver::poll(stack, &mut sock, &mut handler) {
26            Err(embedded_nal::nb::Error::WouldBlock) => {
27                // See <https://github.com/rust-embedded-community/embedded-nal/issues/47>
28                std::thread::sleep(std::time::Duration::from_millis(50));
29            }
30            e => e.expect("UDP error during send/receive"),
31        }
32    }
33}
More examples
Hide additional examples
examples/std_embedded_nal_minicoaptcpserver.rs (line 15)
9fn run<S>(stack: &mut S)
10where
11    S: embedded_nal::TcpFullStack + embedded_nal_tcpextensions::TcpExactStack,
12{
13    let mut sock = stack.socket().expect("Can't create a socket");
14
15    let log = Some(coap_message_demos::log::Log::start_once());
16
17    let mut handler = coap_message_demos::full_application_tree(log);
18
19    stack.bind(&mut sock, 5683).expect("Can't bind to port");
20    info!("Server is ready.");
21
22    let mut pool = embedded_nal_minimal_coaptcpserver::ServerPool::<S, 4, 1152>::new(sock);
23
24    loop {
25        pool.poll(stack, &mut handler)
26            .expect("Actual error in polling (accepting?)");
27        // See <https://github.com/rust-embedded-community/embedded-nal/issues/47>
28        std::thread::sleep(std::time::Duration::from_millis(50));
29    }
30}
examples/std_embedded_nal_coap.rs (line 15)
6async fn run<S>(stack: &mut S)
7where
8    S: embedded_nal_async::UdpStack,
9{
10    let mut sock = stack
11        .bind_multiple(core::net::SocketAddr::new("::".parse().unwrap(), 5683))
12        .await
13        .expect("Can't create a socket");
14
15    let log = Some(coap_message_demos::log::Log::start_once());
16
17    let mut handler = coap_message_demos::full_application_tree(log);
18
19    info!("Server is ready.");
20
21    let coap = embedded_nal_coap::CoAPShared::<3>::new();
22    let (client, server) = coap.split();
23
24    // going with an embassy_futures join instead of an async_std::task::spawn b/c CoAPShared is not
25    // Sync, and async_std expects to work in multiple threads
26    embassy_futures::join::join(
27        async {
28            use rand::SeedableRng;
29            server
30                .run(
31                    &mut sock,
32                    &mut handler,
33                    &mut rand::rngs::StdRng::from_entropy(),
34                )
35                .await
36                .expect("UDP error")
37        },
38        run_client_operations(client),
39    )
40    .await;
41}
examples/coaplite.rs (line 8)
7fn main() {
8    let log = Some(coap_message_demos::log::Log::start_once());
9
10    let mut handler = coap_message_demos::full_application_tree(log);
11
12    let socket = UdpSocket::bind("localhost:5683").unwrap();
13    let mut buf = [0; 1280];
14
15    loop {
16        let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");
17
18        let packet = Packet::from_bytes(&buf[..size]).unwrap();
19        let request = CoapRequest::from_packet(packet, src);
20
21        let extracted = handler.extract_request_data(&request.message);
22
23        let mut response = request.response.unwrap();
24        match extracted {
25            Ok(extracted) => {
26                if let Err(e2) = handler.build_response(&mut response.message, extracted) {
27                    response.message.payload = Default::default();
28                    response.message.clear_all_options();
29                    e2.render(&mut response.message).unwrap();
30                }
31            }
32            Err(e) => {
33                e.render(&mut response.message).unwrap();
34            }
35        }
36
37        let packet = response.message.to_bytes().unwrap();
38        socket
39            .send_to(&packet[..], &src)
40            .expect("Could not send the data");
41    }
42}
examples/coap_crate.rs (line 11)
8fn main() {
9    let addr = "localhost:5683";
10
11    let log = coap_message_demos::log::Log::start_once();
12
13    Runtime::new().unwrap().block_on(async move {
14        let server = Server::new_udp(addr).unwrap();
15        println!("Server up on {}", addr);
16        info!("Server up on {}", addr);
17
18        use coap_handler::Handler;
19
20        // Unlike in coap 0.11, the run closure is now Fn, so we can't have an exclusive reference
21        // to a handler but need to fan out. To avoid the need for a mutex, we just use a handler
22        // factory.
23        //
24        // Note that this may have "funny" effects w/rt the CBOR editable items, which are shared
25        // only across handlers spawned from a single full_application_tree call.
26        let handler = || coap_message_demos::full_application_tree(Some(log));
27
28        server
29            .run(
30                move |mut request: Box<coap_lite::CoapRequest<std::net::SocketAddr>>| {
31                    let mut handler = handler();
32
33                    // We can just unwrap because the tokio based surver unwinds crashing handlers.
34                    // We could still choose to rather render the errors, because a) it'd provide
35                    // concrete information to the client and b) it sends a response instead of
36                    // leaving the client hanging -- see the coap-lite example for how that is
37                    // done.
38
39                    let extracted = handler.extract_request_data(&request.message).unwrap();
40
41                    if let Some(r) = request.response.as_mut() {
42                        handler.build_response(&mut r.message, extracted).unwrap();
43                    }
44
45                    async { request }
46                },
47            )
48            .await
49            .unwrap();
50    });
51}
Source

pub fn handler(&self) -> BufferHandler<'_, 1024>

Obtain a coap_handler::Handler implementation from the Log

This is exposed as a separate type rather than on Log in order to reuse the same handler implementation for more than just the log.

Trait Implementations§

Source§

impl Log for Log

Source§

fn enabled(&self, metadata: &Metadata<'_>) -> bool

Determines if a log message with the specified metadata would be logged. Read more
Source§

fn log(&self, record: &Record<'_>)

Logs the Record. Read more
Source§

fn flush(&self)

Flushes any buffered records. Read more

Auto Trait Implementations§

§

impl !Freeze for Log

§

impl !RefUnwindSafe for Log

§

impl Send for Log

§

impl Sync for Log

§

impl Unpin for Log

§

impl UnwindSafe for Log

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.