|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -//! Logging |
| 11 | +/*! |
12 | 12 |
|
13 |
| -use fmt; |
14 |
| -use option::*; |
15 |
| -use os; |
16 |
| -use rt; |
17 |
| -use rt::logging::{Logger, StdErrLogger}; |
| 13 | +Logging |
18 | 14 |
|
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. |
23 | 18 |
|
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. |
36 | 59 |
|
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!(...)); |
38 | 78 | }
|
| 79 | +``` |
39 | 80 |
|
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. |
44 | 89 |
|
| 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) { |
45 | 106 | unsafe {
|
46 | 107 | let optional_task: Option<*mut Task> = Local::try_unsafe_borrow();
|
47 | 108 | match optional_task {
|
|
0 commit comments