Skip to content

Commit 88593fc

Browse files
committed
Document logging and remove old functions
This adds a large doc-block to the top of the std::logging module explaining how to use it. This is mostly just making sure that all the information in the manual's section about logging is also here (in case someone decides to look into this module first). This also removes the old console_{on,off} methods. As far as I can tell, the functions were only used by the compiler, and there's no reason for them to be used because they're all turned off by default anyway (maybe they were turned on by default at some point...) I believe that this is the final nail in the coffin and closes #5021
1 parent a1ffb06 commit 88593fc

File tree

5 files changed

+90
-73
lines changed

5 files changed

+90
-73
lines changed

src/librustc/rustc.rs

-3
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ pub fn describe_debug_flags() {
194194
}
195195

196196
pub fn run_compiler(args: &[~str], demitter: @diagnostic::Emitter) {
197-
// Don't display log spew by default. Can override with RUST_LOG.
198-
::std::logging::console_off();
199-
200197
let mut args = args.to_owned();
201198
let binary = args.shift().to_managed();
202199

src/libstd/logging.rs

+88-27
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,101 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Logging
11+
/*!
1212
13-
use fmt;
14-
use option::*;
15-
use os;
16-
use rt;
17-
use rt::logging::{Logger, StdErrLogger};
13+
Logging
1814
19-
/// Turns on logging to stdout globally
20-
pub fn console_on() {
21-
rt::logging::console_on();
22-
}
15+
This module is used by the compiler when emitting output for the logging family
16+
of macros. The methods of this module shouldn't necessarily be used directly,
17+
but rather through the logging macros defined.
2318
24-
/**
25-
* Turns off logging to stdout globally
26-
*
27-
* Turns off the console unless the user has overridden the
28-
* runtime environment's logging spec, e.g. by setting
29-
* the RUST_LOG environment variable
30-
*/
31-
pub fn console_off() {
32-
// If RUST_LOG is set then the console can't be turned off
33-
if os::getenv("RUST_LOG").is_some() {
34-
return;
35-
}
19+
There are five macros that the logging subsystem uses:
20+
21+
* `log!(level, ...)` - the generic logging macro, takes a level as a u32 and any
22+
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
27+
28+
All of these macros use the same style of syntax as the `format!` syntax
29+
extension. Details about the syntax can be found in the documentation of
30+
`std::fmt` along with the Rust tutorial/manual
31+
32+
## Enabling logging
33+
34+
Log levels are controlled on a per-module basis, and by default all logging is
35+
disabled except for `error!` (a log level of 1). Logging is controlled via the
36+
`RUST_LOG` environment variable. The value of this environment variable is a
37+
comma-separated list of logging directives. A logging directive is of the form:
38+
39+
```
40+
path::to::module=log_level
41+
```
42+
43+
The path to the module is rooted in the name of the crate it was compiled for,
44+
so if your program is contained in a file `hello.rs`, for example, to turn on
45+
logging for this file you would use a value of `RUST_LOG=hello`. Furthermore,
46+
this path is a prefix-search, so all modules nested in the specified module will
47+
also have logging enabled.
48+
49+
The actual `log_level` is optional to specify. If omitted, all logging will be
50+
enabled. If specified, the it must be either a numeric in the range of 1-255, or
51+
it must be one of the strings `debug`, `error`, `info`, or `warn`. If a numeric
52+
is specified, then all logging less than or equal to that numeral is enabled.
53+
For example, if logging level 3 is active, error, warn, and info logs will be
54+
printed, but debug will be omitted.
55+
56+
As the log level for a module is optional, the module to enable logging for is
57+
also optional. If only a `log_level` is provided, then the global log level for
58+
all modules is set to this value.
3659
37-
rt::logging::console_off();
60+
Some examples of valid values of `RUST_LOG` are:
61+
62+
```
63+
hello // turns on all logging for the 'hello' module
64+
info // turns on all info logging
65+
hello=debug // turns on debug logging for 'hello'
66+
hello=3 // turns on info logging for 'hello'
67+
hello,std::hashmap // turns on hello, and std's hashmap logging
68+
error,hello=warn // turn on global error logging and also warn for hello
69+
```
70+
71+
## Performance and Side Effects
72+
73+
Each of these macros will expand to code similar to:
74+
75+
```rust
76+
if log_level <= my_module_log_level() {
77+
::std::logging::log(log_level, format!(...));
3878
}
79+
```
3980
40-
#[allow(missing_doc)]
41-
pub fn log(_level: u32, args: &fmt::Arguments) {
42-
use rt::task::Task;
43-
use rt::local::Local;
81+
What this means is that each of these macros are very cheap at runtime if
82+
they're turned off (just a load and an integer comparison). This also means that
83+
if logging is disabled, none of the components of the log will be executed.
84+
85+
## Useful Values
86+
87+
For convenience, if a value of `::help` is set for `RUST_LOG`, a program will
88+
start, print out all modules registered for logging, and then exit.
4489
90+
*/
91+
92+
use fmt;
93+
use option::*;
94+
use rt::local::Local;
95+
use rt::logging::{Logger, StdErrLogger};
96+
use rt::task::Task;
97+
98+
/// This function is called directly by the compiler when using the logging
99+
/// macros. This function does not take into account whether the log level
100+
/// specified is active or not, it will always log something if this method is
101+
/// called.
102+
///
103+
/// It is not recommended to call this function directly, rather it should be
104+
/// invoked through the logging family of macros.
105+
pub fn log(_level: u32, args: &fmt::Arguments) {
45106
unsafe {
46107
let optional_task: Option<*mut Task> = Local::try_unsafe_borrow();
47108
match optional_task {

src/libstd/rt/logging.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use fmt;
1212
use from_str::from_str;
13-
use libc::{uintptr_t, exit};
13+
use libc::exit;
1414
use option::{Some, None, Option};
1515
use rt;
1616
use rt::util::dumb_println;
@@ -182,9 +182,7 @@ pub struct StdErrLogger;
182182

183183
impl Logger for StdErrLogger {
184184
fn log(&mut self, args: &fmt::Arguments) {
185-
if should_log_console() {
186-
fmt::writeln(self as &mut rt::io::Writer, args);
187-
}
185+
fmt::writeln(self as &mut rt::io::Writer, args);
188186
}
189187
}
190188

@@ -220,21 +218,6 @@ pub fn init() {
220218
}
221219
}
222220

223-
#[fixed_stack_segment] #[inline(never)]
224-
pub fn console_on() { unsafe { rust_log_console_on() } }
225-
226-
#[fixed_stack_segment] #[inline(never)]
227-
pub fn console_off() { unsafe { rust_log_console_off() } }
228-
229-
#[fixed_stack_segment] #[inline(never)]
230-
fn should_log_console() -> bool { unsafe { rust_should_log_console() != 0 } }
231-
232-
extern {
233-
fn rust_log_console_on();
234-
fn rust_log_console_off();
235-
fn rust_should_log_console() -> uintptr_t;
236-
}
237-
238221
// Tests for parse_logging_spec()
239222
#[test]
240223
fn parse_logging_spec_valid() {

src/rt/rust_builtin.cpp

-21
Original file line numberDiff line numberDiff line change
@@ -326,27 +326,6 @@ rust_mktime(rust_tm* timeptr) {
326326
return mktime(&t);
327327
}
328328

329-
static lock_and_signal log_lock;
330-
static bool log_to_console = true;
331-
332-
extern "C" CDECL void
333-
rust_log_console_on() {
334-
scoped_lock with(log_lock);
335-
log_to_console = true;
336-
}
337-
338-
extern "C" CDECL void
339-
rust_log_console_off() {
340-
scoped_lock with(log_lock);
341-
log_to_console = false;
342-
}
343-
344-
extern "C" CDECL uintptr_t
345-
rust_should_log_console() {
346-
scoped_lock with(log_lock);
347-
return log_to_console;
348-
}
349-
350329
extern "C" lock_and_signal*
351330
rust_create_little_lock() {
352331
return new lock_and_signal();

src/rt/rustrt.def.in

-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ rust_get_stderr
1818
rust_list_dir_val
1919
rust_list_dir_wfd_size
2020
rust_list_dir_wfd_fp_buf
21-
rust_log_console_on
22-
rust_log_console_off
23-
rust_should_log_console
2421
rust_unset_sigprocmask
2522
rust_env_pairs
2623
upcall_rust_personality

0 commit comments

Comments
 (0)