From 07e993c7a1eb4f5ad023f70e43097d8fe535ef4d Mon Sep 17 00:00:00 2001 From: Dabo Ross Date: Thu, 1 Jan 2015 18:06:10 -0800 Subject: [PATCH] Update for latest rustc, and start using a custom fern::Error enum for error results, instead of just IoError. This releases 0.1.4. --- Cargo.toml | 2 +- src/api.rs | 5 +++-- src/errors.rs | 21 +++++++++++++++++++++ src/lib.rs | 2 ++ src/local.rs | 5 +++-- src/loggers.rs | 10 ++++++---- 6 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 src/errors.rs diff --git a/Cargo.toml b/Cargo.toml index 1441142..5d2bf21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "fern" -version = "0.1.3" +version = "0.1.4" authors = ["Dabo Ross "] description = "Fern is a simple runtime-configurable logging library" diff --git a/src/api.rs b/src/api.rs index 0b1177b..36affc9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -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 diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..202efec --- /dev/null +++ b/src/errors.rs @@ -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 for Error { + fn from_error(error: io::IoError) -> Error { + Error::Io(error) + } +} + +impl error::FromError> for Error { + fn from_error(error: sync::PoisonError) -> Error { + Error::Poison(format!("{}", error)) + } +} diff --git a/src/lib.rs b/src/lib.rs index 9d880d4..3fa7445 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,6 +128,7 @@ //! type out `sync::Arc>` 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; @@ -137,3 +138,4 @@ pub mod local; mod api; mod config; mod loggers; +mod errors; diff --git a/src/local.rs b/src/local.rs index 803a4f2..8c91813 100644 --- a/src/local.rs +++ b/src/local.rs @@ -5,7 +5,8 @@ use std::cell; use std::sync; -use std::io; + +use errors::Error; use api; use config; @@ -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) }) diff --git a/src/loggers.rs b/src/loggers.rs index 95e4db5..0deb5d5 100644 --- a/src/loggers.rs +++ b/src/loggers.rs @@ -2,6 +2,7 @@ use std::io; use std::io::stdio; use std::sync; +use errors::Error; use api; use api::Level; use config; @@ -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(()); } @@ -76,8 +77,9 @@ impl WriterLogger { } impl api::Logger for WriterLogger { - 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(()); } } @@ -87,7 +89,7 @@ impl api::Logger for WriterLogger { 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(()); } }