bp_util/
loglevel.rs

1// Modern, minimalistic & standard-compliant cold wallet library.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2020-2023 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2020-2023 LNP/BP Standards Association. All rights reserved.
9// Copyright (C) 2020-2023 Dr Maxim Orlovsky. All rights reserved.
10//
11// Licensed under the Apache License, Version 2.0 (the "License");
12// you may not use this file except in compliance with the License.
13// You may obtain a copy of the License at
14//
15//     http://www.apache.org/licenses/LICENSE-2.0
16//
17// Unless required by applicable law or agreed to in writing, software
18// distributed under the License is distributed on an "AS IS" BASIS,
19// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20// See the License for the specific language governing permissions and
21// limitations under the License.
22
23use std::env;
24
25use log::LevelFilter;
26
27/// Represents desired logging verbosity level
28#[derive(Copy, Clone, PartialEq, Eq, Debug, Display)]
29pub enum LogLevel {
30    #[display("none")]
31    None = 0,
32
33    /// Report only errors to `stderr` and normal program output to stdin
34    /// (if it is not directed to a file). Corresponds to zero verbosity
35    /// flags.
36    #[display("error")]
37    Error,
38
39    /// Report warning messages and errors, plus standard program output.
40    /// Corresponds to a single `-v` verbosity flag.
41    #[display("warn")]
42    Warn,
43
44    /// Report genetic information messages, warnings and errors.
45    /// Corresponds to a double `-vv` verbosity flag.
46    #[display("info")]
47    Info,
48
49    /// Report debugging information and all non-trace messages, including
50    /// general information, warnings and errors.
51    /// Corresponds to triple `-vvv` verbosity flag.
52    #[display("debug")]
53    Debug,
54
55    /// Print all possible messages including tracing information.
56    /// Corresponds to quadruple `-vvvv` verbosity flag.
57    #[display("trace")]
58    Trace,
59}
60
61impl From<u8> for LogLevel {
62    fn from(val: u8) -> Self { Self::from_verbosity_flag_count(val) }
63}
64
65impl From<LogLevel> for u8 {
66    fn from(log_level: LogLevel) -> Self { log_level.verbosity_flag_count() }
67}
68
69impl LogLevel {
70    /// Indicates number of required verbosity flags
71    pub fn verbosity_flag_count(&self) -> u8 { *self as u8 }
72
73    /// Constructs enum value from a given number of verbosity flags
74    pub fn from_verbosity_flag_count(level: u8) -> Self {
75        match level {
76            0 => LogLevel::None,
77            1 => LogLevel::Error,
78            2 => LogLevel::Warn,
79            3 => LogLevel::Info,
80            4 => LogLevel::Debug,
81            _ => LogLevel::Trace,
82        }
83    }
84
85    /// Applies log level to the system
86    pub fn apply(&self) {
87        log::set_max_level(LevelFilter::Trace);
88        if env::var("RUST_LOG").is_err() {
89            env::set_var("RUST_LOG", self.to_string());
90        }
91        env_logger::init();
92    }
93}