use syslog_rs::sy_async::AsyncSyslog;
use tokio::sync::OnceCell;
use tokio::time::{sleep, Duration};

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

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


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

pub static SYSLOG: OnceCell<AsyncSyslog> = OnceCell::const_new();

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

#[tokio::main]
async fn main()
{
    #[cfg(target_family = "unix")]
    let syslog_provider = 
        SyslogLocal::new();

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

    let syslog =
        AsyncSyslog::openlog(
                Some("example_async"), 
                LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
                LogFacility::LOG_DAEMON,
                syslog_provider
            )
            .await
            .unwrap();


    SYSLOG.get_or_init(|| async { syslog }).await;


    logdebug!("test message async start!");
    
    SYSLOG.get().unwrap().vsyslog(Priority::LOG_DEBUG, "test 21").await;

    sleep(Duration::from_micros(10)).await;

    SYSLOG.get().unwrap().change_identity("new_identity").await;

    logdebug!("test message new identity!");

    sleep(Duration::from_micros(10)).await;

    logdebug!("test 123!");
    logdebug!("test 123123! end ");

    return;
}
