1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crossbeam_channel;
use std::marker::Send;
use std::fmt::Debug;
use crate::logentry::LogEntry;
use crate::flareio::*;
use crate::flareworker::*;
use crate::mode::LogMode;

pub(crate) type LogEntrySender<T> = crossbeam_channel::Sender<T>;
pub(crate) type LogEntryReceiver<T> = crossbeam_channel::Receiver<T>;
pub(crate) type SerializedSender = crossbeam_channel::Sender<String>;
pub(crate) type SerializedReceiver = crossbeam_channel::Receiver<String>;

pub struct FlareIoManager {
    pub io: FlareIoWorker,
}

impl FlareIoManager {
    pub fn new(filename: String, bufwriter_cap: usize, mode: LogMode) -> Self {
        let ioworker = FlareIoWorker::new(
            FlareIoOptions::new().mode(mode).filename(filename).bufwriter_cap(bufwriter_cap),
        );

        FlareIoManager {
            io: ioworker,
        }
    }

    pub fn inner_ref(&self) -> &Self {
        self
    }
}

pub struct Flare<T>
where T: LogEntry + Send + Clone + Debug {
    pub workpool: FlareWorkerPool<T>,
}

impl<T: 'static> Flare<T>
where T: LogEntry + Send + Clone + Debug  {
    pub fn new(threads: u32, iomanager: &FlareIoManager) -> Self {
        if threads < 1 {
            panic!("Not enough threads, minimum of 1.")
        }

        let workers = FlareWorkerPool::new(iomanager.io.sender.clone(), threads as usize);

        Flare {
            workpool: workers,
        }
    }

    pub fn log(&self, entry: T) {
        self.workpool.sender.send(entry).unwrap();
    }
}