lib.rs (2681B)
1 #![cfg_attr(not(feature = "std"), no_std)] 2 3 extern crate alloc; 4 5 mod error; 6 7 #[cfg(feature = "std")] 8 mod init; 9 #[cfg(feature = "std")] 10 mod options; 11 12 pub use error::{Error, Result}; 13 14 #[cfg(feature = "std")] 15 pub use init::{init_logging, init_stdout}; 16 #[cfg(feature = "std")] 17 pub use options::{LogFileLayout, LoggingOptions}; 18 19 use tracing::{debug, error, info}; 20 21 #[inline] 22 pub fn log_info<S: AsRef<str>>(msg: S) { 23 info!("{}", msg.as_ref()); 24 } 25 26 #[inline] 27 pub fn log_error<S: AsRef<str>>(msg: S) { 28 error!("{}", msg.as_ref()); 29 } 30 31 #[inline] 32 pub fn log_debug<S: AsRef<str>>(msg: S) { 33 debug!("{}", msg.as_ref()); 34 } 35 36 #[cfg(not(feature = "std"))] 37 pub fn init_no_std() -> Result<()> { 38 Ok(()) 39 } 40 41 pub fn init_default() -> Result<()> { 42 #[cfg(feature = "std")] 43 { 44 init_stdout() 45 } 46 #[cfg(not(feature = "std"))] 47 { 48 init_no_std() 49 } 50 } 51 52 #[cfg(test)] 53 mod tests { 54 #[cfg(not(feature = "std"))] 55 use super::{init_default, init_no_std}; 56 use super::{log_debug, log_error, log_info}; 57 58 #[test] 59 fn logging_helpers_are_callable() { 60 log_info("info"); 61 log_error("error"); 62 log_debug("debug"); 63 } 64 65 #[test] 66 fn logging_helpers_cover_enabled_paths() { 67 struct TestSubscriber; 68 69 impl tracing::Subscriber for TestSubscriber { 70 fn enabled(&self, _: &tracing::Metadata<'_>) -> bool { 71 true 72 } 73 74 fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id { 75 tracing::span::Id::from_u64(1) 76 } 77 78 fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {} 79 80 fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {} 81 82 fn event(&self, _: &tracing::Event<'_>) {} 83 84 fn enter(&self, _: &tracing::span::Id) {} 85 86 fn exit(&self, _: &tracing::span::Id) {} 87 } 88 89 log_info("info"); 90 log_error("error"); 91 log_debug("debug"); 92 let _ = tracing::subscriber::set_global_default(TestSubscriber); 93 let span = tracing::span!(tracing::Level::INFO, "log_span", value = 1); 94 let other = tracing::span!(tracing::Level::INFO, "log_span_other"); 95 span.record("value", &2); 96 span.follows_from(other.id()); 97 let _enter = span.enter(); 98 tracing::event!(tracing::Level::INFO, "log_event"); 99 drop(_enter); 100 log_info("info"); 101 log_error("error"); 102 log_debug("debug"); 103 } 104 105 #[cfg(not(feature = "std"))] 106 #[test] 107 fn no_std_init_paths_are_callable() { 108 assert!(init_no_std().is_ok()); 109 assert!(init_default().is_ok()); 110 } 111 }