

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

use syslog_rs::{SyslogApi, SyncSyslog};
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: LazyLock<SyncSyslog> = LazyLock::new(|| 
    {

        #[cfg(target_family = "unix")]
        let syslog_provider = 
            SyslogLocal::new();

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

        SyncSyslog::openlog(
            Some("example"), 
            LogStat::LOG_CONS | 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 message1!");

    SYSLOG.change_identity(Some("heave_load")).unwrap();

    logdebug!("test message from new ident");

    let thread_hndl1 = 
        std::thread::spawn(move || 
            {
                for i in 1..10000
                {
                    logdebug!("message from thread 1 {} dur: {:?}", i, std::time::SystemTime::now());
                    std::thread::sleep(Duration::from_nanos(1500));
                }
            }
        );

    let thread_hndl2 = 
        std::thread::spawn(move || 
            {
                for i in 1..10000
                {
                    logdebug!("message from thread 2 {} dur: {:?}", i, std::time::SystemTime::now());
                    std::thread::sleep(Duration::from_nanos(2000));
                }
            }
        );

    for i in 1..10000
    {
        logdebug!("message from thread main {} dur: {:?}", i, std::time::SystemTime::now());
        std::thread::sleep(Duration::from_nanos(2700));
    }

    let _ = thread_hndl1.join();
    let _ = thread_hndl2.join();

    logdebug!("joined");
    thread::sleep(Duration::from_micros(10));

    return;
}

