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
//! Utility function to setup logging for a Plugin instance.

use crate::{
    common::{
        error::Result,
        log::{init, proxy::LogProxy, tee_file::TeeFile, Log, LogRecord},
    },
    host::configuration::PluginLogConfiguration,
};
use ipc_channel::ipc::IpcSender;

/// Setup logging for a Plugin instance.
///
/// Given a [`PluginConfiguration`], start the configured loggers. Consumes
/// the given log channel sender.
///
/// Starts a thread-local ['LogProxy`], given a [`LoglevelFilter`] bigger
/// than [`Off`] which forwards log records to the simulator [`LogThread`].
/// Starts [`TeeFile`] loggers, given a non-empty vector of [`TeeFile`], to
/// forward log records to output files.
pub fn setup_logging(
    configuration: &PluginLogConfiguration,
    log_channel: IpcSender<LogRecord>,
) -> Result<()> {
    let mut loggers = Vec::with_capacity(1 + configuration.tee_files.len());
    loggers.push(LogProxy::boxed(
        configuration.name.as_str(),
        configuration.verbosity,
        log_channel,
    ) as Box<dyn Log>);
    let tee_files: Result<Vec<_>> = configuration
        .tee_files
        .clone()
        .into_iter()
        .map(TeeFile::new)
        .collect();
    loggers.extend(tee_files?.into_iter().map(|l| Box::new(l) as Box<dyn Log>));
    init(loggers)?;
    Ok(())
}