

use std::thread;
use std::time::Duration;



use syslog_rs::formatters::DefaultSyslogFormatterFile;
use syslog_rs::sy_async::AsyncSyslog;
use syslog_rs::{LogFacility, LogStat, Priority, SyslogFile};
use tokio::sync::OnceCell;

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


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

#[tokio::main]
async fn main()
{

    let syslog =
        AsyncSyslog::openlog_with(
            Some("example"), 
            LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
            LogFacility::LOG_DAEMON,
            SyslogFile::new("/tmp/async_file.log")
        )
        .await.unwrap();

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

    let hndl = 
        tokio::spawn(async 
            {
                logdebug!("test message from task!1");
                logdebug!("test message from task!2");
                logdebug!("test message from task!3");
                logdebug!("test message from task!4");
            }
        );

    let hndl2 = 
        tokio::spawn(async 
            {
                logdebug!("test 2 message from task!1");
                logdebug!("test 2 message from task!2");
                logdebug!("test 2 message from task!3");
                logdebug!("test 2 message from task!4");
            }
        );

    logdebug!("test message1!");
    logdebug!("test message2!");
    logdebug!("test message3!");
    logdebug!("test message4!");

    let _ = hndl.await;
    let _ = hndl2.await;

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

    return;
}

