Struct coap_message_demos::log::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)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn run<S>(stack: &mut S)
where
    S: UdpFullStack,
{
    let mut sock = stack.socket().expect("Can't create a socket");

    let log = Some(coap_message_demos::log::Log::start_once());

    let mut handler = coap_message_demos::full_application_tree(log);

    stack.bind(&mut sock, 5683).expect("Can't bind to port");
    info!("Server is ready.");

    loop {
        match embedded_nal_minimal_coapserver::poll(stack, &mut sock, &mut handler) {
            Err(embedded_nal::nb::Error::WouldBlock) => {
                // See <https://github.com/rust-embedded-community/embedded-nal/issues/47>
                std::thread::sleep(std::time::Duration::from_millis(50));
            }
            e => e.expect("UDP error during send/receive"),
        }
    }
}
More examples
Hide additional examples
examples/std_embedded_nal_minicoaptcpserver.rs (line 15)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn run<S>(stack: &mut S)
where
    S: embedded_nal::TcpFullStack + embedded_nal_tcpextensions::TcpExactStack,
{
    let mut sock = stack.socket().expect("Can't create a socket");

    let log = Some(coap_message_demos::log::Log::start_once());

    let mut handler = coap_message_demos::full_application_tree(log);

    stack.bind(&mut sock, 5683).expect("Can't bind to port");
    info!("Server is ready.");

    let mut pool = embedded_nal_minimal_coaptcpserver::ServerPool::<S, 4, 1152>::new(sock);

    loop {
        pool.poll(stack, &mut handler)
            .expect("Actual error in polling (accepting?)");
        // See <https://github.com/rust-embedded-community/embedded-nal/issues/47>
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
}
examples/coaplite.rs (line 7)
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
fn main() {
    let log = Some(coap_message_demos::log::Log::start_once());

    let mut handler = coap_message_demos::full_application_tree(log);

    let socket = UdpSocket::bind("localhost:5683").unwrap();
    let mut buf = [0; 1280];

    loop {
        let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");

        let packet = Packet::from_bytes(&buf[..size]).unwrap();
        let request = CoapRequest::from_packet(packet, src);

        let extracted = handler.extract_request_data(&request.message);

        let mut response = request.response.unwrap();
        handler.build_response(&mut response.message, extracted);

        let packet = response.message.to_bytes().unwrap();
        socket
            .send_to(&packet[..], &src)
            .expect("Could not send the data");
    }
}
examples/coap_crate.rs (line 13)
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
fn main() {
    let addr = "localhost:5683";

    let log = Some(coap_message_demos::log::Log::start_once());

    Runtime::new().unwrap().block_on(async move {
        let mut server = Server::new(addr).unwrap();
        println!("Server up on {}", addr);
        info!("Server up on {}", addr);

        use coap_handler::Handler;

        let mut handler = coap_message_demos::full_application_tree(log);

        server
            .run(move |mut request| {
                // Having a shared reference to a single handler works for this server because it
                // allows the handler to be FnMut and not Sync -- essentially, because the server
                // promises not to become multithreaded.

                let extracted = handler.extract_request_data(&request.message);

                if let Some(r) = request.response.as_mut() {
                    handler.build_response(&mut r.message, extracted);
                }

                // By running all the processing in the sync part of the function (because the
                // coap-handler doesn't cater for async right now), the &mut handler reference we use
                // does not need to live across possible await points, and is thus usable.

                async { request.response }
            })
            .await
            .unwrap();
    });
}
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.

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<T> for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.