

use std::{sync::LazyLock, thread};
use std::time::Duration;
use std::fmt::Write;

use syslog_rs::formatters::DefaultSyslogFormatter;
use syslog_rs::sy_sync_queue::SyslogQueueChannel;
use syslog_rs::{QueuedSyslog, SyStreamApi, SyslogApi};
use syslog_rs::sync::DefaultQueueAdapter;
use syslog_rs::{LogFacility, LogStat, Priority};

#[cfg(target_family = "unix")]
use syslog_rs::{SyslogLocal};

#[cfg(target_family = "windows")]
use syslog_rs::{WindowsEvent};

#[cfg(target_family = "unix")]
pub type DefaultLocalSyslogDestination = SyslogLocal;
#[cfg(target_family = "windows")]
pub type DefaultLocalSyslogDestination = WindowsEvent;

pub static SYSLOG: LazyLock<QueuedSyslog<DefaultQueueAdapter, DefaultSyslogFormatter, DefaultLocalSyslogDestination>> = 
    LazyLock::new(|| 
        {
            #[cfg(target_family = "unix")]
            let syslog_provider = 
                SyslogLocal::new();

            #[cfg(target_family = "windows")]   
            let syslog_provider = 
                WindowsEvent::new();

            QueuedSyslog
                ::<_, _, _>
                ::openlog_with(
                    Some("example_sync_queue"), 
                    LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
                    LogFacility::LOG_DAEMON,
                    syslog_provider
                )
                .unwrap()
        }
    );

macro_rules! logdebug 
{
    ($($arg:tt)*) => (
        SYSLOG.syslog(Priority::LOG_DEBUG, format!($($arg)*).into())
    )
}

pub fn main()
{
    logdebug!("test message queue! adapter: {}", 
        <DefaultQueueAdapter as SyslogQueueChannel<DefaultSyslogFormatter, DefaultLocalSyslogDestination>>::ADAPTER_NAME);

    thread::sleep(Duration::from_micros(10));

    let d = "890";

    let _ = write!(SYSLOG.stream(Priority::LOG_DEBUG), "queue test {} 123  queue", d);

    std::thread::sleep(Duration::from_secs(1));

    return;
}

