bflog 0.1.0

Very tiny logging framework made for microservices.
Documentation
use std::io::{Stderr, Stdout};

use super::prelude::*;

pub enum Output {
  Stdout(Stdout),
  Stderr(Stderr),
}

impl Output {
  pub fn new(severity: &Severity) -> Output {
    match severity {
      Severity::Debug | Severity::Info => Output::Stdout(io::stdout()),
      Severity::Warn | Severity::Error => Output::Stderr(io::stderr()),
    }
  }

  pub fn set_color(&mut self, color: Color) -> io::Result<()> {
    write!(self, "{}", color.fg())
  }

  pub fn reset_colors(&mut self) -> io::Result<()> {
    write!(self, "\x1b[0m")
  }
}

impl Write for Output {
  fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
    match self {
      Output::Stdout(stdout) => stdout.write(buf),
      Output::Stderr(stderr) => stderr.write(buf),
    }
  }

  fn flush(&mut self) -> io::Result<()> {
    match self {
      Output::Stdout(stdout) => stdout.flush(),
      Output::Stderr(stderr) => stderr.flush(),
    }
  }
}

pub enum Color {
  Black,
  Red,
  Green,
  Yellow,
  Blue,
  Magenta,
  Cyan,
  White,
  BrightBlack,
  BrightRed,
  BrightGreen,
  BrightYellow,
  BrightBlue,
  BrightMagenta,
  BrightCyan,
  BrightWhite,
}

impl Color {
  fn fg(&self) -> &'static str {
    match self {
      Color::Black => "\x1b[30m",
      Color::Red => "\x1b[31m",
      Color::Green => "\x1b[32m",
      Color::Yellow => "\x1b[33m",
      Color::Blue => "\x1b[34m",
      Color::Magenta => "\x1b[35m",
      Color::Cyan => "\x1b[36m",
      Color::White => "\x1b[37m",
      Color::BrightBlack => "\x1b[30;1m",
      Color::BrightRed => "\x1b[31;1m",
      Color::BrightGreen => "\x1b[32;1m",
      Color::BrightYellow => "\x1b[33;1m",
      Color::BrightBlue => "\x1b[34;1m",
      Color::BrightMagenta => "\x1b[35;1m",
      Color::BrightCyan => "\x1b[36;1m",
      Color::BrightWhite => "\x1b[37;1m",
    }
  }
}