1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/fern/0.6.2")]
//! Efficient, configurable logging in Rust.
//!
//! # fern 0.4.4, 0.5.\*, 0.6.\* security warning - `colored` feature + global allocator
//!
//! One of our downstream dependencies, [atty](https://docs.rs/atty/), through
//! [colored], has an unsoundness issue:
//! <https://rustsec.org/advisories/RUSTSEC-2021-0145.html>
//!
//! This shows up in one situation: if you're using `colored` (the crate, or our
//! feature), and a custom global allocator.
//!
//! I will be releasing `fern` 0.7.0, removing `colored` as a dependency. This
//! may add another color crate, or may just document usage of alternatives
//! (such as [`owo-colors`](https://docs.rs/owo-colors/) +
//! [`enable-ansi-support`](https://docs.rs/enable-ansi-support/0.2.1/enable_ansi_support/)).
//!
//! In the meantime, if you're using `#[global_allocator]`, I highly recommend
//! removing the `fern/colored` feature.
//!
//! Or, for minimal code changes, you can also enable the `colored/no-colors`
//! feature:
//!
//! ```text
//! cargo add colored --features no-color
//! ```
//!
//! With the `no-color` feature, the vulnerable code will still be present, but
//! unless you use any of the following APIs manually, it will never be called:
//!
//! - [`colored::control::set_override`]
//! - [`colored::control::unset_override`]
//! - [`colored::control::ShouldColorize::from_env`]
//! - [`colored::control::SHOULD_COLORIZE`][struct@colored::control::SHOULD_COLORIZE]
//!   (referencing this `lazy_static!` variable will initialize it, running the
//!   vulnerable code)
//!
//! See <https://github.com/daboross/fern/issues/113> for further discussion.
//!
//! # Depending on fern
//!
//! Ensure you require both fern and log in your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! log = "0.4"
//! fern = "0.6"
//! ```
//!
//! # Example setup
//!
//! With fern, all logger configuration is done via builder-like methods on
//! instances of the [`Dispatch`] structure.
//!
//! Here's an example logger which formats messages, and sends everything Debug
//! and above to both stdout and an output.log file:
//!
//! ```no_run
//! use log::{debug, error, info, trace, warn};
//! use std::time::SystemTime;
//!
//! fn setup_logger() -> Result<(), fern::InitError> {
//!     fern::Dispatch::new()
//!         .format(|out, message, record| {
//!             out.finish(format_args!(
//!                 "[{} {} {}] {}",
//!                 humantime::format_rfc3339_seconds(SystemTime::now()),
//!                 record.level(),
//!                 record.target(),
//!                 message
//!             ))
//!         })
//!         .level(log::LevelFilter::Debug)
//!         .chain(std::io::stdout())
//!         .chain(fern::log_file("output.log")?)
//!         .apply()?;
//!     Ok(())
//! }
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     setup_logger()?;
//!
//!     info!("Hello, world!");
//!     warn!("Warning!");
//!     debug!("Now exiting.");
//!
//!     Ok(())
//! }
//! ```
//!
//! Let's unwrap this:
//!
//!
//! ```
//! fern::Dispatch::new()
//! # ;
//! ```
//!
//! [`Dispatch::new`] creates an empty configuration.
//!
//! ```
//! # fern::Dispatch::new()
//! .format(|out, message, record| {
//!     out.finish(format_args!(
//!         "..."
//!     ))
//! })
//! # ;
//! ```
//!
//! This incantation sets the `Dispatch` format! The closure taking in
//! `out, message, record` will be called once for each message going through
//! the dispatch, and the formatted log message will be used for any downstream
//! consumers.
//!
//! Do any work you want in this closure, and then call `out.finish` at the end.
//! The callback-style result passing with `out.finish(format_args!())` lets us
//! format without any intermediate string allocation.
//!
//! [`format_args!`] has the same format as [`println!`], just returning a
//! not-yet-written result we can use internally.
//!
//! ```
//! std::time::SystemTime::now()
//! # ;
//! ```
//!
//! [`std::time::SystemTime::now`] retrieves the current time.
//!
//! ```
//! humantime::format_rfc3339_seconds(
//!     // ...
//!     # std::time::SystemTime::now()
//! )
//! # ;
//! ```
//!
//! [`humantime::format_rfc3339_seconds`] formats the current time into an
//! RFC3339 timestamp, with second-precision.
//!
//! RFC3339 looks like `2018-02-14T00:28:07Z`, always using UTC, ignoring system
//! timezone.
//!
//! `humantime` is a nice light dependency, but only offers this one format.
//! For more custom time formatting, I recommend
//! [`chrono`](https://docs.rs/chrono/) or [`time`](https://docs.rs/time/).
//!
//! Now, back to the [`Dispatch`] methods:
//!
//! ```
//! # fern::Dispatch::new()
//! .level(log::LevelFilter::Debug)
//! # ;
//! ```
//!
//! Sets the minimum logging level for all modules, if not overwritten with
//! [`Dispatch::level_for`], to [`Level::Debug`][log::Level::Debug].
//!
//! ```
//! # fern::Dispatch::new()
//! .chain(std::io::stdout())
//! # ;
//! ```
//!
//! Adds a child to the logger. With this, all messages which pass the filters
//! will be sent to stdout.
//!
//! [`Dispatch::chain`] accepts [`Stdout`], [`Stderr`], [`File`] and other
//! [`Dispatch`] instances.
//!
//! ```
//! # fern::Dispatch::new()
//! .chain(fern::log_file("output.log")?)
//! # ; <Result<(), Box<dyn std::error::Error>>>::Ok(())
//! ```
//!
//! Adds a second child sending messages to the file "output.log".
//!
//! See [`log_file`].
//!
//! ```
//! # fern::Dispatch::new()
//! // ...
//! .apply()
//! # ;
//! ```
//!
//! Consumes the configuration and instantiates it as the current runtime global
//! logger.
//!
//! This will fail if and only if `.apply()` or equivalent form another crate
//! has already been used this runtime.
//!
//! Since the binary crate is the only one ever setting up logging, and it's
//! usually done near the start of `main`, the [`Dispatch::apply`] result can be
//! reasonably unwrapped: it's a bug if any crate is calling this method more
//! than once.
//!
//! ---
//!
//! The final output will look like:
//!
//! ```text
//! [2023-03-18T20:12:50Z INFO cmd_program] Hello, world!
//! [2023-03-18T20:12:50Z WARN cmd_program] Warning!
//! [2023-03-18T20:12:50Z DEBUG cmd_program] Now exiting.
//! ```
//!
//! # Logging
//!
//! Once the logger has been set, it will pick up all logging calls from your
//! crate and all libraries you depend on.
//!
//! ```rust
//! # use log::{debug, error, info, trace, warn};
//!
//! # fn setup_logger() -> Result<(), fern::InitError> {
//! fern::Dispatch::new()
//!     // ...
//!     .apply()?;
//! # Ok(())
//! # }
//!
//! # fn main() {
//! # setup_logger().ok(); // we're ok with this not succeeding.
//! trace!("Trace message");
//! debug!("Debug message");
//! info!("Info message");
//! warn!("Warning message");
//! error!("Error message");
//! # }
//! ```
//!
//! # More
//!
//! The [`Dispatch`] documentation has example usages of each method, and the
//! [full example program] might be useful for using fern in a larger
//! application context.
//!
//! See the [colors] module for examples using ANSI terminal coloring.
//!
//! See the [syslog] module for examples outputting to the unix syslog, or the
//! [syslog full example program] for a more realistic sample.
//!
//! See the [meta] module for information on getting logging-within-logging
//! working correctly.
//!
//! [`Stdout`]: std::io::Stdout
//! [`Stderr`]: std::io::Stderr
//! [`File`]: std::fs::File
//! [full example program]: https://github.com/daboross/fern/tree/fern-0.6.2/examples/cmd-program.rs
//! [syslog full example program]: https://github.com/daboross/fern/tree/fern-0.6.2/examples/syslog.rs
//! [`humantime::format_rfc3339_seconds`]: https://docs.rs/humantime/2/humantime/fn.format_rfc3339_seconds.html
use std::{
    convert::AsRef,
    fmt,
    fs::{File, OpenOptions},
    io,
    path::Path,
};

