1use std::env::{self, VarError};
37use std::fmt::{self, Display};
38use std::fs::File;
39use std::io::{self, IsTerminal};
40use std::sync::Mutex;
41
42use tracing::dispatcher::SetGlobalDefaultError;
43use tracing::{Event, Subscriber};
44use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
45use tracing_subscriber::fmt::FmtContext;
46use tracing_subscriber::fmt::format::{self, FmtSpan, FormatEvent, FormatFields};
47use tracing_subscriber::fmt::writer::BoxMakeWriter;
48use tracing_subscriber::layer::SubscriberExt;
49use tracing_subscriber::{Layer, Registry};
50pub use {tracing, tracing_core, tracing_subscriber};
52
53pub struct LoggerConfig {
56 pub filter: Result<String, VarError>,
57 pub color_logs: Result<String, VarError>,
58 pub verbose_entry_exit: Result<String, VarError>,
59 pub verbose_thread_ids: Result<String, VarError>,
60 pub backtrace: Result<String, VarError>,
61 pub json: Result<String, VarError>,
62 pub output_target: Result<String, VarError>,
63 pub wraptree: Result<String, VarError>,
64 pub lines: Result<String, VarError>,
65}
66
67impl LoggerConfig {
68 pub fn from_env(env: &str) -> Self {
69 LoggerConfig {
71 filter: env::var(env),
72 color_logs: env::var(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_COLOR", env))
})format!("{env}_COLOR")),
73 verbose_entry_exit: env::var(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_ENTRY_EXIT", env))
})format!("{env}_ENTRY_EXIT")),
74 verbose_thread_ids: env::var(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_THREAD_IDS", env))
})format!("{env}_THREAD_IDS")),
75 backtrace: env::var(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_BACKTRACE", env))
})format!("{env}_BACKTRACE")),
76 wraptree: env::var(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_WRAPTREE", env))
})format!("{env}_WRAPTREE")),
77 lines: env::var(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_LINES", env))
})format!("{env}_LINES")),
78 json: env::var(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_FORMAT_JSON", env))
})format!("{env}_FORMAT_JSON")),
79 output_target: env::var(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_OUTPUT_TARGET", env))
})format!("{env}_OUTPUT_TARGET")),
80 }
81 }
82}
83
84pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
86 init_logger_with_additional_layer(cfg, Registry::default)
87}
88
89pub trait BuildSubscriberRet:
95 tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync
96{
97}
98
99impl<
100 T: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync,
101> BuildSubscriberRet for T
102{
103}
104
105pub fn init_logger_with_additional_layer<F, T>(
109 cfg: LoggerConfig,
110 build_subscriber: F,
111) -> Result<(), Error>
112where
113 F: FnOnce() -> T,
114 T: BuildSubscriberRet,
115{
116 let filter = match cfg.filter {
117 Ok(env) => EnvFilter::new(env),
118 _ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
119 };
120
121 let color_logs = match cfg.color_logs {
122 Ok(value) => match value.as_ref() {
123 "always" => true,
124 "never" => false,
125 "auto" => stderr_isatty(),
126 _ => return Err(Error::InvalidColorValue(value)),
127 },
128 Err(VarError::NotPresent) => stderr_isatty(),
129 Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
130 };
131
132 let verbose_entry_exit = match cfg.verbose_entry_exit {
133 Ok(v) => &v != "0",
134 Err(_) => false,
135 };
136
137 let verbose_thread_ids = match cfg.verbose_thread_ids {
138 Ok(v) => &v == "1",
139 Err(_) => false,
140 };
141
142 let lines = match cfg.lines {
143 Ok(v) => &v == "1",
144 Err(_) => false,
145 };
146
147 let json = match cfg.json {
148 Ok(v) => &v == "1",
149 Err(_) => false,
150 };
151
152 let output_target: BoxMakeWriter = match cfg.output_target {
153 Ok(v) => match File::options().create(true).write(true).truncate(true).open(&v) {
154 Ok(i) => BoxMakeWriter::new(Mutex::new(i)),
155 Err(e) => {
156 {
::std::io::_eprint(format_args!("couldn\'t open {0} as a log target: {1:?}\n",
v, e));
};eprintln!("couldn't open {v} as a log target: {e:?}");
157 BoxMakeWriter::new(io::stderr)
158 }
159 },
160 Err(_) => BoxMakeWriter::new(io::stderr),
161 };
162
163 let layer = if json {
164 let format = tracing_subscriber::fmt::format()
165 .json()
166 .with_span_list(true)
167 .with_source_location(true);
168
169 let fmt_layer = tracing_subscriber::fmt::layer()
170 .json()
171 .event_format(format)
172 .with_writer(output_target)
173 .with_target(true)
174 .with_ansi(false)
175 .with_thread_ids(verbose_thread_ids)
176 .with_thread_names(verbose_thread_ids)
177 .with_span_events(FmtSpan::ACTIVE);
178 Layer::boxed(fmt_layer)
179 } else {
180 let mut layer = tracing_tree::HierarchicalLayer::default()
181 .with_writer(output_target)
182 .with_ansi(color_logs)
183 .with_targets(true)
184 .with_verbose_exit(verbose_entry_exit)
185 .with_verbose_entry(verbose_entry_exit)
186 .with_indent_amount(2)
187 .with_indent_lines(lines)
188 .with_thread_ids(verbose_thread_ids)
189 .with_thread_names(verbose_thread_ids);
190
191 if let Ok(v) = cfg.wraptree {
192 match v.parse::<usize>() {
193 Ok(v) => layer = layer.with_wraparound(v),
194 Err(_) => return Err(Error::InvalidWraptree(v)),
195 }
196 }
197
198 Layer::boxed(layer)
199 };
200
201 let subscriber = build_subscriber();
202 match cfg.backtrace {
204 Ok(backtrace_target) => {
205 let fmt_layer = tracing_subscriber::fmt::layer()
206 .with_writer(io::stderr)
207 .without_time()
208 .event_format(BacktraceFormatter { backtrace_target });
209 let subscriber = subscriber.with(layer).with(fmt_layer).with(filter);
210 tracing::subscriber::set_global_default(subscriber)?;
211 }
212 Err(_) => {
213 tracing::subscriber::set_global_default(subscriber.with(layer).with(filter))?;
214 }
215 };
216
217 Ok(())
218}
219
220struct BacktraceFormatter {
221 backtrace_target: String,
222}
223
224impl<S, N> FormatEvent<S, N> for BacktraceFormatter
225where
226 S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
227 N: for<'a> FormatFields<'a> + 'static,
228{
229 fn format_event(
230 &self,
231 _ctx: &FmtContext<'_, S, N>,
232 mut writer: format::Writer<'_>,
233 event: &Event<'_>,
234 ) -> fmt::Result {
235 let target = event.metadata().target();
236 if !target.contains(&self.backtrace_target) {
237 return Ok(());
238 }
239 let backtrace = std::backtrace::Backtrace::force_capture();
242 writer.write_fmt(format_args!("stack backtrace: \n{0:?}\n", backtrace))writeln!(writer, "stack backtrace: \n{backtrace:?}")
243 }
244}
245
246pub fn stdout_isatty() -> bool {
247 io::stdout().is_terminal()
248}
249
250pub fn stderr_isatty() -> bool {
251 io::stderr().is_terminal()
252}
253
254#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Error {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Error::InvalidColorValue(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidColorValue", &__self_0),
Error::NonUnicodeColorValue =>
::core::fmt::Formatter::write_str(f, "NonUnicodeColorValue"),
Error::InvalidWraptree(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidWraptree", &__self_0),
Error::AlreadyInit(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"AlreadyInit", &__self_0),
}
}
}Debug)]
255pub enum Error {
256 InvalidColorValue(String),
257 NonUnicodeColorValue,
258 InvalidWraptree(String),
259 AlreadyInit(SetGlobalDefaultError),
260}
261
262impl std::error::Error for Error {}
263
264impl Display for Error {
265 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
266 match self {
267 Error::InvalidColorValue(value) => formatter.write_fmt(format_args!("invalid log color value \'{0}\': expected one of always, never, or auto",
value))write!(
268 formatter,
269 "invalid log color value '{value}': expected one of always, never, or auto",
270 ),
271 Error::NonUnicodeColorValue => formatter.write_fmt(format_args!("non-Unicode log color value: expected one of always, never, or auto"))write!(
272 formatter,
273 "non-Unicode log color value: expected one of always, never, or auto",
274 ),
275 Error::InvalidWraptree(value) => formatter.write_fmt(format_args!("invalid log WRAPTREE value \'{0}\': expected a non-negative integer",
value))write!(
276 formatter,
277 "invalid log WRAPTREE value '{value}': expected a non-negative integer",
278 ),
279 Error::AlreadyInit(tracing_error) => Display::fmt(tracing_error, formatter),
280 }
281 }
282}
283
284impl From<SetGlobalDefaultError> for Error {
285 fn from(tracing_error: SetGlobalDefaultError) -> Self {
286 Error::AlreadyInit(tracing_error)
287 }
288}