Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #![cfg_attr(test, deny(warnings))]
2 #![deny(missing_docs)]
3 #![doc(html_root_url = "https://docs.rs/pretty_env_logger/0.5.0")]
4
5 //! A logger configured via an environment variable which writes to standard
6 //! error with nice colored output for log levels.
7 //!
8 //! ## Example
9 //!
10 //! ```
11 //! extern crate pretty_env_logger;
12 //! #[macro_use] extern crate log;
13 //!
14 //! fn main() {
15 //! pretty_env_logger::init();
16 //!
17 //! trace!("a trace example");
18 //! debug!("deboogging");
19 //! info!("such information");
20 //! warn!("o_O");
21 //! error!("boom");
22 //! }
23 //! ```
24 //!
25 //! Run the program with the environment variable `RUST_LOG=trace`.
26 //!
27 //! ## Defaults
28 //!
29 //! The defaults can be setup by calling `init()` or `try_init()` at the start
30 //! of the program.
31 //!
32 //! ## Enable logging
33 //!
34 //! This crate uses [env_logger][] internally, so the same ways of enabling
35 //! logs through an environment variable are supported.
36 //!
37 //! [env_logger]: https://docs.rs/env_logger
38
39 #[doc(hidden)]
40 pub extern crate env_loggerrust-analyzer cargo pretty_env_logger 0.5.0 env_logger/;
41
42 extern crate logrust-analyzer cargo pretty_env_logger 0.5.0 log/;
43
44 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
45 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#};
46
47 use env_loggerrust-analyzer cargo env_logger 0.11.11 crate/::{
•48 fmtrust-analyzer cargo env_logger 0.11.11 fmt/::stylerust-analyzer cargo anstyle 1.0.14 crate/::{AnsiColorrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#, Colorrust-analyzer cargo anstyle 1.0.14 color/Color#, Stylerust-analyzer cargo anstyle 1.0.14 style/Style#},
49 Builderrust-analyzer cargo env_logger 0.11.11 logger/Builder#,
50 };
51 use logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#;
52
•53 const BSTYLErust-analyzer cargo pretty_env_logger 0.5.0 BSTYLE.: Stylerust-analyzer cargo anstyle 1.0.14 style/Style# = Stylerust-analyzer cargo anstyle 1.0.14 style/Style#::newrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]new().().boldrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]bold().();
54
55 /// Initializes the global logger with a pretty env logger.
56 ///
57 /// This should be called early in the execution of a Rust program, and the
58 /// global logger may only be initialized once. Future initialization attempts
59 /// will return an error.
60 ///
61 /// # Panics
62 ///
63 /// This function fails to set the global logger if one has already been set.
64 pub fn initrust-analyzer cargo pretty_env_logger 0.5.0 init().() {
65 try_initrust-analyzer cargo pretty_env_logger 0.5.0 try_init().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
66 }
67
68 /// Initializes the global logger with a timed pretty env logger.
69 ///
70 /// This should be called early in the execution of a Rust program, and the
71 /// global logger may only be initialized once. Future initialization attempts
72 /// will return an error.
73 ///
74 /// # Panics
75 ///
76 /// This function fails to set the global logger if one has already been set.
77 pub fn init_timedrust-analyzer cargo pretty_env_logger 0.5.0 init_timed().() {
78 try_init_timedrust-analyzer cargo pretty_env_logger 0.5.0 try_init_timed().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
79 }
80
81 /// Initializes the global logger with a pretty env logger.
82 ///
83 /// This should be called early in the execution of a Rust program, and the
84 /// global logger may only be initialized once. Future initialization attempts
85 /// will return an error.
86 ///
87 /// # Errors
88 ///
89 /// This function fails to set the global logger if one has already been set.
90 pub fn try_initrust-analyzer cargo pretty_env_logger 0.5.0 try_init().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), logrust-analyzer cargo log 0.4.34 crate/::SetLoggerErrorrust-analyzer cargo log 0.4.34 SetLoggerError#> {
91 try_init_custom_envrust-analyzer cargo pretty_env_logger 0.5.0 try_init_custom_env().("RUST_LOG")
92 }
93
94 /// Initializes the global logger with a timed pretty env logger.
95 ///
96 /// This should be called early in the execution of a Rust program, and the
97 /// global logger may only be initialized once. Future initialization attempts
98 /// will return an error.
99 ///
100 /// # Errors
101 ///
102 /// This function fails to set the global logger if one has already been set.
103 pub fn try_init_timedrust-analyzer cargo pretty_env_logger 0.5.0 try_init_timed().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), logrust-analyzer cargo log 0.4.34 crate/::SetLoggerErrorrust-analyzer cargo log 0.4.34 SetLoggerError#> {
104 try_init_timed_custom_envrust-analyzer cargo pretty_env_logger 0.5.0 try_init_timed_custom_env().("RUST_LOG")
105 }
106
107 /// Initialized the global logger with a pretty env logger, with a custom variable name.
108 ///
109 /// This should be called early in the execution of a Rust program, and the
110 /// global logger may only be initialized once. Future initialization attempts
111 /// will return an error.
112 ///
113 /// # Panics
114 ///
115 /// This function fails to set the global logger if one has already been set.
116 pub fn init_custom_envrust-analyzer cargo pretty_env_logger 0.5.0 init_custom_env().(environment_variable_name: &str) {
117 try_init_custom_envrust-analyzer cargo pretty_env_logger 0.5.0 try_init_custom_env().(environment_variable_name).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
118 }
119
120 /// Initialized the global logger with a pretty env logger, with a custom variable name.
121 ///
122 /// This should be called early in the execution of a Rust program, and the
123 /// global logger may only be initialized once. Future initialization attempts
124 /// will return an error.
125 ///
126 /// # Errors
127 ///
128 /// This function fails to set the global logger if one has already been set.
129 pub fn try_init_custom_envrust-analyzer cargo pretty_env_logger 0.5.0 try_init_custom_env().(environment_variable_name: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), logrust-analyzer cargo log 0.4.34 crate/::SetLoggerErrorrust-analyzer cargo log 0.4.34 SetLoggerError#> {
130 let mut builder = formatted_builderrust-analyzer cargo pretty_env_logger 0.5.0 formatted_builder().();
131
132 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) = ::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().(environment_variable_name) {
133 builder.parse_filtersrust-analyzer cargo env_logger 0.11.11 logger/impl#[Builder]parse_filters().(&s);
134 }
135
136 builder.try_initrust-analyzer cargo env_logger 0.11.11 logger/impl#[Builder]try_init().()
137 }
138
139 /// Initialized the global logger with a timed pretty env logger, with a custom variable name.
140 ///
141 /// This should be called early in the execution of a Rust program, and the
142 /// global logger may only be initialized once. Future initialization attempts
143 /// will return an error.
144 ///
145 /// # Errors
146 ///
147 /// This function fails to set the global logger if one has already been set.
148 pub fn try_init_timed_custom_envrust-analyzer cargo pretty_env_logger 0.5.0 try_init_timed_custom_env().(
149 environment_variable_name: &str,
150 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), logrust-analyzer cargo log 0.4.34 crate/::SetLoggerErrorrust-analyzer cargo log 0.4.34 SetLoggerError#> {
151 let mut builder = formatted_timed_builderrust-analyzer cargo pretty_env_logger 0.5.0 formatted_timed_builder().();
152
153 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) = ::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().(environment_variable_name) {
154 builder.parse_filtersrust-analyzer cargo env_logger 0.11.11 logger/impl#[Builder]parse_filters().(&s);
155 }
156
157 builder.try_initrust-analyzer cargo env_logger 0.11.11 logger/impl#[Builder]try_init().()
158 }
159
160 /// Returns a `env_logger::Builder` for further customization.
161 ///
162 /// This method will return a colored and formatted `env_logger::Builder`
163 /// for further customization. Refer to env_logger::Build crate documentation
164 /// for further details and usage.
165 pub fn formatted_builderrust-analyzer cargo pretty_env_logger 0.5.0 formatted_builder().() -> Builderrust-analyzer cargo env_logger 0.11.11 logger/Builder# {
166 let mut builder = Builderrust-analyzer cargo env_logger 0.11.11 logger/Builder#::newrust-analyzer cargo env_logger 0.11.11 logger/impl#[Builder]new().();
167
168 builder.formatrust-analyzer cargo env_logger 0.11.11 logger/impl#[Builder]format().(|f, record| {
169 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#;
170
171 let target = record.targetrust-analyzer cargo log 0.4.34 impl#[`Record<'a>`]target().();
172 let max_width = max_target_widthrust-analyzer cargo pretty_env_logger 0.5.0 max_target_width().(target);
173
•174 let (lstyle, level) = colored_levelrust-analyzer cargo pretty_env_logger 0.5.0 colored_level().(record.levelrust-analyzer cargo log 0.4.34 impl#[`Record<'a>`]level().());
175
•176 let target = Paddedrust-analyzer cargo pretty_env_logger 0.5.0 Padded# {
177 valuerust-analyzer cargo pretty_env_logger 0.5.0 Padded#value.: target,
178 widthrust-analyzer cargo pretty_env_logger 0.5.0 Padded#width.: max_width,
•179 };
180
•181 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(
182 f,
183 " {}{}{:#} {}{}{:#} > {}",
184 lstyle,
185 level,
186 lstyle,
187 BSTYLErust-analyzer cargo pretty_env_logger 0.5.0 BSTYLE.,
188 target,
189 BSTYLErust-analyzer cargo pretty_env_logger 0.5.0 BSTYLE.,
190 record.argsrust-analyzer cargo log 0.4.34 impl#[`Record<'a>`]args().(),
191 )
192 });
193
194 builder
195 }
196
197 /// Returns a `env_logger::Builder` for further customization.
198 ///
199 /// This method will return a colored and time formatted `env_logger::Builder`
200 /// for further customization. Refer to env_logger::Build crate documentation
201 /// for further details and usage.
202 pub fn formatted_timed_builderrust-analyzer cargo pretty_env_logger 0.5.0 formatted_timed_builder().() -> Builderrust-analyzer cargo env_logger 0.11.11 logger/Builder# {
203 let mut builder = Builderrust-analyzer cargo env_logger 0.11.11 logger/Builder#::newrust-analyzer cargo env_logger 0.11.11 logger/impl#[Builder]new().();
204
205 builder.formatrust-analyzer cargo env_logger 0.11.11 logger/impl#[Builder]format().(|f, record| {
206 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#;
207 let target = record.targetrust-analyzer cargo log 0.4.34 impl#[`Record<'a>`]target().();
208 let max_width = max_target_widthrust-analyzer cargo pretty_env_logger 0.5.0 max_target_width().(target);
209
•210 let (lstyle, level) = colored_levelrust-analyzer cargo pretty_env_logger 0.5.0 colored_level().(record.levelrust-analyzer cargo log 0.4.34 impl#[`Record<'a>`]level().());
211
•212 let target = Paddedrust-analyzer cargo pretty_env_logger 0.5.0 Padded# {
213 valuerust-analyzer cargo pretty_env_logger 0.5.0 Padded#value.: target,
214 widthrust-analyzer cargo pretty_env_logger 0.5.0 Padded#width.: max_width,
•215 };
216
217 let time = f.timestamp_millisrust-analyzer cargo env_logger 0.11.11 fmt/humantime/impl#[Formatter]timestamp_millis().();
218
•219 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(
220 f,
221 " {}{}{:#} {} {}{}{:#} > {}",
222 lstyle,
223 time,
224 lstyle,
225 level,
226 BSTYLErust-analyzer cargo pretty_env_logger 0.5.0 BSTYLE.,
227 target,
228 BSTYLErust-analyzer cargo pretty_env_logger 0.5.0 BSTYLE.,
229 record.argsrust-analyzer cargo log 0.4.34 impl#[`Record<'a>`]args().(),
230 )
231 });
232
233 builder
234 }
235
236 struct Paddedrust-analyzer cargo pretty_env_logger 0.5.0 Padded#<T> {
237 valuerust-analyzer cargo pretty_env_logger 0.5.0 Padded#value.: T,
238 widthrust-analyzer cargo pretty_env_logger 0.5.0 Padded#width.: usize,
239 }
240
241 impl<T: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Paddedrust-analyzer cargo pretty_env_logger 0.5.0 Padded#<T> {
242 fn fmtrust-analyzer cargo pretty_env_logger 0.5.0 impl#[`Padded<T>`][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
243 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{: <width$}", self.valuerust-analyzer cargo pretty_env_logger 0.5.0 Padded#value., width = self.widthrust-analyzer cargo pretty_env_logger 0.5.0 Padded#width.)
244 }
245 }
246
247 static MAX_MODULE_WIDTHrust-analyzer cargo pretty_env_logger 0.5.0 MAX_MODULE_WIDTH.: AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize# = AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]new().(0);
248
249 fn max_target_widthrust-analyzer cargo pretty_env_logger 0.5.0 max_target_width().(target: &str) -> usize {
250 let max_width = MAX_MODULE_WIDTHrust-analyzer cargo pretty_env_logger 0.5.0 MAX_MODULE_WIDTH..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#);
251 if max_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().target.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() {
252 MAX_MODULE_WIDTHrust-analyzer cargo pretty_env_logger 0.5.0 MAX_MODULE_WIDTH..storerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]store().(target.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(), Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#);
253 target.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()
254 } else {
255 max_width
256 }
257 }
258
•259 const fn colored_levelrust-analyzer cargo pretty_env_logger 0.5.0 colored_level().(level: Levelrust-analyzer cargo log 0.4.34 Level#) -> (Stylerust-analyzer cargo anstyle 1.0.14 style/Style#, &'static str) {
260 let style = Stylerust-analyzer cargo anstyle 1.0.14 style/Style#::newrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]new().();
261 let (style, level) = match level {
262 Levelrust-analyzer cargo log 0.4.34 Level#::Tracerust-analyzer cargo log 0.4.34 Level#Trace# => (
263 style.fg_colorrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]fg_color().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Colorrust-analyzer cargo anstyle 1.0.14 color/Color#::Ansirust-analyzer cargo anstyle 1.0.14 color/Color#Ansi#(AnsiColorrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#::Magentarust-analyzer cargo anstyle 1.0.14 color/AnsiColor#Magenta#))),
264 "TRACE",
265 ),
266 Levelrust-analyzer cargo log 0.4.34 Level#::Debugrust-analyzer cargo log 0.4.34 Level#Debug# => (style.fg_colorrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]fg_color().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Colorrust-analyzer cargo anstyle 1.0.14 color/Color#::Ansirust-analyzer cargo anstyle 1.0.14 color/Color#Ansi#(AnsiColorrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#::Bluerust-analyzer cargo anstyle 1.0.14 color/AnsiColor#Blue#))), "DEBUG"),
267 Levelrust-analyzer cargo log 0.4.34 Level#::Inforust-analyzer cargo log 0.4.34 Level#Info# => (style.fg_colorrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]fg_color().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Colorrust-analyzer cargo anstyle 1.0.14 color/Color#::Ansirust-analyzer cargo anstyle 1.0.14 color/Color#Ansi#(AnsiColorrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#::Greenrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#Green#))), "INFO "),
268 Levelrust-analyzer cargo log 0.4.34 Level#::Warnrust-analyzer cargo log 0.4.34 Level#Warn# => (
269 style.fg_colorrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]fg_color().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Colorrust-analyzer cargo anstyle 1.0.14 color/Color#::Ansirust-analyzer cargo anstyle 1.0.14 color/Color#Ansi#(AnsiColorrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#::Yellowrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#Yellow#))),
270 "WARN ",
271 ),
272 Levelrust-analyzer cargo log 0.4.34 Level#::Errorrust-analyzer cargo log 0.4.34 Level#Error# => (style.fg_colorrust-analyzer cargo anstyle 1.0.14 style/impl#[Style]fg_color().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Colorrust-analyzer cargo anstyle 1.0.14 color/Color#::Ansirust-analyzer cargo anstyle 1.0.14 color/Color#Ansi#(AnsiColorrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#::Redrust-analyzer cargo anstyle 1.0.14 color/AnsiColor#Red#))), "ERROR"),
273 };
274 (style, level)
275 }