about summary refs log tree commit diff
path: root/src/libstd/rt
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-03-08 22:11:44 -0800
committerAlex Crichton <alex@alexcrichton.com>2014-03-15 22:26:36 -0700
commitcc6ec8df95fbd8163b7c2c6c34469fb96b704e66 (patch)
treee42135ffbacece4c4353511f08ab9457c45fbe9f /src/libstd/rt
parente49c30a89a2f7fd4277bdbf5722a09b1d5700507 (diff)
downloadrust-cc6ec8df95fbd8163b7c2c6c34469fb96b704e66.tar.gz
rust-cc6ec8df95fbd8163b7c2c6c34469fb96b704e66.zip
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:

* The crate map has always been a bit of a code smell among rust programs. It
  has difficulty being loaded on almost all platforms, and it's used almost
  exclusively for logging and only logging. Removing the crate map is one of the
  end goals of this movement.

* The compiler has a fair bit of special support for logging. It has the
  __log_level() expression as well as generating a global word per module
  specifying the log level. This is unfairly favoring the built-in logging
  system, and is much better done purely in libraries instead of the compiler
  itself.

* Initialization of logging is much easier to do if there is no reliance on a
  magical crate map being available to set module log levels.

* If the logging library can be written outside of the standard library, there's
  no reason that it shouldn't be. It's likely that we're not going to build the
  highest quality logging library of all time, so third-party libraries should
  be able to provide just as high-quality logging systems as the default one
  provided in the rust distribution.

With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:

