flarelog 0.3.2

flarelog is an ergonomic logging library for Rust with a focus on simplicity and speed.
Documentation
use crossbeam_channel;
use std::thread;
use std::io::BufWriter;
use std::io::prelude::*;
use std::fs::OpenOptions;
use crate::flare::{SerializedSender, SerializedReceiver};
use crate::mode::LogMode;

pub struct FlareIoWorker {
    pub thread_handle: thread::JoinHandle<()>,
    pub sender: SerializedSender,
}

impl FlareIoWorker {
    pub fn new(options: FlareIoOptions) -> Self {
        let (s, r) = crossbeam_channel::unbounded();
        let interface = FlareIoWorkerInterface::new(r.clone());

        let handle = thread::spawn(move || {
            let file = OpenOptions::new().write(true).append(true).create(true).open(options.filename).unwrap();
            let mut writer = BufWriter::with_capacity(options.bufwriter_cap, file);

            loop {
                let stringentry = interface.receiver.recv().unwrap();

                match options.mode {
                    LogMode::File => {
                        let _ = writer.write(stringentry.as_bytes());
                        let _ = writer.write(&[b'\n']);
                    },
                    LogMode::TerminalAndFile => {
                        let _ = writer.write(stringentry.as_bytes());
                        let _ = writer.write(&[b'\n']);
                        println!("{}", stringentry);
                    },
                }
            }
        });

        Self {
            thread_handle: handle,
            sender: s,
        }
    }
}

#[derive(Default)]
pub struct FlareIoOptions {
    pub mode: LogMode,
    pub filename: String,
    pub bufwriter_cap: usize,
}

impl FlareIoOptions {
    pub fn new() -> Self {
        Self {
            mode: LogMode::default(),
            bufwriter_cap: 0,
            filename: String::new(),
        }
    }

    pub fn mode(mut self, mode: LogMode) -> Self {
        self.mode = mode;
        self
    }

    pub fn bufwriter_cap(mut self, bufwriter_cap: usize) -> Self {
        self.bufwriter_cap = bufwriter_cap;
        self
    }

    pub fn filename(mut self, filename: String) -> Self {
        self.filename = filename;
        self
    }
}

#[derive(Clone)]
pub struct FlareIoWorkerInterface {
    pub receiver: SerializedReceiver,
}

impl FlareIoWorkerInterface {
    pub fn new(receiver: SerializedReceiver) -> Self {
        Self {
            receiver,
        }
    }
}