Skip to content

Commit

Permalink
Release version 0.1.2:
Browse files Browse the repository at this point in the history
- This adds type aliases BoxedLogger and ArcLogger, because BoxedLogger is a lot easier to type than Box<Logger + Sync + Send>
  • Loading branch information
daboross committed Dec 24, 2014
1 parent dd7eea5 commit 009d11f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "fern"
version = "0.1.1"
version = "0.1.2"
authors = ["Dabo Ross <[email protected]>"]
description = "Fern is a simple runtime-configurable logging library"

Expand Down
9 changes: 9 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;
use std::fmt;
use std::sync;

/// Basic logger trait. Something you can send messages to.
#[unstable]
Expand All @@ -9,6 +10,14 @@ pub trait Logger {
fn log(&self, level: &Level, message: &str) -> io::IoResult<()>;
}

/// Type alias for a boxed logger
#[unstable]
pub type BoxedLogger = Box<Logger + Sync + Send>;

/// Type alias for a reference counted boxed logger
#[unstable]
pub type ArcLogger = sync::Arc<Box<Logger + Sync + Send>>;

/// A logging level - definition of how severe your message is.
#[unstable]
pub enum Level {
Expand Down
14 changes: 7 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ pub enum OutputConfig {
Stderr,
/// Custom logger - all messages sent here will just be sent on to the logger implementation you provide
#[unstable]
Custom(Box<api::Logger + Sync + Send>),
Custom(api::BoxedLogger),
}

#[experimental]
impl OutputConfig {
/// Builds this OutputConfig into an actual Logger that you can send messages to. This will open any files, get handles to stdout/stderr if need, etc.
#[unstable]
pub fn into_logger(self) -> io::IoResult<Box<api::Logger + Sync + Send>> {
pub fn into_logger(self) -> io::IoResult<api::BoxedLogger> {
return Ok(match self {
OutputConfig::Parent(config) => try!(config.into_logger()),
OutputConfig::File(ref path) => {
// workaround for error if this is all on one line
let log = box try!(loggers::WriterLogger::<io::File>::with_file(path));
log as Box<api::Logger + Sync + Send>
log as api::BoxedLogger
},
OutputConfig::Stdout => box loggers::WriterLogger::<stdio::StdWriter>::with_stdout() as Box<api::Logger + Sync + Send>,
OutputConfig::Stderr => box loggers::WriterLogger::<stdio::StdWriter>::with_stderr() as Box<api::Logger + Sync + Send>,
OutputConfig::Stdout => box loggers::WriterLogger::<stdio::StdWriter>::with_stdout() as api::BoxedLogger,
OutputConfig::Stderr => box loggers::WriterLogger::<stdio::StdWriter>::with_stderr() as api::BoxedLogger,
OutputConfig::Custom(log) => log,
});
}
Expand All @@ -63,9 +63,9 @@ impl OutputConfig {
impl LoggerConfig {
/// Builds this LoggerConfig into an actual Logger that you can send messages to. This will build all parent OutputConfig loggers as well.
#[unstable]
pub fn into_logger(self) -> io::IoResult<Box<api::Logger + Sync + Send>> {
pub fn into_logger(self) -> io::IoResult<api::BoxedLogger> {
let LoggerConfig {format, level, output} = self;
let log = try!(loggers::ConfigurationLogger::new(format, output, level));
return Ok(box log as Box<api::Logger + Sync + Send>);
return Ok(box log as api::BoxedLogger);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Fern is a runtime-configurable rust logging library.
pub use api::{Logger, Level};
pub use api::{Logger, BoxedLogger, ArcLogger, Level};
pub use config::{LoggerConfig, OutputConfig};

mod api;
Expand Down
11 changes: 6 additions & 5 deletions src/loggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::io;
use std::io::stdio;
use std::sync;

use api::{Logger, Level};
use api;
use api::Level;
use config;

pub struct ConfigurationLogger {
pub output: Vec<Box<Logger + Sync + Send>>,
pub output: Vec<api::BoxedLogger>,
pub level: Level,
pub format: Box<Fn(&str, &Level) -> String + Sync + Send>,
}
Expand All @@ -16,7 +17,7 @@ impl ConfigurationLogger {
-> io::IoResult<ConfigurationLogger> {

let output = try!(config_output.into_iter().fold(Ok(Vec::new()),
|processed: io::IoResult<Vec<Box<Logger + Sync + Send>>>, next: config::OutputConfig| {
|processed: io::IoResult<Vec<api::BoxedLogger>>, next: config::OutputConfig| {
// If an error has already been found, don't try to process any future outputs, just continue passing along the error.
let mut processed_so_far = try!(processed);
return match next.into_logger() {
Expand All @@ -37,7 +38,7 @@ impl ConfigurationLogger {
}
}

impl Logger for ConfigurationLogger {
impl api::Logger for ConfigurationLogger {
fn log(&self, level: &Level, msg: &str) -> io::IoResult<()> {
if level.as_int() < self.level.as_int() {
return Ok(());
Expand Down Expand Up @@ -74,7 +75,7 @@ impl <T: io::Writer + Send> WriterLogger<T> {
}
}

impl <T: io::Writer + Send> Logger for WriterLogger<T> {
impl <T: io::Writer + Send> api::Logger for WriterLogger<T> {
fn log(&self, _level: &Level, message: &str) -> io::IoResult<()> {
return self.writer.lock().write_line(message);
}
Expand Down

0 comments on commit 009d11f

Please sign in to comment.