Skip to main content

wasefire_logger/
lib.rs

1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![cfg_attr(not(any(test, feature = "log")), no_std)]
16
17#[cfg(not(feature = "defmt"))]
18mod no_defmt {
19    use core::fmt::{Debug, Display, Formatter, Result};
20    pub use core::panic;
21
22    pub struct Debug2Format<T>(pub T);
23    impl<T: Debug> Display for Debug2Format<T> {
24        fn fmt(&self, f: &mut Formatter<'_>) -> Result {
25            self.0.fmt(f)
26        }
27    }
28
29    pub struct Display2Format<T>(pub T);
30    impl<T: Display> Display for Display2Format<T> {
31        fn fmt(&self, f: &mut Formatter<'_>) -> Result {
32            self.0.fmt(f)
33        }
34    }
35}
36
37#[cfg(not(any(feature = "log", feature = "defmt")))]
38mod custom {
39    #[macro_export]
40    macro_rules! println {
41        ($($args: expr),*$(,)?) => { if false { $(let _ = $args;)* } };
42    }
43
44    #[macro_export]
45    macro_rules! trace {
46        ($($args: expr),*$(,)?) => { if false { $(let _ = $args;)* } };
47    }
48
49    #[macro_export]
50    macro_rules! debug {
51        ($($args: expr),*$(,)?) => { if false { $(let _ = $args;)* } };
52    }
53
54    #[macro_export]
55    macro_rules! info {
56        ($($args: expr),*$(,)?) => { if false { $(let _ = $args;)* } };
57    }
58
59    #[macro_export]
60    macro_rules! warn {
61        ($($args: expr),*$(,)?) => { if false { $(let _ = $args;)* } };
62    }
63
64    #[macro_export]
65    macro_rules! error {
66        ($($args: expr),*$(,)?) => { if false { $(let _ = $args;)* } };
67    }
68}
69
70#[cfg(feature = "log")]
71pub use std::println;
72
73#[cfg(feature = "defmt")]
74pub use defmt::{
75    Debug2Format, Display2Format, debug, error, flush, info, panic, println, trace, warn,
76};
77#[cfg(feature = "log")]
78pub use log::{debug, error, info, trace, warn};
79#[cfg(not(feature = "defmt"))]
80pub use no_defmt::*;
81
82#[cfg(feature = "log")]
83pub fn flush() {
84    log::logger().flush();
85}
86#[cfg(not(any(feature = "log", feature = "defmt")))]
87pub fn flush() {}
88
89#[cfg(not(feature = "defmt"))]
90pub trait MaybeFormat {}
91#[cfg(not(feature = "defmt"))]
92impl<T> MaybeFormat for T {}
93
94#[cfg(feature = "defmt")]
95pub trait MaybeFormat: defmt::Format {}
96#[cfg(feature = "defmt")]
97impl<T: defmt::Format> MaybeFormat for T {}