near_facsimile/
logging.rs

1/*
2Copyright 2022 Marek Suchánek <msuchane@redhat.com>
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use color_eyre::Result;
18use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
19
20/// Initialize the handlers for logging and error reporting.
21pub fn init_log_and_errors(verbose: u8) -> Result<()> {
22    color_eyre::install()?;
23
24    // Use the local time zone in log messages.
25    let config = ConfigBuilder::new()
26        // TODO: There's probably a bug in the type signature of set_time_offset_to_local,
27        // which prevents us from using `?` on it. Report to upstream.
28        .set_time_offset_to_local()
29        .expect("Failed to determine the local time zone.")
30        .set_time_level(LevelFilter::Debug)
31        .build();
32
33    let log_level = match verbose {
34        0 => LevelFilter::Info,
35        1 => LevelFilter::Debug,
36        _ => LevelFilter::Trace,
37    };
38
39    TermLogger::init(
40        log_level,
41        config,
42        // Mixed mode prints errors to stderr and info to stdout. Not sure about the other levels.
43        TerminalMode::default(),
44        // Try to use color if possible.
45        ColorChoice::Auto,
46    )?;
47
48    Ok(())
49}