* The core change of this migration is that there is no longer a physical
  log-level per module. This concept is still emulated (it is quite useful), but
  there is now only a global log level, not a local one. This global log level
  is a reflection of the maximum of all log levels specified. The previously
  generated logging code looked like:

    if specified_level <= __module_log_level() {
        println!(...)
    }

  The newly generated code looks like:

    if specified_level <= ::log::LOG_LEVEL {
        if ::log::module_enabled(module_path!()) {
            println!(...)
        }
    }

  Notably, the first layer of checking is still intended to be "super fast" in
  that it's just a load of a global word and a compare. The second layer of
  checking is executed to determine if the current module does indeed have
  logging turned on.

  This means that if any module has a debug log level turned on, all modules
  with debug log levels get a little bit slower (they all do more expensive
  dynamic checks to determine if they're turned on or not).

  Semantically, this migration brings no change in this respect, but
  runtime-wise, this will have a perf impact on some code.

* A `RUST_LOG=::help` directive will no longer print out a list of all modules
  that can be logged. This is because the crate map will no longer specify the
  log levels of all modules, so the list of modules is not known. Additionally,
  warnings can no longer be provided if a malformed logging directive was
  supplied.

The new "hello world" for logging looks like:

    #[phase(syntax, link)]
    extern crate log;

    fn main() {
        debug!("Hello, world!");
    }
Diffstat (limited to 'src/libstd/rt')
-rw-r--r--src/libstd/rt/logging.rs314
-rw-r--r--src/libstd/rt/mod.rs4
-rw-r--r--src/libstd/rt/task.rs5
3 files changed, 0 insertions, 323 deletions
diff --git a/src/libstd/rt/logging.rs b/src/libstd/rt/logging.rs
deleted file mode 100644
index aa024a53b89..00000000000
--- a/src/libstd/rt/logging.rs
+++ /dev/null
@@ -1,314 +0,0 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-use container::Container;
-use from_str::from_str;
-use iter::Iterator;
-use libc::exit;
-use option::{Some, None, Option};
-use os;
-use rt::crate_map::{ModEntry, CrateMap, iter_crate_map, get_crate_map};
-use str::{Str, StrSlice};
-use vec::{ImmutableVector, MutableTotalOrdVector, OwnedVector};
-use vec_ng::Vec;
-
-struct LogDirective<'a> {
-    name: Option<&'a str>,
-    level: u32
-}
-
-static MAX_LOG_LEVEL: u32 = 255;
-static DEFAULT_LOG_LEVEL: u32 = 1;
-static log_level_names : &'static[&'static str] = &'static["error", "warn", "info", "debug"];
-
-/// Parse an individual log level that is either a number or a symbolic log level
-fn parse_log_level(level: &str) -> Option<u32> {
-    let num = from_str::<u32>(level);
-    let mut log_level;
-    match num {
-        Some(num) => {
-            if num < MAX_LOG_LEVEL {
-                log_level = Some(num);
-            } else {
-                log_level = Some(MAX_LOG_LEVEL);
-            }
-        }
-        _ => {
-            let position = log_level_names.iter().position(|&name| name == level);
-            match position {
-                Some(position) => {
-                    log_level = Some(::cmp::min(MAX_LOG_LEVEL, (position + 1) as u32))
-                },
-                _ => {
-                    log_level = None;
-                }
-            }
-        }
-    }
-    log_level
-}
-
-/// Parse a logging specification string (e.g: "crate1,crate2::mod3,crate3::x=1")
-/// and return a vector with log directives.
-/// Valid log levels are 0-255, with the most likely ones being 1-4 (defined in std::).
-/// Also supports string log levels of error, warn, info, and debug
-fn parse_logging_spec<'a>(spec: &'a str) -> Vec<LogDirective<'a>> {
-    let mut dirs = Vec::new();
-    for s in spec.split(',') {
-        if s.len() == 0 { continue }
-        let mut parts = s.split('=');
-        let log_level;
-        let name;
-        match (parts.next(), parts.next(), parts.next()) {
-            (Some(part0), None, None) => {
-                //if the single argument is a log-level string or number,
-                //treat that as a global fallback
-                let possible_log_level = parse_log_level(part0);
-                match possible_log_level {
-                    Some(num) => {
-                        name = None;
-                        log_level = num;
-                    },
-                    None => {
-                        log_level = MAX_LOG_LEVEL;
-                        name = Some(part0);
-                    }
-                }
-            }
-            (Some(part0), Some(part1), None) => {
-                let possible_log_level = parse_log_level(part1);
-                match possible_log_level {
-                    Some(num) => {
-                        name = Some(part0);
-                        log_level = num;
-                    },
-                    _ => {
-                        rterrln!("warning: invalid logging spec '{}', \
-                                  ignoring it", part1);
-                        continue
-                    }
-                }
-            },
-            _ => {
-                rterrln!("warning: invalid logging spec '{}', \
-                          ignoring it", s);
-                continue
-            }
-        }
-        dirs.push(LogDirective { name: name, level: log_level });
-    }
-    return dirs;
-}
-
-/// Set the log level of an entry in the crate map depending on the vector
-/// of log directives
-fn update_entry(dirs: &[LogDirective], entry: &ModEntry) -> u32 {
-    let mut new_lvl: u32 = DEFAULT_LOG_LEVEL;
-    let mut longest_match = -1i;
-    for dir in dirs.iter() {
-        match dir.name {
-            None => {
-                if longest_match == -1 {
-                    longest_match = 0;
-                    new_lvl = dir.level;
-                }
-            }
-            Some(ref dir_name) => {
-                let name = entry.name;
-                let len = dir_name.len() as int;
-                if name.starts_with(*dir_name) &&
-                    len >= longest_match {
-                    longest_match = len;
-                    new_lvl = dir.level;
-                }
-            }
-        };
-    }
-    unsafe { *entry.log_level = new_lvl; }
-    if longest_match >= 0 { return 1; } else { return 0; }
-}
-
-/// Set log level for every entry in crate_map according to the sepecification
-/// in settings
-fn update_log_settings(crate_map: &CrateMap, settings: &str) {
-    if settings == "::help" || settings == "?" {
-        rterrln!("\nCrate log map:\n");
-
-        let mut entries = Vec::new();
-        iter_crate_map(crate_map, |entry| entries.push(entry.name));
-        entries.as_mut_slice().sort();
-
-        for name in entries.iter() {
-            rterrln!(" {}", *name);
-        }
-        unsafe { exit(1); }
-    }
-    let dirs = parse_logging_spec(settings);
-
-    let mut n_matches: u32 = 0;
-    iter_crate_map(crate_map, |entry| {
-        let m = update_entry(dirs.as_slice(), entry);
-        n_matches += m;
-    });
-
-    if n_matches < (dirs.len() as u32) {
-        rterrln!("warning: got {} RUST_LOG specs but only matched\n\
-                  {} of them. You may have mistyped a RUST_LOG spec. \n\
-                  Use RUST_LOG=::help to see the list of crates and modules.\n",
-                 dirs.len(), n_matches);
-    }
-}
-
-/// Configure logging by traversing the crate map and setting the
-/// per-module global logging flags based on the logging spec
-pub fn init() {
-    let log_spec = os::getenv("RUST_LOG");
-    match get_crate_map() {
-        Some(crate_map) => {
-            match log_spec {
-                Some(spec) => update_log_settings(crate_map, spec.as_slice()),
-                None => update_log_settings(crate_map, ""),
-            }
-        },
-        _ => {
-            match log_spec {
-                Some(_) => {
-                    rterrln!("warning: RUST_LOG set, but no crate map found.");
-                },
-                None => {}
-            }
-        }
-    }
-}
-
-// Tests for parse_logging_spec()
-#[test]
-fn parse_logging_spec_valid() {
-    let dirs = parse_logging_spec("crate1::mod1=1,crate1::mod2,crate2=4");
-    let dirs = dirs.as_slice();
-    assert_eq!(dirs.len(), 3);
-    assert_eq!(dirs[0].name, Some("crate1::mod1"));
-    assert_eq!(dirs[0].level, 1);
-
-    assert_eq!(dirs[1].name, Some("crate1::mod2"));
-    assert_eq!(dirs[1].level, MAX_LOG_LEVEL);
-
-    assert_eq!(dirs[2].name, Some("crate2"));
-    assert_eq!(dirs[2].level, 4);
-}
-
-#[test]
-fn parse_logging_spec_invalid_crate() {
-    // test parse_logging_spec with multiple = in specification
-    let dirs = parse_logging_spec("crate1::mod1=1=2,crate2=4");
-    let dirs = dirs.as_slice();
-    assert_eq!(dirs.len(), 1);
-    assert_eq!(dirs[0].name, Some("crate2"));
-    assert_eq!(dirs[0].level, 4);
-}
-
-#[test]
-fn parse_logging_spec_invalid_log_level() {
-    // test parse_logging_spec with 'noNumber' as log level
-    let dirs = parse_logging_spec("crate1::mod1=noNumber,crate2=4");
-    let dirs = dirs.as_slice();
-    assert_eq!(dirs.len(), 1);
-    assert_eq!(dirs[0].name, Some("crate2"));
-    assert_eq!(dirs[0].level, 4);
-}
-
-#[test]
-fn parse_logging_spec_string_log_level() {
-    // test parse_logging_spec with 'warn' as log level
-    let dirs = parse_logging_spec("crate1::mod1=wrong,crate2=warn");
-    let dirs = dirs.as_slice();
-    assert_eq!(dirs.len(), 1);
-    assert_eq!(dirs[0].name, Some("crate2"));
-    assert_eq!(dirs[0].level, 2);
-}
-
-#[test]
-fn parse_logging_spec_global() {
-    // test parse_logging_spec with no crate
-    let dirs = parse_logging_spec("warn,crate2=4");
-    let dirs = dirs.as_slice();
-    assert_eq!(dirs.len(), 2);
-    assert_eq!(dirs[0].name, None);
-    assert_eq!(dirs[0].level, 2);
-    assert_eq!(dirs[1].name, Some("crate2"));
-    assert_eq!(dirs[1].level, 4);
-}
-
-// Tests for update_entry
-#[test]
-fn update_entry_match_full_path() {
-    let dirs = [LogDirective { name: Some("crate1::mod1"), level: 2 },
-                LogDirective { name: Some("crate2"), level: 3 }];
-    let mut level = 0;
-    {
-        let entry = &ModEntry { name: "crate1::mod1", log_level: &mut level };
-        assert_eq!(update_entry(dirs, entry), 1);
-    }
-    assert_eq!(level, 2);
-}
-
-#[test]
-fn update_entry_no_match() {
-    let dirs = [LogDirective { name: Some("crate1::mod1"), level: 2 },
-                LogDirective { name: Some("crate2"), level: 3 }];
-    let mut level = 0;
-    {
-        let entry = &ModEntry { name: "crate3::mod1", log_level: &mut level };
-        assert_eq!(update_entry(dirs, entry), 0);
-    }
-    assert_eq!(level, DEFAULT_LOG_LEVEL);
-}
-
-#[test]
-fn update_entry_match_beginning() {
-    let dirs = [LogDirective { name: Some("crate1::mod1"), level: 2 },
-                LogDirective { name: Some("crate2"), level: 3 }];
-    let mut level = 0;
-    {
-        let entry= &ModEntry {name: "crate2::mod1", log_level: &mut level};
-        assert_eq!(update_entry(dirs, entry), 1);
-    }
-    assert_eq!(level, 3);
-}
-
-#[test]
-fn update_entry_match_beginning_longest_match() {
-    let dirs = [LogDirective { name: Some("crate1::mod1"), level: 2 },
-                LogDirective { name: Some("crate2"), level: 3 },
-                LogDirective { name: Some("crate2::mod"), level: 4 }];
-    let mut level = 0;
-    {
-        let entry = &ModEntry { name: "crate2::mod1", log_level: &mut level };
-        assert_eq!(update_entry(dirs, entry), 1);
-    }
-    assert_eq!(level, 4);
-}
-
-#[test]
-fn update_entry_match_default() {
-    let dirs = [LogDirective { name: Some("crate1::mod1"), level: 2 },
-                LogDirective { name: None, level: 3 }];
-    let mut level = 0;
-    {
-        let entry = &ModEntry { name: "crate1::mod1", log_level: &mut level };
-        assert_eq!(update_entry(dirs, entry), 1);
-    }
-    assert_eq!(level, 2);
-    {
-        let entry = &ModEntry { name: "crate2::mod2", log_level: &mut level };
-        assert_eq!(update_entry(dirs, entry), 1);
-    }
-    assert_eq!(level, 3);
-}
diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs
index a58826daa49..84e547619df 100644
--- a/src/libstd/rt/mod.rs
+++ b/src/libstd/rt/mod.rs
@@ -104,9 +104,6 @@ pub mod env;
 /// The local, managed heap
 pub mod local_heap;
 
