use std::sync::OnceLock;
use std::thread;
use std::time::Duration;

use syslog_rs::{SyncSyslog, SyslogApi};

#[cfg(target_os = "linux")]
use syslog_rs::{formatters::FormatRfc3146, SyslogLocal};

#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
use syslog_rs::{formatters::{FormatRfc5424}, SyslogLocal};

#[cfg(target_os = "windows")]
use syslog_rs::{formatters::{FormatWindows}, WindowsEvent};

use syslog_rs::{LogStat, LogFacility, Priority};

#[cfg(target_os = "linux")]
pub static SYNC_SYSLOG: OnceLock<SyncSyslog<FormatRfc3146, SyslogLocal>> = OnceLock::new();
#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
pub static SYNC_SYSLOG: OnceLock<SyncSyslog<FormatRfc5424, SyslogLocal>> = OnceLock::new();
#[cfg(target_os = "windows")]
pub static SYNC_SYSLOG: OnceLock<SyncSyslog<FormatWindows, WindowsEvent>> = OnceLock::new();

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

pub fn main()
{
    #[cfg(target_family = "unix")]
    let syslog = 
        SyncSyslog
            ::<_, _>
            ::openlog_with(
                Some("formatter_sel"), 
                LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
                LogFacility::LOG_DAEMON,
                SyslogLocal::new()
            ).unwrap();

    #[cfg(target_family = "windows")]
    let syslog = 
        SyncSyslog
            ::<_, _>
            ::openlog_with(
                Some("formatter_sel"), 
                LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
                LogFacility::LOG_DAEMON,
                WindowsEvent::new()
            ).unwrap();

    SYNC_SYSLOG.get_or_init(|| syslog);

    logdebug!("test message!");

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

    return;
}
