1#![deny(missing_docs)]
5
6#[cfg(target_os = "fuchsia")]
13mod fuchsia;
14#[cfg(target_os = "fuchsia")]
15use self::fuchsia as implementation;
16
17#[cfg(not(target_os = "fuchsia"))]
18mod portable;
19#[cfg(not(target_os = "fuchsia"))]
20use self::portable as implementation;
21
22pub use diagnostics_log_types::Severity;
23pub use implementation::*;
24
25pub(crate) fn install_panic_hook(prefix: Option<&'static str>) {
27 let previous_hook = std::panic::take_hook();
28 std::panic::set_hook(Box::new(move |info| {
29 let message = format!("{info}");
30 let prefix = prefix.unwrap_or("PANIC");
31 log::logger().log(
32 &log::RecordBuilder::new()
33 .level(log::Level::Error)
34 .line(Some(info.location().unwrap().line()))
35 .file(Some(info.location().unwrap().file()))
36 .key_values(&[("info", message.as_str())])
37 .args(format_args!("{prefix}"))
38 .build(),
39 );
40 previous_hook(info);
41 }));
42}
43
44pub trait SeverityExt {
46 fn severity(&self) -> Severity;
48
49 fn raw_severity(&self) -> u8 {
51 self.severity() as u8
52 }
53}
54
55impl SeverityExt for log::Metadata<'_> {
56 fn severity(&self) -> Severity {
57 match self.level() {
58 log::Level::Error => Severity::Error,
59 log::Level::Warn => Severity::Warn,
60 log::Level::Info => Severity::Info,
61 log::Level::Debug => Severity::Debug,
62 log::Level::Trace => Severity::Trace,
63 }
64 }
65}
66
67pub struct PublishOptions<'t> {
71 pub(crate) publisher: PublisherOptions<'t>,
72 pub(crate) install_panic_hook: bool,
73 pub(crate) panic_prefix: Option<&'static str>,
74}
75
76impl PublishOptions<'_> {
77 pub fn install_panic_hook(mut self, enable: bool) -> Self {
81 self.install_panic_hook = enable;
82 self
83 }
84
85 #[cfg(target_os = "fuchsia")]
88 pub fn always_log_file_line(mut self) -> Self {
89 self.publisher.always_log_file_line = true;
90 self
91 }
92
93 pub fn panic_prefix(mut self, prefix: &'static str) -> Self {
95 self.panic_prefix = Some(prefix);
96 self
97 }
98}
99
100macro_rules! publisher_options {
101 ($(($name:ident, $self:ident, $($self_arg:ident),*)),*) => {
102 $(
103 impl<'t> $name<'t> {
104 pub fn tags(mut $self, tags: &'t [&'t str]) -> Self {
111 let this = &mut $self$(.$self_arg)*;
112 this.tags = tags;
113 $self
114 }
115
116 pub fn enable_metatag(mut $self, metatag: Metatag) -> Self {
120 let this = &mut $self$(.$self_arg)*;
121 this.metatags.insert(metatag);
122 $self
123 }
124
125 pub fn minimum_severity(mut $self, severity: impl Into<Severity>) -> Self {
129 let this = &mut $self$(.$self_arg)*;
130 this.interest.min_severity = Some(severity.into().into());
131 $self
132 }
133 }
134 )*
135 };
136}
137
138publisher_options!((PublisherOptions, self,), (PublishOptions, self, publisher));