Skip to content

Commit

Permalink
Update for latest rustc, and start using a custom fern::Error enum fo…
Browse files Browse the repository at this point in the history
…r error results, instead of just IoError.

This releases 0.1.4.
  • Loading branch information
daboross committed Jan 2, 2015
1 parent b375024 commit 07e993c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 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.3"
version = "0.1.4"
authors = ["Dabo Ross <[email protected]>"]
description = "Fern is a simple runtime-configurable logging library"

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

use errors::Error;

/// Basic logger trait. Something you can send messages to.
#[unstable]
pub trait Logger {
/// Logs a given message - puts this message where-ever this logger means to put it
#[unstable]
fn log(&self, level: &Level, message: &str) -> io::IoResult<()>;
fn log(&self, level: &Level, message: &str) -> Result<(), Error>;
}

/// Type alias for a boxed logger
Expand Down
21 changes: 21 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::io;
use std::sync;
use std::error;

#[deriving(Show)]
pub enum Error {
Io(io::IoError),
Poison(String),
}

impl error::FromError<io::IoError> for Error {
fn from_error(error: io::IoError) -> Error {
Error::Io(error)
}
}

impl <T> error::FromError<sync::PoisonError<T>> for Error {
fn from_error(error: sync::PoisonError<T>) -> Error {
Error::Poison(format!("{}", error))
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
//! type out `sync::Arc<Box<fern::Logger + Sync + Send>>` for all the function declarations, you
//! can use `fern::ArcLogger` instead.
pub use errors::Error;
pub use api::{Logger, BoxedLogger, ArcLogger, Level};
pub use config::{LoggerConfig, OutputConfig};
pub use loggers::NullLogger;
Expand All @@ -137,3 +138,4 @@ pub mod local;
mod api;
mod config;
mod loggers;
mod errors;
5 changes: 3 additions & 2 deletions src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use std::cell;
use std::sync;
use std::io;

use errors::Error;
use api;
use config;

Expand All @@ -32,7 +33,7 @@ pub fn set_thread_logger(logger: api::ArcLogger) {
/// For a more friendly interface which will automatically report errors, and allows inline
/// formatting, try using the `log!()` macro in the `fern_macros` package.
#[experimental]
pub fn log(level: &api::Level, msg: &str) -> io::IoResult<()> {
pub fn log(level: &api::Level, msg: &str) -> Result<(), Error> {
THREAD_LOGGER.with(|logger| {
logger.borrow().log(level, msg)
})
Expand Down
10 changes: 6 additions & 4 deletions src/loggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io;
use std::io::stdio;
use std::sync;

use errors::Error;
use api;
use api::Level;
use config;
Expand Down Expand Up @@ -39,7 +40,7 @@ impl ConfigurationLogger {
}

impl api::Logger for ConfigurationLogger {
fn log(&self, level: &Level, msg: &str) -> io::IoResult<()> {
fn log(&self, level: &Level, msg: &str) -> Result<(), Error> {
if level.as_int() < self.level.as_int() {
return Ok(());
}
Expand Down Expand Up @@ -76,8 +77,9 @@ impl <T: io::Writer + Send> 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);
fn log(&self, _level: &Level, message: &str) -> Result<(), Error> {
try!(try!(self.writer.lock()).write_line(message));
return Ok(());
}
}

Expand All @@ -87,7 +89,7 @@ impl <T: io::Writer + Send> api::Logger for WriterLogger<T> {
pub struct NullLogger;

impl api::Logger for NullLogger {
fn log(&self, _level: &Level, _message: &str) -> io::IoResult<()> {
fn log(&self, _level: &Level, _message: &str) -> Result<(), Error> {
return Ok(());
}
}

0 comments on commit 07e993c

Please sign in to comment.