#[cfg(all(not(windows), any(feature = "syslog-4", feature = "syslog-6")))]
use std::collections::HashMap;

pub use crate::{
    builders::{Dispatch, Output, Panic},
    errors::InitError,
    log_impl::FormatCallback,
};

mod builders;
mod errors;
mod log_impl;

#[cfg(feature = "colored")]
pub mod colors;
#[cfg(all(
    feature = "syslog-3",
    feature = "syslog-4",
    // disable on windows when running doctests, as the code itself only runs on
    // linux. enable on windows otherwise because it's a documentation-only
    // module.
    any(not(windows), not(doctest))
))]
pub mod syslog;

pub mod meta;

/// A type alias for a log formatter.
///
/// As of fern `0.5`, the passed `fmt::Arguments` will always be the same as
/// the given `log::Record`'s `.args()`.
pub type Formatter = dyn Fn(FormatCallback, &fmt::Arguments, &log::Record) + Sync + Send + 'static;

/// A type alias for a log filter. Returning true means the record should
/// succeed - false means it should fail.
pub type Filter = dyn Fn(&log::Metadata) -> bool + Send + Sync + 'static;

#[cfg(feature = "date-based")]
pub use crate::builders::DateBased;

#[cfg(all(not(windows), feature = "syslog-4"))]
type Syslog4Rfc3164Logger = syslog4::Logger<syslog4::LoggerBackend, String, syslog4::Formatter3164>;

