Skip to content

Commit 69186ef

Browse files
committedDec 3, 2013
auto merge of #10768 : Blei/rust/logging-enabled-macros, r=alexcrichton
This is useful when the information that is needed to do useful logging is expensive to produce.
2 parents 899217c + a689171 commit 69186ef

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed
 

‎src/libstd/logging.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ There are five macros that the logging subsystem uses:
2020
2121
* `log!(level, ...)` - the generic logging macro, takes a level as a u32 and any
2222
related `format!` arguments
23-
* `debug!(...)` - a macro hard-wired to the log level of 4
24-
* `info!(...)` - a macro hard-wired to the log level of 3
25-
* `warn!(...)` - a macro hard-wired to the log level of 2
26-
* `error!(...)` - a macro hard-wired to the log level of 1
23+
* `debug!(...)` - a macro hard-wired to the log level of `DEBUG`
24+
* `info!(...)` - a macro hard-wired to the log level of `INFO`
25+
* `warn!(...)` - a macro hard-wired to the log level of `WARN`
26+
* `error!(...)` - a macro hard-wired to the log level of `ERROR`
2727
2828
All of these macros use the same style of syntax as the `format!` syntax
2929
extension. Details about the syntax can be found in the documentation of
30-
`std::fmt` along with the Rust tutorial/manual
30+
`std::fmt` along with the Rust tutorial/manual.
31+
32+
If you want to check at runtime if a given logging level is enabled (e.g. if the
33+
information you would want to log is expensive to produce), you can use the
34+
following macro:
35+
36+
* `log_enabled!(level)` - returns true if logging of the given level is enabled
3137
3238
## Enabling logging
3339
@@ -95,6 +101,15 @@ use rt::local::Local;
95101
use rt::logging::{Logger, StdErrLogger};
96102
use rt::task::Task;
97103

104+
/// Debug log level
105+
pub static DEBUG: u32 = 4;
106+
/// Info log level
107+
pub static INFO: u32 = 3;
108+
/// Warn log level
109+
pub static WARN: u32 = 2;
110+
/// Error log level
111+
pub static ERROR: u32 = 1;
112+
98113
/// This function is called directly by the compiler when using the logging
99114
/// macros. This function does not take into account whether the log level
100115
/// specified is active or not, it will always log something if this method is

‎src/libsyntax/ext/expand.rs

+7
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,13 @@ pub fn std_macros() -> @str {
754754
if cfg!(not(ndebug)) { log!(4u32, $($arg)*) }
755755
))
756756
757+
macro_rules! log_enabled(
758+
($lvl:expr) => ( {
759+
let lvl = $lvl;
760+
lvl <= __log_level() && (lvl != 4 || cfg!(not(ndebug)))
761+
} )
762+
)
763+
757764
macro_rules! fail(
758765
() => (
759766
fail!("explicit failure")
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// compile-flags:--cfg ndebug
13+
// exec-env:RUST_LOG=logging-enabled-debug=debug
14+
15+
use std::logging;
16+
17+
fn main() {
18+
if log_enabled!(logging::DEBUG) {
19+
fail!("what?! debugging?");
20+
}
21+
}

‎src/test/run-pass/logging-enabled.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// exec-env:RUST_LOG=logging-enabled=info
13+
14+
use std::logging;
15+
16+
fn main() {
17+
if log_enabled!(logging::DEBUG) {
18+
fail!("what?! debugging?");
19+
}
20+
if !log_enabled!(logging::INFO) {
21+
fail!("what?! no info?");
22+
}
23+
}

0 commit comments

Comments
 (0)