-/// The Logger trait and implementations
-pub mod logging;
-
 /// Crate map
 pub mod crate_map;
 
@@ -183,7 +180,6 @@ pub fn init(argc: int, argv: **u8) {
     unsafe {
         args::init(argc, argv);
         env::init();
-        logging::init();
         local_ptr::init();
         at_exit_imp::init();
     }
diff --git a/src/libstd/rt/task.rs b/src/libstd/rt/task.rs
index 86e69560e9d..8c617c1b59b 100644
--- a/src/libstd/rt/task.rs
+++ b/src/libstd/rt/task.rs
@@ -21,7 +21,6 @@ use comm::Sender;
 use io::Writer;
 use iter::{Iterator, Take};
 use local_data;
-use logging::Logger;
 use ops::Drop;
 use option::{Option, Some, None};
 use prelude::drop;
@@ -51,7 +50,6 @@ pub struct Task {
     destroyed: bool,
     name: Option<SendStr>,
 
-    logger: Option<~Logger>,
     stdout: Option<~Writer>,
     stderr: Option<~Writer>,
 
@@ -95,7 +93,6 @@ impl Task {
             death: Death::new(),
             destroyed: false,
             name: None,
-            logger: None,
             stdout: None,
             stderr: None,
             imp: None,
@@ -129,11 +126,9 @@ impl Task {
                 #[allow(unused_must_use)]
                 fn close_outputs() {
                     let mut task = Local::borrow(None::<Task>);
-                    let logger = task.get().logger.take();
                     let stderr = task.get().stderr.take();
                     let stdout = task.get().stdout.take();
                     drop(task);
-                    drop(logger); // loggers are responsible for flushing
                     match stdout { Some(mut w) => { w.flush(); }, None => {} }
                     match stderr { Some(mut w) => { w.flush(); }, None => {} }
                 }