use std::{cell::RefCell, thread, time::Duration};

use syslog_rs::{formatters::DefaultSyslogFormatter, LogFacility, LogStat, Priority, SyslogApi, SingleSyslog};

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

#[cfg(target_family = "unix")]
thread_local! 
{
    // Could add pub to make it public to whatever Foo already is public to.
    static SYSLOG: RefCell<SingleSyslog<DefaultSyslogFormatter, SyslogLocal>> = 
        RefCell::new(
            SingleSyslog
                ::<_, _>
                ::openlog_with(Some("test"), LogStat::LOG_PID, LogFacility::LOG_DAEMON, SyslogLocal::new())
                    .unwrap()
        );
}

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

#[cfg(target_family = "windows")]
thread_local! 
{
    // Could add pub to make it public to whatever Foo already is public to.
    static SYSLOG: RefCell<SingleSyslog<DefaultSyslogFormatter, WindowsEvent>> = 
        RefCell::new(
            SingleSyslog
                ::<_, _>
                ::openlog_with(Some("test"), LogStat::LOG_PID, LogFacility::LOG_DAEMON, WindowsEvent::new())
                    .unwrap()
        );
}

pub fn main()
{
  
    SYSLOG
        .with_borrow(|b| 
            b.syslog(Priority::LOG_DEBUG, "start".into())
        );

    let thread_hndl = 
        std::thread::spawn(move || 
            {
                SYSLOG
                    .with_borrow(|b| 
                        b.syslog(Priority::LOG_DEBUG, "start thread".into())
                    );

                SYSLOG
                    .with_borrow(|b| 
                        b.change_identity(Some("thread1"))
                    )
                    .unwrap();

                SYSLOG
                    .with_borrow(|b| 
                        b.syslog(Priority::LOG_DEBUG, "end thread new identity".into())
                    );
            }
        );

    let _ = thread_hndl.join();

    SYSLOG
        .with_borrow(|b| 
            b.syslog(Priority::LOG_DEBUG, "end".into())
        );

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

    return;
}