tracing_subscriber_init/
utils.rs

1// Copyright (c) 2023 tracing-subscriber-init developers
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use tracing::Level;
10use tracing_subscriber::fmt::format::FmtSpan;
11
12use crate::TracingConfig;
13
14/// Determine the effective logging level based on quiet and verbosity settings.
15#[cfg(debug_assertions)]
16#[must_use]
17pub fn get_effective_level(quiet: u8, verbosity: u8) -> Level {
18    if verbosity > 0 {
19        match verbosity {
20            1 => Level::DEBUG,
21            _ => Level::TRACE,
22        }
23    } else if quiet > 0 {
24        match quiet {
25            1 => Level::WARN,
26            _ => Level::ERROR,
27        }
28    } else {
29        Level::INFO
30    }
31}
32
33/// Determine the effective logging level based on quiet and verbosity settings.
34#[cfg(not(debug_assertions))]
35#[must_use]
36pub fn get_effective_level(_quiet: u8, verbosity: u8) -> Level {
37    match verbosity {
38        0 => Level::ERROR,
39        1 => Level::WARN,
40        2 => Level::INFO,
41        3 => Level::DEBUG,
42        4 | _ => Level::TRACE,
43    }
44}
45
46#[doc(hidden)]
47#[derive(Clone, Copy, Debug)]
48pub struct TestAll;
49
50impl TracingConfig for TestAll {
51    fn quiet(&self) -> u8 {
52        0
53    }
54
55    fn verbose(&self) -> u8 {
56        3
57    }
58
59    #[cfg(feature = "json")]
60    fn with_current_span(&self) -> bool {
61        true
62    }
63
64    fn with_file(&self) -> bool {
65        true
66    }
67
68    fn with_line_number(&self) -> bool {
69        true
70    }
71
72    fn with_span_events(&self) -> Option<FmtSpan> {
73        Some(FmtSpan::FULL)
74    }
75
76    #[cfg(feature = "json")]
77    fn with_span_list(&self) -> bool {
78        true
79    }
80
81    fn with_target(&self) -> bool {
82        true
83    }
84
85    fn with_thread_ids(&self) -> bool {
86        true
87    }
88
89    fn with_thread_names(&self) -> bool {
90        true
91    }
92}
93
94#[cfg(test)]
95pub(crate) mod test {
96    use super::get_effective_level;
97    use crate::TracingConfig;
98    use tracing::Level;
99
100    #[cfg(debug_assertions)]
101    #[test]
102    fn get_effective_level_works() {
103        assert_eq!(Level::INFO, get_effective_level(0, 0));
104        assert_eq!(Level::DEBUG, get_effective_level(0, 1));
105        assert_eq!(Level::TRACE, get_effective_level(0, 2));
106        assert_eq!(Level::TRACE, get_effective_level(0, 3));
107        assert_eq!(Level::WARN, get_effective_level(1, 0));
108        assert_eq!(Level::DEBUG, get_effective_level(1, 1));
109        assert_eq!(Level::TRACE, get_effective_level(1, 2));
110        assert_eq!(Level::TRACE, get_effective_level(1, 3));
111        assert_eq!(Level::ERROR, get_effective_level(2, 0));
112        assert_eq!(Level::DEBUG, get_effective_level(2, 1));
113        assert_eq!(Level::TRACE, get_effective_level(2, 2));
114        assert_eq!(Level::TRACE, get_effective_level(2, 3));
115    }
116
117    #[cfg(not(debug_assertions))]
118    #[test]
119    fn get_effective_level_works() {
120        assert_eq!(Level::ERROR, get_effective_level(0, 0));
121        assert_eq!(Level::WARN, get_effective_level(0, 1));
122        assert_eq!(Level::INFO, get_effective_level(0, 2));
123        assert_eq!(Level::DEBUG, get_effective_level(0, 3));
124        assert_eq!(Level::TRACE, get_effective_level(0, 4));
125        assert_eq!(Level::TRACE, get_effective_level(0, 5));
126    }
127
128    #[derive(Clone, Debug)]
129    pub(crate) struct TestConfig;
130
131    impl TracingConfig for TestConfig {
132        fn quiet(&self) -> u8 {
133            0
134        }
135
136        fn verbose(&self) -> u8 {
137            1
138        }
139    }
140
141    #[derive(Clone, Debug)]
142    #[allow(dead_code)]
143    pub(crate) struct TestJson;
144
145    impl TracingConfig for TestJson {
146        fn quiet(&self) -> u8 {
147            0
148        }
149
150        fn verbose(&self) -> u8 {
151            1
152        }
153
154        #[cfg(feature = "json")]
155        fn with_current_span(&self) -> bool {
156            true
157        }
158
159        #[cfg(feature = "json")]
160        fn with_span_list(&self) -> bool {
161            true
162        }
163    }
164}