Skip to content

Commit

Permalink
Format Rust code using rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Oct 30, 2022
1 parent ba50c1d commit 0f4ef2e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
41 changes: 27 additions & 14 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::easy_termcolor::{color, EasyTermColor};
use crate::Colors;
use atty::Stream;
use log::Level;
use std::collections::HashMap;
use std::fmt::Display;
Expand All @@ -34,10 +37,7 @@ use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use atty::Stream;
use termcolor::{ColorChoice, ColorSpec, StandardStream};
use crate::Colors;
use crate::easy_termcolor::{color, EasyTermColor};

pub trait Backend {
type Error: Display;
Expand All @@ -57,28 +57,41 @@ pub static ENABLE_STDOUT: AtomicBool = AtomicBool::new(true);

pub struct StdBackend {
smart_stderr: bool,
colors: Colors
colors: Colors,
}

fn write_msg(stream: StandardStream, target: &str, msg: &str, level: Level) {
let t = ColorSpec::new().set_bold(true).clone();
EasyTermColor(stream).write('<').color(t).write(target).reset().write("> ")
.write('[').color(color(level)).write(level).reset().write(']')
.write(format!(" {}", msg)).lf();
EasyTermColor(stream)
.write('<')
.color(t)
.write(target)
.reset()
.write("> ")
.write('[')
.color(color(level))
.write(level)
.reset()
.write(']')
.write(format!(" {}", msg))
.lf();
}

impl StdBackend {
pub fn new(smart_stderr: bool, colors: Colors) -> StdBackend {
StdBackend { smart_stderr, colors }
StdBackend {
smart_stderr,
colors,
}
}

fn get_stream(&self, level: Level) -> Stream {
match self.smart_stderr {
false => Stream::Stdout,
true => match level {
Level::Error => Stream::Stderr,
_ => Stream::Stdout
}
_ => Stream::Stdout,
},
}
}
}
Expand All @@ -95,20 +108,20 @@ impl Backend for StdBackend {
let use_termcolor = match self.colors {
Colors::Disabled => false,
Colors::Enabled => true,
Colors::Auto => atty::is(stream)
Colors::Auto => atty::is(stream),
};
match use_termcolor {
true => {
let val = match stream {
Stream::Stderr => StandardStream::stderr(ColorChoice::Always),
_ => StandardStream::stdout(ColorChoice::Always)
_ => StandardStream::stdout(ColorChoice::Always),
};
write_msg(val, target, msg, level);
},
}
false => {
match stream {
Stream::Stderr => eprintln!("<{}> [{}] {}", target, level, msg),
_ => println!("<{}> [{}] {}", target, level, msg)
_ => println!("<{}> [{}] {}", target, level, msg),
};
}
};
Expand Down
27 changes: 21 additions & 6 deletions src/easy_termcolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::fmt::Display;
use log::Level;
use std::fmt::Display;
use termcolor::{Color, ColorSpec};

pub struct EasyTermColor<T: termcolor::WriteColor>(pub T);
Expand Down Expand Up @@ -56,10 +56,25 @@ impl<T: termcolor::WriteColor> EasyTermColor<T> {

pub fn color(level: Level) -> ColorSpec {
match level {
Level::Error => ColorSpec::new().set_fg(Some(Color::Red)).set_bold(true).clone(),
Level::Warn => ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true).clone(),
Level::Info => ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true).clone(),
Level::Debug => ColorSpec::new().set_fg(Some(Color::Blue)).set_bold(true).clone(),
Level::Trace => ColorSpec::new().set_fg(Some(Color::Cyan)).set_bold(true).clone()
Level::Error => ColorSpec::new()
.set_fg(Some(Color::Red))
.set_bold(true)
.clone(),
Level::Warn => ColorSpec::new()
.set_fg(Some(Color::Yellow))
.set_bold(true)
.clone(),
Level::Info => ColorSpec::new()
.set_fg(Some(Color::Green))
.set_bold(true)
.clone(),
Level::Debug => ColorSpec::new()
.set_fg(Some(Color::Blue))
.set_bold(true)
.clone(),
Level::Trace => ColorSpec::new()
.set_fg(Some(Color::Cyan))
.set_bold(true)
.clone(),
}
}
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
#![allow(clippy::needless_doctest_main)]

mod backend;
mod internal;
mod easy_termcolor;
mod internal;

use crate::backend::ENABLE_STDOUT;
use bp3d_fs::dirs::App;
use crossbeam_channel::Receiver;
use log::{Level, Log};
use once_cell::sync::Lazy;
use std::path::PathBuf;
use std::sync::atomic::Ordering;
use crate::backend::ENABLE_STDOUT;

/// Represents a log message in the [LogBuffer](crate::LogBuffer).
#[derive(Clone)]
Expand Down Expand Up @@ -96,7 +96,7 @@ pub enum Colors {

/// Color printing is automatic (if current terminal is a tty, print with colors, otherwise
/// print without colors).
Auto
Auto,
}

impl Default for Colors {
Expand Down Expand Up @@ -172,7 +172,7 @@ impl Default for Logger {
colors: Colors::default(),
smart_stderr: true,
std: None,
file: None
file: None,
}
}
}
Expand Down Expand Up @@ -230,8 +230,8 @@ impl Logger {
/// application, only that calling this function may be slow due to thread management.
pub fn start(self) -> Guard {
let _ = log::set_logger(&*BP3D_LOGGER); // Ignore the error
// (we can't do anything if there's already a logger set;
// unfortunately that is a limitation of the log crate)
// (we can't do anything if there's already a logger set;
// unfortunately that is a limitation of the log crate)

BP3D_LOGGER.start_new_thread(self); // Re-start the logging thread with the new configuration.
BP3D_LOGGER.enable(true); // Enable logging.
Expand Down

0 comments on commit 0f4ef2e

Please sign in to comment.