Skip to content

Commit

Permalink
Add BuildWithCustomFormat trait
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Mar 17, 2019
1 parent f0f39d4 commit 53b4eac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use slog::Logger;
use slog::{Drain, Logger};
use slog_term::Decorator;
use std::fmt::Debug;

use file::FileLoggerBuilder;
use null::NullLoggerBuilder;
Expand All @@ -11,6 +13,16 @@ pub trait Build {
fn build(&self) -> Result<Logger>;
}

pub trait BuildWithCustomFormat {
type Decorator: Decorator;

fn build_with_custom_format<F, D>(&self, f: F) -> Result<Logger>
where
F: FnOnce(Self::Decorator) -> Result<D>,
D: Drain + Send + 'static,
D::Err: Debug;
}

/// Logger builder.
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
Expand Down
18 changes: 16 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::time::{Duration, Instant};
use misc::{module_and_line, timezone_to_timestamp_fn};
use types::KVFilterParameters;
use types::{Format, OverflowStrategy, Severity, SourceLocation, TimeZone};
use {Build, Config, ErrorKind, Result};
use {Build, BuildWithCustomFormat, Config, ErrorKind, Result};

/// A logger builder which build loggers that write log records to the specified file.
///
Expand Down Expand Up @@ -197,9 +197,23 @@ impl Build for FileLoggerBuilder {
Ok(logger)
}
}
impl BuildWithCustomFormat for FileLoggerBuilder {
type Decorator = PlainDecorator<FileAppender>;

fn build_with_custom_format<F, D>(&self, f: F) -> Result<Logger>
where
F: FnOnce(Self::Decorator) -> Result<D>,
D: Drain + Send + 'static,
D::Err: Debug,
{
let decorator = PlainDecorator::new(self.appender.clone());
let drain = track!(f(decorator))?;
Ok(self.build_with_drain(drain))
}
}

#[derive(Debug)]
struct FileAppender {
pub struct FileAppender {
path: PathBuf,
file: Option<BufWriter<File>>,
truncate: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extern crate tempfile;
extern crate trackable;
extern crate regex;

pub use build::{Build, LoggerBuilder};
pub use build::{Build, BuildWithCustomFormat, LoggerBuilder};
pub use config::{Config, LoggerConfig};
pub use error::{Error, ErrorKind};
pub use misc::set_stdlog_logger;
Expand Down
18 changes: 16 additions & 2 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io;
use misc::{module_and_line, timezone_to_timestamp_fn};
use types::KVFilterParameters;
use types::{Format, OverflowStrategy, Severity, SourceLocation, TimeZone};
use {Build, Config, Result};
use {Build, BuildWithCustomFormat, Config, Result};

/// A logger builder which build loggers that output log records to the terminal.
///
Expand Down Expand Up @@ -151,6 +151,20 @@ impl Build for TerminalLoggerBuilder {
Ok(logger)
}
}
impl BuildWithCustomFormat for TerminalLoggerBuilder {
type Decorator = Decorator;

fn build_with_custom_format<F, D>(&self, f: F) -> Result<Logger>
where
F: FnOnce(Self::Decorator) -> Result<D>,
D: Drain + Send + 'static,
D::Err: Debug,
{
let decorator = self.destination.to_decorator();
let drain = track!(f(decorator))?;
Ok(self.build_with_drain(drain))
}
}

/// The destination to which log records will be outputted.
///
Expand Down Expand Up @@ -192,7 +206,7 @@ impl Destination {
}
}

enum Decorator {
pub enum Decorator {
Term(TermDecorator),
PlainStdout(PlainDecorator<io::Stdout>),
PlainStderr(PlainDecorator<io::Stderr>),
Expand Down

0 comments on commit 53b4eac

Please sign in to comment.