#[cfg(all(not(windows), feature = "syslog-4"))]
type Syslog4Rfc5424Logger = syslog4::Logger<
    syslog4::LoggerBackend,
    (i32, HashMap<String, HashMap<String, String>>, String),
    syslog4::Formatter5424,
>;

#[cfg(all(not(windows), feature = "syslog-6"))]
type Syslog6Rfc3164Logger = syslog6::Logger<syslog6::LoggerBackend, syslog6::Formatter3164>;

#[cfg(all(not(windows), feature = "syslog-6"))]
type Syslog6Rfc5424Logger = syslog6::Logger<syslog6::LoggerBackend, syslog6::Formatter5424>;

#[cfg(all(not(windows), feature = "syslog-4"))]
type Syslog4TransformFn =
    dyn Fn(&log::Record) -> (i32, HashMap<String, HashMap<String, String>>, String) + Send + Sync;

#[cfg(all(not(windows), feature = "syslog-6"))]
type Syslog6TransformFn =
    dyn Fn(&log::Record) -> (u32, HashMap<String, HashMap<String, String>>, String) + Send + Sync;

/// Convenience method for opening a log file with common options.
///
/// Equivalent to:
///
/// ```no_run
/// std::fs::OpenOptions::new()
///     .write(true)
///     .create(true)
///     .append(true)
///     .open("filename")
/// # ;
/// ```
///
/// See [`OpenOptions`] for more information.
///
/// [`OpenOptions`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html
#[inline]
pub fn log_file<P: AsRef<Path>>(path: P) -> io::Result<File> {
    OpenOptions::new()
        .write(true)
        .create(true)
        .append(true)
        .open(path)
}

/// Convenience method for opening a re-openable log file with common options.
///
/// The file opening is equivalent to:
///
/// ```no_run
/// std::fs::OpenOptions::new()
///     .write(true)
///     .create(true)
///     .append(true)
///     .open("filename")
/// # ;
/// ```
///
/// See [`OpenOptions`] for more information.
///
/// [`OpenOptions`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html
///
/// This function is not available on Windows, and it requires the `reopen-03`
/// feature to be enabled.
#[cfg(all(not(windows), feature = "reopen-03"))]
#[inline]
pub fn log_reopen(path: &Path, signal: Option<libc::c_int>) -> io::Result<reopen03::Reopen<File>> {
    let p = path.to_owned();
    let r = reopen03::Reopen::new(Box::new(move || log_file(&p)))?;

    if let Some(s) = signal {
        r.handle().register_signal(s)?;
    }
    Ok(r)
}

/// Convenience method for opening a re-openable log file with common options.
///
/// The file opening is equivalent to:
///
/// ```no_run
/// std::fs::OpenOptions::new()
///     .write(true)
///     .create(true)
///     .append(true)
///     .open("filename")
/// # ;
/// ```
///
/// See [`OpenOptions`] for more information.
///
/// [`OpenOptions`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html
///
/// This function requires the `reopen-1` feature to be enabled.
#[cfg(all(not(windows), feature = "reopen-1"))]
#[inline]
pub fn log_reopen1<S: IntoIterator<Item = libc::c_int>>(
    path: &Path,
    signals: S,
) -> io::Result<reopen1::Reopen<File>> {
    let p = path.to_owned();
    let r = reopen1::Reopen::new(Box::new(move || log_file(&p)))?;

    for s in signals {
        r.handle().register_signal(s)?;
    }
    Ok(r)
}